Compare commits

...

5 commits

Author SHA1 Message Date
Peter Cai a4c9d30e22 Use alphanumeric sorting for animation icon packs
Otherwise it will missort numbers like "11" and "1"
2022-11-20 21:40:14 -05:00
Peter Cai 231b188320 Add cat and horse icon packs from RunCat
Thanks to Kyome22/RunCat_for_windows
2022-11-20 21:36:40 -05:00
Peter Cai 877fae2007 Add support for configuration via env variables 2022-11-20 21:33:09 -05:00
Peter Cai 66c44df03a Vary animation speed based on CPU load 2022-11-20 21:25:12 -05:00
Peter Cai 66871b07a9 Implement the animation 2022-11-20 21:02:17 -05:00
42 changed files with 214 additions and 6 deletions

147
Cargo.lock generated
View file

@ -2,6 +2,12 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "alphanumeric-sort"
version = "1.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77e9c9abb82613923ec78d7a461595d52491ba7240f3c64c0bbe0e6d98e0fce0"
[[package]]
name = "anyhow"
version = "1.0.66"
@ -83,13 +89,68 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "core-foundation-sys"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"
[[package]]
name = "crossbeam-channel"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521"
dependencies = [
"cfg-if",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96bf8df95e795db1a4aca2957ad884a2df35413b24bbeb3114422f3cc21498e8"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset 0.7.1",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "422f23e724af1240ec469ea1e834d87a4b59ce2efe2c6a96256b0c47e2fd86aa"
dependencies = [
"cfg-if",
]
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "field-offset"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e1c54951450cbd39f3dbcf1005ac413b49487dabf18a720ad2383eccfeffb92"
dependencies = [
"memoffset",
"memoffset 0.6.5",
"rustc_version",
]
@ -350,6 +411,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "libappindicator"
version = "0.7.1"
@ -408,6 +478,34 @@ dependencies = [
"autocfg",
]
[[package]]
name = "memoffset"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4"
dependencies = [
"autocfg",
]
[[package]]
name = "ntapi"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc51db7b362b205941f71232e56c625156eb9a929f8cf74a428fd5bc094a4afc"
dependencies = [
"winapi",
]
[[package]]
name = "num_cpus"
version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6058e64324c71e02bc2b150e4f3bc8286db6c83092132ffa3f6b1eab0f9def5"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "once_cell"
version = "1.16.0"
@ -443,8 +541,11 @@ dependencies = [
name = "parrotd"
version = "0.1.0"
dependencies = [
"alphanumeric-sort",
"glib",
"gtk",
"libappindicator",
"sysinfo",
]
[[package]]
@ -528,6 +629,29 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rayon"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e060280438193c554f654141c9ea9417886713b7acd75974c85b18a69a88e0b"
dependencies = [
"crossbeam-deque",
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cac410af5d00ab6884528b4ab69d1e8e146e8d471201800fa1b4524126de6ad3"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"num_cpus",
]
[[package]]
name = "rustc_version"
version = "0.3.3"
@ -537,6 +661,12 @@ dependencies = [
"semver",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "semver"
version = "0.11.0"
@ -587,6 +717,21 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "sysinfo"
version = "0.26.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c375d5fd899e32847b8566e10598d6e9f1d9b55ec6de3cdf9e7da4bdc51371bc"
dependencies = [
"cfg-if",
"core-foundation-sys",
"libc",
"ntapi",
"once_cell",
"rayon",
"winapi",
]
[[package]]
name = "system-deps"
version = "6.0.3"

View file

@ -8,3 +8,6 @@ edition = "2021"
[dependencies]
libappindicator = "0.7"
gtk = "0.15"
glib = "0.15"
sysinfo = "0.26"
alphanumeric-sort = "1.4"

BIN
res/cat/dark_cat_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 671 B

BIN
res/cat/dark_cat_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 681 B

BIN
res/cat/dark_cat_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 643 B

BIN
res/cat/dark_cat_3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 658 B

BIN
res/cat/dark_cat_4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 638 B

BIN
res/cat/light_cat_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 791 B

BIN
res/cat/light_cat_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 777 B

BIN
res/cat/light_cat_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 782 B

BIN
res/cat/light_cat_3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 784 B

BIN
res/cat/light_cat_4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 766 B

BIN
res/horse/dark_horse_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 762 B

BIN
res/horse/dark_horse_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 748 B

BIN
res/horse/dark_horse_10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 815 B

BIN
res/horse/dark_horse_11.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 803 B

BIN
res/horse/dark_horse_12.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 771 B

BIN
res/horse/dark_horse_13.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 736 B

BIN
res/horse/dark_horse_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 770 B

BIN
res/horse/dark_horse_3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 726 B

BIN
res/horse/dark_horse_4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 725 B

BIN
res/horse/dark_horse_5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 785 B

BIN
res/horse/dark_horse_6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 810 B

BIN
res/horse/dark_horse_7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 823 B

BIN
res/horse/dark_horse_8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 806 B

BIN
res/horse/dark_horse_9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 845 B

BIN
res/horse/light_horse_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 739 B

BIN
res/horse/light_horse_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 736 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 782 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 769 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 755 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 711 B

BIN
res/horse/light_horse_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 725 B

BIN
res/horse/light_horse_3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 706 B

BIN
res/horse/light_horse_4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 704 B

BIN
res/horse/light_horse_5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 764 B

BIN
res/horse/light_horse_6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 779 B

BIN
res/horse/light_horse_7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 795 B

BIN
res/horse/light_horse_8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 779 B

BIN
res/horse/light_horse_9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 803 B

View file

@ -7,13 +7,29 @@ pub use err::*;
use libappindicator::*;
use gtk::prelude::*;
use pack::*;
use std::cell::RefCell;
use std::time::{Duration, Instant};
use sysinfo::*;
fn main() {
gtk::init().unwrap();
let mut icon_pack = IconPack::new("./res", "parrot", PackVariant::Dark).unwrap();
thread_local! {
static SYSTEM: RefCell<System> = RefCell::new(System::new_all());
}
fn update_frame_time() -> Duration {
return SYSTEM.with(|system| {
let mut system = system.borrow_mut();
system.refresh_cpu_specifics(CpuRefreshKind::new().with_cpu_usage());
let num_cpus = system.cpus().len();
let load: f32 = system.cpus().iter()
.map(|cpu| cpu.cpu_usage() / num_cpus as f32)
.sum();
return Duration::from_millis(10 + 2 * load as u64); // min 10 (~100Hz), max 210 (~5Hz)
})
}
fn init_indicator(icon_pack: &IconPack) -> AppIndicator {
let mut indicator = AppIndicator::new("parrotd", "");
indicator.set_icon_theme_path(icon_pack.pack_path());
indicator.set_icon_full(icon_pack.next_icon_path(), "parrotd");
indicator.set_status(AppIndicatorStatus::Active);
let mut m = gtk::Menu::new();
let mi = gtk::CheckMenuItem::with_label("I'm a parrot");
@ -23,5 +39,40 @@ fn main() {
m.append(&mi);
indicator.set_menu(&mut m);
m.show_all();
return indicator;
}
fn main() {
gtk::init().unwrap();
let res_dir = std::env::var("PARROTD_RESOURCE_DIR").unwrap_or("./res".to_owned());
let pack_name = std::env::var("PARROTD_ICON_PACK").unwrap_or("parrot".to_owned());
let variant = PackVariant::parse(
&std::env::var("PARROTD_VARIANT").unwrap_or("dark".to_owned())).expect("Invalid variant name");
let mut icon_pack = IconPack::new(res_dir, &pack_name, variant).unwrap();
let mut indicator = init_indicator(&icon_pack);
// Animation task
let mut last_update = Instant::now();
let mut last_update_frame_time = Instant::now();
let mut frame_time = update_frame_time();
glib::timeout_add_local(Duration::from_millis(1) /* we need granularity */, move || {
let now = Instant::now();
if now - last_update_frame_time >= Duration::from_millis(100) {
// Update frame time
frame_time = update_frame_time();
last_update_frame_time = now;
}
if now - last_update >= frame_time {
// Update animation
indicator.set_icon(icon_pack.next_icon_path());
last_update = now;
}
Continue(true)
});
gtk::main();
}

View file

@ -2,6 +2,7 @@ use crate::*;
use std::path::{Path, PathBuf};
#[allow(dead_code)]
#[derive(Debug)]
pub enum PackVariant {
Light,
Dark,
@ -14,6 +15,14 @@ impl PackVariant {
PackVariant::Dark => "dark",
}
}
pub fn parse(s: &str) -> Option<PackVariant> {
match s {
"light" => Some(PackVariant::Light),
"dark" => Some(PackVariant::Dark),
_ => None
}
}
}
#[allow(unused)]
@ -60,7 +69,7 @@ impl IconPack {
icons.push(name.split_at(name.len() - ".png".len()).0.to_string());
}
icons.sort(); // Alphanumerical ordering
alphanumeric_sort::sort_str_slice(&mut icons); // Alphanumerical ordering
let num_icons = icons.len();