Skip to content

Commit c16c56b

Browse files
committed
chore: Refactor physical_layout.rs
1 parent 11a620a commit c16c56b

File tree

3 files changed

+76
-62
lines changed

3 files changed

+76
-62
lines changed

backend/src/board.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ impl DaemonBoard {
8080
.0
8181
.layout
8282
.physical
83+
.keys
8384
.iter()
8485
.map(|i| Key::new(&self_, i))
8586
.collect();

backend/src/layout/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct Layout {
1212
pub default: KeyMap,
1313
keymap: HashMap<String, u16>,
1414
scancode_names: HashMap<u16, String>,
15-
pub(crate) physical: Vec<PhysicalLayoutKey>,
15+
pub(crate) physical: PhysicalLayout,
1616
pub(crate) layout: HashMap<String, (u8, u8)>,
1717
pub(crate) leds: HashMap<String, Vec<u8>>,
1818
}
@@ -78,9 +78,7 @@ impl Layout {
7878
let (keymap, scancode_names) = parse_keymap_json(keymap_json);
7979
let layout = serde_json::from_str(layout_json).unwrap();
8080
let leds = serde_json::from_str(leds_json).unwrap();
81-
let physical = serde_json::from_str::<PhysicalLayout>(physical_json)
82-
.unwrap()
83-
.keys();
81+
let physical = PhysicalLayout::from_str(physical_json);
8482
Self {
8583
meta,
8684
default,

backend/src/layout/physical_layout.rs

Lines changed: 73 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,104 +3,119 @@ use serde::Deserialize;
33

44
use crate::{Rect, Rgb};
55

6-
pub(crate) struct PhysicalLayoutKey {
7-
pub logical: (u8, u8),
8-
pub physical: Rect,
9-
pub physical_name: String,
10-
pub background_color: Rgb,
6+
pub(crate) struct PhysicalLayout {
7+
pub meta: PhysicalLayoutMeta,
8+
pub keys: Vec<PhysicalLayoutKey>,
119
}
1210

13-
#[derive(Debug, Deserialize)]
14-
pub struct PhysicalLayout(pub Vec<PhysicalLayoutEntry>);
15-
1611
impl PhysicalLayout {
17-
pub(crate) fn keys(&self) -> Vec<PhysicalLayoutKey> {
12+
pub fn from_str(physical_json: &str) -> Self {
13+
let json = serde_json::from_str::<PhysicalLayoutJson>(physical_json).unwrap();
14+
1815
let mut keys = Vec::new();
1916

2017
let mut row_i = 0;
2118
let mut col_i = 0;
2219
let mut physical = Rect::new(0.0, 0.0, 1.0, 1.0);
2320
let mut background_color = Rgb::new(0xcc, 0xcc, 0xcc);
21+
let mut meta = None;
2422

25-
for entry in &self.0 {
26-
if let PhysicalLayoutEntry::Row(row) = entry {
27-
for i in &row.0 {
28-
match i {
29-
PhysicalKeyEnum::Meta(meta) => {
30-
debug!("Key metadata {:?}", meta);
31-
physical.x += meta.x;
32-
physical.y -= meta.y;
33-
physical.w = meta.w.unwrap_or(physical.w);
34-
physical.h = meta.h.unwrap_or(physical.h);
35-
background_color = meta
36-
.c
37-
.as_ref()
38-
.map(|c| {
39-
let err = format!("Failed to parse color {}", c);
40-
Rgb::parse(&c[1..]).expect(&err)
41-
})
42-
.unwrap_or(background_color);
43-
}
44-
PhysicalKeyEnum::Name(name) => {
45-
keys.push(PhysicalLayoutKey {
46-
logical: (row_i as u8, col_i as u8),
47-
physical,
48-
physical_name: name.clone(),
49-
background_color,
50-
});
51-
52-
physical.x += physical.w;
53-
54-
physical.w = 1.0;
55-
physical.h = 1.0;
56-
57-
col_i += 1;
23+
for entry in json.0 {
24+
match entry {
25+
PhysicalLayoutEntry::Meta(data) => {
26+
meta = Some(data);
27+
}
28+
PhysicalLayoutEntry::Row(row) => {
29+
for i in &row.0 {
30+
match i {
31+
PhysicalKeyEnum::Meta(meta) => {
32+
debug!("Key metadata {:?}", meta);
33+
physical.x += meta.x;
34+
physical.y -= meta.y;
35+
physical.w = meta.w.unwrap_or(physical.w);
36+
physical.h = meta.h.unwrap_or(physical.h);
37+
background_color = meta
38+
.c
39+
.as_ref()
40+
.map(|c| {
41+
let err = format!("Failed to parse color {}", c);
42+
Rgb::parse(&c[1..]).expect(&err)
43+
})
44+
.unwrap_or(background_color);
45+
}
46+
PhysicalKeyEnum::Name(name) => {
47+
keys.push(PhysicalLayoutKey {
48+
logical: (row_i as u8, col_i as u8),
49+
physical,
50+
physical_name: name.clone(),
51+
background_color,
52+
});
53+
54+
physical.x += physical.w;
55+
56+
physical.w = 1.0;
57+
physical.h = 1.0;
58+
59+
col_i += 1;
60+
}
5861
}
5962
}
60-
}
6163

62-
physical.x = 0.0;
63-
physical.y -= 1.0;
64+
physical.x = 0.0;
65+
physical.y -= 1.0;
6466

65-
col_i = 0;
66-
row_i += 1;
67+
col_i = 0;
68+
row_i += 1;
69+
}
6770
}
6871
}
6972

70-
keys
73+
let meta = meta.expect("No layout meta");
74+
75+
Self { keys, meta }
7176
}
7277
}
7378

79+
pub(crate) struct PhysicalLayoutKey {
80+
pub logical: (u8, u8),
81+
pub physical: Rect,
82+
pub physical_name: String,
83+
pub background_color: Rgb,
84+
}
85+
86+
#[derive(Debug, Deserialize)]
87+
struct PhysicalLayoutJson(Vec<PhysicalLayoutEntry>);
88+
7489
#[derive(Debug, Deserialize)]
7590
#[serde(untagged)]
76-
pub enum PhysicalLayoutEntry {
91+
enum PhysicalLayoutEntry {
7792
Meta(PhysicalLayoutMeta),
7893
Row(PhysicalRow),
7994
}
8095

8196
#[derive(Debug, Deserialize)]
82-
pub struct PhysicalLayoutMeta {
97+
pub(crate) struct PhysicalLayoutMeta {
8398
pub name: String,
8499
pub author: String,
85100
}
86101

87102
#[derive(Debug, Deserialize)]
88-
pub struct PhysicalRow(pub Vec<PhysicalKeyEnum>);
103+
struct PhysicalRow(Vec<PhysicalKeyEnum>);
89104

90105
#[derive(Debug, Deserialize)]
91106
#[serde(untagged)]
92-
pub enum PhysicalKeyEnum {
107+
enum PhysicalKeyEnum {
93108
Name(String),
94109
Meta(PhysicalKeyMeta),
95110
}
96111

97112
#[derive(Debug, Deserialize)]
98-
pub struct PhysicalKeyMeta {
113+
struct PhysicalKeyMeta {
99114
#[serde(default)]
100-
pub x: f64,
115+
x: f64,
101116
#[serde(default)]
102-
pub y: f64,
103-
pub w: Option<f64>,
104-
pub h: Option<f64>,
105-
pub c: Option<String>,
117+
y: f64,
118+
w: Option<f64>,
119+
h: Option<f64>,
120+
c: Option<String>,
106121
}

0 commit comments

Comments
 (0)