2023-08-02 11:54:07 +02:00
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2023-12-07 15:27:52 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
2023-10-16 16:19:04 -04:00
|
|
|
use cosmic_config::CosmicConfigEntry;
|
|
|
|
|
use cosmic_theme::ThemeMode;
|
2023-12-07 15:27:52 -05:00
|
|
|
use iced_core::window::Id;
|
2023-10-16 16:19:04 -04:00
|
|
|
|
2023-09-14 01:25:01 +02:00
|
|
|
use crate::Theme;
|
|
|
|
|
|
2023-08-02 11:54:07 +02:00
|
|
|
/// Status of the nav bar and its panels.
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct NavBar {
|
|
|
|
|
active: bool,
|
|
|
|
|
toggled: bool,
|
|
|
|
|
toggled_condensed: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// COSMIC-specific settings for windows.
|
|
|
|
|
#[allow(clippy::struct_excessive_bools)]
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct Window {
|
2023-10-12 13:23:59 +02:00
|
|
|
/// Label to display as context drawer title.
|
|
|
|
|
pub context_title: String,
|
|
|
|
|
/// Label to display as header bar title.
|
2023-09-13 15:26:51 +02:00
|
|
|
pub header_title: String,
|
2023-08-08 18:09:57 -04:00
|
|
|
pub use_template: bool,
|
2023-12-21 11:53:19 -07:00
|
|
|
pub content_container: bool,
|
2023-08-02 11:54:07 +02:00
|
|
|
pub can_fullscreen: bool,
|
|
|
|
|
pub sharp_corners: bool,
|
2023-10-12 13:23:59 +02:00
|
|
|
pub show_context: bool,
|
2023-08-02 11:54:07 +02:00
|
|
|
pub show_headerbar: bool,
|
|
|
|
|
pub show_window_menu: bool,
|
|
|
|
|
pub show_maximize: bool,
|
|
|
|
|
pub show_minimize: bool,
|
|
|
|
|
height: u32,
|
|
|
|
|
width: u32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// COSMIC-specific application settings
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct Core {
|
|
|
|
|
/// Enables debug features in cosmic/iced.
|
|
|
|
|
pub debug: bool,
|
|
|
|
|
|
|
|
|
|
/// Whether the window is too small for the nav bar + main content.
|
|
|
|
|
is_condensed: bool,
|
|
|
|
|
|
|
|
|
|
/// Current status of the nav bar panel.
|
|
|
|
|
nav_bar: NavBar,
|
|
|
|
|
|
|
|
|
|
/// Scaling factor used by the application
|
|
|
|
|
scale_factor: f32,
|
|
|
|
|
|
2023-10-26 14:21:07 -04:00
|
|
|
pub(super) theme_sub_counter: u64,
|
2023-09-14 01:25:01 +02:00
|
|
|
/// Last known system theme
|
|
|
|
|
pub(super) system_theme: Theme,
|
|
|
|
|
|
2023-10-16 16:19:04 -04:00
|
|
|
/// Theme mode
|
|
|
|
|
pub(super) system_theme_mode: ThemeMode,
|
|
|
|
|
|
2023-12-07 15:27:52 -05:00
|
|
|
pub(super) title: HashMap<Id, String>,
|
2023-09-18 07:45:11 +02:00
|
|
|
|
2023-08-02 11:54:07 +02:00
|
|
|
pub window: Window,
|
2023-09-18 07:45:11 +02:00
|
|
|
|
2023-08-08 18:09:57 -04:00
|
|
|
#[cfg(feature = "applet")]
|
2023-09-18 07:45:11 +02:00
|
|
|
pub applet: crate::applet::Context,
|
2023-11-13 16:28:48 -05:00
|
|
|
|
2023-11-21 15:18:39 -05:00
|
|
|
#[cfg(feature = "single-instance")]
|
|
|
|
|
pub(crate) single_instance: bool,
|
2023-12-15 17:00:08 -05:00
|
|
|
|
|
|
|
|
#[cfg(feature = "dbus-config")]
|
|
|
|
|
pub(crate) settings_daemon: Option<cosmic_settings_daemon::CosmicSettingsDaemonProxy<'static>>,
|
2023-08-02 11:54:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Core {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
debug: false,
|
|
|
|
|
is_condensed: false,
|
|
|
|
|
nav_bar: NavBar {
|
|
|
|
|
active: true,
|
|
|
|
|
toggled: true,
|
|
|
|
|
toggled_condensed: true,
|
|
|
|
|
},
|
|
|
|
|
scale_factor: 1.0,
|
2023-12-07 15:27:52 -05:00
|
|
|
title: HashMap::new(),
|
2023-10-26 14:21:07 -04:00
|
|
|
theme_sub_counter: 0,
|
2023-09-14 01:25:01 +02:00
|
|
|
system_theme: crate::theme::active(),
|
2023-10-16 16:19:04 -04:00
|
|
|
system_theme_mode: ThemeMode::config()
|
|
|
|
|
.map(|c| {
|
|
|
|
|
ThemeMode::get_entry(&c).unwrap_or_else(|(errors, mode)| {
|
|
|
|
|
for e in errors {
|
|
|
|
|
tracing::error!("{e}");
|
|
|
|
|
}
|
|
|
|
|
mode
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or_default(),
|
2023-08-02 11:54:07 +02:00
|
|
|
window: Window {
|
2023-10-12 13:23:59 +02:00
|
|
|
context_title: String::new(),
|
2023-09-13 15:26:51 +02:00
|
|
|
header_title: String::new(),
|
2023-08-08 18:09:57 -04:00
|
|
|
use_template: true,
|
2023-12-21 11:53:19 -07:00
|
|
|
content_container: true,
|
2023-08-02 11:54:07 +02:00
|
|
|
can_fullscreen: false,
|
|
|
|
|
sharp_corners: false,
|
2023-10-12 13:23:59 +02:00
|
|
|
show_context: false,
|
2023-08-02 11:54:07 +02:00
|
|
|
show_headerbar: true,
|
|
|
|
|
show_maximize: true,
|
|
|
|
|
show_minimize: true,
|
|
|
|
|
show_window_menu: false,
|
|
|
|
|
height: 0,
|
|
|
|
|
width: 0,
|
|
|
|
|
},
|
2023-08-08 18:09:57 -04:00
|
|
|
#[cfg(feature = "applet")]
|
2023-09-18 07:45:11 +02:00
|
|
|
applet: crate::applet::Context::default(),
|
2023-11-21 15:18:39 -05:00
|
|
|
#[cfg(feature = "single-instance")]
|
2023-11-13 16:28:48 -05:00
|
|
|
single_instance: false,
|
2023-12-15 17:00:08 -05:00
|
|
|
#[cfg(feature = "dbus-config")]
|
|
|
|
|
settings_daemon: None,
|
2023-08-02 11:54:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Core {
|
|
|
|
|
/// Whether the window is too small for the nav bar + main content.
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn is_condensed(&self) -> bool {
|
|
|
|
|
self.is_condensed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The scaling factor used by the application.
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn scale_factor(&self) -> f32 {
|
|
|
|
|
self.scale_factor
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Changes the scaling factor used by the application.
|
|
|
|
|
pub(crate) fn set_scale_factor(&mut self, factor: f32) {
|
|
|
|
|
self.scale_factor = factor;
|
|
|
|
|
self.is_condensed_update();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-12 13:36:23 +02:00
|
|
|
/// Set context drawer header title
|
|
|
|
|
pub fn set_context_title(&mut self, title: String) {
|
|
|
|
|
self.window.context_title = title;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set header bar title
|
|
|
|
|
pub fn set_header_title(&mut self, title: String) {
|
|
|
|
|
self.window.header_title = title;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-02 11:54:07 +02:00
|
|
|
/// Whether to show or hide the main window's content.
|
|
|
|
|
pub(crate) fn show_content(&self) -> bool {
|
|
|
|
|
!self.is_condensed || !self.nav_bar.toggled_condensed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Call this whenever the scaling factor or window width has changed.
|
|
|
|
|
#[allow(clippy::cast_precision_loss)]
|
|
|
|
|
fn is_condensed_update(&mut self) {
|
|
|
|
|
self.is_condensed = (600.0 * self.scale_factor) > self.window.width as f32;
|
|
|
|
|
self.nav_bar_update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Whether the nav panel is visible or not
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn nav_bar_active(&self) -> bool {
|
|
|
|
|
self.nav_bar.active
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn nav_bar_toggle(&mut self) {
|
|
|
|
|
self.nav_bar.toggled = !self.nav_bar.toggled;
|
|
|
|
|
self.nav_bar_set_toggled_condensed(self.nav_bar.toggled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn nav_bar_toggle_condensed(&mut self) {
|
|
|
|
|
self.nav_bar_set_toggled_condensed(!self.nav_bar.toggled_condensed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn nav_bar_set_toggled_condensed(&mut self, toggled: bool) {
|
|
|
|
|
self.nav_bar.toggled_condensed = toggled;
|
|
|
|
|
self.nav_bar_update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn nav_bar_update(&mut self) {
|
|
|
|
|
self.nav_bar.active = if self.is_condensed {
|
|
|
|
|
self.nav_bar.toggled_condensed
|
|
|
|
|
} else {
|
|
|
|
|
self.nav_bar.toggled
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set the height of the main window.
|
|
|
|
|
pub(crate) fn set_window_height(&mut self, new_height: u32) {
|
|
|
|
|
self.window.height = new_height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set the width of the main window.
|
|
|
|
|
pub(crate) fn set_window_width(&mut self, new_width: u32) {
|
|
|
|
|
self.window.width = new_width;
|
|
|
|
|
self.is_condensed_update();
|
|
|
|
|
}
|
2023-10-16 16:19:04 -04:00
|
|
|
|
|
|
|
|
/// Get the current system theme
|
|
|
|
|
pub fn system_theme(&self) -> &Theme {
|
|
|
|
|
&self.system_theme
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
|
/// Get the current system theme mode
|
|
|
|
|
pub fn system_theme_mode(&self) -> ThemeMode {
|
|
|
|
|
self.system_theme_mode
|
|
|
|
|
}
|
2023-12-15 17:00:08 -05:00
|
|
|
|
|
|
|
|
#[cfg(feature = "dbus-config")]
|
|
|
|
|
pub fn watch_config<T: CosmicConfigEntry + Send + Sync + Default + 'static + Clone>(
|
|
|
|
|
&self,
|
|
|
|
|
config_id: &'static str,
|
2023-12-28 18:25:12 -05:00
|
|
|
) -> iced::Subscription<cosmic_config::dbus::Update<T>> {
|
2023-12-15 17:00:08 -05:00
|
|
|
if let Some(settings_daemon) = self.settings_daemon.clone() {
|
2023-12-28 18:25:12 -05:00
|
|
|
cosmic_config::dbus::watcher_subscription(settings_daemon, config_id, false)
|
|
|
|
|
} else {
|
|
|
|
|
iced::Subscription::none()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "dbus-config")]
|
|
|
|
|
pub fn watch_state<T: CosmicConfigEntry + Send + Sync + Default + 'static + Clone>(
|
|
|
|
|
&self,
|
|
|
|
|
state_id: &'static str,
|
|
|
|
|
) -> iced::Subscription<cosmic_config::dbus::Update<T>> {
|
|
|
|
|
if let Some(settings_daemon) = self.settings_daemon.clone() {
|
|
|
|
|
cosmic_config::dbus::watcher_subscription(settings_daemon, state_id, true)
|
2023-12-15 17:00:08 -05:00
|
|
|
} else {
|
|
|
|
|
iced::Subscription::none()
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-02 11:54:07 +02:00
|
|
|
}
|