Skip to content

Commit ccdad74

Browse files
committed
stash
1 parent 3a9393d commit ccdad74

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

src/main.rs

+37-20
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,13 @@ fn load_image<T>(array: &[T]) -> id {
4545

4646
fn init_cocoa() -> (cocoa::base::id, cocoa::base::id) {
4747
let app = unsafe {
48-
let app = NSApp();
49-
app
48+
NSApp()
5049
};
5150

5251
let button = unsafe {
5352
app.setActivationPolicy_(NSApplicationActivationPolicyProhibited);
54-
5553
let status_item = NSStatusBar::systemStatusBar(nil).statusItemWithLength_(NSVariableStatusItemLength);
56-
let button: cocoa::base::id = status_item.button();
57-
58-
button
54+
status_item.button()
5955
};
6056

6157
return (app, button);
@@ -64,6 +60,8 @@ fn init_cocoa() -> (cocoa::base::id, cocoa::base::id) {
6460
fn main() {
6561
let (app, button) = init_cocoa();
6662
let (tx, rx) = mpsc::channel();
63+
msg_send![button, setAction:sel!(onButtonClick:)];
64+
6765
// let (tx_mute, rx_mute) = mpsc::channel();
6866

6967
let tx_ptr = &tx as *const Sender<bool> as u64;
@@ -73,7 +71,7 @@ fn main() {
7371
audio::set_mic_live(false);
7472

7573
thread::spawn(move || {
76-
input_property_listener(tx_ptr)
74+
input_property_listener(tx_ptr);
7775
});
7876

7977
thread::spawn(move || {
@@ -84,25 +82,40 @@ fn main() {
8482
// hack_keep_muted(rx_mute);
8583
// });
8684

87-
thread::spawn(move || {
88-
let btn = button_ptr as cocoa::base::id;
85+
// set the initial state of the icon
86+
tx.send(audio::get_mute_from_all_devices()).unwrap();
8987

90-
let muted = load_image(include_bytes!("../assets/muted.png"));
91-
let unmuted = load_image(include_bytes!("../assets/unmuted.png"));
88+
// thread to watch the button clicks
89+
{
90+
let tx = tx.clone();
91+
thread::spawn(move || {
92+
gui_click_listener(tx);
93+
});
94+
}
95+
96+
thread::spawn(move || {
97+
let btn = button_ptr as cocoa::base::id;
98+
let muted = load_image(include_bytes!("../assets/muted-dark.png"));
99+
let unmuted = load_image(include_bytes!("../assets/unmuted-dark.png"));
92100

93101
loop {
94-
if rx.recv().unwrap() {
95-
unsafe { btn.setImage_(unmuted) };
96-
// let _ = tx_mute.send(true);
97-
} else {
98-
unsafe { btn.setImage_(muted)} ;
99-
// let _ = tx_mute.send(false);
100-
}
102+
match rx.recv() {
103+
Ok(b) => {
104+
if b {
105+
unsafe { btn.setImage_(unmuted) };
106+
// let _ = tx_mute.send(true);
107+
} else {
108+
unsafe { btn.setImage_(muted)} ;
109+
// let _ = tx_mute.send(false);
110+
}
111+
},
112+
Err(e) => {
113+
println!("{}", e)
114+
}
115+
}
101116
}
102117
});
103118

104-
// set the initial state of the icon
105-
tx.send(audio::get_mute_from_all_devices()).unwrap();
106119
unsafe { app.run(); }
107120
}
108121

@@ -125,6 +138,10 @@ fn main() {
125138
// }
126139
// }
127140

141+
fn gui_click_listener(tx: Sender<bool>) {
142+
tx.send(false).unwrap();
143+
}
144+
128145
fn hardware_change_listener(tx_ptr: u64) {
129146
extern fn listener(_id: AudioObjectID,
130147
_addresses_count: u32,

0 commit comments

Comments
 (0)