Skip to content

Commit

Permalink
Merge pull request #223 from mtgto/back-key-binding
Browse files Browse the repository at this point in the history
前の変換候補を選択するキーバインドを登録可能にする
  • Loading branch information
mtgto authored Oct 12, 2024
2 parents 34a22f8 + 40e9025 commit d3a3871
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
4 changes: 4 additions & 0 deletions macSKK/KeyBinding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ struct KeyBinding: Identifiable, Hashable {
case enter
/// デフォルトはSpaceキー
case space
/// 前の変換候補に移動する。デフォルトはxキー。
case backwardCandidate
/// 補完候補の確定用。デフォルトはTabキー
case tab
/// デフォルトはBackspaceキーとCtrl-hキー
Expand Down Expand Up @@ -316,6 +318,8 @@ struct KeyBinding: Identifiable, Hashable {
return KeyBinding(action, [Input(key: .code(0x24), modifierFlags: [], optionalModifierFlags: [.shift, .option])])
case .space:
return KeyBinding(action, [Input(key: .code(0x31), modifierFlags: [])])
case .backwardCandidate:
return KeyBinding(action, [Input(key: .character("x"), modifierFlags: [])])
case .tab:
return KeyBinding(action, [Input(key: .code(0x30), modifierFlags: [], optionalModifierFlags: .shift)])
case .backspace:
Expand Down
10 changes: 8 additions & 2 deletions macSKK/KeyBindingSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,24 @@ struct KeyBindingSet: Identifiable, Hashable {
guard let id = dict["id"] as? String, let keyBindings = dict["keyBindings"] as? [[String: Any]], let version = dict["version"] as? Int else {
return nil
}
var values: [KeyBinding] = []
if version != Self.serializeVersion {
logger.error("シリアライズバージョンが合わないためキーバインド \(id, privacy: .public) が読み込めません。(現在: \(Self.serializeVersion), 環境設定: \(version)")
logger.error("シリアライズバージョンが合わないためキーバインド \(id, privacy: .public) が読み込めません。(現在: \(Self.serializeVersion), 環境設定: \(version))")
return nil
}
var values: [KeyBinding] = []
for dict in keyBindings {
guard let keyBinding = KeyBinding(dict: dict) else {
logger.warning("キーバインド \(id, privacy: .public) の読み込みに失敗しました")
return nil
}
values.append(keyBinding)
}
// 不足しているキーバインドがあればデフォルト値を設定する
KeyBinding.defaultKeyBindingSettings.forEach { keyBinding in
if values.allSatisfy({ $0.action != keyBinding.action }) {
values.append(keyBinding)
}
}
self.init(id: id, values: values)
}

Expand Down
10 changes: 5 additions & 5 deletions macSKK/StateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ final class StateMachine {
case .eisu:
// 何もしない (OSがIMEの切り替えはしてくれる)
return true
case .unregister, nil:
case .unregister, .backwardCandidate, nil:
break
}

Expand Down Expand Up @@ -738,7 +738,7 @@ final class StateMachine {
return true
case .abbrev, .up, .down, .registerPaste, .eisu, .kana:
return true
case .unregister, .none:
case .unregister, .backwardCandidate, .none:
break
}

Expand Down Expand Up @@ -1063,6 +1063,8 @@ final class StateMachine {
}
updateMarkedText()
return true
case .backwardCandidate:
return handleSelectingPrevious(diff: -1, selecting: selecting)
case .tab:
return true
case .stickyShift, .hiragana, .hankakuKana:
Expand Down Expand Up @@ -1139,9 +1141,7 @@ final class StateMachine {
}

if let input = action.event.charactersIgnoringModifiers {
if input == "x" {
return handleSelectingPrevious(diff: -1, selecting: selecting)
} else if input == "." && action.shiftIsPressed() {
if input == "." && action.shiftIsPressed() {
// 選択中候補で確定し、接尾辞入力に移行。
// カーソル位置より右に文字列がある場合は接頭辞入力として扱う (無視してもいいかも)
addWordToUserDict(yomi: selecting.yomi, okuri: selecting.okuri, candidate: selecting.candidates[selecting.candidateIndex])
Expand Down
1 change: 1 addition & 0 deletions macSKK/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"KeyBindingActionStickyshift" = "Sticky Shift";
"KeyBindingActionEnter" = "Enter";
"KeyBindingActionSpace" = "Space";
"KeyBindingActionBackwardcandidate" = "Select backward candidate";
"KeyBindingActionTab" = "Tab";
"KeyBindingActionBackspace" = "Backspace";
"KeyBindingActionDelete" = "Delete";
Expand Down
1 change: 1 addition & 0 deletions macSKK/ja.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"KeyBindingActionStickyshift" = "Sticky Shift";
"KeyBindingActionEnter" = "Enter";
"KeyBindingActionSpace" = "Space";
"KeyBindingActionBackwardcandidate" = "前の変換候補を選択";
"KeyBindingActionTab" = "Tab";
"KeyBindingActionBackspace" = "Backspace";
"KeyBindingActionDelete" = "Delete";
Expand Down
7 changes: 5 additions & 2 deletions macSKKTests/StateMachineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,19 @@ final class StateMachineTests: XCTestCase {
@MainActor func testHandleNormalPrintable() {
let stateMachine = StateMachine(initialState: IMEState(inputMode: .direct))
let expectation = XCTestExpectation()
stateMachine.inputMethodEvent.collect(3).sink { events in
stateMachine.inputMethodEvent.collect(4).sink { events in
XCTAssertEqual(events[0], .fixedText("c"))
XCTAssertEqual(events[1], .fixedText("C"))
XCTAssertEqual(events[2], .fixedText("X"))
XCTAssertEqual(events[3], .fixedText("x"))
expectation.fulfill()
}.store(in: &cancellables)
XCTAssertTrue(stateMachine.handle(printableKeyEventAction(character: "c")))
XCTAssertTrue(stateMachine.handle(printableKeyEventAction(character: "c", withShift: true)))
// 変換候補選択画面で登録解除へ遷移するキー。Normalではなにも起きない
XCTAssertTrue(stateMachine.handle(printableKeyEventAction(character: "x", withShift: true)))
// 変換候補選択画面で前の候補へ遷移するキー。Normalではなにも起きない
XCTAssertTrue(stateMachine.handle(printableKeyEventAction(character: "x")))
wait(for: [expectation], timeout: 1.0)
}

Expand Down Expand Up @@ -2983,7 +2986,7 @@ final class StateMachineTests: XCTestCase {
case "q":
return withShift ? .japanese : .toggleKana
case "x":
return withShift ? .unregister : nil
return withShift ? .unregister : .backwardCandidate
case ";":
return withShift ? nil : .stickyShift
case "/":
Expand Down

0 comments on commit d3a3871

Please sign in to comment.