Working on keyboard stage, closures!
This commit is contained in:
+163
-89
@@ -15,131 +15,205 @@
|
|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
/*
|
/*
|
||||||
This is TimeZone stage, used to select timezone
|
This is Keyboar stage, used to select Keyboard Layouts
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use crate::kira_scroll_list;
|
||||||
use crate::stage::{ConfigValue, StageAction, StageResult};
|
use crate::stage::{ConfigValue, StageAction, StageResult};
|
||||||
use iced::{Alignment, Length, widget};
|
use iced::{Alignment, Length, widget};
|
||||||
use rust_i18n::t;
|
use rust_i18n::t;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
mod scroll_list;
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct Layout {
|
||||||
|
layout: String,
|
||||||
|
description: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Layout {
|
||||||
|
pub fn from_str_touple(v: &(&str, &str)) -> Self {
|
||||||
|
Self {
|
||||||
|
layout: v.0.trim().to_string(),
|
||||||
|
description: v.1.trim().to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct TimeZoneData {
|
pub struct Variant {
|
||||||
region: String,
|
variant: String,
|
||||||
zone: String,
|
description: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TimeZoneData {
|
impl Variant {
|
||||||
fn from_str(s: &str) -> Option<Self> {
|
pub fn from_str_touple(v: &(&str, &str)) -> Self {
|
||||||
let idx = s.find('/')?;
|
Self {
|
||||||
let (region, zone) = s.split_at(idx);
|
variant: v.0.trim().to_string(),
|
||||||
Some(Self {
|
description: v.1.trim().to_string(),
|
||||||
region: region.into(),
|
}
|
||||||
zone: zone.trim_start_matches('/').into(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for TimeZoneData {
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct KeyboarLayout {
|
||||||
|
layout: Layout,
|
||||||
|
variant: Variant,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct KeyboarModel {
|
||||||
|
model: String,
|
||||||
|
description: String,
|
||||||
|
}
|
||||||
|
impl KeyboarModel {
|
||||||
|
pub fn from_str_touple(v: &(&str, &str)) -> Self {
|
||||||
|
Self {
|
||||||
|
model: v.0.trim().to_string(),
|
||||||
|
description: v.1.trim().to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl std::fmt::Display for KeyboarLayout {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "{}/{}", self.region, self.zone)
|
write!(f, "{}/{}", &self.layout.layout, &self.variant.variant)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct TimeZoneStage {
|
pub struct KeyboardStage {
|
||||||
time_zones: Result<HashMap<String, Vec<String>>, String>,
|
models: Vec<KeyboarModel>,
|
||||||
regions: Vec<String>,
|
layouts: Vec<Layout>,
|
||||||
region_id: Option<usize>,
|
variants_map: HashMap<String, Vec<Variant>>,
|
||||||
zones: Vec<String>,
|
variants: Vec<Variant>,
|
||||||
zone_id: Option<usize>,
|
model: Option<KeyboarModel>,
|
||||||
selected_zone: Option<TimeZoneData>,
|
layout: Option<Layout>,
|
||||||
selected_zone_text: String,
|
variant: Option<Variant>,
|
||||||
|
keyboard_layouts: Vec<KeyboarLayout>,
|
||||||
|
selected_kbd_layout_id: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Message {
|
pub enum Message {
|
||||||
SelectRegion(scroll_list::ListViewMessage),
|
SelectKeyboardModel(kira_scroll_list::ListViewMessage),
|
||||||
SelectZone(scroll_list::ListViewMessage),
|
SelectLayout(kira_scroll_list::ListViewMessage),
|
||||||
|
SelectVariant(kira_scroll_list::ListViewMessage),
|
||||||
|
|
||||||
|
AddLayout(usize),
|
||||||
|
RemoveLayout(usize),
|
||||||
|
SelectAddedLayout(kira_scroll_list::ListViewMessage),
|
||||||
|
|
||||||
Next,
|
Next,
|
||||||
Back,
|
Back,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Getting list of awailable timezones from system.
|
const XKB_RULES_FILE_NAME: &str = "/usr/share/X11/xkb/rules/evdev.lst";
|
||||||
fn get_timezones_blocking() -> Result<Vec<TimeZoneData>, String> {
|
|
||||||
use std::process::Command;
|
/// Getting list of awailable keyboard layouts from from xkb/rules/evdev.lst file.
|
||||||
//timedatectl list-timezones --no-pager
|
fn get_layouts_blocking() -> std::io::Result<(Vec<KeyboarModel>, Vec<Layout>, HashMap<String, Vec<Variant>>)> {
|
||||||
match Command::new("timedatectl")
|
use std::fs::File;
|
||||||
.arg("list-timezones")
|
use std::io::{BufReader, Read};
|
||||||
.arg("--no-pager")
|
|
||||||
.output()
|
let mut file_content = String::new();
|
||||||
{
|
{
|
||||||
Ok(cmd_output) => {
|
let locgen_file = File::open(XKB_RULES_FILE_NAME)?;
|
||||||
if cmd_output.status.success() {
|
let mut reader = BufReader::new(locgen_file);
|
||||||
//Etc/UTC
|
reader.read_to_string(&mut file_content)?;
|
||||||
match String::from_utf8(cmd_output.stdout) {
|
|
||||||
Ok(str_tz_list) => Ok(str_tz_list
|
|
||||||
.split("\n")
|
|
||||||
.filter(|sb| sb.contains('/'))
|
|
||||||
.filter_map(|str_t_zone| TimeZoneData::from_str(str_t_zone.trim()))
|
|
||||||
.collect::<Vec<TimeZoneData>>()),
|
|
||||||
Err(ex) => Err(format!("Exception while converting to UTF8 {}", ex)),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Err(format!(
|
|
||||||
"Error getting timezones list: {}",
|
|
||||||
String::from_utf8(cmd_output.stderr).unwrap_or("UNKNOWN".into())
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(ex) => Err(format!("Exception while trying to list time zones {}", ex)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TimeZoneStage {
|
let raw_sections: Vec<String> = file_content.split("! ").map(str::to_string).collect();
|
||||||
pub fn new() -> Self {
|
|
||||||
match get_timezones_blocking() {
|
|
||||||
Ok(time_zones_data_list) => {
|
|
||||||
let mut time_zones_map: HashMap<String, Vec<String>> = HashMap::new();
|
|
||||||
for tz_data in &time_zones_data_list {
|
|
||||||
if let Some(zones) = time_zones_map.get_mut(&tz_data.region) {
|
|
||||||
zones.push(tz_data.zone.clone());
|
|
||||||
} else {
|
|
||||||
time_zones_map.insert(tz_data.region.clone(), vec![tz_data.zone.clone()]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// sort zones alphabetically
|
|
||||||
time_zones_map.values_mut().for_each(|v| v.sort());
|
|
||||||
// sort region names alphabetically
|
|
||||||
let mut regions: Vec<String> = time_zones_map.keys().cloned().collect();
|
|
||||||
regions.sort();
|
|
||||||
|
|
||||||
Self {
|
|
||||||
regions: regions,
|
// Parsing models data
|
||||||
zones: Vec::new(),
|
let models: Vec<KeyboarModel> = raw_sections
|
||||||
time_zones: Ok(time_zones_map),
|
.iter()
|
||||||
region_id: None,
|
.find(|s| s.starts_with("model"))
|
||||||
zone_id: None,
|
.and_then(|s| {
|
||||||
selected_zone: None,
|
Some(
|
||||||
selected_zone_text: String::new(),
|
s.lines()
|
||||||
|
.skip(1)
|
||||||
|
.filter_map(|s| s.trim().split_once(char::is_whitespace))
|
||||||
|
.map(|s_spl| KeyboarModel::from_str_touple(&s_spl))
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap_or_else(Vec::new);
|
||||||
|
|
||||||
|
// Parsing layots data
|
||||||
|
let layouts: Vec<Layout> = raw_sections
|
||||||
|
.iter()
|
||||||
|
.find(|s| s.starts_with("layout"))
|
||||||
|
.and_then(|s| {
|
||||||
|
Some(
|
||||||
|
s.lines()
|
||||||
|
.skip(1)
|
||||||
|
.filter_map(|s| s.trim().split_once(char::is_whitespace))
|
||||||
|
.map(|s_spl| Layout::from_str_touple(&s_spl))
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap_or_else(Vec::new);
|
||||||
|
|
||||||
|
// parsing variants data to HashMap
|
||||||
|
let mut variants: HashMap<String, Vec<Variant>> = HashMap::new();
|
||||||
|
|
||||||
|
raw_sections
|
||||||
|
.iter()
|
||||||
|
.find(|s| s.starts_with("variant"))
|
||||||
|
.and_then(|s| {
|
||||||
|
Some(
|
||||||
|
s.lines()
|
||||||
|
.skip(1)
|
||||||
|
.filter_map(|s| s.trim().split_once(char::is_whitespace))
|
||||||
|
.filter_map(|(vrnt, l_plus_descr)| {
|
||||||
|
l_plus_descr
|
||||||
|
.trim()
|
||||||
|
.split_once(": ")
|
||||||
|
.and_then(|(loc, descr)| {
|
||||||
|
Some((loc.trim(), Variant::from_str_touple(&(vrnt, descr))))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.for_each(|v| {
|
||||||
|
let maybe_key = variants
|
||||||
|
.get_mut(v.0)
|
||||||
|
.and_then(|vrn_vec| Some(vrn_vec.push(v.1)));
|
||||||
|
if maybe_key.is_none() {
|
||||||
|
variants.insert(v.0.to_string(), vec![v.1]);
|
||||||
}
|
}
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok((models, layouts, variants))
|
||||||
|
}
|
||||||
|
|
||||||
|
impl KeyboardStage {
|
||||||
|
pub fn new() -> Result<Self, String> {
|
||||||
|
match get_layouts_blocking() {
|
||||||
|
Ok((models, layouts, variants)) => {
|
||||||
|
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
|
||||||
|
layouts: layouts,
|
||||||
|
variants_map: variants,
|
||||||
|
variants: Vec::new(),
|
||||||
|
model: Some(models[0].clone()),
|
||||||
|
models: models,
|
||||||
|
layout: None,
|
||||||
|
variant: None,
|
||||||
|
keyboard_layouts: Vec::new(),
|
||||||
|
selected_kbd_layout_id: None,
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
Err(ex) => {
|
Err(ex) => {
|
||||||
println!(
|
let err = format!(
|
||||||
"Exception while trying to get time zones list from system {}",
|
"Exception while trying to get kayboard layouts data from system {}",
|
||||||
ex
|
ex
|
||||||
);
|
);
|
||||||
Self {
|
Err(err)
|
||||||
time_zones: Err(ex),
|
|
||||||
regions: Vec::new(),
|
|
||||||
zones: Vec::new(),
|
|
||||||
region_id: None,
|
|
||||||
zone_id: None,
|
|
||||||
selected_zone: None,
|
|
||||||
selected_zone_text: String::new(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,3 +20,4 @@ pub mod license;
|
|||||||
pub mod network;
|
pub mod network;
|
||||||
pub mod timezone;
|
pub mod timezone;
|
||||||
pub mod locale;
|
pub mod locale;
|
||||||
|
pub mod keyboard;
|
||||||
Reference in New Issue
Block a user