Skip to content

Commit a8decbd

Browse files
authored
Merge pull request JakeStanger#996 from JakeStanger/refactor/into-popup
refactor(module): remove redundant `into_popup` parameters
2 parents 09ba381 + 8575300 commit a8decbd

File tree

8 files changed

+41
-77
lines changed

8 files changed

+41
-77
lines changed

src/modules/clipboard.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use gtk::{Button, EventBox, Image, Label, Orientation, RadioButton, Widget};
1717
use serde::Deserialize;
1818
use std::collections::HashMap;
1919
use std::ops::Deref;
20-
use tokio::sync::{broadcast, mpsc};
20+
use tokio::sync::mpsc;
2121
use tracing::{debug, error};
2222

2323
#[derive(Debug, Deserialize, Clone)]
@@ -158,19 +158,16 @@ impl Module<Button> for ClipboardModule {
158158
tx.send_spawn(ModuleUpdateEvent::TogglePopup(button.popup_id()));
159159
});
160160

161-
let rx = context.subscribe();
162161
let popup = self
163-
.into_popup(context.controller_tx.clone(), rx, context, info)
162+
.into_popup(context, info)
164163
.into_popup_parts(vec![&button]);
165164

166165
Ok(ModuleParts::new(button.deref().clone(), popup))
167166
}
168167

