feat: Add HeaderBar widget

This commit is contained in:
Michael Aaron Murphy 2022-10-07 05:20:06 +02:00
parent daf4cb1eb8
commit b848931920
No known key found for this signature in database
GPG key ID: B2732D4240C9212C
3 changed files with 120 additions and 5 deletions

View file

@ -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"
]

View file

@ -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<Flags: Default>() -> iced::Settings<Flags> {
let mut settings = iced::Settings::default();
settings.default_font = match font::FONT {

100
src/widget/headerbar.rs Normal file
View file

@ -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<T>(&self) -> Element<'_, T>
where T: Clone + From<WindowMsg> + '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<Element<'_, T>> = 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()
}
}