From 6720b8277c91d05f3bd6a83d92191cba96f51873 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Mon, 27 May 2024 21:49:49 +0200 Subject: [PATCH] feat: CosmicTk::header_size density config --- Cargo.toml | 4 +- src/app/core.rs | 13 ----- src/app/cosmic.rs | 4 +- src/app/mod.rs | 6 +-- src/config/mod.rs | 102 +++++++++++++++++++++++++++++++++-- src/config/toolkit.rs | 43 --------------- src/widget/header_bar.rs | 21 ++++---- src/widget/nav_bar_toggle.rs | 4 -- 8 files changed, 118 insertions(+), 79 deletions(-) delete mode 100644 src/config/toolkit.rs diff --git a/Cargo.toml b/Cargo.toml index 8b7a2b3c..ad7570de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ desktop = ["process", "dep:freedesktop-desktop-entry", "dep:mime", "dep:shlex"] # Enables keycode serialization serde-keycode = ["iced_core/serde"] # Prevents multiple separate process instances. -single-instance = ["dep:zbus", "serde", "ron"] +single-instance = ["dep:zbus", "ron"] # smol async runtime smol = ["iced/smol", "zbus?/async-io"] tokio = [ @@ -80,7 +80,7 @@ mime = { version = "0.3.17", optional = true } nix = { version = "0.27", features = ["process"], optional = true } palette = "0.7.3" rfd = { version = "0.14.0", optional = true } -serde = { version = "1.0.180", optional = true } +serde = { version = "1.0.180", features = ["derive"]} slotmap = "1.0.6" thiserror = "1.0.44" tokio = { version = "1.24.2", optional = true } diff --git a/src/app/core.rs b/src/app/core.rs index 6594f4a7..eeb0ae22 100644 --- a/src/app/core.rs +++ b/src/app/core.rs @@ -73,9 +73,6 @@ pub struct Core { /// Configured theme mode pub(super) system_theme_mode: ThemeMode, - /// Libcosmic toolkit configuration. - pub(super) toolkit_config: CosmicTk, - pub(super) portal_is_dark: Option, pub(super) portal_accent: Option, @@ -123,16 +120,6 @@ impl Default for Core { }) }) .unwrap_or_default(), - toolkit_config: CosmicTk::config() - .map(|c| { - CosmicTk::get_entry(&c).unwrap_or_else(|(errors, mode)| { - for why in errors { - tracing::error!(?why, "CosmicTk config entry error"); - } - mode - }) - }) - .unwrap_or_default(), window: Window { context_title: String::new(), header_title: String::new(), diff --git a/src/app/cosmic.rs b/src/app/cosmic.rs index bdec9233..21ddf636 100644 --- a/src/app/cosmic.rs +++ b/src/app/cosmic.rs @@ -198,7 +198,7 @@ where self.app.subscription().map(super::Message::App), self.app .core() - .watch_config::(crate::config::toolkit::ID) + .watch_config::(crate::config::ID) .map(|update| { for why in update.errors { tracing::error!(?why, "cosmic toolkit config update error"); @@ -628,7 +628,7 @@ impl Cosmic { crate::icon_theme::set_default(config.icon_theme.clone()); } - self.app.core_mut().toolkit_config = config; + crate::config::COSMIC_TK.with(|tk| *tk.borrow_mut() = config); } Message::Focus(f) => { diff --git a/src/app/mod.rs b/src/app/mod.rs index 49d631ee..aa5b5183 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -84,7 +84,7 @@ pub(crate) fn iced_settings( if let Some(icon_theme) = settings.default_icon_theme { crate::icon_theme::set_default(icon_theme); } else { - crate::icon_theme::set_default(core.toolkit_config.icon_theme.clone()); + crate::icon_theme::set_default(crate::config::icon_theme()); } THEME.with(move |t| { @@ -714,11 +714,11 @@ impl ApplicationExt for App { header = header.start(toggle); } - if core.window.show_maximize && core.toolkit_config.show_maximize { + if core.window.show_maximize && crate::config::show_maximize() { header = header.on_maximize(Message::Cosmic(cosmic::Message::Maximize)); } - if core.window.show_minimize && core.toolkit_config.show_minimize { + if core.window.show_minimize && crate::config::show_minimize() { header = header.on_minimize(Message::Cosmic(cosmic::Message::Minimize)); } diff --git a/src/config/mod.rs b/src/config/mod.rs index 0dca84db..15eb0bf5 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -3,6 +3,102 @@ //! Configurations available to libcosmic applications. -pub mod toolkit; -#[doc(inline)] -pub use toolkit::CosmicTk; +use cosmic_config::cosmic_config_derive::CosmicConfigEntry; +use cosmic_config::{Config, CosmicConfigEntry}; +use serde::{Deserialize, Serialize}; +use std::cell::RefCell; +use std::rc::Rc; + +/// ID for the `CosmicTk` config. +pub const ID: &str = "com.system76.CosmicTk"; + +thread_local! { + pub static COSMIC_TK: RefCell = RefCell::new(CosmicTk::config() + .map(|c| { + CosmicTk::get_entry(&c).unwrap_or_else(|(errors, mode)| { + for why in errors { + tracing::error!(?why, "CosmicTk config entry error"); + } + mode + }) + }) + .unwrap_or_default()) +} + +/// Apply the theme to other toolkits. +pub fn apply_theme_global() -> bool { + COSMIC_TK.with(|tk| tk.borrow().apply_theme_global) +} + +/// Show minimize button in window header. +pub fn show_minimize() -> bool { + COSMIC_TK.with(|tk| tk.borrow().show_minimize) +} + +/// Show maximize button in window header. +pub fn show_maximize() -> bool { + COSMIC_TK.with(|tk| tk.borrow().show_maximize) +} + +/// Preferred icon theme. +pub fn icon_theme() -> String { + COSMIC_TK.with(|tk| tk.borrow().icon_theme.clone()) +} + +/// Density of CSD/SSD header bars. +pub fn header_size() -> Density { + COSMIC_TK.with(|tk| tk.borrow().header_size) +} + +/// Interface density. +pub fn interface_density() -> Density { + COSMIC_TK.with(|tk| tk.borrow().interface_density) +} + +#[derive(Clone, CosmicConfigEntry, Debug, Eq, PartialEq)] +#[version = 1] +pub struct CosmicTk { + /// Apply the theme to other toolkits. + pub apply_theme_global: bool, + + /// Show minimize button in window header. + pub show_minimize: bool, + + /// Show maximize button in window header. + pub show_maximize: bool, + + /// Preferred icon theme. + pub icon_theme: String, + + /// Density of CSD/SSD header bars. + pub header_size: Density, + + /// Interface density. + pub interface_density: Density, +} + +#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] +pub enum Density { + Compact, + #[default] + Standard, +} + +impl Default for CosmicTk { + fn default() -> Self { + Self { + apply_theme_global: false, + show_minimize: true, + show_maximize: true, + icon_theme: String::from("Cosmic"), + header_size: Density::Standard, + interface_density: Density::Standard, + } + } +} + +impl CosmicTk { + pub fn config() -> Result { + Config::new(ID, Self::VERSION) + } +} diff --git a/src/config/toolkit.rs b/src/config/toolkit.rs deleted file mode 100644 index 2f738cb9..00000000 --- a/src/config/toolkit.rs +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2024 System76 -// SPDX-License-Identifier: MPL-2.0 - -//! Configurations for the libcosmic toolkit. - -use cosmic_config::cosmic_config_derive::CosmicConfigEntry; -use cosmic_config::{Config, CosmicConfigEntry}; - -/// ID for the `CosmicTk` config. -pub const ID: &str = "com.system76.CosmicTk"; - -#[derive(Clone, CosmicConfigEntry, Debug, Eq, PartialEq)] -#[version = 1] -pub struct CosmicTk { - /// Show minimize button in window header. - pub show_minimize: bool, - - /// Show maximize button in window header. - pub show_maximize: bool, - - /// Preferred icon theme. - pub icon_theme: String, - - /// Apply the theme to other toolkits. - pub apply_theme_global: bool, -} - -impl Default for CosmicTk { - fn default() -> Self { - Self { - show_minimize: true, - show_maximize: true, - icon_theme: String::from("Cosmic"), - apply_theme_global: false, - } - } -} - -impl CosmicTk { - pub fn config() -> Result { - Config::new(ID, Self::VERSION) - } -} diff --git a/src/widget/header_bar.rs b/src/widget/header_bar.rs index 6d47d2d5..af45fb09 100644 --- a/src/widget/header_bar.rs +++ b/src/widget/header_bar.rs @@ -4,7 +4,7 @@ use crate::{ext::CollectionWidget, widget, Element}; use apply::Apply; use derive_setters::Setters; -use iced::Length; +use iced::{Length, Padding}; use iced_core::{widget::tree, Widget}; use std::borrow::Cow; @@ -99,7 +99,7 @@ impl<'a, Message: Clone + 'static> HeaderBar<'a, Message> { #[must_use] pub fn build(self) -> HeaderBarWidget<'a, Message> { HeaderBarWidget { - header_bar_inner: self.into_element(), + header_bar_inner: self.view(), } } } @@ -253,7 +253,7 @@ impl<'a, Message: Clone + 'static> Widget HeaderBar<'a, Message> { /// Converts the headerbar builder into an Iced element. - pub fn into_element(mut self) -> Element<'a, Message> { + pub fn view(mut self) -> Element<'a, Message> { // Take ownership of the regions to be packed. let start = std::mem::take(&mut self.start); let center = std::mem::take(&mut self.center); @@ -263,6 +263,11 @@ impl<'a, Message: Clone + 'static> HeaderBar<'a, Message> { end.push(widget::horizontal_space(Length::Fixed(12.0)).into()); end.push(self.window_controls()); + let (height, padding) = match crate::config::header_size() { + crate::config::Density::Compact => (36.0, 2.0), + crate::config::Density::Standard => (48.0, 8.0), + }; + // Creates the headerbar widget. let mut widget = widget::row::with_capacity(4) // If elements exist in the start region, append them here. @@ -295,9 +300,9 @@ impl<'a, Message: Clone + 'static> HeaderBar<'a, Message> { .width(Length::Shrink), ) .align_items(iced::Alignment::Center) - .height(Length::Fixed(50.0)) - .padding(8) - .spacing(8) + .height(Length::Fixed(height)) + .padding(padding) + .spacing(padding) .apply(widget::container) .style(crate::theme::Container::HeaderBar { focused: self.focused, @@ -326,9 +331,7 @@ impl<'a, Message: Clone + 'static> HeaderBar<'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) + widget::text::heading(title) .apply(widget::container) .center_x() .center_y() diff --git a/src/widget/nav_bar_toggle.rs b/src/widget/nav_bar_toggle.rs index cd1ec67f..e39144ca 100644 --- a/src/widget/nav_bar_toggle.rs +++ b/src/widget/nav_bar_toggle.rs @@ -42,13 +42,9 @@ impl<'a, Message: 'static + Clone> From> for Element<'a, M }; widget::button::icon(icon) - .padding([8, 16, 8, 16]) .on_press_maybe(nav_bar_toggle.on_toggle) .selected(nav_bar_toggle.selected) .style(nav_bar_toggle.style) - .apply(widget::container) - .center_y() - .height(Length::Fill) .into() } }