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

View File

@@ -100,6 +100,7 @@ fn main() -> eframe::Result<()> {
.with_decorations(false)
.with_transparent(config.transparent)
.with_position(config.position)
.with_taskbar(false)
.with_inner_size(config.inner_size),
..Default::default()
};
@@ -119,9 +120,15 @@ fn main() -> eframe::Result<()> {
"komorebi-bar",
native_options,
Box::new(|cc| {
let frame = cc.egui_ctx.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 || {
let listener = komorebi_client::subscribe("komorebi-bar").unwrap();
@@ -165,7 +172,7 @@ fn main() -> eframe::Result<()> {
)
{
tx_gui.send(notification).unwrap();
frame.request_repaint();
ctx_komorebi.request_repaint();
}
}
Err(error) => {
@@ -347,7 +354,6 @@ impl eframe::App for Komobar {
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
if self.time.enable {
for time in self.time.output() {
ctx.request_repaint();
if ui
.add(
Label::new(format!("🕐 {}", time))
@@ -393,7 +399,7 @@ impl eframe::App for Komobar {
.clicked()
{
if let Err(error) =
Command::new("cmd.exe").args(["/C", "taskmgr.exe"]).output()
Command::new("cmd.exe").args(["/C", "taskmgr.exe"]).spawn()
{
eprintln!("{}", error)
}
@@ -419,7 +425,7 @@ impl eframe::App for Komobar {
"explorer.exe",
disk.split(' ').collect::<Vec<&str>>()[0],
])
.output()
.spawn()
{
eprintln!("{}", error)
}

View File

@@ -1,10 +1,13 @@
use crate::widget::BarWidget;
use std::time::Duration;
use std::time::Instant;
use sysinfo::RefreshKind;
use sysinfo::System;
pub struct Memory {
pub enable: bool,
system: System,
last_updated: Instant,
}
#[derive(Copy, Clone, Debug)]
@@ -14,18 +17,27 @@ pub struct MemoryConfig {
impl From<MemoryConfig> for Memory {
fn from(value: MemoryConfig) -> Self {
let mut system =
System::new_with_specifics(RefreshKind::default().without_cpu().without_processes());
system.refresh_memory();
Self {
enable: value.enable,
system: System::new_with_specifics(
RefreshKind::default().without_cpu().without_processes(),
),
system,
last_updated: Instant::now(),
}
}
}
impl BarWidget for Memory {
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 total = self.system.total_memory();
vec![format!("RAM: {}%", (used * 100) / total)]

View File

@@ -1,4 +1,6 @@
use crate::widget::BarWidget;
use std::time::Duration;
use std::time::Instant;
use sysinfo::Disks;
#[derive(Copy, Clone, Debug)]
@@ -11,6 +13,7 @@ impl From<StorageConfig> for Storage {
Self {
enable: value.enable,
disks: Disks::new_with_refreshed_list(),
last_updated: Instant::now(),
}
}
}
@@ -18,11 +21,16 @@ impl From<StorageConfig> for Storage {
pub struct Storage {
pub enable: bool,
disks: Disks,
last_updated: Instant,
}
impl BarWidget for Storage {
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![];