@@ -45,17 +45,13 @@ fn load_image<T>(array: &[T]) -> id {
45
45
46
46
fn init_cocoa ( ) -> ( cocoa:: base:: id , cocoa:: base:: id ) {
47
47
let app = unsafe {
48
- let app = NSApp ( ) ;
49
- app
48
+ NSApp ( )
50
49
} ;
51
50
52
51
let button = unsafe {
53
52
app. setActivationPolicy_ ( NSApplicationActivationPolicyProhibited ) ;
54
-
55
53
let status_item = NSStatusBar :: systemStatusBar ( nil) . statusItemWithLength_ ( NSVariableStatusItemLength ) ;
56
- let button: cocoa:: base:: id = status_item. button ( ) ;
57
-
58
- button
54
+ status_item. button ( )
59
55
} ;
60
56
61
57
return ( app, button) ;
@@ -64,6 +60,8 @@ fn init_cocoa() -> (cocoa::base::id, cocoa::base::id) {
64
60
fn main ( ) {
65
61
let ( app, button) = init_cocoa ( ) ;
66
62
let ( tx, rx) = mpsc:: channel ( ) ;
63
+ msg_send ! [ button, setAction: sel!( onButtonClick: ) ] ;
64
+
67
65
// let (tx_mute, rx_mute) = mpsc::channel();
68
66
69
67
let tx_ptr = & tx as * const Sender < bool > as u64 ;
@@ -73,7 +71,7 @@ fn main() {
73
71
audio:: set_mic_live ( false ) ;
74
72
75
73
thread:: spawn ( move || {
76
- input_property_listener ( tx_ptr)
74
+ input_property_listener ( tx_ptr) ;
77
75
} ) ;
78
76
79
77
thread:: spawn ( move || {
@@ -84,25 +82,40 @@ fn main() {
84
82
// hack_keep_muted(rx_mute);
85
83
// });
86
84
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 ( ) ;
89
87
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" ) ) ;
92
100
93
101
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
+ }
101
116
}
102
117
} ) ;
103
118
104
- // set the initial state of the icon
105
- tx. send ( audio:: get_mute_from_all_devices ( ) ) . unwrap ( ) ;
106
119
unsafe { app. run ( ) ; }
107
120
}
108
121
@@ -125,6 +138,10 @@ fn main() {
125
138
// }
126
139
// }
127
140
141
+ fn gui_click_listener ( tx : Sender < bool > ) {
142
+ tx. send ( false ) . unwrap ( ) ;
143
+ }
144
+
128
145
fn hardware_change_listener ( tx_ptr : u64 ) {
129
146
extern fn listener ( _id : AudioObjectID ,
130
147
_addresses_count : u32 ,
0 commit comments