Skip to content

Commit

Permalink
Refactor AppTheme and related functions to use Arc for shared ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
surinder83singh committed Dec 9, 2024
1 parent 01e0a96 commit 962f914
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 39 deletions.
7 changes: 4 additions & 3 deletions core/src/egui/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ impl<'panel, Context> Panel<'panel, Context> {
}

pub fn render(self, ui: &mut Ui) {
let icon_size = theme_style().panel_icon_size();
let theme_style = theme_style();
let icon_size = theme_style.panel_icon_size();
let icon_padding = (icon_size.outer - icon_size.inner) / 2.0;
let panel_margin_size = theme_style().panel_margin_size();
let panel_margin_size = theme_style.panel_margin_size();
let panel_width = ui.available_width();
let inner_panel_width = panel_width - panel_margin_size * 2.;

Expand Down Expand Up @@ -190,7 +191,7 @@ impl<'panel, Context> Panel<'panel, Context> {
});
}

let padding = ui.available_height() - theme_style().panel_footer_height;
let padding = ui.available_height() - theme_style.panel_footer_height;
if padding > 0. {
ui.add_space(padding);
}
Expand Down
15 changes: 9 additions & 6 deletions core/src/egui/theme/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,20 @@ impl ThemeColor {
}
}

static mut THEME_COLOR_LIST: Option<HashMap<String, ThemeColor>> = None;
pub fn theme_colors() -> &'static HashMap<String, ThemeColor> {
unsafe {
THEME_COLOR_LIST.get_or_insert_with(|| {
static THEME_COLOR_LIST: Mutex<Option<Arc<HashMap<String, ThemeColor>>>> = Mutex::new(None);

#[inline(always)]
pub fn theme_colors() -> Arc<HashMap<String, ThemeColor>> {
let mut colors_lock = THEME_COLOR_LIST.lock().unwrap();
colors_lock
.get_or_insert_with(|| {
let mut themes = HashMap::new();
[ThemeColor::dark(), ThemeColor::light()]
.into_iter()
.for_each(|theme| {
themes.insert(theme.name.clone(), theme.clone());
});
themes
Arc::new(themes)
})
}
.clone()
}
47 changes: 27 additions & 20 deletions core/src/egui/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ use kaspa_metrics_core::MetricGroup;
mod color;
pub use color::*;
mod style;
pub use style::*;

use crate::imports::*;
pub use style::*;

#[derive(Clone)]
pub struct AppTheme {
pub color: ThemeColor,
pub style: ThemeStyle,
pub color: Arc<ThemeColor>,
pub style: Arc<ThemeStyle>,
}

impl AppTheme {
pub fn new(color: ThemeColor, style: ThemeStyle) -> Self {
pub fn new(color: Arc<ThemeColor>, style: Arc<ThemeStyle>) -> Self {
Self { color, style }
}

Expand All @@ -32,8 +31,8 @@ impl AppTheme {
impl Default for AppTheme {
fn default() -> Self {
Self {
color: ThemeColor::dark(),
style: ThemeStyle::rounded(),
color: Arc::new(ThemeColor::dark()),
style: Arc::new(ThemeStyle::rounded()),
}
}
}
Expand Down Expand Up @@ -68,20 +67,24 @@ impl AsRef<AppTheme> for AppTheme {
}
}

static mut THEME: Option<AppTheme> = None;
static THEME: Mutex<Option<Arc<AppTheme>>> = Mutex::new(None);

#[inline(always)]
pub fn theme() -> &'static AppTheme {
unsafe { THEME.get_or_insert_with(AppTheme::default) }
pub fn theme() -> Arc<AppTheme> {
let mut theme_lock = THEME.lock().unwrap();
theme_lock
.get_or_insert_with(|| Arc::new(AppTheme::default()))
.clone()
}

#[inline(always)]
pub fn theme_color() -> &'static ThemeColor {
&theme().color
pub fn theme_color() -> Arc<ThemeColor> {
Arc::clone(&theme().color)
}

#[inline(always)]
pub fn theme_style() -> &'static ThemeStyle {
&theme().style
pub fn theme_style() -> Arc<ThemeStyle> {
Arc::clone(&theme().style)
}

pub fn apply_theme_by_name(
Expand All @@ -107,7 +110,7 @@ pub fn apply_theme_by_name(
ThemeStyle::default()
});

apply_theme(ctx, AppTheme::new(theme_color, theme_style));
apply_theme(ctx, AppTheme::new(theme_color.into(), theme_style.into()));
}

pub fn apply_theme_color_by_name(ctx: &Context, theme_color_name: impl Into<String>) {
Expand All @@ -120,7 +123,10 @@ pub fn apply_theme_color_by_name(ctx: &Context, theme_color_name: impl Into<Stri
ThemeColor::default()
});

apply_theme(ctx, AppTheme::new(theme_color, theme_style().clone()));
apply_theme(
ctx,
AppTheme::new(theme_color.into(), theme_style().clone()),
);
}

pub fn apply_theme_style_by_name(ctx: &Context, theme_style_name: impl Into<String>) {
Expand All @@ -133,13 +139,14 @@ pub fn apply_theme_style_by_name(ctx: &Context, theme_style_name: impl Into<Stri
ThemeStyle::default()
});

apply_theme(ctx, AppTheme::new(theme_color().clone(), theme_style));
apply_theme(
ctx,
AppTheme::new(theme_color().clone(), theme_style.into()),
);
}

pub fn apply_theme(ctx: &Context, theme: AppTheme) {
unsafe {
THEME = Some(theme.clone());
}
let _ = THEME.lock().unwrap().insert(Arc::new(theme.clone()));
ctx.set_visuals(theme.as_ref().into());
runtime()
.application_events()
Expand Down
15 changes: 9 additions & 6 deletions core/src/egui/theme/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,20 @@ impl ThemeStyle {
}
}

static mut THEME_STYLE_LIST: Option<HashMap<String, ThemeStyle>> = None;
pub fn theme_styles() -> &'static HashMap<String, ThemeStyle> {
unsafe {
THEME_STYLE_LIST.get_or_insert_with(|| {
static THEME_STYLE_LIST: Mutex<Option<Arc<HashMap<String, ThemeStyle>>>> = Mutex::new(None);

#[inline(always)]
pub fn theme_styles() -> Arc<HashMap<String, ThemeStyle>> {
let mut theme_styles_lock = THEME_STYLE_LIST.lock().unwrap();
theme_styles_lock
.get_or_insert_with(|| {
let mut themes = HashMap::new();
[ThemeStyle::rounded(), ThemeStyle::sharp()]
.into_iter()
.for_each(|theme| {
themes.insert(theme.name.clone(), theme.clone());
});
themes
Arc::new(themes)
})
}
.clone()
}
6 changes: 4 additions & 2 deletions core/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,8 @@ impl<'core> Menu<'core> {
ui.label(i18n("Theme Color"));
ui.vertical(|ui| {
ui.horizontal(|ui| {
let current_theme_color_name = theme_color().name();
let theme_color = theme_color();
let current_theme_color_name = theme_color.name();
ui.menu_button(format!("{} ⏷", current_theme_color_name), |ui| {
theme_colors().keys().for_each(|name| {
if name.as_str() != current_theme_color_name
Expand All @@ -552,7 +553,8 @@ impl<'core> Menu<'core> {

ui.label(i18n("Theme Style"));
ui.horizontal(|ui| {
let current_theme_style_name = theme_style().name();
let theme_style = theme_style();
let current_theme_style_name = theme_style.name();
ui.menu_button(format!("{} ⏷", current_theme_style_name), |ui| {
theme_styles().keys().for_each(|name| {
if name.as_str() != current_theme_style_name
Expand Down
6 changes: 4 additions & 2 deletions core/src/modules/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,8 @@ impl Settings {
.show(ui, |ui| {
ui.vertical(|ui| {
ui.horizontal(|ui| {
let current_theme_color_name = theme_color().name();
let theme_color = theme_color();
let current_theme_color_name = theme_color.name();
ui.menu_button(
format!("{} ⏷", current_theme_color_name),
|ui| {
Expand Down Expand Up @@ -492,7 +493,8 @@ impl Settings {
.default_open(true)
.show(ui, |ui| {
ui.horizontal(|ui| {
let current_theme_style_name = theme_style().name();
let theme_style = theme_style();
let current_theme_style_name = theme_style.name();
ui.menu_button(
format!("{} ⏷", current_theme_style_name),
|ui| {
Expand Down

0 comments on commit 962f914

Please sign in to comment.