Skip to content

Commit c6391d4

Browse files
committed
feat: add xkeyboardconfig settings
1 parent 45705b6 commit c6391d4

File tree

7 files changed

+131
-5
lines changed

7 files changed

+131
-5
lines changed

src-tauri/Cargo.lock

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ zbus = { version = "5.1.0", features = ["tokio"] }
2222
tokio = { version = "1.42.0", features = ["rt-multi-thread", "process"] }
2323
eyre = "0.6.12"
2424
nom = "8"
25-
url = "2.5.2"
2625
reqwest = { version = "0.12.8", features = ["json"] }
2726
thiserror = "2"
2827
libc = "0.2.159"
@@ -37,6 +36,7 @@ sysinfo = "0.35"
3736
tauri-plugin-shell = "2.0.0"
3837
tauri-plugin-dialog = "2.0.0"
3938
tauri-plugin-cli = "2.0.0"
39+
serde-xml-rs = "0.8.0"
4040

4141
[features]
4242
# this feature is used for production builds or when `devPath` points to the filesystem

src-tauri/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ use axum::Router;
66
use eyre::ContextCompat;
77
use eyre::OptionExt;
88
use eyre::Result;
9-
use parser::list_zoneinfo;
10-
use parser::ZoneInfo;
9+
use parser::timezone::list_zoneinfo;
10+
use parser::timezone::ZoneInfo;
11+
use parser::xkeyboard::get_keyboard_layouts;
12+
use parser::xkeyboard::KeyboardLayouts;
1113
use rand::prelude::SliceRandom;
1214
use rand::rng;
1315
use reqwest::Client;
@@ -302,6 +304,11 @@ fn list_timezone() -> TauriResult<Vec<ZoneInfo>> {
302304
Ok(list_zoneinfo()?)
303305
}
304306

307+
#[tauri::command]
308+
fn list_xkeyboard_config() -> TauriResult<KeyboardLayouts> {
309+
Ok(get_keyboard_layouts()?)
310+
}
311+
305312
#[tauri::command]
306313
async fn set_config(state: State<'_, DkState<'_>>, config: &str) -> TauriResult<()> {
307314
let proxy = &state.proxy;
@@ -928,6 +935,7 @@ pub async fn run() {
928935
is_lang_already_set,
929936
is_offline_install,
930937
is_block_username,
938+
list_xkeyboard_config,
931939
])
932940
.run(tauri::generate_context!())
933941
.expect("error while running tauri application");

src-tauri/src/parser/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod timezone;
2+
pub mod xkeyboard;
File renamed without changes.

src-tauri/src/parser/xkeyboard.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use eyre::Result;
2+
use serde::{Deserialize, Serialize};
3+
use serde_xml_rs as xml;
4+
use std::{fs::File, io::BufReader};
5+
6+
/// A list of keyboard layouts parsed from `/usr/share/X11/xkb/rules/base.xml`.
7+
#[derive(Debug, Deserialize, Serialize)]
8+
pub struct KeyboardLayouts {
9+
#[serde(rename = "layoutList")]
10+
pub layout_list: LayoutList,
11+
}
12+
13+
impl KeyboardLayouts {
14+
/// Fetch the layouts from the layout list.
15+
pub fn get_layouts(&self) -> &[KeyboardLayout] {
16+
&self.layout_list.layout
17+
}
18+
19+
/// Fetch the layouts from the layout list.
20+
pub fn get_layouts_mut(&mut self) -> &mut [KeyboardLayout] {
21+
&mut self.layout_list.layout
22+
}
23+
}
24+
25+
/// A list of keyboard layouts.
26+
#[derive(Debug, Deserialize, Serialize)]
27+
pub struct LayoutList {
28+
pub layout: Vec<KeyboardLayout>,
29+
}
30+
31+
/// A keyboard layout, which contains an optional list of variants, a name, and a description.
32+
#[derive(Debug, Deserialize, Serialize)]
33+
pub struct KeyboardLayout {
34+
#[serde(rename = "configItem")]
35+
pub config_item: ConfigItem,
36+
#[serde(rename = "variantList")]
37+
pub variant_list: Option<VariantList>,
38+
}
39+
40+
impl KeyboardLayout {
41+
/// Fetches the name of the keyboard layout.
42+
pub fn get_name(&self) -> &str {
43+
&self.config_item.name
44+
}
45+
46+
/// Fetches a description of the layout.
47+
pub fn get_description(&self) -> &str {
48+
&self.config_item.description
49+
}
50+
51+
/// Fetches a list of possible layout variants.
52+
pub fn get_variants(&self) -> Option<&Vec<KeyboardVariant>> {
53+
self.variant_list.as_ref().and_then(|x| x.variant.as_ref())
54+
}
55+
}
56+
57+
/// Contains the name and description of a keyboard layout.
58+
#[derive(Debug, Deserialize, Serialize)]
59+
pub struct ConfigItem {
60+
pub name: String,
61+
#[serde(rename = "shortDescription")]
62+
pub short_description: Option<String>,
63+
pub description: String,
64+
}
65+
66+
/// A list of possible variants of a keyboard layout.
67+
#[derive(Debug, Deserialize, Serialize)]
68+
pub struct VariantList {
69+
pub variant: Option<Vec<KeyboardVariant>>,
70+
}
71+
72+
/// A variant of a keyboard layout.
73+
#[derive(Debug, Deserialize, Serialize)]
74+
pub struct KeyboardVariant {
75+
#[serde(rename = "configItem")]
76+
pub config_item: ConfigItem,
77+
}
78+
79+
impl KeyboardVariant {
80+
/// The name of this variant of a keybaord layout.
81+
pub fn get_name(&self) -> &str {
82+
&self.config_item.name
83+
}
84+
85+
/// A description of this variant of a keyboard layout.
86+
pub fn get_description(&self) -> &str {
87+
&self.config_item.description
88+
}
89+
}
90+
91+
const X11_BASE_RULES: &str = "/usr/share/X11/xkb/rules/base.xml";
92+
93+
/// Fetches a list of keyboard layouts from `/usr/share/X11/xkb/rules/base.xml`.
94+
pub fn get_keyboard_layouts() -> Result<KeyboardLayouts> {
95+
Ok(xml::from_reader(BufReader::new(File::open(
96+
X11_BASE_RULES,
97+
)?))?)
98+
}

src-tauri/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use std::path::Path;
33

44
use eyre::{OptionExt, Result};
55
use reqwest::Client;
6+
use reqwest::Url;
67
use serde::de::DeserializeOwned;
78
use serde::{Deserialize, Serialize};
89
use serde_json::Value;
910
use std::time::Instant;
10-
use url::Url;
1111
use x11rb::connection::Connection;
1212
use x11rb::protocol::xproto::{
1313
AtomEnum, ClientMessageEvent, ConnectionExt as ConnectionExtB, EventMask,

0 commit comments

Comments
 (0)