Skip to content

Commit

Permalink
refactor(tray): move spawn function to tray::utils
Browse files Browse the repository at this point in the history
  • Loading branch information
NotAShelf committed Mar 23, 2024
1 parent 37dbbc1 commit d030b78
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
13 changes: 3 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,17 @@ mod svg;
mod tailscale;
mod tray;

use crate::tray::menu::SysTray;
use crate::tray::utils::start_tray_service;
use std::thread::park;

fn main() {
// initialize logger
env_logger::init();

// start the tray service
let handle = ksni::spawn(SysTray {
ctx: tailscale::status::get_current_status(),
})
.unwrap();
// start tray service
start_tray_service();

// keep the main thread alive
// NOTE: The documentation for park reads:
// "A call to park does not guarantee that the thread will
// remain parked forever, and callers should be prepared for this possibility."
// hence the loop
loop {
park();
}
Expand Down
16 changes: 5 additions & 11 deletions src/tray/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ pub struct SysTray {
}

impl SysTray {
pub fn new() -> Self {
SysTray {
ctx: get_current_status(),
}
}

fn enabled(&self) -> bool {
self.ctx.status.tailscale_up
}
Expand Down Expand Up @@ -112,6 +106,10 @@ impl Tray for SysTray {
ResvgRenderer::load_icon(self.enabled())
}

fn id(&self) -> String {
env!("CARGO_PKG_NAME").into()
}

fn title(&self) -> String {
"Tailray".into()
}
Expand Down Expand Up @@ -215,7 +213,7 @@ impl Tray for SysTray {
.into(),
MenuItem::Separator,
StandardItem {
label: "Exit".into(),
label: "Exit Tailray".into(),
icon_name: "application-exit".into(),
activate: Box::new(|_| std::process::exit(0)),
..Default::default()
Expand All @@ -232,8 +230,4 @@ impl Tray for SysTray {
info!("Watcher offline, shutting down the system Tray.");
false
}

fn id(&self) -> String {
"tailray".to_string()
}
}
1 change: 1 addition & 0 deletions src/tray/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod menu;
pub mod utils;
12 changes: 12 additions & 0 deletions src/tray/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::tailscale;
use crate::tray::menu::SysTray;

pub fn start_tray_service() {
// start the tray service
let _handle = ksni::spawn(SysTray {
ctx: tailscale::status::get_current_status(),
})
.unwrap_or_else(|e| {
panic!("Failed to start the tray service: {}", e);
});
}

0 comments on commit d030b78

Please sign in to comment.