Skip to content

Commit

Permalink
fix: Stop tracking current word if vi-rs has attempted to remove modi…
Browse files Browse the repository at this point in the history
…fications

Fixes #112 #116
  • Loading branch information
huytd committed Jun 1, 2024
1 parent 0242690 commit 69042f5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ version = "0.2.7"
env_logger = "0.10.0"
libc = "0.2.139"
log = "0.4.17"
vi = "0.6.0"
vi = "0.6.1"
bitflags = "1.3.2"
druid = { features = ["image", "png"], git = "https://github.com/huytd/druid", branch = "master" }
druid = { features = [
"image",
"png",
], git = "https://github.com/huytd/druid", branch = "master" }
once_cell = "1.17.0"
auto-launch = "0.5.0"

Expand Down
11 changes: 6 additions & 5 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use druid::{Data, Target};
use log::debug;
use once_cell::sync::{Lazy, OnceCell};
use rdev::{Keyboard, KeyboardState};
use vi::TransformResult;

use crate::platform::{get_active_app_name, KeyModifier};
use crate::{
Expand Down Expand Up @@ -342,18 +343,18 @@ impl InputState {
self.enabled
}

pub fn transform_keys(&self) -> Result<String, ()> {
pub fn transform_keys(&self) -> Result<(String, TransformResult), ()> {
let transform_method = match self.method {
TypingMethod::VNI => vi::vni::transform_buffer,
TypingMethod::Telex => vi::telex::transform_buffer,
};
let result = std::panic::catch_unwind(|| {
let mut output = String::new();
transform_method(self.buffer.chars(), &mut output);
output
let transform_result = transform_method(self.buffer.chars(), &mut output);
(output, transform_result)
});
if let Ok(output) = result {
return Ok(output);
if let Ok((output, transform_result)) = result {
return Ok((output, transform_result));
}
Err(())
}
Expand Down
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const APP_VERSION: &str = env!("CARGO_PKG_VERSION");

fn do_transform_keys(handle: Handle, is_delete: bool) -> bool {
unsafe {
if let Ok(output) = INPUT_STATE.transform_keys() {
if let Ok((output, transform_result)) = INPUT_STATE.transform_keys() {
debug!("Transformed: {:?}", output);
if INPUT_STATE.should_send_keyboard_event(&output) || is_delete {
// This is a workaround for Firefox, where macOS's Accessibility API cannot work.
Expand All @@ -45,6 +45,11 @@ fn do_transform_keys(handle: Handle, is_delete: bool) -> bool {
_ = send_string(handle, &output);
debug!("Sent: {:?}", output);
INPUT_STATE.replace(output);
if transform_result.letter_modification_removed
|| transform_result.tone_mark_removed
{
INPUT_STATE.stop_tracking();
}
return true;
}
}
Expand Down Expand Up @@ -186,7 +191,9 @@ fn event_handler(
|| (c.is_numeric() && modifiers.is_shift())
{
// If special characters detected, dismiss the current tracking word
INPUT_STATE.push(c);
if c.is_numeric() {
INPUT_STATE.push(c);
}
INPUT_STATE.new_word();
} else {
// Otherwise, process the character
Expand Down

0 comments on commit 69042f5

Please sign in to comment.