From 4f07d05ee86ba613e48d075a5052003f9e37712c Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Tue, 5 Mar 2024 16:05:33 +0100 Subject: [PATCH] feat(config): add `CosmicTk` config for configuring window controls --- src/app/core.rs | 18 ++++++++++++++++-- src/app/cosmic.rs | 29 +++++++++++++++++++++++------ src/app/mod.rs | 4 ++-- src/config/mod.rs | 7 +++++++ src/config/toolkit.rs | 35 +++++++++++++++++++++++++++++++++++ src/lib.rs | 5 ++++- 6 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 src/config/mod.rs create mode 100644 src/config/toolkit.rs diff --git a/src/app/core.rs b/src/app/core.rs index 072d53a7..f40ae384 100644 --- a/src/app/core.rs +++ b/src/app/core.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; +use crate::config::CosmicTk; use cosmic_config::CosmicConfigEntry; use cosmic_theme::ThemeMode; use iced_core::window::Id; @@ -63,6 +64,9 @@ 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, @@ -101,8 +105,18 @@ impl Default for Core { system_theme_mode: ThemeMode::config() .map(|c| { ThemeMode::get_entry(&c).unwrap_or_else(|(errors, mode)| { - for e in errors { - tracing::error!("{e}"); + for why in errors { + tracing::error!(?why, "ThemeMode config entry error"); + } + mode + }) + }) + .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 }) diff --git a/src/app/cosmic.rs b/src/app/cosmic.rs index 37565986..45833bab 100644 --- a/src/app/cosmic.rs +++ b/src/app/cosmic.rs @@ -4,6 +4,7 @@ use std::sync::Arc; use super::{command, Application, ApplicationExt, Core, Subscription}; +use crate::config::CosmicTk; use crate::theme::{self, Theme, ThemeType, THEME}; use crate::widget::nav_bar; use crate::{keyboard_nav, Element}; @@ -49,14 +50,16 @@ pub enum Message { NavBar(nav_bar::Id), /// Set scaling factor ScaleFactor(f32), - /// Toggles visibility of the nav bar. - ToggleNavBar, - /// Toggles the condensed status of the nav bar. - ToggleNavBarCondensed, /// Notification of system theme changes. SystemThemeChange(Vec<&'static str>, Theme), /// Notification of system theme mode changes. SystemThemeModeChange(Vec<&'static str>, ThemeMode), + /// Toggles visibility of the nav bar. + ToggleNavBar, + /// Toggles the condensed status of the nav bar. + ToggleNavBarCondensed, + /// Toolkit configuration update + ToolkitConfig(CosmicTk), /// Updates the window maximized state WindowMaximized(window::Id, bool), /// Updates the tracked window geometry. @@ -182,6 +185,16 @@ where let mut subscriptions = vec![ self.app.subscription().map(super::Message::App), + self.app + .core() + .watch_config::(crate::config::toolkit::ID) + .map(|update| { + for why in update.errors { + tracing::error!(?why, "cosmic toolkit config update error"); + } + + super::Message::Cosmic(Message::ToolkitConfig(update.config)) + }), self.app .core() .watch_config::(if self.app.core().system_theme_mode.is_dark { @@ -190,8 +203,8 @@ where cosmic_theme::LIGHT_THEME_ID }) .map(|update| { - for e in update.errors { - tracing::error!("{e}"); + for why in update.errors { + tracing::error!(?why, "cosmic theme config update error"); } Message::SystemThemeChange( update.keys, @@ -540,6 +553,10 @@ impl Cosmic { Message::DesktopSettings(crate::theme::portal::Desktop::Contrast(_)) => { // TODO when high contrast is integrated in settings and all custom themes } + + Message::ToolkitConfig(config) => { + self.app.core_mut().toolkit_config = config; + } } iced::Command::none() diff --git a/src/app/mod.rs b/src/app/mod.rs index 6ac5234f..d7fd3221 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -704,11 +704,11 @@ impl ApplicationExt for App { header = header.start(toggle); } - if core.window.show_maximize { + if core.window.show_maximize && core.toolkit_config.show_maximize { header = header.on_maximize(Message::Cosmic(cosmic::Message::Maximize)); } - if core.window.show_minimize { + if core.window.show_minimize && core.toolkit_config.show_minimize { header = header.on_minimize(Message::Cosmic(cosmic::Message::Minimize)); } diff --git a/src/config/mod.rs b/src/config/mod.rs new file mode 100644 index 00000000..78bf068a --- /dev/null +++ b/src/config/mod.rs @@ -0,0 +1,7 @@ +// Copyright 2024 System76 +// SPDX-License-Identifier: MPL-2.0 + +//! Configurations available to libcosmic applications. + +pub mod toolkit; +pub use toolkit::CosmicTk; diff --git a/src/config/toolkit.rs b/src/config/toolkit.rs new file mode 100644 index 00000000..56395d56 --- /dev/null +++ b/src/config/toolkit.rs @@ -0,0 +1,35 @@ +// 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, +} + +impl Default for CosmicTk { + fn default() -> Self { + Self { + show_minimize: true, + show_maximize: true, + } + } +} + +impl CosmicTk { + pub fn config() -> Result { + Config::new(ID, Self::VERSION) + } +} diff --git a/src/lib.rs b/src/lib.rs index abb49e30..fe23fb5a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ #![allow(clippy::module_name_repetitions)] #[cfg(all(feature = "wayland", feature = "winit"))] -compile_error!("cannot use `wayland` feature with `winit"); +compile_error!("cannot use `wayland` feature with `winit`"); /// Recommended default imports. pub mod prelude { @@ -26,6 +26,9 @@ pub mod applet; pub use iced::Command; pub mod command; + +pub mod config; + pub use cosmic_config; pub use cosmic_theme;