fix: Use globals instead of thread-locals

Better support for multi-threaded applications,
especially cosmic-comp rendering in parallel on
multiple threads, each potentially accessing
global configurations such as the active theme,
icon_theme and more...
This commit is contained in:
Victoria Brekenfeld 2024-08-02 20:00:16 +02:00 committed by Michael Murphy
parent f655710d55
commit b40839638a
16 changed files with 183 additions and 216 deletions

View file

@ -31,95 +31,85 @@ pub fn icon<'a, Message>(handle: impl Into<Handle>) -> Button<'a, Message> {
impl<'a, Message> Button<'a, Message> {
pub fn new(icon: Icon) -> Self {
crate::theme::THEME.with(|theme_cell| {
let theme = theme_cell.borrow();
let theme = theme.cosmic();
let padding = theme.space_xxs();
let guard = crate::theme::THEME.lock().unwrap();
let theme = guard.cosmic();
let padding = theme.space_xxs();
Self {
id: Id::unique(),
label: Cow::Borrowed(""),
tooltip: Cow::Borrowed(""),
on_press: None,
width: Length::Shrink,
height: Length::Shrink,
padding: Padding::from(padding),
spacing: theme.space_xxxs(),
icon_size: if icon.handle.symbolic { 16 } else { 24 },
line_height: 20,
font_size: 14,
font_weight: Weight::Normal,
style: Style::Icon,
variant: icon,
}
})
Self {
id: Id::unique(),
label: Cow::Borrowed(""),
tooltip: Cow::Borrowed(""),
on_press: None,
width: Length::Shrink,
height: Length::Shrink,
padding: Padding::from(padding),
spacing: theme.space_xxxs(),
icon_size: if icon.handle.symbolic { 16 } else { 24 },
line_height: 20,
font_size: 14,
font_weight: Weight::Normal,
style: Style::Icon,
variant: icon,
}
}
/// Applies the **Extra Small** button size preset.
pub fn extra_small(mut self) -> Self {
crate::theme::THEME.with(|theme_cell| {
let theme = theme_cell.borrow();
let theme = theme.cosmic();
let guard = crate::theme::THEME.lock().unwrap();
let theme = guard.cosmic();
self.font_size = 14;
self.font_weight = Weight::Normal;
self.icon_size = 16;
self.line_height = 20;
self.padding = Padding::from(theme.space_xxs());
self.spacing = theme.space_xxxs();
});
self.font_size = 14;
self.font_weight = Weight::Normal;
self.icon_size = 16;
self.line_height = 20;
self.padding = Padding::from(theme.space_xxs());
self.spacing = theme.space_xxxs();
self
}
/// Applies the **Medium** button size preset.
pub fn medium(mut self) -> Self {
crate::theme::THEME.with(|theme_cell| {
let theme = theme_cell.borrow();
let theme = theme.cosmic();
let guard = crate::theme::THEME.lock().unwrap();
let theme = guard.cosmic();
self.font_size = 24;
self.font_weight = Weight::Normal;
self.icon_size = 32;
self.line_height = 32;
self.padding = Padding::from(theme.space_xs());
self.spacing = theme.space_xxs();
});
self.font_size = 24;
self.font_weight = Weight::Normal;
self.icon_size = 32;
self.line_height = 32;
self.padding = Padding::from(theme.space_xs());
self.spacing = theme.space_xxs();
self
}
/// Applies the **Large** button size preset.
pub fn large(mut self) -> Self {
crate::theme::THEME.with(|theme_cell| {
let theme = theme_cell.borrow();
let theme = theme.cosmic();
let guard = crate::theme::THEME.lock().unwrap();
let theme = guard.cosmic();
self.font_size = 28;
self.font_weight = Weight::Normal;
self.icon_size = 40;
self.line_height = 36;
self.padding = Padding::from(theme.space_xs());
self.spacing = theme.space_xxs();
});
self.font_size = 28;
self.font_weight = Weight::Normal;
self.icon_size = 40;
self.line_height = 36;
self.padding = Padding::from(theme.space_xs());
self.spacing = theme.space_xxs();
self
}
/// Applies the **Extra Large** button size preset.
pub fn extra_large(mut self) -> Self {
crate::theme::THEME.with(|theme_cell| {
let theme = theme_cell.borrow();
let theme = theme.cosmic();
let padding = theme.space_xs();
let guard = crate::theme::THEME.lock().unwrap();
let theme = guard.cosmic();
let padding = theme.space_xs();
self.font_size = 32;
self.font_weight = Weight::Light;
self.icon_size = 56;
self.line_height = 44;
self.padding = Padding::from(padding);
self.spacing = theme.space_xxs();
});
self.font_size = 32;
self.font_weight = Weight::Light;
self.icon_size = 56;
self.line_height = 44;
self.padding = Padding::from(padding);
self.spacing = theme.space_xxs();
self
}

View file

@ -44,7 +44,7 @@ pub struct Appearance {
impl Appearance {
// TODO: `Radius` is not `const fn` compatible.
pub fn new() -> Self {
let rad_0 = THEME.with(|t| t.borrow().cosmic().corner_radii.radius_0);
let rad_0 = THEME.lock().unwrap().cosmic().corner_radii.radius_0;
Self {
shadow_offset: Vector::new(0.0, 0.0),
background: None,

View file

@ -51,26 +51,24 @@ impl Text {
impl<'a, Message> Button<'a, Message> {
pub fn new(text: Text) -> Self {
crate::theme::THEME.with(|theme_cell| {
let theme = theme_cell.borrow();
let theme = theme.cosmic();
Self {
id: Id::unique(),
label: Cow::Borrowed(""),
tooltip: Cow::Borrowed(""),
on_press: None,
width: Length::Shrink,
height: Length::Fixed(theme.space_l().into()),
padding: Padding::from([0, theme.space_s()]),
spacing: theme.space_xxxs(),
icon_size: 16,
line_height: 20,
font_size: 14,
font_weight: Weight::Normal,
style: Style::Standard,
variant: text,
}
})
let guard = crate::theme::THEME.lock().unwrap();
let theme = guard.cosmic();
Self {
id: Id::unique(),
label: Cow::Borrowed(""),
tooltip: Cow::Borrowed(""),
on_press: None,
width: Length::Shrink,
height: Length::Fixed(theme.space_l().into()),
padding: Padding::from([0, theme.space_s()]),
spacing: theme.space_xxxs(),
icon_size: 16,
line_height: 20,
font_size: 14,
font_weight: Weight::Normal,
style: Style::Standard,
variant: text,
}
}
pub fn leading_icon(mut self, icon: impl Into<icon::Handle>) -> Self {

View file

@ -423,7 +423,7 @@ impl<'a, Message: 'a + Clone> Widget<Message, crate::Theme, crate::Renderer>
renderer.with_layer(parent_bounds, |renderer| {
let selection_background = theme.selection_background();
let c_rad = THEME.with(|t| t.borrow().cosmic().corner_radii);
let c_rad = THEME.lock().unwrap().cosmic().corner_radii;
// NOTE: Workaround to round the border of the unselected, unhovered image.
if !self.selected && !is_mouse_over {