From b84893192072c5d19e811a986236d7ae8fc9fdfc Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Fri, 7 Oct 2022 05:20:06 +0200 Subject: [PATCH] feat: Add HeaderBar widget --- Cargo.toml | 15 ++++-- src/lib.rs | 10 ++++ src/widget/headerbar.rs | 100 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 src/widget/headerbar.rs diff --git a/Cargo.toml b/Cargo.toml index 531f29a7..3ada9859 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,28 +12,33 @@ debug = ["iced/debug"] [dependencies] freedesktop-icons = "0.2.1" +apply = "0.3.0" [dependencies.iced] git = "https://github.com/pop-os/iced.git" branch = "cosmic-design-system" -#path = "../iced" +# path = "../iced" features = ["cosmic-theme", "svg"] [dependencies.iced_native] git = "https://github.com/pop-os/iced.git" branch = "cosmic-design-system" -#path = "../iced/native" +# path = "../iced/native" features = ["cosmic-theme"] [dependencies.iced_lazy] git = "https://github.com/pop-os/iced.git" branch = "cosmic-design-system" -#path = "../iced/lazy" +# path = "../iced/lazy" + +[dependencies.iced_winit] +git = "https://github.com/pop-os/iced.git" +branch = "cosmic-design-system" +# path = "../iced/winit" [workspace] members = [ - "examples/cosmic", - "examples/text", + "examples/*", "gtk4", "gtk4/widgets" ] diff --git a/src/lib.rs b/src/lib.rs index b9fe34bc..71dcf6a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,19 @@ pub use iced; pub use iced_lazy; +pub use iced_winit; pub mod font; pub mod widget; +#[derive(Clone, Copy, Debug)] +pub enum WindowMsg { + Close, + Drag, + Minimize, + Maximize, + ToggleSidebar, +} + pub fn settings() -> iced::Settings { let mut settings = iced::Settings::default(); settings.default_font = match font::FONT { diff --git a/src/widget/headerbar.rs b/src/widget/headerbar.rs new file mode 100644 index 00000000..f13be711 --- /dev/null +++ b/src/widget/headerbar.rs @@ -0,0 +1,100 @@ +use apply::Apply; +use iced::{self, alignment::Vertical, Element, Length, theme, widget}; +use crate::WindowMsg; + +#[derive(Default)] +pub struct HeaderBar { + pub title: String, + pub nav_title: String, + pub sidebar_active: bool, + pub show_minimize: bool, + pub show_maximize: bool, +} + +impl HeaderBar { + pub fn render(&self) -> Element<'_, T> + where T: Clone + From + 'static + { + let navbutton = { + let text = widget::text(&self.nav_title) + .vertical_alignment(Vertical::Center) + .width(Length::Shrink) + .height(Length::Fill); + + let icon = super::icon( + if self.sidebar_active { + "go-previous-symbolic" + } else { + "go-next-symbolic" + }, + 24, + ) + .width(Length::Units(24)) + .height(Length::Fill); + + widget::row!(text, icon) + .padding(4) + .spacing(4) + .apply(widget::button) + .on_press(T::from(WindowMsg::ToggleSidebar)) + .apply(widget::container) + .center_y() + .height(Length::Fill) + .into() + }; + + let content = widget::container(widget::text(&self.title)) + .center_x() + .center_y() + .width(Length::Fill) + .height(Length::Fill) + .into(); + + let window_controls = { + let mut widgets: Vec> = Vec::with_capacity(3); + + if self.show_minimize { + widgets.push( + super::icon("window-minimize-symbolic", 16) + .apply(widget::button) + .on_press(T::from(WindowMsg::Minimize)) + .style(theme::Button::Primary) + .into(), + ); + } + + if self.show_maximize { + widgets.push( + super::icon("window-maximize-symbolic", 16) + .apply(widget::button) + .on_press(T::from(WindowMsg::Maximize)) + .style(crate::iced::theme::Button::Primary) + .into(), + ); + } + + widgets.push( + super::icon("window-close-symbolic", 16) + .apply(widget::button) + .on_press(T::from(WindowMsg::Close)) + .style(crate::iced::theme::Button::Primary) + .into(), + ); + + widget::row(widgets) + .spacing(8) + .apply(widget::container) + .height(Length::Fill) + .center_y() + .into() + }; + + widget::row(vec![navbutton, content, window_controls]) + .height(Length::Units(48)) + .padding(4) + .apply(widget::event_container) + .center_y() + .on_press(T::from(WindowMsg::Drag)) + .into() + } +}