Skip to content

Commit

Permalink
1.0-alpha6 - Improved built in IME, but answer text field can’t be ce…
Browse files Browse the repository at this point in the history
…ntered, at least for now.
  • Loading branch information
丈槍由紀 committed Feb 12, 2022
1 parent f4e9aa0 commit 2cfadb8
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 38 deletions.
8 changes: 7 additions & 1 deletion KaniManabu-macOS/Support/TKMKanaInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

#import <AppKit/AppKit.h>


@interface TKMKanaInputTextField : NSTextField <NSTextViewDelegate>
@property int StartLocation;
@property (strong)NSTextView *textvieweditor;
@end

NS_ASSUME_NONNULL_BEGIN

extern NSString *TKMConvertKanaText(NSString *text);
Expand All @@ -28,7 +34,7 @@ typedef NS_ENUM(NSInteger, TKMAlphabet) {

@interface TKMKanaInput : NSObject
@property(nonatomic) TKMAlphabet alphabet;
- (NSString *)checkString:(NSString *)currentstring withReplacementString:(NSString *)replacementstring withOldRange:(NSRange)range withCurrentRange:(NSRange)currentRange useCurrentRange:(bool)useCurrentRange;
- (NSDictionary *)checkString:(NSString *)currentstring withReplacementString:(NSString *)replacementstring withOldRange:(NSRange)range withCurrentRange:(NSRange)currentRange useCurrentRange:(bool)useCurrentRange;
@end

NS_ASSUME_NONNULL_END
60 changes: 51 additions & 9 deletions KaniManabu-macOS/Support/TKMKanaInput.m
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,44 @@ void EnsureInitialised() {
return ret;
}

@implementation TKMKanaInputTextField
- (void)keyUp:(NSEvent *)event {
if (event.keyCode == 123 || event.keyCode == 124) {
self.StartLocation = self.currentEditor.selectedRange.location;
}
[super keyDown:event];
}

- (BOOL)becomeFirstResponder
{
if (![super becomeFirstResponder]) {
return NO;
}

NSTextView * fieldEditor = (NSTextView *)[[self window] fieldEditor:YES forObject:self];
fieldEditor.delegate = self;
self.textvieweditor = fieldEditor;
NSClickGestureRecognizer *recognizer = [[NSClickGestureRecognizer alloc] initWithTarget:self action:@selector(singleClickGesture:)];
[self.textvieweditor addGestureRecognizer:recognizer];
return YES;
}

- (void)singleClickGesture:(NSClickGestureRecognizer *)recognizer {
NSLog(@"qq");
if (recognizer.state == NSGestureRecognizerStateEnded) {
[self.textvieweditor setEditable:YES];
if ([self.window makeFirstResponder:self.textvieweditor]) {
NSPoint location = [recognizer locationInView:self.textvieweditor];
NSUInteger position = [_textvieweditor characterIndexForInsertionAtPoint:location];
_textvieweditor.selectedRange = NSMakeRange(position, 0);
self.StartLocation = self.currentEditor.selectedRange.location;
}
}
}


@end

@implementation TKMKanaInput

- (instancetype)init {
Expand All @@ -377,9 +415,9 @@ - (instancetype)init {
return nil;
}

- (NSString *)checkString:(NSString *)currentstring withReplacementString:(NSString *)replacementstring withOldRange:(NSRange)range withCurrentRange:(NSRange)currentRange useCurrentRange:(bool)useCurrentRange {
- (NSDictionary *)checkString:(NSString *)currentstring withReplacementString:(NSString *)replacementstring withOldRange:(NSRange)range withCurrentRange:(NSRange)currentRange useCurrentRange:(bool)useCurrentRange {
if (range.length != 0 || replacementstring.length == 0) {
return currentstring;
return @{@"string" : currentstring, @"newposition" : @(currentRange.location+1)};
}
if (currentRange.location > 0 && replacementstring.length == 1 && useCurrentRange) {
unichar newChar = [replacementstring characterAtIndex:0];
Expand All @@ -399,16 +437,18 @@ - (NSString *)checkString:(NSString *)currentstring withReplacementString:(NSStr
NSRange newrange = NSMakeRange(currentRange.location - 2, 1);
NSString *newstring = [currentstring stringByReplacingCharactersInRange:newrange
withString:replacementString];
return newstring;
return @{@"string" : newstring, @"newposition" : @(currentRange.location)};
}

// Replace n followed by a consonant.
if (newChar != 'n' && [kN characterIsMember:lastChar] &&
![kCanFollowN characterIsMember:newChar]) {
NSString *replacementString =
(lastCharWasUppercase || _alphabet == kTKMAlphabetKatakana) ? @"" : @"";
return [currentstring stringByReplacingCharactersInRange:NSMakeRange(currentRange.location - 2, 1)
withString:replacementString];
NSRange newrange = NSMakeRange(currentRange.location - 2, 1);
NSString *newstring = [currentstring stringByReplacingCharactersInRange:newrange
withString:replacementString];
return @{@"string" : newstring, @"newposition" : @(currentRange.location)};
}
}

Expand All @@ -431,12 +471,14 @@ - (NSString *)checkString:(NSString *)currentstring withReplacementString:(NSStr
replacement = [replacement stringByApplyingTransform:NSStringTransformHiraganaToKatakana
reverse:NO];
}
NSRange newrange = NSMakeRange(range.location, currentstring.length-range.location);
return [currentstring stringByReplacingCharactersInRange:newrange
withString:replacement];
NSRange newrange = NSMakeRange(range.location, text.length);
NSString *nstring = [currentstring stringByReplacingCharactersInRange:newrange
withString:replacement];
int newposition = replacement.length == 2 ? currentRange.location-1 : currentRange.location-(newrange.length-1);
return @{@"string" : nstring, @"newposition" : @(newposition)};
}
}
return currentstring;
return @{@"string" : currentstring, @"newposition" : @(currentRange.location+1)};
}

@end
52 changes: 29 additions & 23 deletions KaniManabu-macOS/Window Controllers/ReviewWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ @interface ReviewWindowController ()
@property (strong) IBOutlet NSTextField *word;
@property (strong) IBOutlet NSTextField *questionprompt;
@property (strong) IBOutlet NSTextField *answerstatus;
@property (strong) IBOutlet NSTextField *answertextfield;
@property (strong) IBOutlet TKMKanaInputTextField *answertextfield;
@property (strong) IBOutlet NSTextField *srslevellabel;
@property (strong) IBOutlet NSButton *answerbtn;
@property int totalitems;
Expand Down Expand Up @@ -146,30 +146,36 @@ - (void)showCloseWindowPrompt {

- (void)controlTextDidChange:(NSNotification *)obj {
if (_questiontype == CardReviewTypeReading && _useKaniManabuIME) {
_currentrange = NSMakeRange(_answertextfield.currentEditor.selectedRange.location, 0);
if (_answertextfield.currentEditor.selectedRange.location < _oldrange.location) {
_oldrange = _currentrange;
}
bool usecurrentlocation = false;
if (_currentrange.location > 1) {
NSString *astring = [_answertextfield.stringValue substringWithRange:NSMakeRange(_currentrange.location-1, 1)];
NSString *bstring = [_answertextfield.stringValue substringWithRange:NSMakeRange(_currentrange.location-2, 1)];
if ([astring isEqualToString:bstring] && [astring caseInsensitiveCompare:@"n"] != NSOrderedSame && [bstring caseInsensitiveCompare:@"n"] != NSOrderedSame) {
usecurrentlocation = true;
}
}
@try{
NSRange substr = NSMakeRange(usecurrentlocation ? _currentrange.location-1 : _oldrange.location, usecurrentlocation ? 1 : _currentrange.location-_oldrange.location);
NSString *replacementstr = [_answertextfield.stringValue substringWithRange:substr];
NSString *proposedstr = [_kanainput checkString:_answertextfield.stringValue withReplacementString:replacementstr withOldRange:_oldrange withCurrentRange:_currentrange useCurrentRange:usecurrentlocation];
if (![proposedstr isEqualToString:_answertextfield.stringValue]) {
_answertextfield.stringValue = proposedstr;
_oldrange = NSMakeRange(usecurrentlocation ? _answertextfield.currentEditor.selectedRange.location - 1 : _answertextfield.currentEditor.selectedRange.location, 0);
if ([obj.object isEqual:_answertextfield]) {
_currentrange = NSMakeRange(_answertextfield.currentEditor.selectedRange.location, 0);
_oldrange = NSMakeRange(_answertextfield.StartLocation, 0);
if (_oldrange.location > _currentrange.location) {
_oldrange = _currentrange;
_answertextfield.StartLocation = _oldrange.location;
}
bool usecurrentlocation = false;
if (_currentrange.location > 1) {
NSString *astring = [_answertextfield.stringValue substringWithRange:NSMakeRange(_currentrange.location-1, 1)];
NSString *bstring = [_answertextfield.stringValue substringWithRange:NSMakeRange(_currentrange.location-2, 1)];
if ([astring isEqualToString:bstring] && [astring caseInsensitiveCompare:@"n"] != NSOrderedSame && [bstring caseInsensitiveCompare:@"n"] != NSOrderedSame) {
usecurrentlocation = true;
}
}
@try{
NSRange substr = NSMakeRange(usecurrentlocation ? _currentrange.location-1 : _oldrange.location, usecurrentlocation ? 1 : _currentrange.location-_oldrange.location); NSString *replacementstr = [_answertextfield.stringValue substringWithRange:substr];
NSDictionary *proposed = [_kanainput checkString:_answertextfield.stringValue withReplacementString:replacementstr withOldRange:_oldrange withCurrentRange:_currentrange useCurrentRange:usecurrentlocation];
NSString *newstring = proposed[@"string"];
if (![newstring isEqualToString:_answertextfield.stringValue]) {
_answertextfield.stringValue = newstring;
_answertextfield.currentEditor.selectedRange = NSMakeRange(((NSNumber *)proposed[@"newposition"]).intValue, 0);
_currentrange = NSMakeRange(_answertextfield.currentEditor.selectedRange.location, 0);
_answertextfield.StartLocation = usecurrentlocation ? _answertextfield.currentEditor.selectedRange.location - 1 : _answertextfield.currentEditor.selectedRange.location;
_oldrange = NSMakeRange(_answertextfield.StartLocation, 0);
}
}
@catch (NSException *ex){}
_oldanswerstr = _answertextfield.stringValue;
}
}
@catch (NSException *ex){}
_oldanswerstr = _answertextfield.stringValue;
}
}

Expand Down
10 changes: 5 additions & 5 deletions KaniManabu-macOS/Window Controllers/XIBs/Base.lproj/Review.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="19455" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="19529" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="19455"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="19529"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand Down Expand Up @@ -38,7 +38,7 @@
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="514" height="384"/>
<rect key="screenRect" x="0.0" y="0.0" width="1920" height="1175"/>
<rect key="screenRect" x="0.0" y="0.0" width="2560" height="1415"/>
<value key="minSize" type="size" width="514" height="384"/>
<view key="contentView" wantsLayer="YES" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="514" height="384"/>
Expand Down Expand Up @@ -77,10 +77,10 @@
</textField>
</subviews>
</visualEffectView>
<textField focusRingType="none" verticalHuggingPriority="750" fixedFrame="YES" textCompletion="NO" translatesAutoresizingMaskIntoConstraints="NO" id="V0c-Ys-uF1">
<textField focusRingType="none" verticalHuggingPriority="750" fixedFrame="YES" textCompletion="NO" translatesAutoresizingMaskIntoConstraints="NO" id="V0c-Ys-uF1" customClass="TKMKanaInputTextField">
<rect key="frame" x="0.0" y="13" width="514" height="46"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" focusRingType="none" alignment="center" drawsBackground="YES" id="X2d-ZP-q78">
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" focusRingType="none" alignment="left" drawsBackground="YES" id="X2d-ZP-q78">
<font key="font" metaFont="system" size="24"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
Expand Down
Binary file added KaniManabu-macOS/~$redits.rtf
Binary file not shown.

0 comments on commit 2cfadb8

Please sign in to comment.