Skip to content

Commit 87eaab5

Browse files
ids1024jackpot51
authored andcommitted
improv: Parse color string when layout is parsed
1 parent a915dfe commit 87eaab5

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

src/application/key.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::cell::RefCell;
22

3+
use crate::color::Rgb;
34
use super::page::Page;
45
use super::picker::SCANCODE_LABELS;
56
use super::rect::Rect;
@@ -21,9 +22,9 @@ pub struct Key {
2122
// Currently loaded scancodes and their names
2223
pub(crate) scancodes: RefCell<Vec<(u16, String)>>,
2324
// Background color
24-
pub(crate) background_color: String,
25+
pub(crate) background_color: Rgb,
2526
// Foreground color
26-
pub(crate) foreground_color: String,
27+
pub(crate) foreground_color: Rgb,
2728
}
2829

2930
impl Key {

src/application/keyboard.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::{
2121
str,
2222
};
2323

24+
use crate::color::Rgb;
2425
use crate::daemon::Daemon;
2526
use crate::keyboard::Keyboard as ColorKeyboard;
2627
use crate::keyboard_color_button::KeyboardColorButton;
@@ -478,15 +479,15 @@ impl Keyboard {
478479
};
479480

480481
drawing_area.connect_draw(clone!(@weak kb => @default-panic, move |drawing_area, cr| {
482+
let selected = Rgb::new(0xfb, 0xb8, 0x6c).to_floats();
481483
for (i, k) in kb.keys().iter().enumerate() {
482484
let x = (k.physical.x * SCALE) + MARGIN;
483485
let y = -(k.physical.y * SCALE) + MARGIN;
484486
let w = (k.physical.w * SCALE) - MARGIN * 2.;
485487
let h = (k.physical.h * SCALE) - MARGIN * 2.;
486488

487-
let bg = crate::color::Rgb::parse(&k.background_color[1..]).unwrap().to_floats();
488-
let fg = crate::color::Rgb::parse(&k.foreground_color[1..]).unwrap().to_floats();
489-
let selected = crate::color::Rgb::parse("fbb86c").unwrap().to_floats();
489+
let bg = k.background_color.to_floats();
490+
let fg = k.foreground_color.to_floats();
490491

491492
cr.rectangle(x, y, w, h);
492493
cr.set_source_rgb(bg.0, bg.1, bg.2);

src/application/layout/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::collections::HashMap;
55
mod physical_layout;
66
pub(super) use physical_layout::PhysicalLayout;
77

8+
use crate::color::Rgb;
89
use crate::keymap::KeyMap;
910
use super::key::Key;
1011
use super::rect::Rect;
@@ -90,8 +91,8 @@ impl<'a> Layout<'a> {
9091
let mut y = 0.0;
9192
let mut w = 1.0;
9293
let mut h = 1.0;
93-
let mut background_color = "#cccccc".to_string();
94-
let mut foreground_color = "#000000".to_string();
94+
let mut background_color = Rgb::new(0xcc, 0xcc, 0xcc);
95+
let mut foreground_color = Rgb::new(0x00, 0x00, 0x00);
9596

9697
for entry in &self.physical.0 {
9798
if let PhysicalLayoutEntry::Row(row) = entry {
@@ -103,12 +104,16 @@ impl<'a> Layout<'a> {
103104
y -= meta.y;
104105
w = meta.w.unwrap_or(w);
105106
h = meta.h.unwrap_or(h);
106-
background_color = meta.c.clone().unwrap_or(background_color);
107+
background_color = meta.c.as_ref().map(|c| {
108+
let err = format!("Failed to parse color {}", c);
109+
Rgb::parse(&c[1..]).expect(&err)
110+
}).unwrap_or(background_color);
107111
if let Some(t) = &meta.t {
108112
//TODO: support using different color per line?
109113
//Is this even possible in GTK?
110114
if let Some(t_l) = t.lines().next() {
111-
foreground_color = t_l.to_string();
115+
let err = format!("Failed to parse color {}", t_l);
116+
foreground_color = Rgb::parse(&t_l[1..]).expect(&err);
112117
}
113118
}
114119
}
@@ -140,8 +145,8 @@ impl<'a> Layout<'a> {
140145
electrical: electrical.clone(),
141146
electrical_name: format!("{}, {}", electrical.0, electrical.1),
142147
scancodes: RefCell::new(Vec::new()),
143-
background_color: background_color.clone(),
144-
foreground_color: foreground_color.clone(),
148+
background_color,
149+
foreground_color,
145150
});
146151

147152
x += w;

0 commit comments

Comments
 (0)