diff --git a/Cargo.lock b/Cargo.lock index 9357e78..45e4ac7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -805,7 +805,7 @@ dependencies = [ [[package]] name = "goxkey" -version = "0.2.2" +version = "0.2.3" dependencies = [ "accessibility", "accessibility-sys", diff --git a/Cargo.toml b/Cargo.toml index 28f9c08..7b96b34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ description = "Bộ gõ tiếng Việt mã nguồn mở đa hệ điều hành Gõ Key" edition = "2021" name = "goxkey" -version = "0.2.2" +version = "0.2.3" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -33,4 +33,4 @@ copyright = "Copyright (c) Huy Tran 2023. All rights reserved." icon = ["icons/icon.icns", "icons/icon.png"] identifier = "com.goxkey.app" name = "GoKey" -version = "0.2.2" +version = "0.2.3" diff --git a/src/config.rs b/src/config.rs index 324d632..7feeb47 100644 --- a/src/config.rs +++ b/src/config.rs @@ -21,6 +21,7 @@ pub struct ConfigStore { en_apps: Vec, is_macro_enabled: bool, macro_table: BTreeMap, + is_auto_toggle_enabled: bool, } fn parse_vec_string(line: String) -> Vec { @@ -61,6 +62,11 @@ impl ConfigStore { writeln!(file, "{} = {}", TYPING_METHOD_CONFIG_KEY, self.method)?; writeln!(file, "{} = {}", VN_APPS_CONFIG_KEY, self.vn_apps.join(","))?; writeln!(file, "{} = {}", EN_APPS_CONFIG_KEY, self.en_apps.join(","))?; + writeln!( + file, + "{} = {}", + AUTOS_TOGGLE_ENABLED_CONFIG_KEY, self.is_auto_toggle_enabled + )?; writeln!( file, "{} = {}", @@ -81,6 +87,7 @@ impl ConfigStore { en_apps: Vec::new(), is_macro_enabled: false, macro_table: BTreeMap::new(), + is_auto_toggle_enabled: false, }; let config_path = ConfigStore::get_config_path(); @@ -94,6 +101,9 @@ impl ConfigStore { TYPING_METHOD_CONFIG_KEY => config.method = right.to_string(), VN_APPS_CONFIG_KEY => config.vn_apps = parse_vec_string(right.to_string()), EN_APPS_CONFIG_KEY => config.en_apps = parse_vec_string(right.to_string()), + AUTOS_TOGGLE_ENABLED_CONFIG_KEY => { + config.is_auto_toggle_enabled = matches!(right.trim(), "true") + } MACRO_ENABLED_CONFIG_KEY => { config.is_macro_enabled = matches!(right.trim(), "true") } @@ -157,6 +167,15 @@ impl ConfigStore { self.save(); } + pub fn is_auto_toggle_enabled(&self) -> bool { + self.is_auto_toggle_enabled + } + + pub fn set_auto_toggle_enabled(&mut self, flag: bool) { + self.is_auto_toggle_enabled = flag; + self.save(); + } + pub fn is_macro_enabled(&self) -> bool { self.is_macro_enabled } @@ -191,4 +210,5 @@ const TYPING_METHOD_CONFIG_KEY: &str = "method"; const VN_APPS_CONFIG_KEY: &str = "vn-apps"; const EN_APPS_CONFIG_KEY: &str = "en-apps"; const MACRO_ENABLED_CONFIG_KEY: &str = "is_macro_enabled"; +const AUTOS_TOGGLE_ENABLED_CONFIG_KEY: &str = "is_auto_toggle_enabled"; const MACROS_CONFIG_KEY: &str = "macros"; diff --git a/src/input.rs b/src/input.rs index 34149af..ad1f08c 100644 --- a/src/input.rs +++ b/src/input.rs @@ -160,7 +160,8 @@ pub struct InputState { is_macro_enabled: bool, macro_table: BTreeMap, temporary_disabled: bool, - previous_modifiers: KeyModifier + previous_modifiers: KeyModifier, + is_auto_toggle_enabled: bool, } impl InputState { @@ -178,7 +179,8 @@ impl InputState { is_macro_enabled: config.is_macro_enabled(), macro_table: config.get_macro_table().clone(), temporary_disabled: false, - previous_modifiers: KeyModifier::empty() + previous_modifiers: KeyModifier::empty(), + is_auto_toggle_enabled: config.is_auto_toggle_enabled(), } } @@ -286,6 +288,18 @@ impl InputState { &self.hotkey } + pub fn is_auto_toggle_enabled(&self) -> bool { + self.is_auto_toggle_enabled + } + + pub fn toggle_auto_toggle(&mut self) { + self.is_auto_toggle_enabled = !self.is_auto_toggle_enabled; + CONFIG_MANAGER + .lock() + .unwrap() + .set_auto_toggle_enabled(self.is_auto_toggle_enabled); + } + pub fn is_macro_enabled(&self) -> bool { self.is_macro_enabled } diff --git a/src/main.rs b/src/main.rs index 240a923..75a9e23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,8 +16,8 @@ use platform::{ KEY_TAB, RAW_KEY_GLOBE, }; -use ui::{UIDataAdapter, UPDATE_UI}; use crate::platform::{RAW_ARROW_DOWN, RAW_ARROW_LEFT, RAW_ARROW_RIGHT, RAW_ARROW_UP}; +use ui::{UIDataAdapter, UPDATE_UI}; static UI_EVENT_SINK: OnceCell = OnceCell::new(); const APP_VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -80,6 +80,9 @@ unsafe fn toggle_vietnamese() { } unsafe fn auto_toggle_vietnamese() { + if !INPUT_STATE.is_auto_toggle_enabled() { + return; + } let has_change = INPUT_STATE.update_active_app().is_some(); if !has_change { return; @@ -181,7 +184,7 @@ fn event_handler(handle: Handle, pressed_key: Option, modifiers: Key match keycode { KEY_ENTER | KEY_TAB | KEY_SPACE | KEY_ESCAPE => { INPUT_STATE.new_word(); - }, + } _ => {} } } diff --git a/src/ui.rs b/src/ui.rs index ecf3d50..db8877f 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -24,8 +24,8 @@ pub const UPDATE_UI: Selector = Selector::new("gox-ui.update-ui"); pub const SHOW_UI: Selector = Selector::new("gox-ui.show-ui"); const DELETE_MACRO: Selector = Selector::new("gox-ui.delete-macro"); const ADD_MACRO: Selector = Selector::new("gox-ui.add-macro"); -pub const WINDOW_WIDTH: f64 = 320.0; -pub const WINDOW_HEIGHT: f64 = 345.0; +pub const WINDOW_WIDTH: f64 = 335.0; +pub const WINDOW_HEIGHT: f64 = 375.0; pub fn format_letter_key(c: Option) -> String { if let Some(c) = c { @@ -88,6 +88,7 @@ pub struct UIDataAdapter { typing_method: TypingMethod, hotkey_display: String, launch_on_login: bool, + is_auto_toggle_enabled: bool, // Macro config is_macro_enabled: bool, macro_table: Arc>, @@ -111,6 +112,7 @@ impl UIDataAdapter { typing_method: TypingMethod::Telex, hotkey_display: String::new(), launch_on_login: false, + is_auto_toggle_enabled: false, is_macro_enabled: false, macro_table: Arc::new(Vec::new()), new_macro_from: String::new(), @@ -134,6 +136,7 @@ impl UIDataAdapter { self.typing_method = INPUT_STATE.get_method(); self.hotkey_display = INPUT_STATE.get_hotkey().to_string(); self.is_macro_enabled = INPUT_STATE.is_macro_enabled(); + self.is_auto_toggle_enabled = INPUT_STATE.is_auto_toggle_enabled(); self.launch_on_login = is_launch_on_login(); self.macro_table = Arc::new( INPUT_STATE @@ -325,6 +328,10 @@ impl> Controller for UIController { if old_data.is_macro_enabled != data.is_macro_enabled { INPUT_STATE.toggle_macro_enabled(); } + + if old_data.is_auto_toggle_enabled != data.is_auto_toggle_enabled { + INPUT_STATE.toggle_auto_toggle(); + } } child.update(ctx, old_data, data, env); } @@ -379,6 +386,18 @@ pub fn main_ui_builder() -> impl Widget { .expand_width() .padding(8.0), ) + .with_child( + Flex::row() + .with_child(Label::new("Bật tắt theo ứng dụng")) + .with_child( + Checkbox::new("").lens(UIDataAdapter::is_auto_toggle_enabled), + ) + .cross_axis_alignment(druid::widget::CrossAxisAlignment::Start) + .main_axis_alignment(druid::widget::MainAxisAlignment::SpaceBetween) + .must_fill_main_axis(true) + .expand_width() + .padding(8.0), + ) .with_child( Flex::row() .with_child(Label::new("Gõ tắt"))