Skip to content

Commit

Permalink
refac(tray): update tray struct
Browse files Browse the repository at this point in the history
  • Loading branch information
iholston committed Mar 8, 2024
1 parent fe0f454 commit 1772e01
Showing 1 changed file with 50 additions and 36 deletions.
86 changes: 50 additions & 36 deletions src/tray.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,82 @@
use crate::Acceptor;
use std::sync::atomic::Ordering;
use std::thread;
use tray_icon::menu::{Menu, MenuEvent, MenuItem, PredefinedMenuItem};
use tray_icon::TrayIconBuilder;
use std::sync::atomic::Ordering;
use tray_icon::{Icon, TrayIcon, TrayIconBuilder};
use winit::event_loop::{ControlFlow, EventLoopBuilder};

const ICON_BYTES: &'static [u8] = include_bytes!("../resources/icon.ico");

pub struct TrayApp {
#[allow(dead_code)]
icon: TrayIcon,
start: MenuItem,
pause: MenuItem,
quit: MenuItem,
acceptor: Acceptor,
}

impl TrayApp {
pub fn new(acceptor: Acceptor) -> Self {
TrayApp { acceptor }
}

pub fn run(&mut self) {
let tray_menu = Menu::new();
let menu_start = MenuItem::new("Start", false, None);
let menu_pause = MenuItem::new("Pause", true, None);
let menu_quit = MenuItem::new("Quit", true, None);
let _ = tray_menu.append_items(&[
&menu_start,
&PredefinedMenuItem::separator(),
&menu_pause,
&PredefinedMenuItem::separator(),
&menu_quit,
]);
let menu_channel = MenuEvent::receiver();

let icon = Self::load_icon();
let _tray_icon = TrayIconBuilder::new()
tray_menu
.append_items(&[
&menu_start,
&PredefinedMenuItem::separator(),
&menu_pause,
&PredefinedMenuItem::separator(),
&menu_quit,
])
.unwrap();
let tray_icon = TrayIconBuilder::new()
.with_menu(Box::new(tray_menu))
.with_tooltip("LoL-Acceptor")
.with_icon(icon)
.with_icon(Self::load_icon())
.build()
.unwrap();
TrayApp {
icon: tray_icon,
start: menu_start,
pause: menu_pause,
quit: menu_quit,
acceptor,
}
}

pub fn run(&mut self) {
let mut acceptor_clone = self.acceptor.clone();
thread::spawn(move || {
acceptor_clone.run();
});

let menu_channel = MenuEvent::receiver();
let event_loop = EventLoopBuilder::new().build().unwrap();
let _ = event_loop.run(move |_event, event_loop| {
event_loop.set_control_flow(ControlFlow::Wait);
if let Ok(event) = menu_channel.try_recv() {
if event.id() == menu_start.id() {
self.acceptor.paused.store(false, Ordering::SeqCst);
menu_start.set_enabled(false);
menu_pause.set_enabled(true);
} else if event.id() == menu_pause.id() {
self.acceptor.paused.store(true, Ordering::SeqCst);
menu_start.set_enabled(true);
menu_pause.set_enabled(false);
} else if event.id() == menu_quit.id() {
self.acceptor.terminate.store(true, Ordering::SeqCst);
event_loop.exit();

event_loop
.run(move |_event, event_loop| {
event_loop.set_control_flow(ControlFlow::Wait);
if let Ok(event) = menu_channel.try_recv() {
if event.id() == self.start.id() {
self.acceptor.paused.store(false, Ordering::SeqCst);
self.start.set_enabled(false);
self.pause.set_enabled(true);
} else if event.id() == self.pause.id() {
self.acceptor.paused.store(true, Ordering::SeqCst);
self.start.set_enabled(true);
self.pause.set_enabled(false);
} else if event.id() == self.quit.id() {
self.acceptor.terminate.store(true, Ordering::SeqCst);
event_loop.exit();
}
}
}
});
})
.unwrap();
}

fn load_icon() -> tray_icon::Icon {
fn load_icon() -> Icon {
let (icon_rgba, icon_width, icon_height) = {
let image = image::load_from_memory_with_format(&ICON_BYTES, image::ImageFormat::Ico)
.expect("Failed to open icon path")
Expand All @@ -71,6 +85,6 @@ impl TrayApp {
let rgba = image.into_raw();
(rgba, width, height)
};
tray_icon::Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
}
}

0 comments on commit 1772e01

Please sign in to comment.