Skip to content

Commit

Permalink
v0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaoboy committed Jun 11, 2024
1 parent 0a4a040 commit 3bb9bb9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "run-cat"
version = "0.1.1"
version = "0.1.3"
edition = "2021"
repository = "https://github.com/ahaoboy/run-cat"
homepage = "https://github.com/ahaoboy/run-cat"
Expand All @@ -12,10 +12,8 @@ description = "A cute running cat animation on your windows taskbar."
tray-icon = "0.14.3"
image = "0.25.1"
sysinfo = "0.30.12"
winit="0.29"
winit = "0.29"

# [target."cfg(target_os = \"unix\")".dev-dependencies]
# gtk = "0.18"
[target."cfg(target_os = \"linux\")".dependencies]
libappindicator = "0.9"
dirs = "5"
Expand All @@ -36,6 +34,11 @@ ci = "github"
# The installers to generate for each app
installers = []
# Target platforms to build apps for (Rust target-triple syntax)
targets = ["aarch64-apple-darwin", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc"]
targets = [
"aarch64-apple-darwin",
"x86_64-apple-darwin",
"x86_64-pc-windows-msvc",
]

# Publish jobs to run in CI
pr-run-mode = "plan"
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ fn main() {
"install",
"libgtk-3-dev",
"libxdo-dev",
"libpango1.0-dev",
"libayatana-appindicator3-dev",
"-y",
])
Expand Down
102 changes: 53 additions & 49 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use tray_icon::{
menu::{Menu, MenuEvent, MenuItem},
TrayIconBuilder, TrayIconEvent,
TrayIconBuilder,
};
use winit::event_loop::{ControlFlow, EventLoop};

Expand All @@ -28,7 +28,7 @@ fn get_fps(cpu: f32) -> u64 {
return fps;
}
}
return 30;
30
}

fn main() {
Expand Down Expand Up @@ -76,7 +76,9 @@ fn main() {
let dark_item = MenuItem::new("dark", true, None);
let light_item = MenuItem::new("light", true, None);

tray_menu.append_items(&[&dark_item, &light_item, &quit_item]).unwrap();
tray_menu
.append_items(&[&dark_item, &light_item, &quit_item])
.unwrap();

let tray_icon = TrayIconBuilder::new()
.with_menu(Box::new(tray_menu))
Expand All @@ -97,53 +99,55 @@ fn main() {
let cpu_gap = 300;
let mut last_avg_cpu_usage = 0.;

event_loop.run(move |_, event_loop| {
// We add delay of 16 ms (60fps) to event_loop to reduce cpu load.
// This can be removed to allow ControlFlow::Poll to poll on each cpu cycle
// Alternatively, you can set ControlFlow::Wait or use TrayIconEvent::set_event_handler,
// see https://github.com/tauri-apps/tray-icon/issues/83#issuecomment-1697773065
event_loop.set_control_flow(ControlFlow::WaitUntil(
std::time::Instant::now() + std::time::Duration::from_millis(16),
));

if let Ok(event) = menu_channel.try_recv() {
if event.id == quit_item.id() {
std::process::exit(0);
}
if event.id == dark_item.id() {
theme = "dark";
event_loop
.run(move |_, event_loop| {
// We add delay of 16 ms (60fps) to event_loop to reduce cpu load.
// This can be removed to allow ControlFlow::Poll to poll on each cpu cycle
// Alternatively, you can set ControlFlow::Wait or use TrayIconEvent::set_event_handler,
// see https://github.com/tauri-apps/tray-icon/issues/83#issuecomment-1697773065
event_loop.set_control_flow(ControlFlow::WaitUntil(
std::time::Instant::now() + std::time::Duration::from_millis(16),
));

if let Ok(event) = menu_channel.try_recv() {
if event.id == quit_item.id() {
std::process::exit(0);
}
if event.id == dark_item.id() {
theme = "dark";
}
if event.id == light_item.id() {
theme = "light";
}
}
if event.id == light_item.id() {
theme = "light";
}
}

let now = std::time::Instant::now();

sys.refresh_cpu();
let avg = if last_refresh_cpu.elapsed().as_millis() < cpu_gap {
last_avg_cpu_usage
} else {
last_refresh_cpu = now;
let avg =
sys.cpus().iter().map(|i| i.cpu_usage()).sum::<f32>() / sys.cpus().len() as f32;
last_avg_cpu_usage = avg;
avg
};

let fps: u64 = get_fps(avg);
let ms = 1000 / fps;

if last_update_ts.elapsed().as_millis() >= ms.into() {
last_update_ts = now;
let list = match theme {
"dark" => &dark_cat_icons,
"light" => &light_cat_icons,
_ => &dark_cat_icons,
let now = std::time::Instant::now();

sys.refresh_cpu();
let avg = if last_refresh_cpu.elapsed().as_millis() < cpu_gap {
last_avg_cpu_usage
} else {
last_refresh_cpu = now;
let avg =
sys.cpus().iter().map(|i| i.cpu_usage()).sum::<f32>() / sys.cpus().len() as f32;
last_avg_cpu_usage = avg;
avg
};
let icon = &list[c % dark_cat_icons.len()];
c += 1;
tray_icon.set_icon(Some(icon.clone())).unwrap();
}
}).unwrap();

let fps: u64 = get_fps(avg);
let ms = 1000 / fps;

if last_update_ts.elapsed().as_millis() >= ms.into() {
last_update_ts = now;
let list = match theme {
"dark" => &dark_cat_icons,
"light" => &light_cat_icons,
_ => &dark_cat_icons,
};
let icon = &list[c % dark_cat_icons.len()];
c += 1;
tray_icon.set_icon(Some(icon.clone())).unwrap();
}
})
.unwrap();
}

0 comments on commit 3bb9bb9

Please sign in to comment.