Skip to content

Commit 0b995b7

Browse files
ids1024jackpot51
authored andcommitted
feat: Use KeyboardColorButton in configurator
1 parent 3469c61 commit 0b995b7

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

src/application/keyboard.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ use std::{
1616
},
1717
};
1818

19-
use crate::color::Rgb;
2019
use crate::daemon::Daemon;
20+
use crate::keyboard::Keyboard as ColorKeyboard;
21+
use crate::keyboard_color_button::KeyboardColorButton;
2122
use super::key::Key;
2223
use super::picker::Picker;
2324
use super::rect::Rect;
@@ -456,18 +457,14 @@ button {
456457
gdk::RGBA::black()
457458
};
458459

459-
let color_button = gtk::ColorButton::with_rgba(&color_rgba);
460-
color_button.set_halign(gtk::Align::Fill);
461-
let kb = self.clone();
462-
color_button.connect_color_set(move |this| {
463-
let rgba = this.get_rgba();
464-
let color = Rgb::from_floats(rgba.red, rgba.green, rgba.blue);
465-
if let Some(ref daemon) = kb.daemon_opt {
466-
if let Err(err) = daemon.set_color(kb.daemon_board, color) {
467-
eprintln!("{}", err);
468-
}
469-
}
470-
});
460+
let color_keyboard = if let Some(ref daemon) = self.daemon_opt {
461+
ColorKeyboard::new_daemon(daemon.clone(), self.daemon_board)
462+
} else {
463+
464+
ColorKeyboard::new_dummy()
465+
};
466+
let color_button = KeyboardColorButton::new(color_keyboard).widget().clone();
467+
color_button.set_valign(gtk::Align::Center);
471468

472469
for page in &[
473470
"Layer 1",

src/keyboard.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::iter::Iterator;
1111
use std::rc::{Rc, Weak};
1212

1313
use crate::color::Rgb;
14+
use crate::daemon::Daemon;
1415

1516
const DBUS_NAME: &'static str = "com.system76.PowerDaemon";
1617
const DBUS_KEYBOARD_IFACE: &'static str = "com.system76.PowerDaemon.Keyboard";
@@ -32,6 +33,10 @@ enum KeyboardImplementation {
3233
Dummy {
3334
properties: HashMap<&'static str, RefCell<Variant>>,
3435
},
36+
Daemon {
37+
daemon: Rc<dyn Daemon>,
38+
board: usize,
39+
}
3540
}
3641

3742
struct KeyboardInner {
@@ -144,7 +149,14 @@ impl Keyboard {
144149
keyboard
145150
}
146151

147-
fn new_dummy() -> Self {
152+
pub(crate) fn new_daemon(daemon: Rc<dyn Daemon>, board: usize) -> Self {
153+
Self::new(KeyboardImplementation::Daemon {
154+
daemon,
155+
board,
156+
})
157+
}
158+
159+
pub(crate) fn new_dummy() -> Self {
148160
let mut properties = HashMap::new();
149161
properties.insert("max_brightness", RefCell::new(100i32.to_variant()));
150162
properties.insert("brightness", RefCell::new(0i32.to_variant()));
@@ -179,6 +191,7 @@ impl Keyboard {
179191
.unwrap();
180192
}
181193
KeyboardImplementation::Dummy { .. } => {}
194+
KeyboardImplementation::Daemon { .. } => {}
182195
}
183196
}
184197

@@ -191,6 +204,15 @@ impl Keyboard {
191204
KeyboardImplementation::Dummy { properties } => {
192205
Ok(Some(properties.get(name).unwrap().borrow().clone()))
193206
}
207+
KeyboardImplementation::Daemon { ref daemon, board } => {
208+
match name {
209+
"max-brightness" => daemon.max_brightness(*board).map(|b| Some(b.to_variant())).map_err(Error::msg),
210+
"brightness" => daemon.brightness(*board).map(|b| Some(b.to_variant())).map_err(Error::msg),
211+
"color" => daemon.color(*board).map(|b| Some(b.to_string().to_variant())).map_err(Error::msg),
212+
"name" => Ok(Some("".to_variant())),
213+
_ => unreachable!(),
214+
}
215+
}
194216
}
195217
}
196218

@@ -205,6 +227,13 @@ impl Keyboard {
205227
KeyboardImplementation::Dummy { properties } => {
206228
*properties.get(name).unwrap().borrow_mut() = value;
207229
}
230+
KeyboardImplementation::Daemon { ref daemon, board } => {
231+
match name {
232+
"brightness" => daemon.set_brightness(*board, value.get().unwrap()).map_err(Error::msg)?,
233+
"color" => daemon.set_color(*board, Rgb::parse(&value.get::<String>().unwrap()).unwrap()).map_err(Error::msg)?,
234+
_ => unreachable!(),
235+
};
236+
}
208237
}
209238
Ok(())
210239
}

0 commit comments

Comments
 (0)