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

View File

@ -1,6 +1,5 @@
/*! State of the emulated keyboard and keys */
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt;
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
pub fn generate_keymap(
keystates: &HashMap::<String, Rc<RefCell<KeyState>>>
keystates: &HashMap::<String, KeyState>
) -> Result<String, FormattingError> {
let mut buf: Vec<u8> = Vec::new();
writeln!(
@ -164,7 +163,6 @@ pub fn generate_keymap(
)?;
for (name, state) in keystates.iter() {
let state = state.borrow();
if let Action::Submit { text: _, keys } = &state.action {
if let 0 = keys.len() { eprintln!("Key {} has no keysyms", name); };
for (named_keysym, keycode) in keys.iter().zip(&state.keycodes) {
@ -191,7 +189,7 @@ pub fn generate_keymap(
)?;
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() {
write!(
buf,

View File

@ -1,3 +1,8 @@
/*! Assorted helpers */
use std::collections::HashMap;
use std::iter::FromIterator;
pub mod c {
use std::cell::RefCell;
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))
)
}