Skip to content

Commit

Permalink
Implement transform when delete with backspace key
Browse files Browse the repository at this point in the history
  • Loading branch information
huytd committed Jan 27, 2023
1 parent f8cad7a commit 2782e35
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
14 changes: 11 additions & 3 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ impl InputState {
self.should_track
}

pub fn is_buffer_empty(&self) -> bool {
self.buffer.is_empty()
}

pub fn new_word(&mut self) {
if !self.buffer.is_empty() {
self.clear();
Expand Down Expand Up @@ -106,8 +110,13 @@ impl InputState {
!self.buffer.eq(word)
}

pub fn get_backspace_count(&self) -> usize {
self.display_buffer.chars().count() - 1
pub fn get_backspace_count(&self, is_delete: bool) -> usize {
let dp_len = self.display_buffer.chars().count();
if is_delete && dp_len >= 1 {
dp_len
} else {
dp_len - 1
}
}

pub fn replace(&mut self, buf: String) {
Expand All @@ -131,7 +140,6 @@ impl InputState {

pub fn pop(&mut self) {
self.buffer.pop();
self.display_buffer.pop();
if self.buffer.is_empty() {
self.new_word();
}
Expand Down
53 changes: 31 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,20 @@ use ui::{UIDataAdapter, UPDATE_UI};

static UI_EVENT_SINK: OnceCell<ExtEventSink> = OnceCell::new();

fn process_character(handle: Handle, c: char, modifiers: KeyModifier) -> bool {
fn do_transform_keys(handle: Handle, is_delete: bool) -> bool {
unsafe {
if modifiers.is_super() || modifiers.is_control() || modifiers.is_alt() {
INPUT_STATE.new_word();
} else if INPUT_STATE.is_tracking() {
INPUT_STATE.push(if modifiers.is_shift() {
c.to_ascii_uppercase()
} else {
c
});
if INPUT_STATE.should_transform_keys(&c) {
let output = INPUT_STATE.transform_keys();
debug!("Transformed: {:?}", output);
if INPUT_STATE.should_send_keyboard_event(&output) {
let backspace_count = INPUT_STATE.get_backspace_count();
debug!("Backspace count: {}", backspace_count);
_ = send_backspace(handle, backspace_count);
_ = send_string(handle, &output);
INPUT_STATE.replace(output);
return true;
}
}
let output = INPUT_STATE.transform_keys();
debug!("Transformed: {:?}", output);
if INPUT_STATE.should_send_keyboard_event(&output) || is_delete {
let backspace_count = INPUT_STATE.get_backspace_count(is_delete);
debug!("Backspace count: {}", backspace_count);
_ = send_backspace(handle, backspace_count);
_ = send_string(handle, &output);
INPUT_STATE.replace(output);
return true;
}
}

return false;
}

Expand All @@ -62,6 +52,11 @@ fn event_handler(handle: Handle, keycode: Option<char>, modifiers: KeyModifier)
}
KEY_DELETE => {
INPUT_STATE.pop();
if !INPUT_STATE.is_buffer_empty() {
return do_transform_keys(handle, true);
} else {
INPUT_STATE.clear();
}
}
c => {
if "()[]{}<>/\\!@#$%^&*-_=+|~`'\"".contains(c)
Expand All @@ -71,7 +66,21 @@ fn event_handler(handle: Handle, keycode: Option<char>, modifiers: KeyModifier)
INPUT_STATE.new_word();
} else {
// Otherwise, process the character
return process_character(handle, c, modifiers);
if modifiers.is_super()
|| modifiers.is_control()
|| modifiers.is_alt()
{
INPUT_STATE.new_word();
} else if INPUT_STATE.is_tracking() {
INPUT_STATE.push(if modifiers.is_shift() {
c.to_ascii_uppercase()
} else {
c
});
if INPUT_STATE.should_transform_keys(&c) {
return do_transform_keys(handle, false);
}
}
}
}
}
Expand Down

0 comments on commit 2782e35

Please sign in to comment.