Skip to main content

cosmic/config/
mod.rs

1// Copyright 2024 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4//! Configurations available to libcosmic applications.
5
6use crate::cosmic_theme::Density;
7use cosmic_config::cosmic_config_derive::CosmicConfigEntry;
8use cosmic_config::{Config, CosmicConfigEntry};
9use std::sync::{LazyLock, RwLock};
10
11/// ID for the `CosmicTk` config.
12pub const ID: &str = "com.system76.CosmicTk";
13
14pub static COSMIC_TK: LazyLock<RwLock<CosmicTk>> = LazyLock::new(|| {
15    RwLock::new(
16        CosmicTk::config()
17            .map(|c| {
18                CosmicTk::get_entry(&c).unwrap_or_else(|(errors, mode)| {
19                    for why in errors.into_iter().filter(cosmic_config::Error::is_err) {
20                        if let cosmic_config::Error::GetKey(_, err) = &why {
21                            if err.kind() == std::io::ErrorKind::NotFound {
22                                // No system default config installed; don't error
23                                continue;
24                            }
25                        }
26                        tracing::error!(?why, "CosmicTk config entry error");
27                    }
28                    mode
29                })
30            })
31            .unwrap_or_default(),
32    )
33});
34
35/// Apply the theme to other toolkits.
36#[allow(clippy::missing_panics_doc)]
37pub fn apply_theme_global() -> bool {
38    COSMIC_TK.read().unwrap().apply_theme_global
39}
40
41/// Show minimize button in window header.
42#[allow(clippy::missing_panics_doc)]
43pub fn show_minimize() -> bool {
44    COSMIC_TK.read().unwrap().show_minimize
45}
46
47/// Show maximize button in window header.
48#[allow(clippy::missing_panics_doc)]
49pub fn show_maximize() -> bool {
50    COSMIC_TK.read().unwrap().show_maximize
51}
52
53/// Density of CSD/SSD header bars.
54#[allow(clippy::missing_panics_doc)]
55pub fn header_size() -> Density {
56    COSMIC_TK.read().unwrap().header_size
57}
58
59/// Interface density.
60#[allow(clippy::missing_panics_doc)]
61pub fn interface_density() -> Density {
62    COSMIC_TK.read().unwrap().interface_density
63}
64
65#[derive(Clone, CosmicConfigEntry, Debug, Eq, PartialEq)]
66#[version = 1]
67pub struct CosmicTk {
68    /// Apply the theme to other toolkits.
69    pub apply_theme_global: bool,
70
71    /// Show minimize button in window header.
72    pub show_minimize: bool,
73
74    /// Show maximize button in window header.
75    pub show_maximize: bool,
76
77    /// Density of CSD/SSD header bars.
78    pub header_size: Density,
79
80    /// Interface density.
81    pub interface_density: Density,
82}
83
84impl Default for CosmicTk {
85    fn default() -> Self {
86        Self {
87            apply_theme_global: false,
88            show_minimize: true,
89            show_maximize: true,
90            header_size: Density::Standard,
91            interface_density: Density::Standard,
92        }
93    }
94}
95
96impl CosmicTk {
97    #[inline]
98    pub fn config() -> Result<Config, cosmic_config::Error> {
99        Config::new(ID, Self::VERSION)
100    }
101}