Skip to content

Commit dec5e55

Browse files
committed
feat: Inital code showing color in border
1 parent 9996427 commit dec5e55

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

backend/src/board.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use glib::subclass::prelude::*;
1+
use glib::{
2+
prelude::*,
3+
subclass::{prelude::*, Signal},
4+
};
5+
use once_cell::sync::Lazy;
26
use std::{cell::Cell, collections::HashMap, rc::Rc};
37

48
use crate::{BoardId, Daemon, DerefCell, Key, KeyMap, Layer, Layout};
@@ -16,7 +20,7 @@ pub struct DaemonBoardInner {
1620
keys: DerefCell<Vec<Key>>,
1721
layers: DerefCell<Vec<Layer>>,
1822
max_brightness: DerefCell<i32>,
19-
pub(crate) leds_changed: Cell<bool>,
23+
leds_changed: Cell<bool>,
2024
has_led_save: DerefCell<bool>,
2125
has_matrix: DerefCell<bool>,
2226
}
@@ -28,7 +32,14 @@ impl ObjectSubclass for DaemonBoardInner {
2832
type Type = DaemonBoard;
2933
}
3034

31-
impl ObjectImpl for DaemonBoardInner {}
35+
impl ObjectImpl for DaemonBoardInner {
36+
fn signals() -> &'static [Signal] {
37+
static SIGNALS: Lazy<Vec<Signal>> = Lazy::new(|| {
38+
vec![Signal::builder("leds-changed", &[], glib::Type::UNIT.into()).build()]
39+
});
40+
SIGNALS.as_ref()
41+
}
42+
}
3243

3344
glib::wrapper! {
3445
pub struct DaemonBoard(ObjectSubclass<DaemonBoardInner>);
@@ -69,8 +80,7 @@ impl DaemonBoard {
6980
self_.inner().has_matrix.set(has_matrix);
7081

7182
let keys = self_
72-
.inner()
73-
.layout
83+
.layout()
7484
.physical
7585
.keys
7686
.iter()
@@ -86,10 +96,23 @@ impl DaemonBoard {
8696
Ok(self_)
8797
}
8898

89-
pub(crate) fn inner(&self) -> &DaemonBoardInner {
99+
fn inner(&self) -> &DaemonBoardInner {
90100
DaemonBoardInner::from_instance(self)
91101
}
92102

103+
pub(crate) fn set_leds_changed(&self) {
104+
self.inner().leds_changed.set(true);
105+
self.emit_by_name("leds-changed", &[]).unwrap();
106+
}
107+
108+
pub fn connect_leds_changed<F: Fn() + 'static>(&self, cb: F) {
109+
self.connect_local("leds-changed", false, move |_| {
110+
cb();
111+
None
112+
})
113+
.unwrap();
114+
}
115+
93116
pub(crate) fn daemon(&self) -> &dyn Daemon {
94117
self.inner().daemon.as_ref()
95118
}

backend/src/key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl Key {
138138
board.daemon().set_color(board.board(), *index, color)?;
139139
}
140140
self.led_color.set(Some(color));
141-
board.inner().leds_changed.set(true);
141+
board.set_leds_changed();
142142
Ok(())
143143
}
144144

backend/src/layer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl Layer {
7171
.daemon()
7272
.set_mode(board.board(), self.layer, mode.index, speed)?;
7373
self.mode.set(Some((mode.index, speed)));
74-
board.inner().leds_changed.set(true);
74+
board.set_leds_changed();
7575
Ok(())
7676
}
7777

@@ -85,7 +85,7 @@ impl Layer {
8585
.daemon()
8686
.set_brightness(board.board(), self.index, brightness)?;
8787
self.brightness.set(brightness);
88-
board.inner().leds_changed.set(true);
88+
board.set_leds_changed();
8989
Ok(())
9090
}
9191

@@ -97,7 +97,7 @@ impl Layer {
9797
let board = self.board();
9898
board.daemon().set_color(board.board(), self.index, color)?;
9999
self.color.set(color);
100-
board.inner().leds_changed.set(true);
100+
board.set_leds_changed();
101101
Ok(())
102102
}
103103
}

src/application/keyboard_layer.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use cascade::cascade;
2+
use glib::clone;
23
use gtk::prelude::*;
34
use gtk::subclass::prelude::*;
45
use std::{
@@ -8,7 +9,7 @@ use std::{
89

910
use super::Page;
1011
use crate::{DerefCell, SelectedKeys};
11-
use backend::{DaemonBoard, Key, Rect, Rgb};
12+
use backend::{DaemonBoard, Key, Layer, Rect, Rgb};
1213

1314
const SCALE: f64 = 64.0;
1415
const MARGIN: f64 = 2.;
@@ -82,12 +83,28 @@ impl WidgetImpl for KeyboardLayerInner {
8283
fn draw(&self, widget: &KeyboardLayer, cr: &cairo::Context) -> Inhibit {
8384
self.parent_draw(widget, cr);
8485

86+
let layer = widget.page().layer().map(|i| &self.board.layers()[i]);
87+
let (is_per_key, has_hue) = layer
88+
.and_then(Layer::mode)
89+
.map(|x| (x.0.is_per_key(), x.0.has_hue))
90+
.unwrap_or_default();
8591
let selected = Rgb::new(0xfb, 0xb8, 0x6c).to_floats();
92+
8693
for (i, k) in widget.keys().iter().enumerate() {
8794
let Rect { x, y, w, h } = scale_rect(&k.physical);
8895

8996
let mut bg = k.background_color.to_floats();
9097

98+
let border_color = layer.and_then(|layer| {
99+
if is_per_key {
100+
k.color()
101+
} else if has_hue {
102+
Some(layer.color())
103+
} else {
104+
None
105+
}
106+
});
107+
91108
if k.pressed() {
92109
// Invert colors if pressed
93110
bg.0 = 1.0 - bg.0;
@@ -116,6 +133,11 @@ impl WidgetImpl for KeyboardLayerInner {
116133
cr.set_source_rgb(selected.0, selected.1, selected.2);
117134
cr.set_line_width(4.);
118135
cr.stroke();
136+
} else if let Some(color) = border_color {
137+
let color = color.to_rgb().to_floats();
138+
cr.set_source_rgb(color.0, color.1, color.2);
139+
cr.set_line_width(1.);
140+
cr.stroke();
119141
}
120142

121143
// Draw label
@@ -198,6 +220,7 @@ impl KeyboardLayer {
198220
glib::Object::new::<Self>(&[]).unwrap();
199221
..set_size_request(width, height);
200222
};
223+
board.connect_leds_changed(clone!(@weak obj => move || obj.queue_draw()));
201224
obj.inner().page.set(page);
202225
obj.inner().board.set(board);
203226
obj

0 commit comments

Comments
 (0)