Skip to content

Commit

Permalink
feat: Add an option to enable the Auto Vietnamese Toggle mode
Browse files Browse the repository at this point in the history
  • Loading branch information
huytd committed Mar 22, 2024
1 parent 5ac0249 commit 4521540
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
20 changes: 20 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct ConfigStore {
en_apps: Vec<String>,
is_macro_enabled: bool,
macro_table: BTreeMap<String, String>,
is_auto_toggle_enabled: bool,
}

fn parse_vec_string(line: String) -> Vec<String> {
Expand Down Expand Up @@ -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,
"{} = {}",
Expand All @@ -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();
Expand All @@ -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")
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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";
18 changes: 16 additions & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ pub struct InputState {
is_macro_enabled: bool,
macro_table: BTreeMap<String, String>,
temporary_disabled: bool,
previous_modifiers: KeyModifier
previous_modifiers: KeyModifier,
is_auto_toggle_enabled: bool,
}

impl InputState {
Expand All @@ -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(),
}
}

Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExtEventSink> = OnceCell::new();
const APP_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -181,7 +184,7 @@ fn event_handler(handle: Handle, pressed_key: Option<PressedKey>, modifiers: Key
match keycode {
KEY_ENTER | KEY_TAB | KEY_SPACE | KEY_ESCAPE => {
INPUT_STATE.new_word();
},
}
_ => {}
}
}
Expand Down
23 changes: 21 additions & 2 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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<char>) -> String {
if let Some(c) = c {
Expand Down Expand Up @@ -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<Vec<MacroEntry>>,
Expand All @@ -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(),
Expand All @@ -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
Expand Down Expand Up @@ -325,6 +328,10 @@ impl<W: Widget<UIDataAdapter>> Controller<UIDataAdapter, W> 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);
}
Expand Down Expand Up @@ -379,6 +386,18 @@ pub fn main_ui_builder() -> impl Widget<UIDataAdapter> {
.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"))
Expand Down

0 comments on commit 4521540

Please sign in to comment.