Skip to content

Commit

Permalink
improv: Define Page enum instead of just using strings
Browse files Browse the repository at this point in the history
Slightly more efficient, allows exhaustive matching, etc.
  • Loading branch information
ids1024 authored and jackpot51 committed Oct 19, 2020
1 parent dbbb3d2 commit 044a2aa
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
18 changes: 9 additions & 9 deletions src/application/key.rs
Expand Up @@ -3,6 +3,7 @@ use std::{
collections::HashMap,
};

use super::page::Page;
use super::picker::Picker;
use super::rect::Rect;

Expand All @@ -28,7 +29,7 @@ pub struct Key {
pub(crate) foreground_color: String,
// GTK buttons by page
//TODO: clean up this crap
pub(crate) gtk: HashMap<String, gtk::Button>,
pub(crate) gtk: HashMap<Page, gtk::Button>,
}

impl Key {
Expand Down Expand Up @@ -85,28 +86,27 @@ button {{
}

pub fn refresh(&self, picker: &Picker) {
for (page, button) in self.gtk.iter() {
button.set_label(match page.as_str() {
"Layer 1" => {
for (layer, button) in self.gtk.iter() {
button.set_label(match layer {
Page::Layer1 => {
let scancode_name = &self.scancodes[0].1;
if let Some(picker_key) = picker.keys.get(scancode_name) {
&picker_key.text
} else {
scancode_name
}
},
"Layer 2" => {
Page::Layer2 => {
let scancode_name = &self.scancodes[1].1;
if let Some(picker_key) = picker.keys.get(scancode_name) {
&picker_key.text
} else {
scancode_name
}
},
"Keycaps" => &self.physical_name,
"Logical" => &self.logical_name,
"Electrical" => &self.electrical_name,
_ => "",
Page::Keycaps => &self.physical_name,
Page::Logical => &self.logical_name,
Page::Electrical => &self.electrical_name,
});
}
}
Expand Down
13 changes: 4 additions & 9 deletions src/application/keyboard.rs
Expand Up @@ -17,6 +17,7 @@ use crate::daemon::Daemon;
use crate::keyboard::Keyboard as ColorKeyboard;
use crate::keyboard_color_button::KeyboardColorButton;
use super::key::Key;
use super::page::Page;
use super::picker::Picker;
use super::rect::Rect;

Expand Down Expand Up @@ -448,14 +449,8 @@ button {
let color_button = KeyboardColorButton::new(color_keyboard).widget().clone();
color_button.set_valign(gtk::Align::Center);

for page in &[
"Layer 1",
"Layer 2",
"Keycaps",
"Logical",
"Electrical"
] {
let page_label = gtk::Label::new(Some(page));
for page in Page::iter_all() {
let page_label = gtk::Label::new(Some(page.name()));
let fixed = gtk::Fixed::new();
notebook.append_page(&fixed, Some(&page_label));

Expand Down Expand Up @@ -514,7 +509,7 @@ button {

let mut keys = self.keys.borrow_mut();
let k = &mut keys[i];
k.gtk.insert(page.to_string(), button);
k.gtk.insert(page, button);
k.refresh(&self.picker);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/application/mod.rs
Expand Up @@ -7,6 +7,7 @@ use crate::daemon::{Daemon, DaemonClient, daemon_server};

mod key;
mod keyboard;
mod page;
mod picker;
mod rect;

Expand Down
30 changes: 30 additions & 0 deletions src/application/page.rs
@@ -0,0 +1,30 @@
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Page {
Layer1,
Layer2,
Keycaps,
Logical,
Electrical,
}

impl Page {
pub fn name(&self) -> &'static str {
match self {
Self::Layer1 => "Layer 1",
Self::Layer2 => "Layer 2",
Self::Keycaps => "Keycaps",
Self::Logical => "Logical",
Self::Electrical => "Electrical"
}
}

pub fn iter_all() -> impl Iterator<Item=Self> {
vec![
Self::Layer1,
Self::Layer2,
Self::Keycaps,
Self::Logical,
Self::Electrical,
].into_iter()
}
}

0 comments on commit 044a2aa

Please sign in to comment.