feat(config): add CosmicTk config for configuring window controls

This commit is contained in:
Michael Aaron Murphy 2024-03-05 16:05:33 +01:00 committed by Jeremy Soller
parent 912f0665ef
commit 4f07d05ee8
6 changed files with 87 additions and 11 deletions

View file

@ -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<bool>,
pub(super) portal_accent: Option<Srgba>,
@ -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
})

View file

@ -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::CosmicTk>(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::<cosmic_theme::Theme>(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<T: Application> Cosmic<T> {
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()

View file

@ -704,11 +704,11 @@ impl<App: Application> 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));
}

7
src/config/mod.rs Normal file
View file

@ -0,0 +1,7 @@
// Copyright 2024 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
//! Configurations available to libcosmic applications.
pub mod toolkit;
pub use toolkit::CosmicTk;

35
src/config/toolkit.rs Normal file
View file

@ -0,0 +1,35 @@
// Copyright 2024 System76 <info@system76.com>
// 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, cosmic_config::Error> {
Config::new(ID, Self::VERSION)
}
}

View file

@ -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;