Skip to content

Commit

Permalink
Add Commit hotkey (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
Riey authored Feb 25, 2021
1 parent 38d64f6 commit 8adc073
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Add `TreatJongseongAsChoseong` addon
* Add `FlexibleComposeOrder` addon [#318](https://github.com/Riey/kime/issues/318)
* Check LANG env in kime-check [#317](https://github.com/Riey/kime/issues/317)
* Add `Commit` hotkey, `ConsumeIfProcessed` hotkey result [#315](https://github.com/Riey/kime/issues/315)

## 1.1.3

Expand Down
8 changes: 8 additions & 0 deletions docs/CONFIGURATION.ko.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ Super-Space:
한글모드로 바꿉니다
##### Commit
현재 조합상태를 종료하고 커밋합니다
#### result
##### Bypass
Expand All @@ -81,6 +85,10 @@ Super-Space:
키 처리를 종료합니다
##### ConsumeIfProcessed
단축키가 실행됐을 경우에는 Consume처럼, 아닐때는 Bypass처럼 동작합니다.
## xim_preedit_font
XIM에서 쓸 편집창 글꼴과 크기입니다.
Expand Down
8 changes: 8 additions & 0 deletions docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Set english mode

Set hangul mode

##### Commit

End current preedit state then commit

#### result

##### Bypass
Expand All @@ -69,6 +73,10 @@ Bypass key to continue key process

Consume key to end key process

##### ConsumeIfProcessed

When hotkey processed it act like Consume otherwise it act like Bypass

### default

```yaml
Expand Down
2 changes: 2 additions & 0 deletions src/engine/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ pub enum HotkeyBehavior {
ToggleHangul,
ToHangul,
ToEnglish,
Commit,
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
pub enum HotkeyResult {
Consume,
Bypass,
ConsumeIfProcessed,
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
Expand Down
45 changes: 33 additions & 12 deletions src/engine/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ use ahash::AHashMap;
use std::io::Read;

use self::characters::KeyValue;
use self::config::{HotkeyBehavior, HotkeyResult};
use self::state::HangulState;

pub use self::config::{Addon, Config, RawConfig};
pub use self::config::{Addon, Config, Hotkey, HotkeyBehavior, HotkeyResult, RawConfig};
pub use self::input_result::InputResult;
pub use self::keycode::{Key, KeyCode, ModifierState};

Expand Down Expand Up @@ -100,27 +99,49 @@ impl InputEngine {

pub fn press_key(&mut self, key: Key, config: &Config) -> InputResult {
if let Some(hotkey) = config.hotkeys.get(&key) {
let first = self.enable_hangul;
let mut processed = false;
let mut ret = InputResult::empty();

match hotkey.behavior() {
HotkeyBehavior::ToEnglish => {
self.enable_hangul = false;
if self.enable_hangul {
self.enable_hangul = false;
ret |= InputResult::LANGUAGE_CHANGED;
processed = true;
}
}
HotkeyBehavior::ToHangul => {
self.enable_hangul = true;
if !self.enable_hangul {
self.enable_hangul = true;
ret |= InputResult::LANGUAGE_CHANGED;
processed = true;
}
}
HotkeyBehavior::ToggleHangul => {
self.enable_hangul = !self.enable_hangul;
ret |= InputResult::LANGUAGE_CHANGED;
processed = true;
}
HotkeyBehavior::Commit => {
if self
.state
.preedit_result()
.contains(InputResult::HAS_PREEDIT)
{
self.state.clear_preedit();
ret |= InputResult::NEED_RESET;
processed = true;
}
}
}

let mut ret = match hotkey.result() {
HotkeyResult::Bypass => self.bypass(),
HotkeyResult::Consume => InputResult::CONSUMED | self.state.preedit_result(),
};

if self.enable_hangul != first {
ret.insert(InputResult::LANGUAGE_CHANGED);
match (hotkey.result(), processed) {
(HotkeyResult::Bypass, _) | (HotkeyResult::ConsumeIfProcessed, false) => {
ret |= self.bypass();
}
(HotkeyResult::Consume, _) | (HotkeyResult::ConsumeIfProcessed, true) => {
ret |= InputResult::CONSUMED | self.state.preedit_result();
}
}

ret
Expand Down
45 changes: 44 additions & 1 deletion src/engine/core/tests/dubeolsik.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::collections::BTreeMap;

use enumset::EnumSet;
use kime_engine_core::{Addon, Config, InputEngine, InputResult, Key, KeyCode::*, RawConfig};
use kime_engine_core::{
Addon, Config, Hotkey, InputEngine, InputResult, Key, KeyCode::*, RawConfig,
};

fn default_config() -> Config {
Config::from_raw_config(
Expand All @@ -22,6 +26,17 @@ fn addon_config(addon: EnumSet<Addon>) -> Config {
)
}

fn hotkey_config(hotkeys: BTreeMap<Key, Hotkey>) -> Config {
Config::from_raw_config(
RawConfig {
layout: "dubeolsik".into(),
hotkeys,
..Default::default()
},
None,
)
}

#[track_caller]
fn test_input_impl(config: &Config, word_commit: bool, keys: &[(Key, &str, &str)]) {
let mut engine = InputEngine::new(word_commit);
Expand Down Expand Up @@ -67,6 +82,11 @@ fn test_input_with_addon(keys: &[(Key, &str, &str)], addon: EnumSet<Addon>) {
test_input_impl(&addon_config(addon), false, keys);
}

#[track_caller]
fn test_input_with_hotkey(keys: &[(Key, &str, &str)], hotkeys: BTreeMap<Key, Hotkey>) {
test_input_impl(&hotkey_config(hotkeys), false, keys);
}

#[track_caller]
fn test_word_input(keys: &[(Key, &str, &str)]) {
test_input_impl(&default_config(), true, keys);
Expand All @@ -80,6 +100,29 @@ fn flexible_compose_order_addon() {
);
}

#[test]
fn space_commit() {
test_input_with_hotkey(
&[
(Key::normal(R), "ㄱ", ""),
(Key::normal(K), "가", ""),
(Key::normal(Space), "", "가"),
(Key::normal(S), "ㄴ", ""),
(Key::normal(K), "나", ""),
(Key::normal(Space), "", "나"),
(Key::normal(Space), "", "PASS"),
],
std::iter::once((
Key::normal(Space),
Hotkey::new(
kime_engine_core::HotkeyBehavior::Commit,
kime_engine_core::HotkeyResult::ConsumeIfProcessed,
),
))
.collect(),
)
}

#[test]
fn word_hello() {
test_word_input(&[
Expand Down

0 comments on commit 8adc073

Please sign in to comment.