Skip to content

Commit

Permalink
fix(regression): GTK refactor causing updates to be missed
Browse files Browse the repository at this point in the history
Regression introduced by recent GTK refactor.

The `glib_recv` macros  previously using the passed in expression as the receiver, which was causing a new receiver to be created *every* time an event was received. This caused some peculiar behaviours where some events just never got through if sent too close to each other.

This was most obvious in the `workspaces` module.

Fixes #381
  • Loading branch information
JakeStanger committed Dec 31, 2023
1 parent b2a37a3 commit b4d7545
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/dynamic_value/dynamic_bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl DynamicBool {
_ => self,
};

let (tx, mut rx) = mpsc::channel(32);
let (tx, rx) = mpsc::channel(32);

glib_recv_mpsc!(rx, val => f(val));

Expand Down
2 changes: 1 addition & 1 deletion src/dynamic_value/dynamic_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ where
let tokens = parse_input(input);

let label_parts = arc_mut!(vec![]);
let (tx, mut rx) = mpsc::channel(32);
let (tx, rx) = mpsc::channel(32);

for (i, segment) in tokens.into_iter().enumerate() {
match segment {
Expand Down
2 changes: 1 addition & 1 deletion src/image/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl<'a> ImageProvider<'a> {
#[cfg(feature = "http")]
if let ImageLocation::Remote(url) = &self.location {
let url = url.clone();
let (tx, mut rx) = mpsc::channel(64);
let (tx, rx) = mpsc::channel(64);

spawn(async move {
let bytes = Self::get_bytes_from_http(url).await;
Expand Down
2 changes: 1 addition & 1 deletion src/ipc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Ipc {
///
/// Once started, the server will begin accepting connections.
pub fn start(&self, application: &Application, ironbar: Rc<Ironbar>) {
let (cmd_tx, mut cmd_rx) = mpsc::channel(32);
let (cmd_tx, cmd_rx) = mpsc::channel(32);
let (res_tx, mut res_rx) = mpsc::channel(32);

let path = self.path.clone();
Expand Down
16 changes: 10 additions & 6 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ macro_rules! try_send {
/// ```
#[macro_export]
macro_rules! glib_recv {
($rx:expr, $val:ident => $expr:expr) => {
($rx:expr, $val:ident => $expr:expr) => {{
glib::spawn_future_local(async move {
while let Ok($val) = $rx.recv().await {
// re-delcare in case ie `context.subscribe()` is passed directly
let mut rx = $rx;
while let Ok($val) = rx.recv().await {
$expr
}
});
};
}};
}

/// Spawns a `GLib` future on the local thread, and calls `rx.recv()`
Expand All @@ -83,13 +85,15 @@ macro_rules! glib_recv {
/// ```
#[macro_export]
macro_rules! glib_recv_mpsc {
($rx:expr, $val:ident => $expr:expr) => {
($rx:expr, $val:ident => $expr:expr) => {{
glib::spawn_future_local(async move {
while let Some($val) = $rx.recv().await {
// re-delcare in case ie `context.subscribe()` is passed directly
let mut rx = $rx;
while let Some($val) = rx.recv().await {
$expr
}
});
};
}};
}

/// Locks a `Mutex`.
Expand Down
2 changes: 1 addition & 1 deletion src/modules/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Module<Button> for ClipboardModule {
fn into_popup(
self,
tx: mpsc::Sender<Self::ReceiveMessage>,
mut rx: broadcast::Receiver<Self::SendMessage>,
rx: broadcast::Receiver<Self::SendMessage>,
_info: &ModuleInfo,
) -> Option<gtk::Box>
where
Expand Down
4 changes: 2 additions & 2 deletions src/modules/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl Module<Button> for ClockModule {
let format = self.format.clone();
let locale = Locale::try_from(self.locale.as_str()).unwrap_or(Locale::POSIX);

let mut rx = context.subscribe();
let rx = context.subscribe();
glib_recv!(rx, date => {
let date_string = format!("{}", date.format_localized(&format, locale));
label.set_label(&date_string);
Expand All @@ -126,7 +126,7 @@ impl Module<Button> for ClockModule {
fn into_popup(
self,
_tx: mpsc::Sender<Self::ReceiveMessage>,
mut rx: broadcast::Receiver<Self::SendMessage>,
rx: broadcast::Receiver<Self::SendMessage>,
_info: &ModuleInfo,
) -> Option<gtk::Box> {
let container = gtk::Box::new(Orientation::Vertical, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/custom/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl CustomWidget for ProgressWidget {
let script = Script::from(value);
let progress = progress.clone();

let (tx, mut rx) = mpsc::channel(128);
let (tx, rx) = mpsc::channel(128);

spawn(async move {
script
Expand Down
2 changes: 1 addition & 1 deletion src/modules/custom/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl CustomWidget for SliderWidget {
let script = Script::from(value);
let scale = scale.clone();

let (tx, mut rx) = mpsc::channel(128);
let (tx, rx) = mpsc::channel(128);

spawn(async move {
script
Expand Down
4 changes: 2 additions & 2 deletions src/modules/launcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ impl Module<gtk::Box> for LauncherModule {
let mut buttons = IndexMap::<String, ItemButton>::new();

let tx = context.tx.clone();
let mut rx = context.subscribe();
let rx = context.subscribe();
glib_recv!(rx, event => {
match event {
LauncherUpdate::AddItem(item) => {
Expand Down Expand Up @@ -428,7 +428,7 @@ impl Module<gtk::Box> for LauncherModule {
fn into_popup(
self,
controller_tx: mpsc::Sender<Self::ReceiveMessage>,
mut rx: broadcast::Receiver<Self::SendMessage>,
rx: broadcast::Receiver<Self::SendMessage>,
_info: &ModuleInfo,
) -> Option<gtk::Box> {
const MAX_WIDTH: i32 = 250;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn register_popup_content(
/// and communicating update messages between controllers and widgets/popups.
fn setup_receiver<TSend>(
tx: broadcast::Sender<TSend>,
mut rx: mpsc::Receiver<ModuleUpdateEvent<TSend>>,
rx: mpsc::Receiver<ModuleUpdateEvent<TSend>>,
popup: Rc<RefCell<Popup>>,
name: &'static str,
id: usize,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/music/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl Module<Button> for MusicModule {
let button = button.clone();

let tx = context.tx.clone();
let mut rx = context.subscribe();
let rx = context.subscribe();

glib_recv!(rx, event => {
let ControllerEvent::Update(mut event) = event else {
Expand Down Expand Up @@ -263,7 +263,7 @@ impl Module<Button> for MusicModule {
fn into_popup(
self,
tx: mpsc::Sender<Self::ReceiveMessage>,
mut rx: broadcast::Receiver<Self::SendMessage>,
rx: broadcast::Receiver<Self::SendMessage>,
info: &ModuleInfo,
) -> Option<gtk::Box> {
let icon_theme = info.icon_theme;
Expand Down
4 changes: 2 additions & 2 deletions src/modules/upower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl Module<gtk::Button> for UpowerModule {
label.set_angle(info.bar_position.get_angle());
let format = self.format.clone();

let mut rx = context.subscribe();
let rx = context.subscribe();
glib_recv!(rx, properties => {
let format = format.replace("{percentage}", &properties.percentage.to_string());
let icon_name = String::from("icon:") + &properties.icon_name;
Expand All @@ -203,7 +203,7 @@ impl Module<gtk::Button> for UpowerModule {
fn into_popup(
self,
_tx: mpsc::Sender<Self::ReceiveMessage>,
mut rx: broadcast::Receiver<Self::SendMessage>,
rx: broadcast::Receiver<Self::SendMessage>,
_info: &ModuleInfo,
) -> Option<gtk::Box>
where
Expand Down
2 changes: 1 addition & 1 deletion src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn load_css(style_path: PathBuf) {
GTK_STYLE_PROVIDER_PRIORITY_USER as u32,
);

let (tx, mut rx) = mpsc::channel(8);
let (tx, rx) = mpsc::channel(8);

spawn(async move {
let style_path2 = style_path.clone();
Expand Down

0 comments on commit b4d7545

Please sign in to comment.