Skip to content

Commit cd2e046

Browse files
committed
refactor: global fltk sender macro
1 parent 99fa7a4 commit cd2e046

7 files changed

Lines changed: 114 additions & 137 deletions

File tree

‎player/src/connection.rs‎

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use protocol::{AudioData, Message, Notification, Request, Response, RoomOptions,
1616

1717
use crate::{
1818
audio::{AudioCommand, AudioStatusRx, AudioThread, AudioThreadHandle},
19-
gui::{connection_window::ServerStatus, UIThreadHandle, UIUpdateEvent},
19+
gui::{connection_window::ServerStatus, ui_update, UIUpdateEvent},
2020
preferences::Server,
2121
transmit::{TransmitCommand, TransmitThread, TransmitThreadHandle},
2222
AudioStatus, ConnectionState, RoomState, State,
@@ -122,8 +122,6 @@ pub struct ConnectionActor {
122122

123123
state: Arc<RwLock<State>>,
124124

125-
ui: UIThreadHandle,
126-
127125
transmit: TransmitThreadHandle,
128126
audio: AudioThreadHandle,
129127
audio_data_rx: mpsc::UnboundedReceiver<AudioData>,
@@ -135,7 +133,6 @@ pub struct ConnectionActor {
135133
impl ConnectionActor {
136134
pub async fn spawn(
137135
state: Arc<RwLock<State>>,
138-
ui: UIThreadHandle,
139136
server: Server,
140137
) -> Result<ConnectionActorHandle, Box<dyn Error>> {
141138
let (tx, rx) = mpsc::unbounded_channel();
@@ -158,7 +155,6 @@ impl ConnectionActor {
158155
let mut t = ConnectionActor::new(
159156
state2,
160157
server,
161-
ui,
162158
transmit2,
163159
audio2,
164160
audio_data_rx,
@@ -204,7 +200,6 @@ impl ConnectionActor {
204200
pub async fn new(
205201
state: Arc<RwLock<State>>,
206202
server: Server,
207-
ui: UIThreadHandle,
208203
transmit: TransmitThreadHandle,
209204
audio: AudioThreadHandle,
210205
audio_data_rx: mpsc::UnboundedReceiver<AudioData>,
@@ -223,8 +218,6 @@ impl ConnectionActor {
223218
next_id: 0,
224219
pending: HashMap::new(),
225220

226-
ui,
227-
228221
transmit,
229222
audio,
230223
audio_data_rx,
@@ -306,22 +299,22 @@ impl ConnectionActor {
306299
},
307300
None => 0.0
308301
};
309-
self.ui.update(UIUpdateEvent::SetTime(elapsed, total));
302+
ui_update!(UIUpdateEvent::SetTime(elapsed, total));
310303
}
311304
AudioStatus::Buffering(is_buffering) => {
312305
let mut s = self.state.write().await;
313306
s.connection.as_mut().unwrap().room.as_mut().unwrap().buffering = is_buffering;
314307

315-
self.ui.update(UIUpdateEvent::Status);
308+
ui_update!(UIUpdateEvent::Status);
316309
}
317310
AudioStatus::Finished => {
318-
self.ui.update(UIUpdateEvent::Status);
311+
ui_update!(UIUpdateEvent::Status);
319312
}
320313
AudioStatus::Visualizer(bars) => {
321-
self.ui.update(UIUpdateEvent::Visualizer(bars))
314+
ui_update!(UIUpdateEvent::Visualizer(bars))
322315
}
323316
AudioStatus::Buffer(val) => {
324-
self.ui.update(UIUpdateEvent::Buffer(val))
317+
ui_update!(UIUpdateEvent::Buffer(val))
325318
}
326319
}
327320
},
@@ -421,8 +414,8 @@ impl ConnectionActor {
421414
let mut room = conn.room.as_mut().expect("lol");
422415
room.queue = queue;
423416

424-
self.ui.update(UIUpdateEvent::QueueChanged);
425-
self.ui.update(UIUpdateEvent::Status);
417+
ui_update!(UIUpdateEvent::QueueChanged);
418+
ui_update!(UIUpdateEvent::Status);
426419
}
427420
Notification::Playing(playing) => {
428421
// println!("got playing: {:?}", playing);
@@ -439,14 +432,14 @@ impl ConnectionActor {
439432
}
440433
}
441434

442-
self.ui.update(UIUpdateEvent::QueueChanged);
443-
self.ui.update(UIUpdateEvent::Status);
435+
ui_update!(UIUpdateEvent::QueueChanged);
436+
ui_update!(UIUpdateEvent::Status);
444437
}
445438
Notification::ConnectedUsers(list) => {
446439
let mut room = conn.room.as_mut().expect("lol");
447440
room.connected_users = list;
448441

449-
self.ui.update(UIUpdateEvent::UserListChanged);
442+
ui_update!(UIUpdateEvent::UserListChanged);
450443
}
451444
Notification::Room(maybe_room) => {
452445
match maybe_room {
@@ -466,10 +459,10 @@ impl ConnectionActor {
466459
}
467460
}
468461

469-
self.ui.update(UIUpdateEvent::RoomChanged);
470-
self.ui.update(UIUpdateEvent::QueueChanged);
471-
self.ui.update(UIUpdateEvent::UserListChanged);
472-
self.ui.update(UIUpdateEvent::Status);
462+
ui_update!(UIUpdateEvent::RoomChanged);
463+
ui_update!(UIUpdateEvent::QueueChanged);
464+
ui_update!(UIUpdateEvent::UserListChanged);
465+
ui_update!(UIUpdateEvent::Status);
473466
}
474467
Notification::RoomList(list) => {
475468
// TODO: we should probably do the server status state differently
@@ -478,8 +471,7 @@ impl ConnectionActor {
478471
rooms: Some(list),
479472
tried: true,
480473
};
481-
self.ui
482-
.update(UIUpdateEvent::UpdateConnectionTreePartial(s));
474+
ui_update!(UIUpdateEvent::UpdateConnectionTreePartial(s));
483475
}
484476
}
485477
}

‎player/src/gui.rs‎

Lines changed: 55 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::sync::Arc;
33
use fltk::app;
44
use fltk::app::App;
55
use fltk::app::Receiver;
6-
use fltk::app::Sender;
76
use fltk::button::*;
87
use fltk::draw;
98
use fltk::enums::*;
@@ -38,6 +37,32 @@ use self::play_status::PlayStatusIcon;
3837
use self::preferences_window::PrefsWindow;
3938
use 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)]
4368
pub 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-
122136
type UiTx = tokio::sync::mpsc::UnboundedSender<UIEvent>;
123137
type UiRx = tokio::sync::mpsc::UnboundedReceiver<UIEvent>;
124138
pub 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

168181
impl 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

Comments
 (0)