169168
fn into_popup(
170169
self,
171-
tx: mpsc::Sender<Self::ReceiveMessage>,
172-
rx: broadcast::Receiver<Self::SendMessage>,
173-
_context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
170+
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
174171
_info: &ModuleInfo,
175172
) -> Option<gtk::Box>
176173
where
@@ -188,7 +185,7 @@ impl Module<Button> for ClipboardModule {
188185

189186
{
190187
let hidden_option = hidden_option.clone();
191-
rx.recv_glib(move |event| {
188+
context.subscribe().recv_glib(move |event| {
192189
match event {
193190
ControllerEvent::Add(id, item) => {
194191
debug!("Adding new value with ID {}", id);
@@ -250,7 +247,7 @@ impl Module<Button> for ClipboardModule {
250247
button_wrapper.set_above_child(true);
251248

252249
{
253-
let tx = tx.clone();
250+
let tx = context.controller_tx.clone();
254251
button_wrapper.connect_button_press_event(
255252
move |button_wrapper, event| {
256253
// left click
@@ -272,7 +269,7 @@ impl Module<Button> for ClipboardModule {
272269
remove_button.style_context().add_class("btn-remove");
273270

274271
{
275-
let tx = tx.clone();
272+
let tx = context.controller_tx.clone();
276273
let entries = entries.clone();
277274
let row = row.clone();
278275

src/modules/clock.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use color_eyre::Result;
55
use gtk::prelude::*;
66
use gtk::{Align, Button, Calendar, Label, Orientation};
77
use serde::Deserialize;
8-
use tokio::sync::{broadcast, mpsc};
8+
use tokio::sync::mpsc;
99
use tokio::time::sleep;
1010

1111
use crate::channels::{AsyncSenderExt, BroadcastReceiverExt};
@@ -145,22 +145,15 @@ impl Module<Button> for ClockModule {
145145
});
146146

147147
let popup = self
148-
.into_popup(
149-
context.controller_tx.clone(),
150-
context.subscribe(),
151-
context,
152-
info,
153-
)
148+
.into_popup(context, info)
154149
.into_popup_parts(vec![&button]);
155150

156151
Ok(ModuleParts::new(button, popup))
157152
}
158153

159154
fn into_popup(
160155
self,
161-
_tx: mpsc::Sender<Self::ReceiveMessage>,
162-
rx: broadcast::Receiver<Self::SendMessage>,
163-
_context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
156+
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
164157
_info: &ModuleInfo,
165158
) -> Option<gtk::Box> {
166159
let container = gtk::Box::new(Orientation::Vertical, 0);
@@ -180,7 +173,7 @@ impl Module<Button> for ClockModule {
180173
let format = self.format_popup;
181174
let locale = Locale::try_from(self.locale.as_str()).unwrap_or(Locale::POSIX);
182175

183-
rx.recv_glib(move |date| {
176+
context.subscribe().recv_glib(move |date| {
184177
let date_string = format!("{}", date.format_localized(&format, locale));
185178
clock.set_label(&date_string);
186179
});

src/modules/custom/mod.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use gtk::{Button, IconTheme, Orientation};
2525
use serde::Deserialize;
2626
use std::cell::RefCell;
2727
use std::rc::Rc;
28-
use tokio::sync::{broadcast, mpsc};
28+
use tokio::sync::mpsc;
2929
use tracing::{debug, error};
3030

3131
#[derive(Debug, Deserialize, Clone)]
@@ -258,12 +258,7 @@ impl Module<gtk::Box> for CustomModule {
258258
.map_or(usize::MAX, PopupButton::popup_id);
259259

260260
let popup = self
261-
.into_popup(
262-
context.controller_tx.clone(),
263-
context.subscribe(),
264-
context,
265-
info,
266-
)
261+
.into_popup(context, info)
267262
.into_popup_parts_owned(popup_buttons.take());
268263

269264
Ok(ModuleParts {
@@ -274,8 +269,6 @@ impl Module<gtk::Box> for CustomModule {
274269

275270
fn into_popup(
276271
self,
277-
tx: mpsc::Sender<Self::ReceiveMessage>,
278-
_rx: broadcast::Receiver<Self::SendMessage>,
279272
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
280273
info: &ModuleInfo,
281274
) -> Option<gtk::Box>
@@ -287,7 +280,7 @@ impl Module<gtk::Box> for CustomModule {
287280
if let Some(popup) = self.popup {
288281
let custom_context = CustomWidgetContext {
289282
info,
290-
tx: &tx,
283+
tx: &context.controller_tx,
291284
bar_orientation: Orientation::Horizontal,
292285
is_popup: true,
293286
icon_theme: info.icon_theme,

src/modules/launcher/mod.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use serde::Deserialize;
2121
use std::ops::Deref;
2222
use std::process::{Command, Stdio};
2323
use std::sync::Arc;
24-
use tokio::sync::{broadcast, mpsc};
24+
use tokio::sync::mpsc;
2525
use tracing::{debug, error, trace};
2626

2727
#[derive(Debug, Deserialize, Clone)]
@@ -599,10 +599,7 @@ impl Module<gtk::Box> for LauncherModule {
599599
rx.recv_glib(handle_event);
600600
}
601601

602-
let rx = context.subscribe();
603-
let popup = self
604-
.into_popup(context.controller_tx.clone(), rx, context, info)
605-
.into_popup_parts(vec![]); // since item buttons are dynamic, they pass their geometry directly
602+
let popup = self.into_popup(context, info).into_popup_parts(vec![]); // since item buttons are dynamic, they pass their geometry directly
606603

607604
Ok(ModuleParts {
608605
widget: container,
@@ -612,9 +609,7 @@ impl Module<gtk::Box> for LauncherModule {
612609

613610
fn into_popup(
614611
self,
615-
controller_tx: mpsc::Sender<Self::ReceiveMessage>,
616-
rx: broadcast::Receiver<Self::SendMessage>,
617-
_context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
612+
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
618613
_info: &ModuleInfo,
619614
) -> Option<gtk::Box> {
620615
const MAX_WIDTH: i32 = 250;
@@ -630,7 +625,7 @@ impl Module<gtk::Box> for LauncherModule {
630625

631626
{
632627
let container = container.clone();
633-
rx.recv_glib(move |event| {
628+
context.subscribe().recv_glib(move |event| {
634629
match event {
635630
LauncherUpdate::AddItem(item) => {
636631
let app_id = item.app_id.clone();
@@ -647,7 +642,7 @@ impl Module<gtk::Box> for LauncherModule {
647642
button.label.truncate(self.truncate_popup);
648643

649644
{
650-
let tx = controller_tx.clone();
645+
let tx = context.controller_tx.clone();
651646
button.connect_clicked(move |_| {
652647
tx.send_spawn(ItemEvent::FocusWindow(win.id));
653648
});
@@ -673,7 +668,7 @@ impl Module<gtk::Box> for LauncherModule {
673668
button.label.truncate(self.truncate_popup);
674669

675670
{
676-
let tx = controller_tx.clone();
671+
let tx = context.controller_tx.clone();
677672
button.connect_clicked(move |_button| {
678673
tx.send_spawn(ItemEvent::FocusWindow(win.id));
679674
});

src/modules/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,6 @@ where
270270

271271
fn into_popup(
272272
self,
273-
_tx: mpsc::Sender<Self::ReceiveMessage>,
274-
_rx: broadcast::Receiver<Self::SendMessage>,
275273
_context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
276274
_info: &ModuleInfo,
277275
) -> Option<gtk::Box>

src/modules/music/mod.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use glib::{Propagation, PropertySet};
99
use gtk::prelude::*;
1010
use gtk::{Button, IconTheme, Label, Orientation, Scale};
1111
use regex::Regex;
12-
use tokio::sync::{broadcast, mpsc};
12+
use tokio::sync::mpsc;
1313
use tracing::error;
1414

1515
pub use self::config::MusicModule;
@@ -257,19 +257,16 @@ impl Module<Button> for MusicModule {
257257
});
258258
};
259259

260-
let rx = context.subscribe();
261260
let popup = self
262-
.into_popup(context.controller_tx.clone(), rx, context, info)
261+
.into_popup(context, info)
263262
.into_popup_parts(vec![&button]);
264263

265264
Ok(ModuleParts::new(button, popup))
266265
}
267266

268267
fn into_popup(
269268
self,
270-
tx: mpsc::Sender<Self::ReceiveMessage>,
271-
rx: broadcast::Receiver<Self::SendMessage>,
272-
_context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
269+
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
273270
info: &ModuleInfo,
274271
) -> Option<gtk::Box> {
275272
let icon_theme = info.icon_theme;
@@ -338,27 +335,27 @@ impl Module<Button> for MusicModule {
338335
main_container.add(&volume_box);
339336
container.add(&main_container);
340337

341-
let tx_prev = tx.clone();
338+
let tx_prev = context.controller_tx.clone();
342339
btn_prev.connect_clicked(move |_| {
343340
tx_prev.send_spawn(PlayerCommand::Previous);
344341
});
345342

346-
let tx_play = tx.clone();
343+
let tx_play = context.controller_tx.clone();
347344
btn_play.connect_clicked(move |_| {
348345
tx_play.send_spawn(PlayerCommand::Play);
349346
});
350347

351-
let tx_pause = tx.clone();
348+
let tx_pause = context.controller_tx.clone();
352349
btn_pause.connect_clicked(move |_| {
353350
tx_pause.send_spawn(PlayerCommand::Pause);
354351
});
355352

356-
let tx_next = tx.clone();
353+
let tx_next = context.controller_tx.clone();
357354
btn_next.connect_clicked(move |_| {
358355
tx_next.send_spawn(PlayerCommand::Next);
359356
});
360357

361-
let tx_vol = tx.clone();
358+
let tx_vol = context.controller_tx.clone();
362359
volume_slider.connect_change_value(move |_, _, val| {
363360
tx_vol.send_spawn(PlayerCommand::Volume(val as u8));
364361
Propagation::Proceed
@@ -392,6 +389,7 @@ impl Module<Button> for MusicModule {
392389

393390
{
394391
let drag_lock = drag_lock.clone();
392+
let tx = context.controller_tx.clone();
395393
progress.connect_button_release_event(move |scale, _| {
396394
let value = scale.value();
397395
tx.send_spawn(PlayerCommand::Seek(Duration::from_secs_f64(value)));
@@ -408,7 +406,7 @@ impl Module<Button> for MusicModule {
408406
let image_size = self.cover_image_size;
409407

410408
let mut prev_cover = None;
411-
rx.recv_glib(move |event| {
409+
context.subscribe().recv_glib(move |event| {
412410
match event {
413411
ControllerEvent::Update(Some(update)) => {
414412
// only update art when album changes

src/modules/upower.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use futures_lite::stream::StreamExt;
33
use gtk::{Button, prelude::*};
44
use gtk::{Label, Orientation};
55
use serde::Deserialize;
6-
use tokio::sync::{broadcast, mpsc};
6+
use tokio::sync::mpsc;
77
use zbus;
88
use zbus::fdo::PropertiesProxy;
99

@@ -225,19 +225,16 @@ impl Module<Button> for UpowerModule {
225225
label.set_label_escaped(&format);
226226
});
227227

228-
let rx = context.subscribe();
229228
let popup = self
230-
.into_popup(context.controller_tx.clone(), rx, context, info)
229+
.into_popup(context, info)
231230
.into_popup_parts(vec![&button]);
232231

233232
Ok(ModuleParts::new(button, popup))
234233
}
235234

236235
fn into_popup(
237236
self,
238-
_tx: mpsc::Sender<Self::ReceiveMessage>,
239-
rx: broadcast::Receiver<Self::SendMessage>,
240-
_context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
237+
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
241238
_info: &ModuleInfo,
242239
) -> Option<gtk::Box>
243240
where
@@ -251,7 +248,7 @@ impl Module<Button> for UpowerModule {
251248
label.add_class("upower-details");
252249
container.add(&label);
253250

254-
rx.recv_glib(move |properties| {
251+
context.subscribe().recv_glib(move |properties| {
255252
let state = properties.state;
256253
let format = match state {
257254
BatteryState::Charging | BatteryState::PendingCharge => {

0 commit comments

Comments
 (0)