Skip to content

Commit c1a74d1

Browse files
committed
Add a system tray icon
* add a system tray icon that indicates permanently if there's a game awaiting a move * add "awaiting" icon, and rename both icons
1 parent 38f9be1 commit c1a74d1

File tree

6 files changed

+80
-13
lines changed

6 files changed

+80
-13
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ edition = "2021"
1010
anyhow = "1.0.56"
1111
clap = { version = "3.1.8", features = [ "derive" ] }
1212
cookie_store = "0.15.1"
13+
cpp_core = "0.6.0"
1314
directories = "4.0.1"
1415
ksni = "0.2.0"
1516
notify-rust = "4.5.8"
File renamed without changes.

assets/ogs_icon_awaiting.png

5.41 KB
Loading

src/main.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ use std::thread;
44
use anyhow::anyhow;
55
use clap::Parser;
66
use notify_rust::Notification;
7-
use qt_core::QString;
8-
use qt_gui::QIcon;
9-
use qt_widgets::{QApplication, QSystemTrayIcon};
107

118
mod config;
129
use config::Config;
1310
mod ogs;
1411
use ogs::{Game, OgsAgent};
1512
mod state;
1613
use state::State;
14+
mod tray;
15+
use tray::run_tray;
1716
mod utils;
1817

1918
#[derive(Parser, Debug)]
@@ -57,16 +56,13 @@ fn main_with_config(config: Config) -> anyhow::Result<()> {
5756
.unwrap();
5857
}
5958

60-
let ogs_icon_clone = ogs_icon.clone();
61-
thread::spawn(move || {
62-
QApplication::init(|_| unsafe {
63-
let icon = QSystemTrayIcon::new();
64-
icon.set_icon(&QIcon::from_q_string(&QString::from_std_str(
65-
ogs_icon_clone.to_str().unwrap(),
66-
)));
67-
icon.show();
68-
QApplication::exec()
69-
});
59+
let mut ogs_icon_awaiting = config.icon_dir.clone();
60+
ogs_icon_awaiting.push("ogs_icon_awaiting.png");
61+
62+
let mut icon = run_tray(if state.games_awaiting_move.is_empty() {
63+
ogs_icon.clone()
64+
} else {
65+
ogs_icon_awaiting.clone()
7066
});
7167

7268
loop {
@@ -100,6 +96,12 @@ fn main_with_config(config: Config) -> anyhow::Result<()> {
10096
}
10197
}
10298

99+
if games_awaiting_move.is_empty() {
100+
icon.set(ogs_icon.clone());
101+
} else {
102+
icon.set(ogs_icon_awaiting.clone())
103+
}
104+
103105
state.games_awaiting_move = games_awaiting_move;
104106
}
105107
}

src/tray.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::path::PathBuf;
2+
use std::rc::Rc;
3+
use std::sync::mpsc;
4+
use std::thread;
5+
6+
use cpp_core::{Ptr, Ref, StaticUpcast};
7+
use qt_core::{slot, QBox, QObject, QString};
8+
use qt_gui::QIcon;
9+
use qt_widgets::{QApplication, QSystemTrayIcon, SlotOfQIcon};
10+
11+
struct TrayIcon(QBox<QSystemTrayIcon>);
12+
13+
pub struct TrayHandle(QBox<SlotOfQIcon>);
14+
15+
unsafe impl Send for TrayHandle {}
16+
17+
impl TrayHandle {
18+
pub fn set(&mut self, filename: PathBuf) {
19+
unsafe {
20+
let icon = QIcon::from_q_string(&QString::from_std_str(filename.to_str().unwrap()));
21+
22+
self.0.slot(&icon);
23+
}
24+
}
25+
}
26+
27+
impl StaticUpcast<QObject> for TrayIcon {
28+
unsafe fn static_upcast(ptr: Ptr<Self>) -> Ptr<QObject> {
29+
ptr.0.as_ptr().static_upcast()
30+
}
31+
}
32+
33+
impl TrayIcon {
34+
#[slot(SlotOfQIcon)]
35+
unsafe fn change_icon(self: &Rc<Self>, icon: Ref<QIcon>) {
36+
self.0.set_icon(icon);
37+
}
38+
}
39+
40+
pub fn run_tray(filename: PathBuf) -> TrayHandle {
41+
let (tx, rx) = mpsc::channel();
42+
43+
unsafe {
44+
thread::spawn(move || {
45+
QApplication::init(|_| {
46+
let icon = QSystemTrayIcon::new();
47+
icon.set_icon(&QIcon::from_q_string(&QString::from_std_str(
48+
filename.to_str().unwrap(),
49+
)));
50+
icon.show();
51+
52+
let icon = Rc::new(TrayIcon(icon));
53+
let slot = TrayHandle(icon.slot_change_icon());
54+
55+
tx.send(slot).unwrap();
56+
57+
QApplication::exec()
58+
});
59+
});
60+
}
61+
62+
rx.recv().unwrap()
63+
}

0 commit comments

Comments
 (0)