Skip to content

Commit 2e535c3

Browse files
committed
improv: Use glib signal for matrix changes
If this doesn't run into issues, it's much cleaner.
1 parent dec5e55 commit 2e535c3

File tree

3 files changed

+21
-32
lines changed

3 files changed

+21
-32
lines changed

backend/src/board.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ impl ObjectSubclass for DaemonBoardInner {
3535
impl ObjectImpl for DaemonBoardInner {
3636
fn signals() -> &'static [Signal] {
3737
static SIGNALS: Lazy<Vec<Signal>> = Lazy::new(|| {
38-
vec![Signal::builder("leds-changed", &[], glib::Type::UNIT.into()).build()]
38+
vec![
39+
Signal::builder("leds-changed", &[], glib::Type::UNIT.into()).build(),
40+
Signal::builder("matrix-changed", &[], glib::Type::UNIT.into()).build(),
41+
]
3942
});
4043
SIGNALS.as_ref()
4144
}
@@ -138,9 +141,20 @@ impl DaemonBoard {
138141
.unwrap_or(false);
139142
changed |= key.pressed.replace(pressed) != pressed;
140143
}
144+
if changed {
145+
self.emit_by_name("matrix-changed", &[]).unwrap();
146+
}
141147
Ok(changed)
142148
}
143149

150+
pub fn connect_matrix_changed<F: Fn() + 'static>(&self, cb: F) {
151+
self.connect_local("matrix-changed", false, move |_| {
152+
cb();
153+
None
154+
})
155+
.unwrap();
156+
}
157+
144158
pub fn max_brightness(&self) -> i32 {
145159
*self.inner().max_brightness
146160
}

src/application/keyboard.rs

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -421,17 +421,6 @@ impl Keyboard {
421421
self.notify("selected");
422422
}
423423

424-
fn redraw(&self) {
425-
self.queue_draw();
426-
// TODO: clean up this hack to only redraw keyboard on main page
427-
if let Some(parent) = self.get_parent() {
428-
parent.queue_draw();
429-
if let Some(grandparent) = parent.get_parent() {
430-
grandparent.queue_draw();
431-
}
432-
}
433-
}
434-
435424
fn refresh(&self) -> bool {
436425
if !self.board().has_matrix() {
437426
return false;
@@ -446,26 +435,11 @@ impl Keyboard {
446435
return true;
447436
}
448437

449-
match self.board().refresh_matrix() {
450-
Ok(changed) => {
451-
if changed {
452-
let keyboard = self;
453-
keyboard.redraw();
454-
// Sometimes the redraw is missed, so send it again in 10ms
455-
glib::timeout_add_local(
456-
time::Duration::from_millis(10),
457-
clone!(@weak keyboard => @default-return glib::Continue(false), move || {
458-
keyboard.redraw();
459-
glib::Continue(false)
460-
}),
461-
);
462-
}
463-
true
464-
}
465-
Err(err) => {
466-
error!("Failed to get matrix: {}", err);
467-
false
468-
}
438+
if let Err(err) = self.board().refresh_matrix() {
439+
error!("Failed to get matrix: {}", err);
440+
false
441+
} else {
442+
true
469443
}
470444
}
471445
}

src/application/keyboard_layer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ impl KeyboardLayer {
221221
..set_size_request(width, height);
222222
};
223223
board.connect_leds_changed(clone!(@weak obj => move || obj.queue_draw()));
224+
board.connect_matrix_changed(clone!(@weak obj => move || obj.queue_draw()));
224225
obj.inner().page.set(page);
225226
obj.inner().board.set(board);
226227
obj

0 commit comments

Comments
 (0)