From 66871b07a95369c7e090d29215ebf50d811333ba Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Sun, 20 Nov 2022 21:02:17 -0500 Subject: [PATCH] Implement the animation --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 26 ++++++++++++++++++++++---- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f43c6ed..0f1e35f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -443,6 +443,7 @@ dependencies = [ name = "parrotd" version = "0.1.0" dependencies = [ + "glib", "gtk", "libappindicator", ] diff --git a/Cargo.toml b/Cargo.toml index 567480e..cfe4fab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ edition = "2021" [dependencies] libappindicator = "0.7" gtk = "0.15" +glib = "0.15" diff --git a/src/main.rs b/src/main.rs index f3bf7e1..8ff4c40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,13 +7,11 @@ pub use err::*; use libappindicator::*; use gtk::prelude::*; use pack::*; +use std::time::{Duration, Instant}; -fn main() { - gtk::init().unwrap(); - let mut icon_pack = IconPack::new("./res", "parrot", PackVariant::Dark).unwrap(); +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 +21,25 @@ fn main() { m.append(&mi); indicator.set_menu(&mut m); m.show_all(); + return indicator; +} + +fn main() { + gtk::init().unwrap(); + + let mut icon_pack = IconPack::new("./res", "parrot", PackVariant::Dark).unwrap(); + let mut indicator = init_indicator(&icon_pack); + + // Animation task + let mut last_update = Instant::now(); + glib::timeout_add_local(Duration::from_millis(1000 / 60) /* maximum 60Hz */, move || { + let now = Instant::now(); + if now - last_update >= Duration::from_millis(100) { + indicator.set_icon(icon_pack.next_icon_path()); + last_update = now; + } + Continue(true) + }); + gtk::main(); }