libcosmic-yoda/src/widget/header_bar.rs

546 lines
17 KiB
Rust
Raw Normal View History

// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use crate::cosmic_theme::{Density, Spacing};
use crate::{Element, theme, widget};
2022-10-09 02:35:03 -07:00
use apply::Apply;
use derive_setters::Setters;
use iced_core::{Length, Size, Vector, Widget, layout, text, widget::tree};
use std::borrow::Cow;
#[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,
on_right_click: None,
2023-08-02 11:54:07 +02:00
start: Vec::new(),
center: Vec::new(),
end: Vec::new(),
density: None,
focused: false,
maximized: false,
2025-10-03 18:19:19 +02:00
sharp_corners: false,
2025-05-18 19:24:27 +02:00
is_ssd: false,
on_double_click: None,
transparent: false,
controls_position: None,
}
}
2022-10-09 02:35:03 -07:00
/// Position of the window control buttons (close/min/max) within the headerbar.
#[derive(
Clone,
Copy,
Debug,
Default,
PartialEq,
Eq,
serde::Serialize,
serde::Deserialize,
)]
pub enum WindowControlsPosition {
/// Controls packed at the start (left on LTR) — macOS style.
/// Internal icon order becomes close → minimize → maximize.
Start,
/// Controls packed at the end (right on LTR) — Linux / GNOME style.
/// Internal icon order is minimize → maximize → close.
#[default]
End,
}
#[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
/// A message emitted when the header is double clicked,
/// usually used to maximize the window.
#[setters(strip_option)]
on_double_click: Option<Message>,
/// A message emitted when the header is right clicked.
#[setters(strip_option)]
on_right_click: Option<Message>,
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>>,
/// Controls the density of the headerbar.
#[setters(strip_option)]
density: Option<Density>,
/// Focused state of the window
focused: bool,
/// Maximized state of the window
maximized: bool,
2025-05-18 19:24:27 +02:00
2025-10-03 18:19:19 +02:00
/// Whether the corners of the window should be sharp
sharp_corners: bool,
2025-05-18 19:24:27 +02:00
/// HeaderBar used for server-side decorations
is_ssd: bool,
/// Whether the headerbar should be transparent
transparent: bool,
/// Side on which the window control buttons (close / minimize / maximize)
/// are rendered. `None` falls back to the user's CosmicTk config
/// (`crate::config::window_controls_position()`). `Some` overrides it.
/// `End` matches Linux / GNOME conventions; `Start` provides macOS-style
/// left-side controls.
#[setters(strip_option)]
controls_position: Option<WindowControlsPosition>,
2023-08-02 11:54:07 +02:00
}
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
}
}
pub struct HeaderBarWidget<'a, Message> {
start: Element<'a, Message>,
center: Option<Element<'a, Message>>,
end: Element<'a, Message>,
}
impl<'a, Message> HeaderBarWidget<'a, Message> {
pub fn new(
start: Element<'a, Message>,
center: Option<Element<'a, Message>>,
end: Element<'a, Message>,
) -> Self {
Self { start, center, end }
}
fn elems(&self) -> impl Iterator<Item = &Element<'a, Message>> {
std::iter::once(&self.start)
.chain(std::iter::once(&self.end))
.chain(self.center.as_ref())
}
fn elems_mut(&mut self) -> impl Iterator<Item = &mut Element<'a, Message>> {
std::iter::once(&mut self.start)
.chain(std::iter::once(&mut self.end))
.chain(self.center.as_mut())
}
}
impl<'a, Message: Clone + 'static> Widget<Message, crate::Theme, crate::Renderer>
for HeaderBarWidget<'a, Message>
{
2024-03-18 16:58:02 -04:00
fn diff(&mut self, tree: &mut tree::Tree) {
if let Some(center) = &mut self.center {
tree.diff_children(&mut [&mut self.start, &mut self.end, center]);
} else {
tree.diff_children(&mut [&mut self.start, &mut self.end]);
}
2024-03-18 16:58:02 -04:00
}
fn children(&self) -> Vec<tree::Tree> {
self.elems().map(tree::Tree::new).collect()
}
fn size(&self) -> Size<Length> {
Size {
width: Length::Fill,
height: Length::Shrink,
}
}
fn layout(
2026-02-10 15:37:41 -05:00
&mut self,
tree: &mut tree::Tree,
renderer: &crate::Renderer,
limits: &layout::Limits,
) -> layout::Node {
let width = limits.max().width;
let height = limits.max().height;
let gap = 8.0;
let end_node =
self.end
.as_widget_mut()
.layout(&mut tree.children[1], renderer, &limits.loose());
let end_width = end_node.size().width;
let start_available = (width - end_width - gap).max(0.0);
let start_node = self.start.as_widget_mut().layout(
&mut tree.children[0],
renderer,
&layout::Limits::new(Size::ZERO, Size::new(start_available, height)),
);
let start_width = start_node.size().width;
let vcenter = |node: layout::Node, x: f32| -> layout::Node {
let dy = ((height - node.size().height) / 2.0).max(0.0);
node.translate(Vector::new(x, dy))
};
let mut child_nodes = Vec::with_capacity(3);
child_nodes.push(vcenter(start_node, 0.0));
child_nodes.push(vcenter(end_node, width - end_width));
if let Some(center) = &mut self.center {
let slot_start = start_width + gap;
let slot_end = (width - end_width - gap).max(slot_start);
let slot_width = slot_end - slot_start;
// this instead of `node.size().width` prevents center jitter as text ellipsizes
let natural_width = center
.as_widget_mut()
.layout(&mut tree.children[2], renderer, &limits.loose())
.size()
.width;
let node = center.as_widget_mut().layout(
&mut tree.children[2],
renderer,
&layout::Limits::new(Size::ZERO, Size::new(slot_width, height)),
);
let ideal_x = (width - natural_width) / 2.0;
let max_x = (width - end_width - gap - natural_width).max(slot_start);
let center_x = ideal_x.clamp(slot_start, max_x);
child_nodes.push(vcenter(node, center_x))
}
layout::Node::with_children(Size::new(width, height), child_nodes)
}
fn draw(
&self,
tree: &tree::Tree,
renderer: &mut crate::Renderer,
2024-01-30 22:14:00 -05:00
theme: &crate::Theme,
style: &iced_core::renderer::Style,
layout: iced_core::Layout<'_>,
cursor: iced_core::mouse::Cursor,
viewport: &iced_core::Rectangle,
) {
self.elems()
.zip(&tree.children)
.zip(layout.children())
.for_each(|((e, s), l)| {
e.as_widget()
.draw(s, renderer, theme, style, l, cursor, viewport);
});
}
2026-02-10 15:37:41 -05:00
fn update(
&mut self,
state: &mut tree::Tree,
2026-02-10 15:37:41 -05:00
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,
2026-02-10 15:37:41 -05:00
) {
self.elems_mut()
.zip(&mut state.children)
.zip(layout.children())
.for_each(|((e, s), l)| {
e.as_widget_mut()
.update(s, event, l, 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 {
self.elems()
.zip(&state.children)
.zip(layout.children())
.map(|((e, s), l)| {
e.as_widget()
.mouse_interaction(s, l, cursor, viewport, renderer)
})
.max()
.unwrap_or(iced_core::mouse::Interaction::None)
}
fn operate(
2026-02-10 15:37:41 -05:00
&mut self,
state: &mut tree::Tree,
layout: iced_core::Layout<'_>,
renderer: &crate::Renderer,
2024-10-16 20:36:46 -04:00
operation: &mut dyn iced_core::widget::Operation<()>,
) {
self.elems_mut()
.zip(&mut state.children)
.zip(layout.children())
.for_each(|((e, s), l)| {
e.as_widget_mut().operate(s, l, renderer, operation);
});
}
fn overlay<'b>(
&'b mut self,
state: &'b mut tree::Tree,
2026-02-10 15:37:41 -05:00
layout: iced_core::Layout<'b>,
renderer: &crate::Renderer,
2026-02-10 15:37:41 -05:00
viewport: &iced_core::Rectangle,
2024-10-16 20:36:46 -04:00
translation: Vector,
2024-01-30 22:14:00 -05:00
) -> Option<iced_core::overlay::Element<'b, Message, crate::Theme, crate::Renderer>> {
self.elems_mut()
.zip(&mut state.children)
.zip(layout.children())
.find_map(|((e, s), l)| {
e.as_widget_mut()
.overlay(s, l, renderer, viewport, translation)
})
}
fn drag_destinations(
&self,
state: &tree::Tree,
layout: iced_core::Layout<'_>,
renderer: &crate::Renderer,
2024-10-16 20:36:46 -04:00
dnd_rectangles: &mut iced_core::clipboard::DndDestinationRectangles,
) {
self.elems()
.zip(&state.children)
.zip(layout.children())
.for_each(|((e, s), l)| {
e.as_widget()
.drag_destinations(s, l, renderer, dnd_rectangles);
});
}
2024-11-11 16:58:38 -05:00
#[cfg(feature = "a11y")]
/// get the a11y nodes for the widget
fn a11y_nodes(
&self,
layout: iced_core::Layout<'_>,
state: &tree::Tree,
p: iced::mouse::Cursor,
) -> iced_accessibility::A11yTree {
iced_accessibility::A11yTree::join(
self.elems()
.zip(&state.children)
.zip(layout.children())
.map(|((e, s), l)| e.as_widget().a11y_nodes(l, s, p)),
)
}
}
impl<'a, Message: Clone + 'static> From<HeaderBarWidget<'a, Message>> for Element<'a, Message> {
fn from(w: HeaderBarWidget<'a, Message>) -> Self {
Element::new(w)
2024-11-11 16:58:38 -05:00
}
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 view(mut self) -> Element<'a, Message> {
let Spacing {
space_xxxs,
space_xxs,
..
} = theme::spacing();
2023-08-02 11:54:07 +02:00
// Take ownership of the regions to be packed.
let mut start = std::mem::take(&mut self.start);
2023-08-02 11:54:07 +02:00
let center = std::mem::take(&mut self.center);
let mut end = std::mem::take(&mut self.end);
// Pack window controls on the configured side (reads CosmicTk
// config when the builder did not set an explicit override).
let controls_position = self
.controls_position
.unwrap_or_else(crate::config::window_controls_position);
let controls = self.window_controls(space_xxs, controls_position);
match controls_position {
WindowControlsPosition::End => end.push(controls),
WindowControlsPosition::Start => start.insert(0, controls),
}
let padding = if self.is_ssd {
[2, 8, 2, 8]
} else {
match (
self.density.unwrap_or_else(crate::config::header_size),
self.maximized, // window border handling
) {
(Density::Compact, true) => [4, 8, 4, 8],
(Density::Compact, false) => [3, 7, 4, 7],
(_, true) => [8, 8, 8, 8],
(_, false) => [7, 7, 8, 7],
}
};
let start = widget::row::with_children(start)
.spacing(space_xxxs)
.align_y(iced::Alignment::Center)
.into();
let center = if !center.is_empty() {
Some(
widget::row::with_children(center)
.spacing(space_xxxs)
2024-10-16 20:36:46 -04:00
.align_y(iced::Alignment::Center)
.into(),
)
} else if !self.title.is_empty() {
Some(
widget::text::heading(self.title)
.wrapping(text::Wrapping::None)
.ellipsize(text::Ellipsize::End(text::EllipsizeHeightLimit::Lines(1)))
.into(),
)
} else {
None
};
let end = widget::row::with_children(end)
.spacing(space_xxs)
2024-10-16 20:36:46 -04:00
.align_y(iced::Alignment::Center)
.into();
let mut widget = HeaderBarWidget::new(start, center, end)
2022-12-19 17:03:13 +01:00
.apply(widget::container)
.class(theme::Container::HeaderBar {
focused: self.focused,
2025-10-03 18:19:19 +02:00
sharp_corners: self.sharp_corners,
transparent: self.transparent,
})
.height(Length::Fixed(32.0 + padding[0] as f32 + padding[2] as f32))
.padding(padding)
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);
if let Some(message) = self.on_drag {
widget = widget.on_drag(message);
}
if let Some(message) = self.on_maximize {
widget = widget.on_release(message);
2022-10-09 02:35:03 -07:00
}
if let Some(message) = self.on_double_click {
widget = widget.on_double_press(message);
}
if let Some(message) = self.on_right_click {
widget = widget.on_right_press(message);
}
widget.into()
}
2022-10-09 02:35:03 -07:00
/// Creates the widget for window controls.
fn window_controls(
&mut self,
spacing: u16,
controls_position: WindowControlsPosition,
) -> Element<'a, Message> {
macro_rules! icon {
($name:expr, $size:expr, $on_press:expr) => {{
widget::icon::from_name($name)
.apply(widget::button::icon)
.padding(8)
.class(theme::Button::HeaderBar)
.selected(self.focused)
.icon_size($size)
.on_press($on_press)
}};
}
2022-10-09 02:35:03 -07:00
let minimize = self
.on_minimize
.take()
.map(|m| icon!("window-minimize-symbolic", 16, m));
let maximize = self.on_maximize.take().map(|m| {
if self.maximized {
icon!("window-restore-symbolic", 16, m)
} else {
icon!("window-maximize-symbolic", 16, m)
}
});
let close = self
.on_close
.take()
.map(|m| icon!("window-close-symbolic", 16, m));
// Icon order follows the OS convention for the chosen side:
// End → minimize, maximize, close (Linux / GNOME)
// Start → close, minimize, maximize (macOS)
let row = widget::row::with_capacity(3);
let row = match controls_position {
WindowControlsPosition::End => row
.push_maybe(minimize)
.push_maybe(maximize)
.push_maybe(close),
WindowControlsPosition::Start => row
.push_maybe(close)
.push_maybe(minimize)
.push_maybe(maximize),
};
row.spacing(spacing)
.align_y(iced::Alignment::Center)
2022-10-09 02:35:03 -07:00
.into()
}
}
impl<'a, Message: Clone + 'static> From<HeaderBar<'a, Message>> for Element<'a, Message> {
fn from(headerbar: HeaderBar<'a, Message>) -> Self {
headerbar.view()
}
}