This commit is contained in:
Valeria Fadeeva 2025-01-20 14:20:02 +05:00
parent e21e92f44e
commit aa35a7f5ef
2 changed files with 108 additions and 9 deletions

View File

@ -10,4 +10,7 @@ edition = "2021"
# tray-item = { path = "../../", features = ["libappindicator"] } # tray-item = { path = "../../", features = ["libappindicator"] }
tray-item = { git = "https://github.com/olback/tray-item-rs", features = ["libappindicator"] } tray-item = { git = "https://github.com/olback/tray-item-rs", features = ["libappindicator"] }
gtk = "0.18" gtk = "0.18"
subprocess = "0.2" glib = "0.20"
[build-dependencies]
glib-build-tools = "0.20"

View File

@ -1,5 +1,46 @@
use tray_item::{TrayItem, IconSource}; // #![cfg_attr(debug_assertions, allow(dead_code, unused_imports, unused_variables, unused_must_use))]
use subprocess;
use {
std::sync::mpsc,
std::thread,
std::time,
std::process::Command,
tray_item::IconSource,
tray_item::TrayItem,
};
// use subprocess;
enum Message {
Quit,
NOP,
UpdateSystem,
UpdateFlatpak
}
fn run_cmd(cmd_string: &str, lang_env_var: Option<&str>) {
if lang_env_var.is_some() {
let key: &str = "LANG";
std::env::set_var(key, lang_env_var.unwrap_or("C.UTF-8"));
}
let args_vec : Vec<&str> = cmd_string.split_whitespace().collect();
if args_vec.len() == 1 {
let cmd = args_vec[0];
let status = Command::new(cmd).output();
println!("{:#?}", status);
}
else {
let cmd = args_vec[0];
let new_args_vec = &args_vec[1..].to_vec();
let status = Command::new(cmd).args(new_args_vec).output();
println!("{:#?}", status);
}
}
fn main() { fn main() {
gtk::init().unwrap(); gtk::init().unwrap();
@ -7,16 +48,71 @@ fn main() {
let mut tray = TrayItem::new("Melawy Arch Linux Updater", IconSource::Resource("software-store")).unwrap(); let mut tray = TrayItem::new("Melawy Arch Linux Updater", IconSource::Resource("software-store")).unwrap();
tray.add_label("Melawy Arch Linux Updater").unwrap(); tray.add_label("Melawy Arch Linux Updater").unwrap();
let (tx, rx) = mpsc::sync_channel::<Message>(2);
tray.add_menu_item("Update system", || { let update_system_tx = tx.clone();
println!("Update system"); tray.add_menu_item("Update system", move|| {
let exit_status = subprocess::Exec::cmd("sudo").arg("arch-linux-updater").join(); update_system_tx.send(Message::UpdateSystem).unwrap();
println!("{:?}", exit_status);
}).unwrap(); }).unwrap();
tray.add_menu_item("Quit", || {
let update_flatpak_tx = tx.clone();
tray.add_menu_item("Update flatpak applications", move || {
update_flatpak_tx.send(Message::UpdateFlatpak).unwrap();
}).unwrap();
let quit_tx = tx.clone();
tray.add_menu_item("Quit", move || {
quit_tx.send(Message::Quit).unwrap();
}).unwrap();
glib::idle_add_local(move || match rx.recv() {
Ok(Message::Quit) => {
gtk::main_quit(); gtk::main_quit();
}).unwrap(); println!("Quit!");
glib::ControlFlow::Break
}
Ok(Message::UpdateSystem) => {
println!("Update system");
let cmd = "sudo arch-linux-updater";
let _ = run_cmd(cmd, None);
glib::ControlFlow::Continue
}
Ok(Message::UpdateFlatpak) => {
println!("Update flatpak applications");
let cmd = "/usr/bin/konsole --noclose --separate -e flatpak update";
let _ = run_cmd(cmd, None);
glib::ControlFlow::Continue
}
_ => {
// println!("Default!");
glib::ControlFlow::Continue
}
});
thread::spawn(move || {
let mut count = 0;
loop {
// Menu doesn't show up until after hitting enter a few times?
//let mut s = String::new();
//std::io::stdin().read_line(&mut s).unwrap();
//if s.as_bytes()[0] == b'q' {
// println!("stopping thread loop!");
// break
//}
// glib::idle_add_local doesn't loop without this?
count += 1;
thread::sleep(time::Duration::from_millis(10));
if count % 100 == 0 {
tx.send(Message::NOP).unwrap();
// println!("Idle loop, {}!", count);
}
}
});
gtk::main(); gtk::main();
} }