Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

接頭辞・接尾辞入力をキーバインドに追加 #305

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions macSKK/CurrentInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ struct CurrentInput: Equatable {
}

init(event: NSEvent) {
if event.modifierFlags.contains(.shift), let character = event.characters?.lowercased().first, Key.characters.contains(character) {
// Shiftを押しながら入力されたキーはキーバインド設定から登録した場合は (NSEvent#charactersIgnoringModifiersが記号のほうを返すため)
// .character("!") のように記号のほうをもっているが、IMKInputController#handleに渡されるNSEventの
// charactersIgnoringModifiersは記号のほうではない ("!"なら"1"になっている) ため、charactersのほうを見る
if let character = event.charactersIgnoringModifiers?.lowercased().first, Key.characters.contains(character) {
key = .character(character)
} else if let character = event.charactersIgnoringModifiers?.lowercased().first, Key.characters.contains(character) {
} else if let character = event.characters?.lowercased().first, Key.characters.contains(character) {
key = .character(character)
} else {
key = .code(event.keyCode)
Expand Down
12 changes: 12 additions & 0 deletions macSKK/KeyBinding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ struct KeyBinding: Identifiable, Hashable {
case registerPaste
/// 選択した文字列を辞書から逆引きして再変換をする。デフォルトはCtrl-/キー
case reconvert
/// 接頭辞・接尾辞の入力。デフォルトは ">" (Shift-.キー)
case affix
/// 英数キー
/// TODO: カスタマイズできなくする?
case eisu
Expand Down Expand Up @@ -94,6 +96,14 @@ struct KeyBinding: Identifiable, Hashable {
} else {
return false
}
case .affix:
if case .composing(_) = inputMethodState {
return true
} else if case .selecting(_) = inputMethodState {
return true
} else {
return false
}
default:
return true
}
Expand Down Expand Up @@ -303,6 +313,8 @@ struct KeyBinding: Identifiable, Hashable {
return KeyBinding(action, [Input(key: .character("y"), modifierFlags: .control)])
case .reconvert:
return KeyBinding(action, [Input(key: .character("/"), modifierFlags: [.control])])
case .affix:
return KeyBinding(action, [Input(key: .character("."), modifierFlags: .shift)])
case .eisu:
return KeyBinding(action, [Input(key: .code(0x66), modifierFlags: [])])
case .kana:
Expand Down
2 changes: 1 addition & 1 deletion macSKK/Settings/KeyEventView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct KeyEventView: View {
.onAppear {
eventMonitor = NSEvent.addLocalMonitorForEvents(matching: [.keyDown]) { event in
characters = event.characters ?? ""
charactersIgnoringModifiers = event.charactersIgnoringModifiers ?? ""
charactersIgnoringModifiers = event.characters(byApplyingModifiers: []) ?? ""
keyCode = event.keyCode.description
var modifiers: [String] = []
if event.modifierFlags.contains(.capsLock) {
Expand Down
52 changes: 32 additions & 20 deletions macSKK/StateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ final class StateMachine {
case .eisu:
// 何もしない (OSがIMEの切り替えはしてくれる)
return true
case .space, .unregister, .backwardCandidate, .toggleAndFixKana, nil:
case .space, .unregister, .backwardCandidate, .toggleAndFixKana, .affix, nil:
break
}

Expand Down Expand Up @@ -791,6 +791,23 @@ final class StateMachine {
updateMarkedText()
}
return true
case .affix:
if state.inputMode == .direct {
break
} else if composing.text.isEmpty {
// 接尾辞の入力開始として扱う
let converted = Global.kanaRule.convert(romaji + ">", punctuation: Global.punctuation)
return handleComposingPrintable(
input: ">",
converted: converted,
action: action,
composing: composing,
specialState: specialState)
} else {
// 接頭辞が入力されたものとして ">" より前で変換を開始する
let newComposing = composing.trim(kanaRule: Global.kanaRule).appendText(Romaji.Moji(firstRomaji: "", kana: ">"))
return handleComposingStartConvert(action, composing: newComposing, specialState: specialState)
}
case .up, .down, .registerPaste, .eisu, .kana, .toggleKana, .reconvert:
return true
case .abbrev, .unregister, .backwardCandidate, .none:
Expand Down Expand Up @@ -834,11 +851,6 @@ final class StateMachine {
let text = composing.text
let okuri = composing.okuri

if input == "." && action.shiftIsPressed() && state.inputMode != .direct && !composing.text.isEmpty { // ">"
// 接頭辞が入力されたものとして ">" より前で変換を開始する
let newComposing = composing.trim(kanaRule: Global.kanaRule).appendText(Romaji.Moji(firstRomaji: "", kana: ">"))
return handleComposingStartConvert(action, composing: newComposing, specialState: specialState)
}
switch state.inputMode {
case .hiragana, .katakana, .hankaku:
// lowercaseMapにエントリがある場合はエントリの方のキーが入力されたと見做す
Expand Down Expand Up @@ -1168,6 +1180,19 @@ final class StateMachine {
updateCandidates(selecting: nil)
updateMarkedText()
return true
case .affix:
// 選択中候補で確定し、接尾辞入力に移行。
// カーソル位置より右に文字列がある場合は接頭辞入力として扱う (無視してもいいかも)
addWordToUserDict(yomi: selecting.yomi, okuri: selecting.okuri, candidate: selecting.candidates[selecting.candidateIndex])
updateCandidates(selecting: nil)
addFixedText(selecting.fixedText(dropLast: false))
if let remain = selecting.remain {
state.inputMethod = .composing(ComposingState(isShift: true, text: remain, romaji: ""))
updateMarkedText()
} else {
state.inputMethod = .composing(ComposingState(isShift: true, text: [], okuri: nil, romaji: ""))
}
return handle(action)
case .registerPaste, .delete, .eisu, .kana, .reconvert:
return true
case .toggleKana, .toggleAndFixKana, .direct, .zenkaku, .abbrev, .japanese:
Expand All @@ -1177,20 +1202,7 @@ final class StateMachine {
}

if let input = action.event.charactersIgnoringModifiers {
if input == "." && action.shiftIsPressed() {
// 選択中候補で確定し、接尾辞入力に移行。
// カーソル位置より右に文字列がある場合は接頭辞入力として扱う (無視してもいいかも)
addWordToUserDict(yomi: selecting.yomi, okuri: selecting.okuri, candidate: selecting.candidates[selecting.candidateIndex])
updateCandidates(selecting: nil)
addFixedText(selecting.fixedText(dropLast: false))
if let remain = selecting.remain {
state.inputMethod = .composing(ComposingState(isShift: true, text: remain, romaji: ""))
updateMarkedText()
} else {
state.inputMethod = .composing(ComposingState(isShift: true, text: [], okuri: nil, romaji: ""))
}
return handle(action)
} else if selecting.candidateIndex >= inlineCandidateCount {
if selecting.candidateIndex >= inlineCandidateCount {
if let first = input.lowercased().first, let index = Global.selectCandidateKeys.firstIndex(of: first), index < displayCandidateCount {
let diff = index - (selecting.candidateIndex - inlineCandidateCount) % displayCandidateCount
if selecting.candidateIndex + diff < selecting.candidates.count {
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 @@ -124,6 +124,7 @@
"KeyBindingActionUnregister" = "Unregister selected candidate";
"KeyBindingActionRegisterpaste" = "Paste on register";
"KeyBindingActionReconvert" = "Reconvert selected text";
"KeyBindingActionAffix" = "Prefix/Suffix";
"KeyBindingActionEisu" = "Eisu Key";
"KeyBindingActionKana" = "Kana Key";

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 @@ -124,6 +124,7 @@
"KeyBindingActionUnregister" = "選択中の変換候補の削除";
"KeyBindingActionRegisterpaste" = "登録モードでペースト";
"KeyBindingActionReconvert" = "選択テキストを再変換";
"KeyBindingActionAffix" = "接頭辞/接尾辞";
"KeyBindingActionEisu" = "英数キー";
"KeyBindingActionKana" = "かなキー";

Expand Down
4 changes: 3 additions & 1 deletion macSKKTests/CurrentInputTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ final class CurrentInputTests: XCTestCase {
let inputShiftQ = CurrentInput(key: .character("q"), modifierFlags: .shift)
let eventQ = generateKeyEvent(modifierFlags: [], characters: "q")
let eventShiftQ = generateKeyEvent(modifierFlags: .shift, characters: "q")

let affix = generateKeyEvent(modifierFlags: .shift, characters: ">", charactersIgnoringModifiers: ".")

XCTAssertEqual(inputQ, CurrentInput(event: eventQ))
XCTAssertEqual(inputShiftQ, CurrentInput(event: eventShiftQ))
XCTAssertNotEqual(inputQ, CurrentInput(event: eventShiftQ))
XCTAssertNotEqual(inputShiftQ, CurrentInput(event: eventQ))
XCTAssertEqual(CurrentInput(event: affix).key, .character("."))
}

func testCurrentInputKeyCodeWithShift() {
Expand Down
2 changes: 2 additions & 0 deletions macSKKTests/StateMachineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3460,6 +3460,8 @@ final class StateMachineTests: XCTestCase {
return withShift ? .unregister : .backwardCandidate
case ";":
return withShift ? nil : .stickyShift
case ".":
return withShift ? .affix : nil
case "/":
return withShift ? nil : .abbrev
case " ":
Expand Down
Loading