keymap: Simplified key state passing

There's no need to treat states as a shared resource before they are placed inside buttons.
This commit is contained in:
Dorota Czaplejewicz
2019-10-09 15:38:21 +00:00
parent 9cd439767e
commit dc2bc46167
3 changed files with 31 additions and 9 deletions

View File

@ -19,6 +19,7 @@ use ::keyboard::{
}; };
use ::resources; use ::resources;
use ::util::c::as_str; use ::util::c::as_str;
use ::util::hash_map_map;
use ::xdg; use ::xdg;
// traits, derives // traits, derives
@ -346,23 +347,31 @@ impl Layout {
}; };
( (
name.into(), name.into(),
Rc::new(RefCell::new(KeyState { KeyState {
pressed: false, pressed: false,
locked: false, locked: false,
keycodes, keycodes,
action, action,
})) }
) )
}); });
let button_states = let button_states
HashMap::<String, Rc<RefCell<KeyState>>>::from_iter( = HashMap::<String, KeyState>::from_iter(
button_states button_states
); );
// TODO: generate from symbols // TODO: generate from symbols
let keymap_str = generate_keymap(&button_states)?; let keymap_str = generate_keymap(&button_states)?;
let button_states_cache = hash_map_map(
button_states,
|name, state| {(
name,
Rc::new(RefCell::new(state))
)}
);
let views = HashMap::from_iter( let views = HashMap::from_iter(
self.views.iter().map(|(name, view)| {( self.views.iter().map(|(name, view)| {(
name.clone(), name.clone(),
@ -386,7 +395,7 @@ impl Layout {
&self.buttons, &self.buttons,
&self.outlines, &self.outlines,
name, name,
button_states.get(name.into()) button_states_cache.get(name.into())
.expect("Button state not created") .expect("Button state not created")
.clone() .clone()
)) ))

View File

@ -1,6 +1,5 @@
/*! State of the emulated keyboard and keys */ /*! State of the emulated keyboard and keys */
use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
use std::io; use std::io;
@ -151,7 +150,7 @@ impl From<io::Error> for FormattingError {
/// Generates a de-facto single level keymap. TODO: actually drop second level /// Generates a de-facto single level keymap. TODO: actually drop second level
pub fn generate_keymap( pub fn generate_keymap(
keystates: &HashMap::<String, Rc<RefCell<KeyState>>> keystates: &HashMap::<String, KeyState>
) -> Result<String, FormattingError> { ) -> Result<String, FormattingError> {
let mut buf: Vec<u8> = Vec::new(); let mut buf: Vec<u8> = Vec::new();
writeln!( writeln!(
@ -164,7 +163,6 @@ pub fn generate_keymap(
)?; )?;
for (name, state) in keystates.iter() { for (name, state) in keystates.iter() {
let state = state.borrow();
if let Action::Submit { text: _, keys } = &state.action { if let Action::Submit { text: _, keys } = &state.action {
if let 0 = keys.len() { eprintln!("Key {} has no keysyms", name); }; if let 0 = keys.len() { eprintln!("Key {} has no keysyms", name); };
for (named_keysym, keycode) in keys.iter().zip(&state.keycodes) { for (named_keysym, keycode) in keys.iter().zip(&state.keycodes) {
@ -191,7 +189,7 @@ pub fn generate_keymap(
)?; )?;
for (name, state) in keystates.iter() { for (name, state) in keystates.iter() {
if let Action::Submit { text: _, keys } = &state.borrow().action { if let Action::Submit { text: _, keys } = &state.action {
for keysym in keys.iter() { for keysym in keys.iter() {
write!( write!(
buf, buf,

View File

@ -1,3 +1,8 @@
/*! Assorted helpers */
use std::collections::HashMap;
use std::iter::FromIterator;
pub mod c { pub mod c {
use std::cell::RefCell; use std::cell::RefCell;
use std::ffi::{ CStr, CString }; use std::ffi::{ CStr, CString };
@ -101,3 +106,13 @@ pub mod c {
} }
} }
} }
pub fn hash_map_map<K, V, F, K1, V1>(map: HashMap<K, V>, mut f: F)
-> HashMap<K1, V1>
where F: FnMut(K, V) -> (K1, V1),
K1: std::cmp::Eq + std::hash::Hash
{
HashMap::from_iter(
map.into_iter().map(|(key, value)| f(key, value))
)
}