Skip to content

Commit

Permalink
use custom anotation window to show translations for macOS Sierra v10.12
Browse files Browse the repository at this point in the history
  • Loading branch information
dongyuwei committed Jan 2, 2017
1 parent a53946f commit 3a1f5a1
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 61 deletions.
18 changes: 0 additions & 18 deletions English.lproj/MainMenu.nib/classes.nib

This file was deleted.

59 changes: 59 additions & 0 deletions English.lproj/MainMenu.nib/designable.nib

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions English.lproj/MainMenu.nib/info.nib

This file was deleted.

Binary file modified English.lproj/MainMenu.nib/keyedobjects.nib
Binary file not shown.
6 changes: 6 additions & 0 deletions hallelujah.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */; };
8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; };
E93074B70A5C264700470842 /* InputMethodKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E93074B60A5C264700470842 /* InputMethodKit.framework */; };
F109B4911E1A83AF00AA7A41 /* AnnotationWinController.m in Sources */ = {isa = PBXBuildFile; fileRef = F109B48F1E1A83AF00AA7A41 /* AnnotationWinController.m */; };
F1CDA2AF1B551A55005086C6 /* InputController.h in Sources */ = {isa = PBXBuildFile; fileRef = F1CDA2A41B55180A005086C6 /* InputController.h */; };
F1CDA2B01B551A55005086C6 /* InputController.m in Sources */ = {isa = PBXBuildFile; fileRef = F1CDA2A51B55180A005086C6 /* InputController.m */; };
F1CDA2B11B551A55005086C6 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = F1CDA2A61B55180A005086C6 /* main.m */; };
Expand All @@ -41,6 +42,8 @@
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
8D1107320486CEB800E47090 /* hallelujah.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = hallelujah.app; sourceTree = BUILT_PRODUCTS_DIR; };
E93074B60A5C264700470842 /* InputMethodKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = InputMethodKit.framework; path = /System/Library/Frameworks/InputMethodKit.framework; sourceTree = "<absolute>"; };
F109B48F1E1A83AF00AA7A41 /* AnnotationWinController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AnnotationWinController.m; path = src/AnnotationWinController.m; sourceTree = "<group>"; };
F109B4901E1A83AF00AA7A41 /* AnnotationWinController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AnnotationWinController.h; path = src/AnnotationWinController.h; sourceTree = "<group>"; };
F1CDA2A41B55180A005086C6 /* InputController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = InputController.h; path = src/InputController.h; sourceTree = "<group>"; };
F1CDA2A51B55180A005086C6 /* InputController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = InputController.m; path = src/InputController.m; sourceTree = "<group>"; };
F1CDA2A61B55180A005086C6 /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = src/main.m; sourceTree = "<group>"; };
Expand All @@ -65,6 +68,8 @@
080E96DDFE201D6D7F000001 /* Classes */ = {
isa = PBXGroup;
children = (
F109B48F1E1A83AF00AA7A41 /* AnnotationWinController.m */,
F109B4901E1A83AF00AA7A41 /* AnnotationWinController.h */,
F1CDA2A41B55180A005086C6 /* InputController.h */,
F1CDA2A51B55180A005086C6 /* InputController.m */,
F1CDA2A61B55180A005086C6 /* main.m */,
Expand Down Expand Up @@ -218,6 +223,7 @@
F1CDA2B11B551A55005086C6 /* main.m in Sources */,
F1CDA2B41B551A55005086C6 /* PJTernarySearchTree.h in Sources */,
F1CDA2B51B551A55005086C6 /* PJTernarySearchTree.m in Sources */,
F109B4911E1A83AF00AA7A41 /* AnnotationWinController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
15 changes: 15 additions & 0 deletions src/AnnotationWinController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#import <Cocoa/Cocoa.h>

@interface AnnotationWinController : NSWindowController{

}

- (void)showWindow:(NSPoint)origin;

- (void)hideWindow;

- (void)setAnnotation:(NSString *)annotation;

+ (id)sharedController;

@end
53 changes: 53 additions & 0 deletions src/AnnotationWinController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#import "AnnotationWinController.h"

static AnnotationWinController *sharedController;

@interface AnnotationWinController ()
@property (retain, nonatomic) IBOutlet NSPanel *panel;
@property (retain, nonatomic) IBOutlet NSTextView *view;
@end

@implementation AnnotationWinController
@synthesize view;
@synthesize panel;

+ (id)sharedController
{
return sharedController;
}

-(void)awakeFromNib{
sharedController = self;
[[self panel] setStyleMask:NSBorderlessWindowMask];
[[self panel] setOpaque:NO];
[[self panel] setBackgroundColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.0]];
[self hideWindow];
}

- (void)showWindow:(NSPoint)origin {
NSSize size;
size.width = 170;
size.height = 258;
[[self panel] setMinSize:size];
[[self panel] setContentSize: size];
[[self panel] setAlphaValue: 0.9];

[[self panel] setFrameTopLeftPoint: origin];
[[self panel] orderFront:nil];
[[self panel] setLevel: CGShieldingWindowLevel() + 1];
[[self panel] setAutodisplay:YES];
}

- (void)hideWindow{
// [[self panel] orderOut:nil];
NSRect rect;
rect.size.width = 0;
rect.size.height = 0;
[[self panel] setFrame:rect display:NO];
}

- (void)setAnnotation:(NSString *)annotation{
[[self view] setString:annotation];
}

@end
2 changes: 2 additions & 0 deletions src/InputController.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import <Cocoa/Cocoa.h>
#import <InputMethodKit/InputMethodKit.h>
#import <AnnotationWinController.h>

@interface InputController : IMKInputController {
NSMutableString* _composedBuffer;
Expand All @@ -10,6 +11,7 @@
BOOL _is_cmd_mode;
NSUInteger _lastModifiers[2];
NSEventType _lastEventTypes[2];
AnnotationWinController* _annotationWin;
}

-(NSMutableString*)composedBuffer;
Expand Down
58 changes: 30 additions & 28 deletions src/InputController.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#import <CoreServices/CoreServices.h>

extern IMKCandidates* sharedCandidates;
extern IMKCandidates* subCandidates;
extern PJTernarySearchTree* trie;
extern NSMutableDictionary* wordsWithFrequency;
extern BOOL defaultEnglishMode;
Expand Down Expand Up @@ -158,7 +157,7 @@ -(void)reset{
[self setOriginalBuffer:@""];
_insertionIndex = 0;
[sharedCandidates hide];
[subCandidates hide];
[_annotationWin hideWindow];
}

-(NSMutableString*)composedBuffer{
Expand Down Expand Up @@ -283,40 +282,43 @@ - (void)candidateSelectionChanged:(NSAttributedString*)candidateString{

_insertionIndex = [candidateString length];

[self showSubCandidates: candidateString];
[self showAnnotation: candidateString];
}

-(void)showSubCandidates:(NSAttributedString*)candidateString{
NSInteger candidateIdentifier = [sharedCandidates selectedCandidate];
NSInteger subCandidateStringIdentifier = [sharedCandidates candidateStringIdentifier: candidateString];

if (candidateIdentifier == subCandidateStringIdentifier) {
NSArray* subList = [self getSubCandidates: candidateString];
if(subList && subList.count > 0){
NSString* phoneticSymbol = [self getPhoneticSymbolOfWord: candidateString];
if([phoneticSymbol length] > 0){
NSArray* list = @[phoneticSymbol];
[subCandidates setCandidateData: [list arrayByAddingObjectsFromArray:subList]];
}else{
[subCandidates setCandidateData: subList];
}

NSRect currentFrame = [sharedCandidates candidateFrame];
NSPoint windowInsertionPoint = NSMakePoint(NSMaxX(currentFrame), NSMaxY(currentFrame));
[subCandidates setCandidateFrameTopLeft:windowInsertionPoint];


[sharedCandidates attachChild:subCandidates toCandidate:(NSInteger)candidateIdentifier type:kIMKSubList];
[sharedCandidates showChild];
- (void)activateServer:(id)sender {
if (_annotationWin == nil){
_annotationWin = [AnnotationWinController sharedController];
}
}

- (void)deactivateServer:(id)sender {
[_annotationWin hideWindow];
}

-(void)showAnnotation:(NSAttributedString*)candidateString{
NSArray* subList = [self getTranslations: candidateString];
if(subList && subList.count > 0){
NSString* translations;
NSString* phoneticSymbol = [self getPhoneticSymbolOfWord: candidateString];
if([phoneticSymbol length] > 0){
NSArray* list = @[phoneticSymbol];
translations = [[list arrayByAddingObjectsFromArray:subList] componentsJoinedByString: @"\n"];
}else{
[subCandidates hide];
translations = [subList componentsJoinedByString: @"\n"];
}
NSRect currentFrame = [sharedCandidates candidateFrame];
NSRect tempRect;
[_currentClient attributesForCharacterIndex:0 lineHeightRectangle:&tempRect];
NSPoint windowInsertionPoint = NSMakePoint(NSMinX(tempRect), NSMinY(tempRect));
windowInsertionPoint.x = windowInsertionPoint.x + currentFrame.size.width;
[_annotationWin setAnnotation: translations];
[_annotationWin showWindow: windowInsertionPoint];
}else{
[subCandidates hide];
[_annotationWin hideWindow];
}
}

-(NSArray*)getSubCandidates: (NSAttributedString*)candidateString{
-(NSArray*)getTranslations: (NSAttributedString*)candidateString{
return translationes[[[candidateString string] lowercaseString]];
}

Expand Down
4 changes: 1 addition & 3 deletions src/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
const NSString* kConnectionName = @"Hallelujah_1_Connection";
IMKServer* server;
IMKCandidates* sharedCandidates;
IMKCandidates* subCandidates;
PJTernarySearchTree* trie;
NSMutableDictionary* wordsWithFrequency;
BOOL defaultEnglishMode;
Expand Down Expand Up @@ -70,7 +69,6 @@ int main(int argc, char *argv[])
bundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]];

sharedCandidates = [[IMKCandidates alloc] initWithServer:server panelType:kIMKSingleColumnScrollingCandidatePanel];
subCandidates = [[IMKCandidates alloc] initWithServer:nil panelType:kIMKSingleColumnScrollingCandidatePanel];

if (!sharedCandidates){
NSLog(@"Fatal error: Cannot initialize shared candidate panel with connection %@.", kConnectionName);
Expand All @@ -88,4 +86,4 @@ int main(int argc, char *argv[])
[[NSApplication sharedApplication] run];

return 0;
}
}

0 comments on commit 3a1f5a1

Please sign in to comment.