From d9eb14e2bc9e8e62cf2d4457fe8948779b6fc22e Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Sat, 28 Sep 2024 16:20:20 +0200 Subject: [PATCH] config: store font names in Arc --- src/config/handler.rs | 13 ++++++++----- src/theme.rs | 14 ++++++++------ src/tree/container.rs | 2 +- src/tree/float.rs | 2 +- src/tree/output.rs | 2 +- src/utils/clonecell.rs | 2 ++ 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/config/handler.rs b/src/config/handler.rs index 418d10c6..0477b79b 100644 --- a/src/config/handler.rs +++ b/src/config/handler.rs @@ -13,7 +13,7 @@ use { output_schedule::map_cursor_hz, scale::Scale, state::{ConnectorData, DeviceHandlerData, DrmDevData, OutputData, State}, - theme::{Color, ThemeSized, DEFAULT_FONT}, + theme::{Color, ThemeSized}, tree::{ move_ws_to_output, ContainerNode, ContainerSplit, FloatNode, Node, NodeVisitorBase, OutputNode, TearingMode, VrrMode, WsMoveConfig, @@ -57,7 +57,7 @@ use { }, libloading::Library, log::Level, - std::{cell::Cell, ops::Deref, rc::Rc, time::Duration}, + std::{cell::Cell, ops::Deref, rc::Rc, sync::Arc, time::Duration}, thiserror::Error, uapi::{c, fcntl_dupfd_cloexec, OwnedFd}, }; @@ -1525,15 +1525,18 @@ impl ConfigProxyHandler { } fn handle_reset_font(&self) { - *self.state.theme.font.borrow_mut() = DEFAULT_FONT.to_string(); + self.state + .theme + .font + .set(self.state.theme.default_font.clone()); } fn handle_set_font(&self, font: &str) { - *self.state.theme.font.borrow_mut() = font.to_string(); + self.state.theme.font.set(Arc::new(font.to_string())); } fn handle_get_font(&self) { - let font = self.state.theme.font.borrow_mut().clone(); + let font = self.state.theme.font.get().to_string(); self.respond(Response::GetFont { font }); } diff --git a/src/theme.rs b/src/theme.rs index ef351d31..44a38635 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -1,7 +1,6 @@ -use std::{ - cell::{Cell, RefCell}, - cmp::Ordering, - ops::Mul, +use { + crate::utils::clonecell::CloneCell, + std::{cell::Cell, cmp::Ordering, ops::Mul, sync::Arc}, }; #[derive(Copy, Clone, Debug, PartialEq)] @@ -290,15 +289,18 @@ pub const DEFAULT_FONT: &str = "monospace 8"; pub struct Theme { pub colors: ThemeColors, pub sizes: ThemeSizes, - pub font: RefCell, + pub font: CloneCell>, + pub default_font: Arc, } impl Default for Theme { fn default() -> Self { + let default_font = Arc::new(DEFAULT_FONT.to_string()); Self { colors: Default::default(), sizes: Default::default(), - font: RefCell::new(DEFAULT_FONT.to_string()), + font: CloneCell::new(default_font.clone()), + default_font, } } } diff --git a/src/tree/container.rs b/src/tree/container.rs index af6f6171..9216a8f2 100644 --- a/src/tree/container.rs +++ b/src/tree/container.rs @@ -673,7 +673,7 @@ impl ContainerNode { let theme = &self.state.theme; let th = theme.sizes.title_height.get(); let bw = theme.sizes.border_width.get(); - let font = theme.font.borrow_mut(); + let font = theme.font.get(); let cwidth = self.width.get(); let cheight = self.height.get(); let ctx = self.state.render_ctx.get(); diff --git a/src/tree/float.rs b/src/tree/float.rs index 11929502..6dd6acf3 100644 --- a/src/tree/float.rs +++ b/src/tree/float.rs @@ -191,7 +191,7 @@ impl FloatNode { false => theme.colors.unfocused_title_text.get(), }; let bw = theme.sizes.border_width.get(); - let font = theme.font.borrow_mut(); + let font = theme.font.get(); let title = self.title.borrow_mut(); let pos = self.position.get(); if pos.width() <= 2 * bw || title.is_empty() { diff --git a/src/tree/output.rs b/src/tree/output.rs index 93b43aef..b8ff7b6b 100644 --- a/src/tree/output.rs +++ b/src/tree/output.rs @@ -377,7 +377,7 @@ impl OutputNode { rd.active_workspace = None; rd.status = None; let mut pos = 0; - let font = self.state.theme.font.borrow_mut(); + let font = self.state.theme.font.get(); let theme = &self.state.theme; let th = theme.sizes.title_height.get(); let scale = self.global.persistent.scale.get(); diff --git a/src/utils/clonecell.rs b/src/utils/clonecell.rs index 08597b75..1364aa81 100644 --- a/src/utils/clonecell.rs +++ b/src/utils/clonecell.rs @@ -9,6 +9,7 @@ use { fmt::{Debug, Formatter}, mem, rc::{Rc, Weak}, + sync::Arc, }, }; @@ -83,6 +84,7 @@ unsafe impl UnsafeCellCloneSafe for Option {} unsafe impl UnsafeCellCloneSafe for Rc {} unsafe impl UnsafeCellCloneSafe for Weak {} +unsafe impl UnsafeCellCloneSafe for Arc {} unsafe impl UnsafeCellCloneSafe for NodeRef {}