@@ -3,7 +3,6 @@ use std::sync::Arc;
33use fltk:: app;
44use fltk:: app:: App ;
55use fltk:: app:: Receiver ;
6- use fltk:: app:: Sender ;
76use fltk:: button:: * ;
87use fltk:: draw;
98use fltk:: enums:: * ;
@@ -38,6 +37,32 @@ use self::play_status::PlayStatusIcon;
3837use self :: preferences_window:: PrefsWindow ;
3938use self :: queue_window:: QueueWindow ;
4039
40+ /// Get the global FLTK sender
41+ macro_rules! sender {
42+ ( ) => {
43+ fltk:: app:: Sender :: <UIEvent >:: get( )
44+ } ;
45+ }
46+ /// Send a UIEvent through the global FLTK sender
47+ macro_rules! ui_send {
48+ ( $expression: expr) => { {
49+ use crate :: gui:: UIEvent ;
50+ let s = fltk:: app:: Sender :: <UIEvent >:: get( ) ;
51+ s. send( $expression) ;
52+ } } ;
53+ }
54+ /// Send a UIUpdateEvent through the global FLTK sender
55+ macro_rules! ui_update {
56+ ( $expression: expr) => { {
57+ use crate :: gui:: UIEvent ;
58+ let s = fltk:: app:: Sender :: <UIEvent >:: get( ) ;
59+ s. send( UIEvent :: Update ( $expression) ) ;
60+ } } ;
61+ }
62+ pub ( crate ) use sender;
63+ pub ( crate ) use ui_send;
64+ pub ( crate ) use ui_update;
65+
4166// only temporary
4267#[ derive( Debug , Clone ) ]
4368pub enum UIEvent {
@@ -108,22 +133,10 @@ impl DragState {
108133 }
109134}
110135
111- #[ derive( Clone ) ]
112- pub struct UIThreadHandle {
113- sender : Sender < UIEvent > ,
114- }
115-
116- impl UIThreadHandle {
117- pub fn update ( & mut self , update : UIUpdateEvent ) {
118- self . sender . send ( UIEvent :: Update ( update) )
119- }
120- }
121-
122136type UiTx = tokio:: sync:: mpsc:: UnboundedSender < UIEvent > ;
123137type UiRx = tokio:: sync:: mpsc:: UnboundedReceiver < UIEvent > ;
124138pub struct UIThread {
125139 app : App ,
126- sender : Sender < UIEvent > ,
127140 receiver : Receiver < UIEvent > ,
128141 tx : UiTx ,
129142
@@ -166,42 +179,35 @@ impl AllWindowEdges {
166179}
167180
168181impl UIThread {
169- // TODO: put the id somewhere nicer
170- pub fn spawn ( state : Arc < RwLock < State > > ) -> ( UIThreadHandle , mpsc:: UnboundedReceiver < UIEvent > ) {
182+ pub fn spawn ( state : Arc < RwLock < State > > ) -> mpsc:: UnboundedReceiver < UIEvent > {
171183 // from anywhere to ui (including ui to ui..?)
172- let ( sender , receiver) = fltk:: app:: channel :: < UIEvent > ( ) ;
184+ let ( _ , receiver) = fltk:: app:: channel :: < UIEvent > ( ) ;
173185
174186 // from main thread logic to ui
175187 let ( ui_tx, ui_rx) = mpsc:: unbounded_channel :: < UIEvent > ( ) ;
176188
177- let thread_sender = sender. to_owned ( ) ;
178189 std:: thread:: spawn ( move || {
179- let mut t = UIThread :: new ( thread_sender , receiver, ui_tx, state) ;
190+ let mut t = UIThread :: new ( receiver, ui_tx, state) ;
180191 t. run ( ) ;
181192 } ) ;
182193
183194 // ui_rx can't be part of the handle because the handle needs to be clone
184195 // only one thing can own and consume from the receiver
185- ( UIThreadHandle { sender } , ui_rx)
196+ ui_rx
186197 }
187198
188- fn new (
189- sender : Sender < UIEvent > ,
190- receiver : Receiver < UIEvent > ,
191- tx : UiTx ,
192- state : Arc < RwLock < State > > ,
193- ) -> Self {
199+ fn new ( receiver : Receiver < UIEvent > , tx : UiTx , state : Arc < RwLock < State > > ) -> Self {
194200 let app = fltk:: app:: App :: default ( ) ;
195201 let widgets = fltk_theme:: WidgetTheme :: new ( fltk_theme:: ThemeType :: Classic ) ;
196202 widgets. apply ( ) ;
197203
198204 let theme = fltk_theme:: ColorTheme :: new ( fltk_theme:: color_themes:: GRAY_THEME ) ;
199205 theme. apply ( ) ;
200206
201- let mut gui = MainWindow :: make_window ( sender . clone ( ) ) ;
202- let queue_gui = QueueWindow :: make_window ( sender . clone ( ) ) ;
203- let connection_gui = ConnectionWindow :: make_window ( sender . clone ( ) , state. clone ( ) ) ;
204- let mut prefs_gui = PrefsWindow :: make_window ( sender . clone ( ) ) ;
207+ let mut gui = MainWindow :: make_window ( ) ;
208+ let queue_gui = QueueWindow :: make_window ( ) ;
209+ let connection_gui = ConnectionWindow :: make_window ( state. clone ( ) ) ;
210+ let mut prefs_gui = PrefsWindow :: make_window ( ) ;
205211 prefs_gui. main_win . show ( ) ;
206212 prefs_gui. load_state ( & state. blocking_read ( ) . preferences ) ;
207213
@@ -210,7 +216,7 @@ impl UIThread {
210216 gui. main_win . show ( ) ;
211217 gui. fix_taskbar_after_show ( ) ;
212218
213- gui. main_win . emit ( sender. clone ( ) , UIEvent :: Quit ) ;
219+ gui. main_win . emit ( sender ! ( ) , UIEvent :: Quit ) ;
214220
215221 // TODO: we'd probably have to communicate this somehow
216222 // for now just pretend
@@ -221,7 +227,6 @@ impl UIThread {
221227
222228 UIThread {
223229 app,
224- sender,
225230 receiver,
226231 tx,
227232
@@ -243,32 +248,28 @@ impl UIThread {
243248 let edges_lock = Arc :: new ( RwLock :: new ( edges_state) ) ;
244249
245250 let mut ds = DragState :: default ( ) . with_id ( 0 ) ;
246- let sender = self . sender . clone ( ) ;
247251 let edges = edges_lock. clone ( ) ;
248- self . gui . main_win . handle ( move |w , ev| {
249- Self :: handle_window_event ( & mut ds , edges . clone ( ) , sender . clone ( ) , w , ev )
250- } ) ;
252+ self . gui
253+ . main_win
254+ . handle ( move |w , ev| Self :: handle_window_event ( & mut ds , edges . clone ( ) , w , ev ) ) ;
251255
252256 let mut ds = DragState :: default ( ) . with_id ( 1 ) ;
253- let sender = self . sender . clone ( ) ;
254257 let edges = edges_lock. clone ( ) ;
255- self . queue_gui . main_win . handle ( move |w , ev| {
256- Self :: handle_window_event ( & mut ds , edges . clone ( ) , sender . clone ( ) , w , ev )
257- } ) ;
258+ self . queue_gui
259+ . main_win
260+ . handle ( move |w , ev| Self :: handle_window_event ( & mut ds , edges . clone ( ) , w , ev ) ) ;
258261
259262 let mut ds = DragState :: default ( ) . with_id ( 2 ) ;
260- let sender = self . sender . clone ( ) ;
261263 let edges = edges_lock. clone ( ) ;
262- self . connection_gui . main_win . handle ( move |w , ev| {
263- Self :: handle_window_event ( & mut ds , edges . clone ( ) , sender . clone ( ) , w , ev )
264- } ) ;
264+ self . connection_gui
265+ . main_win
266+ . handle ( move |w , ev| Self :: handle_window_event ( & mut ds , edges . clone ( ) , w , ev ) ) ;
265267
266268 let mut ds = DragState :: default ( ) ;
267- let sender = self . sender . clone ( ) ;
268269 let edges = edges_lock. clone ( ) ;
269- self . prefs_gui . main_win . handle ( move |w , ev| {
270- Self :: handle_window_event ( & mut ds , edges . clone ( ) , sender . clone ( ) , w , ev )
271- } ) ;
270+ self . prefs_gui
271+ . main_win
272+ . handle ( move |w , ev| Self :: handle_window_event ( & mut ds , edges . clone ( ) , w , ev ) ) ;
272273
273274 while self . app . wait ( ) {
274275 if let Some ( msg) = self . receiver . recv ( ) {
@@ -314,7 +315,7 @@ impl UIThread {
314315 // if already playing, do nothing
315316 }
316317 UIEvent :: BtnStop => {
317- self . send ( UIEvent :: Stop ) ;
318+ ui_send ! ( UIEvent :: Stop ) ;
318319 }
319320 UIEvent :: BtnPause => { }
320321 UIEvent :: BtnNext => { }
@@ -340,7 +341,7 @@ impl UIThread {
340341 self . prefs_gui . main_win . hide ( ) ;
341342 }
342343 UIEvent :: PleaseSavePreferencesWithThisData => {
343- self . sender . send ( UIEvent :: SavePreferences {
344+ ui_send ! ( UIEvent :: SavePreferences {
344345 name: self . prefs_gui. fld_name. value( ) ,
345346 } ) ;
346347 }
@@ -590,15 +591,9 @@ impl UIThread {
590591 }
591592 }
592593
593- // send to our own loop which also sends to main thread
594- fn send ( & self , ev : UIEvent ) {
595- self . sender . send ( ev) ;
596- }
597-
598594 fn handle_window_event (
599595 state : & mut DragState ,
600596 edges : Arc < RwLock < AllWindowEdges > > ,
601- sender : Sender < UIEvent > ,
602597 w : & mut DoubleWindow ,
603598 ev : Event ,
604599 ) -> bool {
@@ -757,7 +752,7 @@ impl UIThread {
757752 }
758753 fltk:: enums:: Event :: Paste => {
759754 if state. dnd && state. released {
760- sender . send ( UIEvent :: DroppedFiles ( app:: event_text ( ) ) ) ;
755+ ui_send ! ( UIEvent :: DroppedFiles ( app:: event_text( ) ) ) ;
761756 state. dnd = false ;
762757 state. released = false ;
763758 true
@@ -776,11 +771,11 @@ impl UIThread {
776771 match dy {
777772 // uhhhhhhhh these are the opposite of what you'd think (???)
778773 app:: MouseWheel :: Up => {
779- sender . send ( UIEvent :: VolumeDown ) ;
774+ ui_send ! ( UIEvent :: VolumeDown ) ;
780775 true
781776 }
782777 app:: MouseWheel :: Down => {
783- sender . send ( UIEvent :: VolumeUp ) ;
778+ ui_send ! ( UIEvent :: VolumeUp ) ;
784779 true
785780 }
786781 _ => false ,
@@ -822,7 +817,7 @@ fn create_horizontal_gradient_frame(
822817 frame
823818}
824819
825- fn add_bar ( win : & mut DoubleWindow , s : Sender < UIEvent > , close_message : UIEvent , title : & str ) {
820+ fn add_bar ( win : & mut DoubleWindow , close_message : UIEvent , title : & str ) {
826821 let mut bar = Group :: new ( 4 , 4 , win. width ( ) - 8 , 17 , "" ) ;
827822 let mut bar_bg = create_horizontal_gradient_frame (
828823 4 ,
@@ -851,7 +846,7 @@ fn add_bar(win: &mut DoubleWindow, s: Sender<UIEvent>, close_message: UIEvent, t
851846 bar_btn_close. set_align ( Align :: Center | Align :: ImageBackdrop ) ;
852847 //bar_btn_close.set_color(Color::White);
853848 //bar_btn_close.set_frame(FrameType::BorderBox);
854- bar_btn_close. emit ( s , close_message) ;
849+ bar_btn_close. emit ( sender ! ( ) , close_message) ;
855850 bar. add ( & bar_btn_close) ;
856851
857852 bar. end ( ) ;
0 commit comments