feat!: implement Application API

This commit is contained in:
Michael Murphy 2023-08-02 11:54:07 +02:00 committed by GitHub
parent e24465ba37
commit a223b60a0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 1420 additions and 138 deletions

View file

@ -10,35 +10,80 @@ use std::borrow::Cow;
#[must_use]
pub fn header_bar<'a, Message>() -> HeaderBar<'a, Message> {
HeaderBar {
title: "".into(),
title: Cow::Borrowed(""),
on_close: None,
on_drag: None,
on_maximize: None,
on_minimize: None,
start: None,
center: None,
end: None,
start: Vec::new(),
center: Vec::new(),
end: Vec::new(),
}
}
#[derive(Setters)]
pub struct HeaderBar<'a, Message> {
#[setters(into)]
/// Defines the title of the window
#[setters(skip)]
title: Cow<'a, str>,
/// A message emitted when the close button is pressed.
#[setters(strip_option)]
on_close: Option<Message>,
/// A message emitted when dragged.
#[setters(strip_option)]
on_drag: Option<Message>,
/// A message emitted when the maximize button is pressed.
#[setters(strip_option)]
on_maximize: Option<Message>,
/// A message emitted when the minimize button is pressed.
#[setters(strip_option)]
on_minimize: Option<Message>,
#[setters(strip_option)]
start: Option<Element<'a, Message>>,
#[setters(strip_option)]
center: Option<Element<'a, Message>>,
#[setters(strip_option)]
end: Option<Element<'a, Message>>,
/// Elements packed at the start of the headerbar.
#[setters(skip)]
start: Vec<Element<'a, Message>>,
/// Elements packed in the center of the headerbar.
#[setters(skip)]
center: Vec<Element<'a, Message>>,
/// Elements packed at the end of the headerbar.
#[setters(skip)]
end: Vec<Element<'a, Message>>,
}
impl<'a, Message: Clone + 'static> HeaderBar<'a, Message> {
/// Defines the title of the window
#[must_use]
pub fn title(mut self, title: impl Into<Cow<'a, str>> + 'a) -> Self {
self.title = title.into();
self
}
/// Pushes an element to the start region.
#[must_use]
pub fn start(mut self, widget: impl Into<Element<'a, Message>> + 'a) -> Self {
self.start.push(widget.into());
self
}
/// Pushes an element to the center region.
#[must_use]
pub fn center(mut self, widget: impl Into<Element<'a, Message>> + 'a) -> Self {
self.center.push(widget.into());
self
}
/// Pushes an element to the end region.
#[must_use]
pub fn end(mut self, widget: impl Into<Element<'a, Message>> + 'a) -> Self {
self.end.push(widget.into());
self
}
}
impl<'a, Message: Clone + 'static> HeaderBar<'a, Message> {
@ -46,16 +91,28 @@ impl<'a, Message: Clone + 'static> HeaderBar<'a, Message> {
pub fn into_element(mut self) -> Element<'a, Message> {
let mut packed: Vec<Element<Message>> = Vec::with_capacity(4);
if let Some(start) = self.start.take() {
// Take ownership of the regions to be packed.
let start = std::mem::take(&mut self.start);
let center = std::mem::take(&mut self.center);
let mut end = std::mem::take(&mut self.end);
// If elements exist in the start region, append them here.
if !start.is_empty() {
packed.push(
widget::container(start)
iced::widget::row(start)
.align_items(iced::Alignment::Center)
.apply(iced::widget::container)
.align_x(iced::alignment::Horizontal::Left)
.into(),
);
}
packed.push(if let Some(center) = self.center.take() {
widget::container(center)
// If elements exist in the center region, use them here.
// This will otherwise use the title as a widget if a title was defined.
packed.push(if !center.is_empty() {
iced::widget::row(center)
.align_items(iced::Alignment::Center)
.apply(iced::widget::container)
.align_x(iced::alignment::Horizontal::Center)
.into()
} else if self.title.is_empty() {
@ -64,15 +121,17 @@ impl<'a, Message: Clone + 'static> HeaderBar<'a, Message> {
self.title_widget()
});
packed.push(if let Some(end) = self.end.take() {
widget::row(vec![end, self.window_controls()])
// Also packs the window controls at the very end.
end.push(self.window_controls());
packed.push(
iced::widget::row(end)
.align_items(iced::Alignment::Center)
.apply(widget::container)
.align_x(iced::alignment::Horizontal::Right)
.into()
} else {
self.window_controls()
});
.into(),
);
// Creates the headerbar widget.
let mut widget = widget::row(packed)
.height(Length::Fixed(50.0))
.padding(8)
@ -82,10 +141,12 @@ impl<'a, Message: Clone + 'static> HeaderBar<'a, Message> {
.center_y()
.apply(widget::mouse_area);
// Assigns a message to emit when the headerbar is dragged.
if let Some(message) = self.on_drag.clone() {
widget = widget.on_press(message);
}
// Assigns a message to emit when the headerbar is double-clicked.
if let Some(message) = self.on_maximize.clone() {
widget = widget.on_release(message);
}

View file

@ -260,7 +260,7 @@ impl<'a> Icon<'a> {
self.hash(&mut hasher);
if self.theme.is_none() {
crate::settings::DEFAULT_ICON_THEME.with(|f| f.borrow().hash(&mut hasher));
crate::icon_theme::DEFAULT.with(|f| f.borrow().hash(&mut hasher));
}
let hash = hasher.finish();
@ -291,7 +291,7 @@ impl<'a, Message: 'static> From<Icon<'a>> for Element<'a, Message> {
#[must_use]
pub fn load_icon(name: &str, size: u16, theme: Option<&str>) -> Option<PathBuf> {
let icon = crate::settings::DEFAULT_ICON_THEME.with(|default_theme| {
let icon = crate::icon_theme::DEFAULT.with(|default_theme| {
let default_theme = default_theme.borrow();
freedesktop_icons::lookup(name)
.with_size(size)

View file

@ -14,6 +14,9 @@ use iced_core::Color;
use crate::{theme, widget::segmented_button, Theme};
pub type Id = segmented_button::Entity;
pub type Model = segmented_button::SingleSelectModel;
/// Navigation side panel for switching between views.
///
/// For details on the model, see the [`segmented_button`] module for more details.

View file

@ -12,23 +12,23 @@ use super::IconSource;
#[derive(Setters)]
pub struct NavBarToggle<Message> {
nav_bar_active: bool,
active: bool,
#[setters(strip_option)]
on_nav_bar_toggled: Option<Message>,
on_toggle: Option<Message>,
}
#[must_use]
pub fn nav_bar_toggle<Message>() -> NavBarToggle<Message> {
NavBarToggle {
nav_bar_active: false,
on_nav_bar_toggled: None,
active: false,
on_toggle: None,
}
}
impl<Message: 'static + Clone> From<NavBarToggle<Message>> for Element<'static, Message> {
impl<'a, Message: 'static + Clone> From<NavBarToggle<Message>> for Element<'a, Message> {
fn from(nav_bar_toggle: NavBarToggle<Message>) -> Self {
let mut widget = super::icon(
if nav_bar_toggle.nav_bar_active {
if nav_bar_toggle.active {
IconSource::svg_from_memory(&include_bytes!("../../res/sidebar-active.svg")[..])
} else {
IconSource::from("open-menu-symbolic")
@ -41,7 +41,7 @@ impl<Message: 'static + Clone> From<NavBarToggle<Message>> for Element<'static,
.padding([8, 16, 8, 16])
.style(theme::Button::Text);
if let Some(message) = nav_bar_toggle.on_nav_bar_toggled {
if let Some(message) = nav_bar_toggle.on_toggle {
widget = widget.on_press(message);
}