libcosmic/src/widget/header_bar.rs

442 lines
14 KiB
Rust
Raw Normal View History

// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use crate::{ext::CollectionWidget, widget, Element};
2022-10-09 02:35:03 -07:00
use apply::Apply;
use derive_setters::Setters;
use iced::{window, Length};
use iced_core::{renderer::Quad, widget::tree, Background, Color, Renderer, Widget};
use std::{borrow::Cow, process::Child};
#[must_use]
pub fn header_bar<'a, Message>() -> HeaderBar<'a, Message> {
HeaderBar {
2023-08-02 11:54:07 +02:00
title: Cow::Borrowed(""),
on_close: None,
on_drag: None,
on_maximize: None,
on_minimize: None,
2023-08-02 11:54:07 +02:00
start: Vec::new(),
center: Vec::new(),
end: Vec::new(),
window_id: None,
}
}
2022-10-09 02:35:03 -07:00
#[derive(Setters)]
pub struct HeaderBar<'a, Message> {
2023-08-02 11:54:07 +02:00
/// Defines the title of the window
#[setters(skip)]
title: Cow<'a, str>,
2023-08-02 11:54:07 +02:00
/// A message emitted when the close button is pressed.
#[setters(strip_option)]
on_close: Option<Message>,
2023-08-02 11:54:07 +02:00
/// A message emitted when dragged.
#[setters(strip_option)]
on_drag: Option<Message>,
2023-08-02 11:54:07 +02:00
/// A message emitted when the maximize button is pressed.
#[setters(strip_option)]
on_maximize: Option<Message>,
2023-08-02 11:54:07 +02:00
/// A message emitted when the minimize button is pressed.
#[setters(strip_option)]
on_minimize: Option<Message>,
2023-08-02 11:54:07 +02:00
/// The window id for the headerbar.
#[setters(strip_option)]
window_id: Option<iced::window::Id>,
2023-08-02 11:54:07 +02:00
/// 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
}
/// Build the widget
#[must_use]
pub fn build(self) -> HeaderBarWidget<'a, Message> {
HeaderBarWidget {
window_id: self.window_id,
header_bar_inner: self.into_element(),
}
}
}
pub struct HeaderBarWidget<'a, Message> {
header_bar_inner: Element<'a, Message>,
window_id: Option<iced::window::Id>,
}
impl<'a, Message: Clone + 'static> Widget<Message, crate::Renderer>
for HeaderBarWidget<'a, Message>
{
fn children(&self) -> Vec<tree::Tree> {
vec![tree::Tree::new(&self.header_bar_inner)]
}
fn width(&self) -> Length {
self.header_bar_inner.width()
}
fn height(&self) -> Length {
self.header_bar_inner.height()
}
fn tag(&self) -> tree::Tag {
tree::Tag::of::<MyState>()
}
fn diff(&mut self, tree: &mut tree::Tree) {
tree.diff_children(&mut [&mut self.header_bar_inner]);
let prev = tree.state.downcast_mut::<MyState>();
if prev.window_id != self.window_id {
*prev = MyState::new(self.window_id);
}
}
fn state(&self) -> tree::State {
let state = MyState::new(self.window_id);
tree::State::new(state)
}
fn layout(
&self,
tree: &mut tree::Tree,
renderer: &crate::Renderer,
limits: &iced_core::layout::Limits,
) -> iced_core::layout::Node {
let child_tree = &mut tree.children[0];
let child = self
.header_bar_inner
.as_widget()
.layout(child_tree, renderer, limits);
iced_core::layout::Node::with_children(child.size(), vec![child])
}
fn draw(
&self,
tree: &tree::Tree,
renderer: &mut crate::Renderer,
theme: &<crate::Renderer as iced_core::Renderer>::Theme,
style: &iced_core::renderer::Style,
layout: iced_core::Layout<'_>,
cursor: iced_core::mouse::Cursor,
viewport: &iced_core::Rectangle,
) {
let layout_children = layout.children().next().unwrap();
let state_children = &tree.children[0];
self.header_bar_inner.as_widget().draw(
state_children,
renderer,
theme,
style,
layout_children,
cursor,
viewport,
);
let state = tree.state.downcast_ref::<MyState>();
if !state.window_has_focus {
let header_bar_appearance =
<crate::Theme as crate::iced_style::container::StyleSheet>::appearance(
theme,
&crate::theme::Container::HeaderBar,
);
let cosmic = theme.cosmic();
let mut neutral_0 = cosmic.palette.neutral_0;
neutral_0.alpha = 0.3;
// draw overlay rectangle
renderer.fill_quad(
Quad {
bounds: layout.bounds(),
border_radius: header_bar_appearance.border_radius,
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
Background::Color(neutral_0.into()),
);
}
}
fn on_event(
&mut self,
state: &mut tree::Tree,
event: iced_core::Event,
layout: iced_core::Layout<'_>,
cursor: iced_core::mouse::Cursor,
renderer: &crate::Renderer,
clipboard: &mut dyn iced_core::Clipboard,
shell: &mut iced_core::Shell<'_, Message>,
viewport: &iced_core::Rectangle,
) -> iced_core::event::Status {
if let iced_core::Event::Window(id, iced_core::window::Event::Focused) = event {
let state = state.state.downcast_mut::<MyState>();
state.focus_window(id);
} else if let iced_core::Event::Window(id, iced_core::window::Event::Unfocused) = event {
let state = state.state.downcast_mut::<MyState>();
state.unfocus_window(id);
}
let child_state = &mut state.children[0];
let child_layout = layout.children().next().unwrap();
self.header_bar_inner.as_widget_mut().on_event(
child_state,
event,
child_layout,
cursor,
renderer,
clipboard,
shell,
viewport,
)
}
fn mouse_interaction(
&self,
state: &tree::Tree,
layout: iced_core::Layout<'_>,
cursor: iced_core::mouse::Cursor,
viewport: &iced_core::Rectangle,
renderer: &crate::Renderer,
) -> iced_core::mouse::Interaction {
let child_tree = &state.children[0];
let child_layout = layout.children().next().unwrap();
self.header_bar_inner.as_widget().mouse_interaction(
child_tree,
child_layout,
cursor,
viewport,
renderer,
)
}
fn operate(
&self,
state: &mut tree::Tree,
layout: iced_core::Layout<'_>,
renderer: &crate::Renderer,
operation: &mut dyn iced_core::widget::Operation<
iced_core::widget::OperationOutputWrapper<Message>,
>,
) {
let child_tree = &mut state.children[0];
let child_layout = layout.children().next().unwrap();
self.header_bar_inner
.as_widget()
.operate(child_tree, child_layout, renderer, operation);
}
fn overlay<'b>(
&'b mut self,
state: &'b mut tree::Tree,
layout: iced_core::Layout<'_>,
renderer: &crate::Renderer,
) -> Option<iced_core::overlay::Element<'b, Message, crate::Renderer>> {
let child_tree = &mut state.children[0];
let child_layout = layout.children().next().unwrap();
self.header_bar_inner
.as_widget_mut()
.overlay(child_tree, child_layout, renderer)
}
2022-10-09 02:35:03 -07:00
}
impl<'a, Message: Clone + 'static> HeaderBar<'a, Message> {
/// Converts the headerbar builder into an Iced element.
pub fn into_element(mut self) -> Element<'a, Message> {
2023-08-02 11:54:07 +02:00
// 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);
// Also packs the window controls at the very end.
end.push(widget::horizontal_space(Length::Fixed(12.0)).into());
2023-08-02 11:54:07 +02:00
end.push(self.window_controls());
2023-08-02 11:54:07 +02:00
// Creates the headerbar widget.
let mut widget = widget::row::with_capacity(4)
// If elements exist in the start region, append them here.
.push(
widget::row::with_children(start)
.align_items(iced::Alignment::Center)
.apply(widget::container)
.align_x(iced::alignment::Horizontal::Left)
2024-01-10 12:56:07 -07:00
.width(Length::Shrink),
)
// If elements exist in the center region, use them here.
// This will otherwise use the title as a widget if a title was defined.
.push(if !center.is_empty() {
widget::row::with_children(center)
.align_items(iced::Alignment::Center)
.apply(widget::container)
.align_x(iced::alignment::Horizontal::Center)
.width(Length::Fill)
.into()
} else if self.title.is_empty() {
widget::horizontal_space(Length::Fill).into()
} else {
self.title_widget()
})
.push(
widget::row::with_children(end)
.align_items(iced::Alignment::Center)
.apply(widget::container)
.align_x(iced::alignment::Horizontal::Right)
2024-01-10 12:56:07 -07:00
.width(Length::Shrink),
)
2024-01-31 10:03:39 -07:00
.align_items(iced::Alignment::Center)
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
.height(Length::Fixed(50.0))
2022-12-21 08:22:52 -07:00
.padding(8)
2023-01-25 06:00:16 +01:00
.spacing(8)
2022-12-19 17:03:13 +01:00
.apply(widget::container)
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
.style(crate::theme::Container::HeaderBar)
2022-12-19 17:03:13 +01:00
.center_y()
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
.apply(widget::mouse_area);
2023-08-02 11:54:07 +02:00
// Assigns a message to emit when the headerbar is dragged.
if let Some(message) = self.on_drag.clone() {
widget = widget.on_drag(message);
}
2023-08-02 11:54:07 +02:00
// Assigns a message to emit when the headerbar is double-clicked.
if let Some(message) = self.on_maximize.clone() {
widget = widget.on_release(message);
2022-10-09 02:35:03 -07:00
}
widget.into()
}
2022-10-09 02:35:03 -07:00
fn title_widget(&mut self) -> Element<'a, Message> {
let mut title = Cow::default();
std::mem::swap(&mut title, &mut self.title);
widget::text(title)
.size(16)
.font(crate::font::FONT_SEMIBOLD)
.apply(widget::container)
2022-10-09 02:35:03 -07:00
.center_x()
.center_y()
.width(Length::Fill)
.height(Length::Fill)
.into()
}
2022-10-09 02:35:03 -07:00
/// Creates the widget for window controls.
fn window_controls(&mut self) -> Element<'a, Message> {
let icon = |icon_bytes, size, on_press| {
2024-01-04 22:12:43 +01:00
widget::icon::from_svg_bytes(icon_bytes)
.symbolic(true)
.apply(widget::button::icon)
.icon_size(size)
.on_press(on_press)
};
2022-10-09 02:35:03 -07:00
widget::row::with_capacity(3)
2024-01-04 22:12:43 +01:00
.push_maybe(self.on_minimize.take().map(|m| {
icon(
&include_bytes!("../../res/icons/window-minimize-symbolic.svg")[..],
16,
m,
)
}))
.push_maybe(self.on_maximize.take().map(|m| {
icon(
&include_bytes!("../../res/icons/window-maximize-symbolic.svg")[..],
16,
m,
)
}))
.push_maybe(self.on_close.take().map(|m| {
icon(
&include_bytes!("../../res/icons/window-close-symbolic.svg")[..],
16,
m,
)
}))
.spacing(8)
.apply(widget::container)
.height(Length::Fill)
2022-10-09 02:35:03 -07:00
.center_y()
.into()
}
}
impl<'a, Message: Clone + 'static> From<HeaderBar<'a, Message>> for Element<'a, Message> {
fn from(headerbar: HeaderBar<'a, Message>) -> Self {
Element::new(headerbar.build())
}
}
impl<'a, Message: Clone + 'static> From<HeaderBarWidget<'a, Message>> for Element<'a, Message> {
fn from(headerbar: HeaderBarWidget<'a, Message>) -> Self {
Element::new(headerbar)
}
}
pub struct MyState {
pub window_id: Option<window::Id>,
pub window_has_focus: bool,
}
impl MyState {
pub fn new(id: Option<window::Id>) -> Self {
Self {
window_id: id,
window_has_focus: id.is_none(),
}
}
pub fn focus_window(&mut self, id: window::Id) {
if self.window_id != Some(id) {
return;
}
self.window_has_focus = true;
}
pub fn unfocus_window(&mut self, id: window::Id) {
if self.window_id != Some(id) {
return;
}
self.window_has_focus = false;
2022-10-09 02:35:03 -07:00
}
}