Skip to main content

cosmic/app/
settings.rs

1// Copyright 2023 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4//! Configure a new COSMIC application.
5
6use crate::Theme;
7use iced_core::Font;
8use iced_core::layout::Limits;
9
10/// Configure a new COSMIC application.
11#[allow(clippy::struct_excessive_bools)]
12#[must_use]
13#[derive(derive_setters::Setters)]
14pub struct Settings {
15    /// Produces a smoother result in some widgets, at a performance cost.
16    pub(crate) antialiasing: bool,
17
18    /// Autosize the window to fit its contents
19    #[cfg(wayland_platform)]
20    pub(crate) autosize: bool,
21
22    /// Set the application to not create a main window
23    pub(crate) no_main_window: bool,
24
25    /// Whether the window should have a border, a title bar, etc. or not.
26    pub(crate) client_decorations: bool,
27
28    /// Enables debug features in cosmic/iced.
29    pub(crate) debug: bool,
30
31    /// The default [`Font`] to be used.
32    ///
33    /// When unset, the font configured by the active theme is used.
34    #[setters(skip)]
35    pub(crate) default_font: Option<Font>,
36
37    /// Name of the icon theme to search by default.
38    #[setters(skip)]
39    pub(crate) default_icon_theme: Option<String>,
40
41    /// Default size of fonts.
42    pub(crate) default_text_size: f32,
43
44    /// Set the default mmap threshold for malloc with mallopt.
45    pub(crate) default_mmap_threshold: Option<i32>,
46
47    /// Whether the window should be resizable or not.
48    /// and the size of the window border which can be dragged for a resize
49    pub(crate) resizable: Option<f64>,
50
51    /// Scale factor to use by default.
52    pub(crate) scale_factor: f32,
53
54    /// Initial size of the window.
55    pub(crate) size: iced::Size,
56
57    /// Limitations of the window size
58    pub(crate) size_limits: Limits,
59
60    /// The theme to apply to the application.
61    pub(crate) theme: Theme,
62
63    /// Whether the window should be transparent.
64    pub(crate) transparent: bool,
65
66    /// Whether the application window should close when the exit button is pressed
67    pub(crate) exit_on_close: bool,
68
69    /// Whether the application should act as a daemon
70    pub(crate) is_daemon: bool,
71}
72
73impl Settings {
74    /// Override the font used by the application.
75    ///
76    /// By default, the font from the active theme is used.
77    pub fn default_font(mut self, value: impl Into<Font>) -> Self {
78        self.default_font = Some(value.into());
79        self
80    }
81
82    /// Sets the default icon theme, passing an empty string will use the icon
83    /// theme from the active theme instead.
84    pub fn default_icon_theme(mut self, value: impl Into<String>) -> Self {
85        let value: String = value.into();
86        self.default_icon_theme = if value.is_empty() { None } else { Some(value) };
87        self
88    }
89}
90
91impl Default for Settings {
92    fn default() -> Self {
93        Self {
94            antialiasing: true,
95            #[cfg(wayland_platform)]
96            autosize: false,
97            no_main_window: false,
98            client_decorations: true,
99            debug: false,
100            default_font: None,
101            default_icon_theme: None,
102            default_text_size: 14.0,
103            default_mmap_threshold: Some(128 * 1024),
104            resizable: Some(8.0),
105            scale_factor: std::env::var("COSMIC_SCALE")
106                .ok()
107                .and_then(|scale| scale.parse::<f32>().ok())
108                .unwrap_or(1.0),
109            size: iced::Size::new(1024.0, 768.0),
110            size_limits: Limits::NONE.min_height(1.0).min_width(1.0),
111            theme: crate::theme::system_preference(),
112            transparent: true,
113            exit_on_close: true,
114            is_daemon: true,
115        }
116    }
117}