Tunes the interface.

This commit is contained in:
zonble 2022-01-28 04:04:44 +08:00
parent 7ee955a55c
commit 056ffeabfd
2 changed files with 87 additions and 52 deletions

View File

@ -314,11 +314,7 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
- (BOOL)handleInput:(KeyHandlerInput *)input state:(InputState *)inState stateCallback:(void (^)(InputState *))stateCallback candidateSelectionCallback:(void (^)(void))candidateSelectionCallback errorCallback:(void (^)(void))errorCallback
{
InputState *state = inState;
// get the unicode character code
UniChar charCode = input.charCode;
// NSInteger keyCode = input.keyCode;
NSEventModifierFlags flags = input.flags;
McBopomofoEmacsKey emacsKey = input.emacsKey;
// if the inputText is empty, it's a function key combination, we ignore it
@ -327,7 +323,7 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
}
// if the composing buffer is empty and there's no reading, and there is some function key combination, we ignore it
BOOL isFunctionKey = ((flags & NSEventModifierFlagCommand) || (flags & NSEventModifierFlagControl) || (flags & NSEventModifierFlagOption) || (flags & NSEventModifierFlagNumericPad));
BOOL isFunctionKey = ([input isCommandHold] || [input isControlHold] || [input isOptionlHold] || [input isNumericPad]);
if (![state isKindOfClass:[InputStateInputting class]] && isFunctionKey) {
return NO;
}
@ -335,13 +331,13 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
// Caps Lock processing : if Caps Lock is on, temporarily disable bopomofo.
if (charCode == 8 || charCode == 13 || [input isAbsorbedArrowKey] || [input isExtraChooseCandidateKey] || [input isCursorForward] || [input isCursorBackward]) {
// do nothing if backspace is pressed -- we ignore the key
} else if (flags & NSAlphaShiftKeyMask) {
} else if ([input isCapsLockOn]) {
// process all possible combination, we hope.
InputStateEmpty *emptyState = [[InputStateEmpty alloc] init];
stateCallback(emptyState);
// first commit everything in the buffer.
if (flags & NSEventModifierFlagShift) {
if ([input isShiftHold]) {
return NO;
}
@ -357,7 +353,7 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
return YES;
}
if (flags & NSEventModifierFlagNumericPad) {
if ([input isNumericPad]) {
if (![input isLeft] && ![input isRight] && ![input isDown] && ![input isUp] && charCode != 32 && isprint(charCode)) {
InputStateEmpty *emptyState = [[InputStateEmpty alloc] init];
stateCallback(emptyState);
@ -458,7 +454,7 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
([input isExtraChooseCandidateKey] || charCode == 32 || (input.useVerticalMode && ([input isVerticalModeOnlyChooseCandidateKey])))) {
if (charCode == 32) {
// if the spacebar is NOT set to be a selection key
if ((flags & NSEventModifierFlagShift) != 0 || !Preferences.chooseCandidateUsingSpace) {
if ([input isShiftHold] || !Preferences.chooseCandidateUsingSpace) {
if (_builder->cursorIndex() >= _builder->length()) {
_bpmfReadingBuffer->clear();
InputStateCommitting *commiting = [[InputStateCommitting alloc] initWithPoppedText:@" "];
@ -488,12 +484,12 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
// MARK: Cursor backward
if ([input isCursorBackward] || emacsKey == McBopomofoEmacsKeyBackward) {
return [self _handleBackwardWithState:state flags:flags stateCallback:stateCallback errorCallback:errorCallback];
return [self _handleBackwardWithState:state input:input stateCallback:stateCallback errorCallback:errorCallback];
}
// MARK: Cursor forward
if ([input isCursorForward] || emacsKey == McBopomofoEmacsKeyForward) {
return [self _handleForwardWithState:state flags:flags stateCallback:stateCallback errorCallback:errorCallback];
return [self _handleForwardWithState:state input:input stateCallback:stateCallback errorCallback:errorCallback];
}
// MARK: Home
@ -598,7 +594,7 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
return YES;
}
- (BOOL)_handleBackwardWithState:(InputState *)state flags:(NSEventModifierFlags)flags stateCallback:(void (^)(InputState *))stateCallback errorCallback:(void (^)(void))errorCallback
- (BOOL)_handleBackwardWithState:(InputState *)state input:(KeyHandlerInput *)input stateCallback:(void (^)(InputState *))stateCallback errorCallback:(void (^)(void))errorCallback
{
if (!_bpmfReadingBuffer->isEmpty()) {
errorCallback();
@ -612,7 +608,7 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
InputStateInputting *currentState = (InputStateInputting *) state;
if (flags & NSEventModifierFlagShift) {
if ([input isShiftHold]) {
// Shift + left
if (_builder->cursorIndex() > 0) {
InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:currentState.composingBuffer cursorIndex:currentState.cursorIndex markerIndex:currentState.cursorIndex - 1];
@ -635,7 +631,7 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
return YES;
}
- (BOOL)_handleForwardWithState:(InputState *)state flags:(NSEventModifierFlags)flags stateCallback:(void (^)(InputState *))stateCallback errorCallback:(void (^)(void))errorCallback
- (BOOL)_handleForwardWithState:(InputState *)state input:(KeyHandlerInput *)input stateCallback:(void (^)(InputState *))stateCallback errorCallback:(void (^)(void))errorCallback
{
if (!_bpmfReadingBuffer->isEmpty()) {
errorCallback();
@ -649,7 +645,7 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
InputStateInputting *currentState = (InputStateInputting *) state;
if (flags & NSEventModifierFlagShift) {
if ([input isShiftHold]) {
// Shift + Right
if (_builder->cursorIndex() < _builder->length()) {
InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:currentState.composingBuffer cursorIndex:currentState.cursorIndex markerIndex:currentState.cursorIndex + 1];
@ -835,7 +831,6 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
candidateSelectionCallback:(void (^)(void))candidateSelectionCallback
errorCallback:(void (^)(void))errorCallback
{
NSLog(@"_handleMarkingState 1");
UniChar charCode = input.charCode;
if (charCode == 27) {
@ -844,8 +839,6 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
return YES;
}
NSLog(@"_handleMarkingState 2");
// Enter
if (charCode == 13) {
if (![self _writeUserPhrase]) {
@ -857,12 +850,15 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
return YES;
}
NSLog(@"_handleMarkingState 3");
NSLog(@"Test 3");
// Shift + left
if (([input isCursorBackward] || input.emacsKey == McBopomofoEmacsKeyBackward)
&& (input.flags & NSEventModifierFlagShift)) {
if (([input isCursorBackward]
// || input.emacsKey == McBopomofoEmacsKeyBackward
)
&& ([input isShiftHold])) {
NSLog(@"Shift + left");
stateCallback(state);
NSUInteger index = state.markerIndex;
if (index > 0) {
index -= 1;
@ -877,12 +873,15 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
return YES;
}
NSLog(@"_handleMarkingState 4");
NSLog(@"Test 4");
// Shift + Right
if (([input isCursorBackward] || input.emacsKey == McBopomofoEmacsKeyForward)
&& (input.flags & NSEventModifierFlagShift)) {
if (([input isCursorBackward]
// || input.emacsKey == McBopomofoEmacsKeyForward
)
&& ([input isShiftHold])) {
NSLog(@"Shift + Right");
stateCallback(state);
NSUInteger index = state.markerIndex;
if (index < state.composingBuffer.length) {
index += 1;
@ -914,7 +913,7 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
BOOL cancelCandidateKey =
(charCode == 27) ||
((_inputMode == kPlainBopomofoModeIdentifier) &&
(charCode == 8 || input.keyCode == KeyCodeDelete));
(charCode == 8 || [input isDelete]));
if (cancelCandidateKey) {
if (_inputMode == kPlainBopomofoModeIdentifier) {

View File

@ -1,6 +1,6 @@
import Cocoa
@objc enum KeyCode: UInt16 {
@objc enum KeyCode: Int {
case none = 0
case enter = 76
case up = 126
@ -17,19 +17,34 @@ import Cocoa
@objc class KeyHandlerInput: NSObject {
@objc private (set) var useVerticalMode: Bool
@objc private (set) var inputText: String?
@objc private (set) var keyCode: UInt16
@objc private (set) var flags: NSEvent.ModifierFlags
@objc private (set) var charCode: UInt16
@objc private (set) var cursorForwardKey: KeyCode
@objc private (set) var cursorBackwardKey: KeyCode
@objc private (set) var extraChooseCandidateKey: KeyCode
@objc private (set) var absorbedArrowKey: KeyCode
@objc private (set) var verticalModeOnlyChooseCandidateKey: KeyCode
@objc private var keyCode: Int
@objc private var flags: NSEvent.ModifierFlags
@objc private var cursorForwardKey: KeyCode
@objc private var cursorBackwardKey: KeyCode
@objc private var extraChooseCandidateKey: KeyCode
@objc private var absorbedArrowKey: KeyCode
@objc private var verticalModeOnlyChooseCandidateKey: KeyCode
@objc private (set) var emacsKey: McBopomofoEmacsKey
@objc init(inputText: String?, keyCode: UInt16, charCode: UInt16, flags: NSEvent.ModifierFlags, isVerticalMode: Bool) {
self.inputText = inputText
self.keyCode = Int(keyCode)
self.charCode = charCode
self.flags = flags
self.useVerticalMode = isVerticalMode
self.emacsKey = EmacsKeyHelper.detect(charCode: charCode, flags: flags)
self.cursorForwardKey = useVerticalMode ? .down : .right
self.cursorBackwardKey = useVerticalMode ? .up : .left
self.extraChooseCandidateKey = useVerticalMode ? .left : .down
self.absorbedArrowKey = useVerticalMode ? .right : .up
self.verticalModeOnlyChooseCandidateKey = useVerticalMode ? absorbedArrowKey : .none
super.init()
}
@objc init(event: NSEvent, isVerticalMode: Bool) {
self.inputText = event.characters
self.keyCode = event.keyCode
self.keyCode = Int(event.keyCode)
self.flags = event.modifierFlags
self.useVerticalMode = isVerticalMode
let charCode: UInt16 = {
@ -39,7 +54,6 @@ import Cocoa
let first = inputText[inputText.startIndex].utf16.first!
return first
}()
self.charCode = charCode
self.emacsKey = EmacsKeyHelper.detect(charCode: charCode, flags: event.modifierFlags)
self.cursorForwardKey = useVerticalMode ? .down : .right
@ -50,66 +64,88 @@ import Cocoa
super.init()
}
@objc var isShiftHold: Bool {
self.flags.contains([.shift])
}
@objc var isCommandHold: Bool {
self.flags.contains([.command])
}
@objc var isControlHold: Bool {
self.flags.contains([.control])
}
@objc var isOptionlHold: Bool {
self.flags.contains([.option])
}
@objc var isCapsLockOn: Bool {
self.flags.contains([.capsLock])
}
@objc var isNumericPad: Bool {
self.flags.contains([.numericPad])
}
@objc var isEnter: Bool {
self.keyCode == KeyCode.enter.rawValue
KeyCode(rawValue: self.keyCode) == KeyCode.enter
}
@objc var isUp: Bool {
self.keyCode == KeyCode.up.rawValue
KeyCode(rawValue: self.keyCode) == KeyCode.up
}
@objc var isDown: Bool {
self.keyCode == KeyCode.down.rawValue
KeyCode(rawValue: self.keyCode) == KeyCode.down
}
@objc var isLeft: Bool {
NSLog("isLeft called \(self.keyCode == KeyCode.left.rawValue)")
return self.keyCode == KeyCode.left.rawValue
KeyCode(rawValue: self.keyCode) == KeyCode.left
}
@objc var isRight: Bool {
NSLog("isRight called \(self.keyCode == KeyCode.right.rawValue)")
return self.keyCode == KeyCode.right.rawValue
KeyCode(rawValue: self.keyCode) == KeyCode.right
}
@objc var isPageUp: Bool {
self.keyCode == KeyCode.pageUp.rawValue
KeyCode(rawValue: self.keyCode) == KeyCode.pageUp
}
@objc var isPageDown: Bool {
self.keyCode == KeyCode.pageDown.rawValue
KeyCode(rawValue: self.keyCode) == KeyCode.pageDown
}
@objc var isHome: Bool {
self.keyCode == KeyCode.home.rawValue
KeyCode(rawValue: self.keyCode) == KeyCode.home
}
@objc var isEnd: Bool {
self.keyCode == KeyCode.end.rawValue
KeyCode(rawValue: self.keyCode) == KeyCode.end
}
@objc var isDelete: Bool {
self.keyCode == KeyCode.delete.rawValue
KeyCode(rawValue: self.keyCode) == KeyCode.delete
}
@objc var isCursorBackward: Bool {
self.keyCode == cursorBackwardKey.rawValue
KeyCode(rawValue: self.keyCode) == cursorBackwardKey
}
@objc var isCursorForward: Bool {
self.keyCode == cursorForwardKey.rawValue
KeyCode(rawValue: self.keyCode) == cursorForwardKey
}
@objc var isAbsorbedArrowKey: Bool {
self.keyCode == absorbedArrowKey.rawValue
KeyCode(rawValue: self.keyCode) == absorbedArrowKey
}
@objc var isExtraChooseCandidateKey: Bool {
self.keyCode == extraChooseCandidateKey.rawValue
KeyCode(rawValue: self.keyCode) == extraChooseCandidateKey
}
@objc var isVerticalModeOnlyChooseCandidateKey: Bool {
self.keyCode == verticalModeOnlyChooseCandidateKey.rawValue
KeyCode(rawValue: self.keyCode) == verticalModeOnlyChooseCandidateKey
}
}