Skip to content

Commit 53b374e

Browse files
committed
Only measure label width on startup and if the interface font have changed.
1 parent 687f955 commit 53b374e

File tree

1 file changed

+73
-49
lines changed

1 file changed

+73
-49
lines changed

src/app.rs

Lines changed: 73 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use cosmic::applet::cosmic_panel_config::PanelSize;
22
use cosmic::applet::{PanelType, Size};
3+
use cosmic::config::FontConfig;
34
use cosmic::cosmic_config::CosmicConfigEntry;
45
use cosmic::cosmic_theme::palette::bool_mask::BoolMask;
56
use cosmic::cosmic_theme::palette::{FromColor, WithAlpha};
@@ -184,6 +185,8 @@ pub struct Minimon {
184185
// Used to measure label width, have to be cached because slow to load
185186
font_system: FontSystem,
186187

188+
interface_font: Option<FontConfig>,
189+
187190
// Pre-calc the max width of labels to avoid panel wobble
188191
label_cpu_width: Option<f32>,
189192
label_gpu_width: Option<f32>,
@@ -246,6 +249,7 @@ pub enum Message {
246249
ToggleMemoryLabel(bool),
247250
ToggleMemoryPercentage(bool),
248251
ConfigChanged(Box<MinimonConfig>),
252+
ThemeChanged(Box<cosmic::config::CosmicTk>),
249253
LaunchSystemMonitor(&'static system_monitors::DesktopApp),
250254
RefreshRateChanged(f64),
251255
LabelSizeChanged(u16),
@@ -321,6 +325,7 @@ impl cosmic::Application for Minimon {
321325
on_ac: true,
322326
data_is_visible: false,
323327
font_system: FontSystem::new(),
328+
interface_font: None,
324329
label_cpu_width: None,
325330
label_gpu_width: None,
326331
label_network_width: None,
@@ -354,7 +359,7 @@ impl cosmic::Application for Minimon {
354359
iced::time::every(time::Duration::from_millis(3000))
355360
}
356361

357-
let mut subs: Vec<Subscription<Message>> = vec![
362+
let mut subscriptions: Vec<Subscription<Message>> = vec![
358363
time_subscription(&self.refresh_rate).map(|_| Message::Tick),
359364
self.core
360365
.watch_config(match self.core.applet.panel_type {
@@ -365,9 +370,15 @@ impl cosmic::Application for Minimon {
365370
.map(|u| Message::ConfigChanged(Box::new(u.config))),
366371
];
367372

368-
subs.push(slow_time_subscription().map(|_| Message::SlowTimer));
373+
subscriptions.push(slow_time_subscription().map(|_| Message::SlowTimer));
374+
375+
subscriptions.push(
376+
self.core
377+
.watch_config("com.system76.CosmicTk")
378+
.map(|u| Message::ThemeChanged(Box::new(u.config))),
379+
);
369380

370-
Subscription::batch(subs)
381+
Subscription::batch(subscriptions)
371382
}
372383

373384
fn on_close_requested(&self, id: Id) -> Option<Message> {
@@ -709,7 +720,18 @@ impl cosmic::Application for Minimon {
709720
/// background thread managed by the application's executor.
710721
fn update(&mut self, message: Self::Message) -> Task<Self::Message> {
711722
match message {
723+
Message::ThemeChanged(cosmictk) => {
724+
let new_font = cosmictk.interface_font;
725+
726+
if self.interface_font.as_ref() != Some(&new_font) {
727+
info!("Message::ThemeChanged. Font is now: {:?}", new_font);
728+
self.interface_font = Some(new_font);
729+
self.calculate_max_label_widths();
730+
}
731+
}
732+
712733
Message::TogglePopup => {
734+
info!("Message::TogglePopup");
713735
return if let Some(p) = self.popup.take() {
714736
self.colorpicker.deactivate();
715737
// but have to go back to sleep if settings closed
@@ -945,7 +967,6 @@ impl cosmic::Application for Minimon {
945967
}
946968
}
947969
}
948-
self.calculate_max_label_widths();
949970
}
950971

951972
Message::ToggleCpuChart(toggled) => {
@@ -1977,10 +1998,13 @@ impl Minimon {
19771998
} else if let Some(w) = width {
19781999
widget::text(text)
19792000
.size(size)
1980-
.width(w).wrapping(iced::core::text::Wrapping::None)
2001+
.width(w)
2002+
.wrapping(iced::core::text::Wrapping::None)
19812003
.align_x(Horizontal::Center)
19822004
} else {
1983-
widget::text(text).size(size).wrapping(iced::core::text::Wrapping::None)
2005+
widget::text(text)
2006+
.size(size)
2007+
.wrapping(iced::core::text::Wrapping::None)
19842008
}
19852009
}
19862010

@@ -2083,7 +2107,7 @@ impl Minimon {
20832107
.lines
20842108
.first()
20852109
.and_then(|line| line.layout_opt())
2086-
.and_then(|layouts| layouts.first().map(|layout| layout.w.ceil()+2.0))
2110+
.and_then(|layouts| layouts.first().map(|layout| layout.w.ceil() + 2.0))
20872111
}
20882112

20892113
fn calculate_max_label_widths(&mut self) {
@@ -2094,56 +2118,56 @@ impl Minimon {
20942118
Family as CosmicTextFamily, Style as TextStyle, Weight as TextWeight,
20952119
};
20962120

2097-
let font = font::default();
2098-
2099-
let family = match font.family {
2100-
IcedFamily::Monospace => CosmicTextFamily::Monospace,
2101-
IcedFamily::Serif => CosmicTextFamily::Serif,
2102-
IcedFamily::SansSerif => CosmicTextFamily::SansSerif,
2103-
IcedFamily::Name(name) => CosmicTextFamily::Name(name),
2104-
IcedFamily::Cursive => CosmicTextFamily::Cursive,
2105-
IcedFamily::Fantasy => CosmicTextFamily::Fantasy,
2106-
};
2121+
if let Some(font) = self.interface_font.clone().map(Into::<iced::Font>::into) {
2122+
let family = match font.family {
2123+
IcedFamily::Monospace => CosmicTextFamily::Monospace,
2124+
IcedFamily::Serif => CosmicTextFamily::Serif,
2125+
IcedFamily::SansSerif => CosmicTextFamily::SansSerif,
2126+
IcedFamily::Name(name) => CosmicTextFamily::Name(name),
2127+
IcedFamily::Cursive => CosmicTextFamily::Cursive,
2128+
IcedFamily::Fantasy => CosmicTextFamily::Fantasy,
2129+
};
21072130

2108-
let weight = match font.weight {
2109-
IcedWeight::Thin => TextWeight::THIN,
2110-
IcedWeight::ExtraLight => TextWeight::EXTRA_LIGHT,
2111-
IcedWeight::Light => TextWeight::LIGHT,
2112-
IcedWeight::Normal => TextWeight::NORMAL,
2113-
IcedWeight::Medium => TextWeight::MEDIUM,
2114-
IcedWeight::Bold => TextWeight::BOLD,
2115-
IcedWeight::ExtraBold => TextWeight::EXTRA_BOLD,
2116-
IcedWeight::Black => TextWeight::BLACK,
2117-
IcedWeight::Semibold => TextWeight::SEMIBOLD,
2118-
};
2131+
let weight = match font.weight {
2132+
IcedWeight::Thin => TextWeight::THIN,
2133+
IcedWeight::ExtraLight => TextWeight::EXTRA_LIGHT,
2134+
IcedWeight::Light => TextWeight::LIGHT,
2135+
IcedWeight::Normal => TextWeight::NORMAL,
2136+
IcedWeight::Medium => TextWeight::MEDIUM,
2137+
IcedWeight::Bold => TextWeight::BOLD,
2138+
IcedWeight::ExtraBold => TextWeight::EXTRA_BOLD,
2139+
IcedWeight::Black => TextWeight::BLACK,
2140+
IcedWeight::Semibold => TextWeight::SEMIBOLD,
2141+
};
21192142

2120-
let style = match font.style {
2121-
IcedStyle::Normal => TextStyle::Normal,
2122-
IcedStyle::Italic => TextStyle::Italic,
2123-
IcedStyle::Oblique => TextStyle::Oblique,
2124-
};
2143+
let style = match font.style {
2144+
IcedStyle::Normal => TextStyle::Normal,
2145+
IcedStyle::Italic => TextStyle::Italic,
2146+
IcedStyle::Oblique => TextStyle::Oblique,
2147+
};
21252148

2126-
let attrs = Attrs::new().family(family).weight(weight).style(style);
2149+
let attrs = Attrs::new().family(family).weight(weight).style(style);
21272150

2128-
let is_horizontal = self.core.applet.is_horizontal();
2151+
let is_horizontal = self.core.applet.is_horizontal();
21292152

2130-
self.label_cpu_width = self.measure_text_width("8.88%", &attrs);
2131-
self.label_gpu_width = self.label_cpu_width;
2153+
self.label_cpu_width = self.measure_text_width("8.88%", &attrs);
2154+
self.label_gpu_width = self.label_cpu_width;
21322155

2133-
self.label_network_width = match (self.config.network1.show_bytes, is_horizontal) {
2134-
(false, false) => self.measure_text_width("8.88M", &attrs),
2135-
(false, true) => self.measure_text_width("8.88 Mbps", &attrs),
2136-
(true, false) => self.measure_text_width("8.88M", &attrs),
2137-
(true, true) => self.measure_text_width("8.88 MB/s", &attrs),
2138-
};
2156+
self.label_network_width = match (self.config.network1.show_bytes, is_horizontal) {
2157+
(false, false) => self.measure_text_width("8.88M", &attrs),
2158+
(false, true) => self.measure_text_width("8.88 Mbps", &attrs),
2159+
(true, false) => self.measure_text_width("8.88M", &attrs),
2160+
(true, true) => self.measure_text_width("8.88 MB/s", &attrs),
2161+
};
21392162

2140-
self.label_disks_width = if is_horizontal {
2141-
self.measure_text_width("8.88 MB/s", &attrs)
2142-
} else {
2143-
self.measure_text_width("8.88M", &attrs)
2144-
};
2163+
self.label_disks_width = if is_horizontal {
2164+
self.measure_text_width("8.88 MB/s", &attrs)
2165+
} else {
2166+
self.measure_text_width("8.88M", &attrs)
2167+
};
21452168

2146-
self.label_w_width = self.measure_text_width("W ", &attrs);
2169+
self.label_w_width = self.measure_text_width("W ", &attrs);
2170+
}
21472171
}
21482172

21492173
fn open_tipping_page_in_browser() {

0 commit comments

Comments
 (0)