perf(bar): use explicit redraw and data refresh strategies

This commit is contained in:
LGUG2Z
2024-08-26 08:01:21 -07:00
parent 18358efed8
commit 92bb9f680b
3 changed files with 36 additions and 10 deletions
+11 -5
View File
@@ -100,6 +100,7 @@ fn main() -> eframe::Result<()> {
.with_decorations(false) .with_decorations(false)
.with_transparent(config.transparent) .with_transparent(config.transparent)
.with_position(config.position) .with_position(config.position)
.with_taskbar(false)
.with_inner_size(config.inner_size), .with_inner_size(config.inner_size),
..Default::default() ..Default::default()
}; };
@@ -119,9 +120,15 @@ fn main() -> eframe::Result<()> {
"komorebi-bar", "komorebi-bar",
native_options, native_options,
Box::new(|cc| { Box::new(|cc| {
let frame = cc.egui_ctx.clone();
let config_cl = config_arc.clone(); let config_cl = config_arc.clone();
let ctx_repainter = cc.egui_ctx.clone();
std::thread::spawn(move || loop {
std::thread::sleep(Duration::from_secs(1));
ctx_repainter.request_repaint();
});
let ctx_komorebi = cc.egui_ctx.clone();
std::thread::spawn(move || { std::thread::spawn(move || {
let listener = komorebi_client::subscribe("komorebi-bar").unwrap(); let listener = komorebi_client::subscribe("komorebi-bar").unwrap();
@@ -165,7 +172,7 @@ fn main() -> eframe::Result<()> {
) )
{ {
tx_gui.send(notification).unwrap(); tx_gui.send(notification).unwrap();
frame.request_repaint(); ctx_komorebi.request_repaint();
} }
} }
Err(error) => { Err(error) => {
@@ -347,7 +354,6 @@ impl eframe::App for Komobar {
ui.with_layout(Layout::right_to_left(Align::Center), |ui| { ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
if self.time.enable { if self.time.enable {
for time in self.time.output() { for time in self.time.output() {
ctx.request_repaint();
if ui if ui
.add( .add(
Label::new(format!("🕐 {}", time)) Label::new(format!("🕐 {}", time))
@@ -393,7 +399,7 @@ impl eframe::App for Komobar {
.clicked() .clicked()
{ {
if let Err(error) = if let Err(error) =
Command::new("cmd.exe").args(["/C", "taskmgr.exe"]).output() Command::new("cmd.exe").args(["/C", "taskmgr.exe"]).spawn()
{ {
eprintln!("{}", error) eprintln!("{}", error)
} }
@@ -419,7 +425,7 @@ impl eframe::App for Komobar {
"explorer.exe", "explorer.exe",
disk.split(' ').collect::<Vec<&str>>()[0], disk.split(' ').collect::<Vec<&str>>()[0],
]) ])
.output() .spawn()
{ {
eprintln!("{}", error) eprintln!("{}", error)
} }
+16 -4
View File
@@ -1,10 +1,13 @@
use crate::widget::BarWidget; use crate::widget::BarWidget;
use std::time::Duration;
use std::time::Instant;
use sysinfo::RefreshKind; use sysinfo::RefreshKind;
use sysinfo::System; use sysinfo::System;
pub struct Memory { pub struct Memory {
pub enable: bool, pub enable: bool,
system: System, system: System,
last_updated: Instant,
} }
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
@@ -14,18 +17,27 @@ pub struct MemoryConfig {
impl From<MemoryConfig> for Memory { impl From<MemoryConfig> for Memory {
fn from(value: MemoryConfig) -> Self { fn from(value: MemoryConfig) -> Self {
let mut system =
System::new_with_specifics(RefreshKind::default().without_cpu().without_processes());
system.refresh_memory();
Self { Self {
enable: value.enable, enable: value.enable,
system: System::new_with_specifics( system,
RefreshKind::default().without_cpu().without_processes(), last_updated: Instant::now(),
),
} }
} }
} }
impl BarWidget for Memory { impl BarWidget for Memory {
fn output(&mut self) -> Vec<String> { fn output(&mut self) -> Vec<String> {
self.system.refresh_memory(); let now = Instant::now();
if now.duration_since(self.last_updated) > Duration::from_secs(10) {
self.system.refresh_memory();
self.last_updated = now;
}
let used = self.system.used_memory(); let used = self.system.used_memory();
let total = self.system.total_memory(); let total = self.system.total_memory();
vec![format!("RAM: {}%", (used * 100) / total)] vec![format!("RAM: {}%", (used * 100) / total)]
+9 -1
View File
@@ -1,4 +1,6 @@
use crate::widget::BarWidget; use crate::widget::BarWidget;
use std::time::Duration;
use std::time::Instant;
use sysinfo::Disks; use sysinfo::Disks;
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
@@ -11,6 +13,7 @@ impl From<StorageConfig> for Storage {
Self { Self {
enable: value.enable, enable: value.enable,
disks: Disks::new_with_refreshed_list(), disks: Disks::new_with_refreshed_list(),
last_updated: Instant::now(),
} }
} }
} }
@@ -18,11 +21,16 @@ impl From<StorageConfig> for Storage {
pub struct Storage { pub struct Storage {
pub enable: bool, pub enable: bool,
disks: Disks, disks: Disks,
last_updated: Instant,
} }
impl BarWidget for Storage { impl BarWidget for Storage {
fn output(&mut self) -> Vec<String> { fn output(&mut self) -> Vec<String> {
self.disks.refresh(); let now = Instant::now();
if now.duration_since(self.last_updated) > Duration::from_secs(10) {
self.disks.refresh();
self.last_updated = now;
}
let mut disks = vec![]; let mut disks = vec![];