Add automatic keyboard and language settings

This commit is contained in:
Ward from fusion-voyager-3 2024-01-17 18:06:24 +03:00
parent c38bc421a9
commit d0259e51b7
3 changed files with 40 additions and 1 deletions

View File

@ -1,4 +1,4 @@
// Use libraries // Use libraries
/// Use all gtk4 libraries (gtk4 -> gtk because cargo) /// Use all gtk4 libraries (gtk4 -> gtk because cargo)
/// Use all libadwaita libraries (libadwaita -> adw because cargo) /// Use all libadwaita libraries (libadwaita -> adw because cargo)
use gtk::prelude::*; use gtk::prelude::*;

View File

@ -14,6 +14,7 @@ use std::io::BufReader;
use std::process::Command; use std::process::Command;
use std::process::Stdio; use std::process::Stdio;
use std::time::Instant; use std::time::Instant;
use std::str;
pub fn keyboard_page(content_stack: &gtk::Stack) { pub fn keyboard_page(content_stack: &gtk::Stack) {
@ -159,6 +160,29 @@ pub fn keyboard_page(content_stack: &gtk::Stack) {
} }
}); });
let mut current_keyboard_cli = Command::new("localectl")
.arg("status")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap_or_else(|e| panic!("failed {}", e));
let mut current_keyboard_grep = Command::new("grep")
.arg("X11 Layout")
.stdin(Stdio::from(current_keyboard_cli.stdout.unwrap())) // Pipe through.
.stdout(Stdio::piped())
.spawn()
.unwrap();
let mut current_keyboard_cut = Command::new("cut")
.arg("-d:")
.arg("-f2")
.stdin(Stdio::from(current_keyboard_grep.stdout.unwrap()))
.stdout(Stdio::piped())
.spawn()
.unwrap();
let current_keyboard_output = current_keyboard_cut.wait_with_output().unwrap();
let current_keyboard = str::from_utf8(&current_keyboard_output.stdout).unwrap().trim();
let mut keyboard_layout_cli = Command::new("localectl") let mut keyboard_layout_cli = Command::new("localectl")
.arg("list-x11-keymap-layouts") .arg("list-x11-keymap-layouts")
.stdin(Stdio::piped()) .stdin(Stdio::piped())
@ -171,6 +195,7 @@ pub fn keyboard_page(content_stack: &gtk::Stack) {
for keyboard_layout in keyboard_layout_reader.lines() { for keyboard_layout in keyboard_layout_reader.lines() {
let keyboard_layout = keyboard_layout.unwrap(); let keyboard_layout = keyboard_layout.unwrap();
let keyboard_layout_clone = keyboard_layout.clone();
let keyboard_layout_checkbutton = gtk::CheckButton::builder() let keyboard_layout_checkbutton = gtk::CheckButton::builder()
.label(keyboard_layout.clone()) .label(keyboard_layout.clone())
.build(); .build();
@ -185,6 +210,9 @@ pub fn keyboard_page(content_stack: &gtk::Stack) {
bottom_next_button_clone2.set_sensitive(true); bottom_next_button_clone2.set_sensitive(true);
} }
}); });
if current_keyboard.contains(&(keyboard_layout_clone)) {
keyboard_layout_checkbutton.set_active(true);
}
} }
// / keyboard_selection_box appends // / keyboard_selection_box appends

View File

@ -14,6 +14,8 @@ use std::io::BufReader;
use std::process::Command; use std::process::Command;
use std::process::Stdio; use std::process::Stdio;
use std::time::Instant; use std::time::Instant;
use std::env;
pub fn language_page(content_stack: &gtk::Stack) { pub fn language_page(content_stack: &gtk::Stack) {
@ -159,6 +161,11 @@ pub fn language_page(content_stack: &gtk::Stack) {
} }
}); });
let current_locale = match env::var_os("LANG") {
Some(v) => v.into_string().unwrap(),
None => panic!("$LANG is not set")
};
let mut locale_cli = Command::new("locale") let mut locale_cli = Command::new("locale")
.arg("-a") .arg("-a")
.stdin(Stdio::piped()) .stdin(Stdio::piped())
@ -183,6 +190,7 @@ pub fn language_page(content_stack: &gtk::Stack) {
for locale in locale_reader.lines() { for locale in locale_reader.lines() {
let locale = locale.unwrap(); let locale = locale.unwrap();
let locale_clone = locale.clone();
let locale_checkbutton = gtk::CheckButton::builder() let locale_checkbutton = gtk::CheckButton::builder()
.label(locale.clone()) .label(locale.clone())
.build(); .build();
@ -197,6 +205,9 @@ pub fn language_page(content_stack: &gtk::Stack) {
bottom_next_button_clone2.set_sensitive(true); bottom_next_button_clone2.set_sensitive(true);
} }
}); });
if current_locale.contains(&(locale_clone)) {
locale_checkbutton.set_active(true);
}
} }
// / language_selection_box appends // / language_selection_box appends