libcosmic/src/widget/header_bar.rs
Ashley Wulber e056e8c830
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

150 lines
4.4 KiB
Rust

// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use crate::{theme, Element};
use apply::Apply;
use derive_setters::Setters;
use iced::{self, widget, Length};
use iced_core::renderer::BorderRadius;
use std::borrow::Cow;
#[must_use]
pub fn header_bar<'a, Message>() -> HeaderBar<'a, Message> {
HeaderBar {
title: "".into(),
on_close: None,
on_drag: None,
on_maximize: None,
on_minimize: None,
start: None,
center: None,
end: None,
}
}
#[derive(Setters)]
pub struct HeaderBar<'a, Message> {
#[setters(into)]
title: Cow<'a, str>,
#[setters(strip_option)]
on_close: Option<Message>,
#[setters(strip_option)]
on_drag: Option<Message>,
#[setters(strip_option)]
on_maximize: Option<Message>,
#[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>>,
}
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> {
let mut packed: Vec<Element<Message>> = Vec::with_capacity(4);
if let Some(start) = self.start.take() {
packed.push(
widget::container(start)
.align_x(iced::alignment::Horizontal::Left)
.into(),
);
}
packed.push(if let Some(center) = self.center.take() {
widget::container(center)
.align_x(iced::alignment::Horizontal::Center)
.into()
} else if self.title.is_empty() {
widget::horizontal_space(Length::Fill).into()
} else {
self.title_widget()
});
packed.push(if let Some(end) = self.end.take() {
widget::row(vec![end, self.window_controls()])
.apply(widget::container)
.align_x(iced::alignment::Horizontal::Right)
.into()
} else {
self.window_controls()
});
let mut widget = widget::row(packed)
.height(Length::Fixed(50.0))
.padding(8)
.spacing(8)
.apply(widget::container)
.style(crate::theme::Container::HeaderBar)
.center_y()
.apply(widget::mouse_area);
if let Some(message) = self.on_drag.clone() {
widget = widget.on_press(message);
}
if let Some(message) = self.on_maximize.clone() {
widget = widget.on_release(message);
}
widget.into()
}
fn title_widget(&mut self) -> Element<'a, Message> {
let mut title = Cow::default();
std::mem::swap(&mut title, &mut self.title);
super::text(title)
.size(18)
.font(crate::font::FONT_SEMIBOLD)
.apply(widget::container)
.center_x()
.center_y()
.width(Length::Fill)
.height(Length::Fill)
.into()
}
/// Creates the widget for window controls.
fn window_controls(&mut self) -> Element<'a, Message> {
let mut widgets: Vec<Element<_>> = Vec::with_capacity(3);
let icon = |name, size, on_press| {
super::icon(name, size)
.force_svg(true)
.style(crate::theme::Svg::SymbolicActive)
.apply(widget::button)
.style(theme::Button::Text)
.on_press(on_press)
};
if let Some(message) = self.on_minimize.take() {
widgets.push(icon("window-minimize-symbolic", 16, message).into());
}
if let Some(message) = self.on_maximize.take() {
widgets.push(icon("window-maximize-symbolic", 16, message).into());
}
if let Some(message) = self.on_close.take() {
widgets.push(icon("window-close-symbolic", 16, message).into());
}
widget::row(widgets)
.spacing(8)
.apply(widget::container)
.height(Length::Fill)
.center_y()
.into()
}
}
impl<'a, Message: Clone + 'static> From<HeaderBar<'a, Message>> for Element<'a, Message> {
fn from(headerbar: HeaderBar<'a, Message>) -> Self {
headerbar.into_element()
}
}