This repository was archived by the owner on Dec 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathlib.rs
More file actions
154 lines (132 loc) · 4.06 KB
/
Copy pathlib.rs
File metadata and controls
154 lines (132 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Systray Lib
#[macro_use]
extern crate log;
#[cfg(target_os = "windows")]
extern crate winapi;
#[cfg(target_os = "windows")]
extern crate kernel32;
#[cfg(target_os = "windows")]
extern crate user32;
#[cfg(target_os = "windows")]
extern crate shell32;
#[cfg(target_os = "macos")]
extern crate cocoa;
#[cfg(target_os = "macos")]
#[macro_use]
extern crate objc;
extern crate libc;
#[cfg(target_os = "linux")]
extern crate gtk;
#[cfg(target_os = "linux")]
extern crate glib;
#[cfg(target_os = "linux")]
extern crate libappindicator;
pub mod api;
use std::collections::HashMap;
use std::sync::mpsc::{channel, Receiver};
#[derive(Clone, Debug)]
pub enum SystrayError {
OsError(String),
NotImplementedError,
UnknownError,
}
pub struct SystrayEvent {
menu_index: u32,
}
impl std::fmt::Display for SystrayError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
match self {
&SystrayError::OsError(ref err_str) => write!(f, "OsError: {}", err_str),
&SystrayError::NotImplementedError => write!(f, "Functionality is not implemented yet"),
&SystrayError::UnknownError => write!(f, "Unknown error occurrred"),
}
}
}
pub struct Application {
window: api::api::Window,
menu_idx: u32,
callback: HashMap<u32, Callback>,
// Each platform-specific window module will set up its own thread for
// dealing with the OS main loop. Use this channel for receiving events from
// that thread.
rx: Receiver<SystrayEvent>,
}
type Callback = Box<(Fn(&mut Application) -> () + 'static)>;
fn make_callback<F>(f: F) -> Callback
where F: std::ops::Fn(&mut Application) -> () + 'static {
Box::new(f) as Callback
}
impl Application {
pub fn new() -> Result<Application, SystrayError> {
let (event_tx, event_rx) = channel();
match api::api::Window::new(event_tx) {
Ok(w) => Ok(Application {
window: w,
menu_idx: 0,
callback: HashMap::new(),
rx: event_rx
}),
Err(e) => Err(e)
}
}
pub fn add_menu_item<F>(&mut self, item_name: &String, f: F) -> Result<u32, SystrayError>
where F: std::ops::Fn(&mut Application) -> () + 'static {
let idx = self.menu_idx;
if let Err(e) = self.window.add_menu_entry(idx, item_name) {
return Err(e);
}
self.callback.insert(idx, make_callback(f));
self.menu_idx += 1;
Ok(idx)
}
pub fn add_menu_separator(&mut self) -> Result<u32, SystrayError> {
let idx = self.menu_idx;
if let Err(e) = self.window.add_menu_separator(idx) {
return Err(e);
}
self.menu_idx += 1;
Ok(idx)
}
pub fn set_icon_from_file(&self, file: &String) -> Result<(), SystrayError> {
self.window.set_icon_from_file(file)
}
pub fn set_icon_from_resource(&self, resource: &String) -> Result<(), SystrayError> {
self.window.set_icon_from_resource(resource)
}
pub fn set_icon_from_buffer(&mut self, buffer: &[u8], width: u32, height: u32)
-> Result<(), SystrayError>
{
self.window.set_icon_from_buffer(buffer, width, height)
}
pub fn shutdown(&self) -> Result<(), SystrayError> {
self.window.shutdown()
}
pub fn set_tooltip(&self, tooltip: &String) -> Result<(), SystrayError> {
self.window.set_tooltip(tooltip)
}
pub fn quit(&mut self) {
self.window.quit()
}
pub fn wait_for_message(&mut self) {
loop {
let msg;
match self.rx.recv() {
Ok(m) => msg = m,
Err(_) => {
self.quit();
break;
}
}
if self.callback.contains_key(&msg.menu_index) {
let f = self.callback.remove(&msg.menu_index).unwrap();
f(self);
self.callback.insert(msg.menu_index, f);
}
}
}
}
impl Drop for Application {
fn drop(&mut self) {
self.shutdown().ok();
}
}