Skip to content

Commit

Permalink
use separate textviews
Browse files Browse the repository at this point in the history
  • Loading branch information
groverlynn committed May 23, 2024
1 parent a1dc0a9 commit b209906
Show file tree
Hide file tree
Showing 14 changed files with 2,417 additions and 2,226 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions Squirrel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,11 @@
C01FCF4B08A954540054247B /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = YES;
CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES;
CLANG_ANALYZER_SECURITY_INSECUREAPI_RAND = YES;
CLANG_ANALYZER_SECURITY_INSECUREAPI_STRCPY = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++17";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_OBJC_ARC = YES;
CODE_SIGN_IDENTITY = "-";
Expand Down Expand Up @@ -695,6 +700,11 @@
C01FCF4C08A954540054247B /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = YES;
CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES;
CLANG_ANALYZER_SECURITY_INSECUREAPI_RAND = YES;
CLANG_ANALYZER_SECURITY_INSECUREAPI_STRCPY = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++17";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_OBJC_ARC = YES;
CODE_SIGN_IDENTITY = "-";
Expand Down
5 changes: 2 additions & 3 deletions SquirrelApplicationDelegate.hh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import <Cocoa/Cocoa.h>
#import "rime_api.h"

@class SquirrelConfig;
@class SquirrelPanel;
Expand All @@ -8,14 +9,12 @@
// outlet of NSApp's instance
@interface SquirrelApplicationDelegate : NSObject <NSApplicationDelegate>

typedef NS_ENUM(NSUInteger, SquirrelNotificationPolicy) {
typedef NS_CLOSED_ENUM(NSUInteger, SquirrelNotificationPolicy) {
kShowNotificationsNever = 0,
kShowNotificationsWhenAppropriate = 1,
kShowNotificationsAlways = 2
};

typedef uintptr_t RimeSessionId;

@property(nonatomic, weak, nullable) IBOutlet NSMenu* menu;
@property(nonatomic, weak, nullable) IBOutlet SquirrelPanel* panel;
@property(nonatomic, weak, nullable) IBOutlet id updater;
Expand Down
63 changes: 29 additions & 34 deletions SquirrelApplicationDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#import "SquirrelConfig.hh"
#import "SquirrelPanel.hh"
#import "macos_keycode.hh"
#import "rime_api.h"
#import <UserNotifications/UserNotifications.h>

static NSString* const kRimeWikiURL = @"https://github.com/rime/home/wiki";
static const CFStringRef kBundleId = CFSTR("im.rime.inputmethod.Squirrel");

@implementation SquirrelApplicationDelegate {
int _switcherKeyEquivalent;
Expand All @@ -15,7 +15,7 @@ @implementation SquirrelApplicationDelegate {

- (IBAction)showSwitcher:(id)sender {
NSLog(@"Show Switcher");
if (_switcherKeyEquivalent) {
if (_switcherKeyEquivalent != 0) {
RimeSessionId session = [sender unsignedLongValue];
rime_get_api()->process_key(session, _switcherKeyEquivalent,
_switcherKeyModifierMask);
Expand Down Expand Up @@ -45,14 +45,14 @@ - (IBAction)openWiki:(id)sender {
[NSWorkspace.sharedWorkspace openURL:[NSURL URLWithString:kRimeWikiURL]];
}

void show_notification(const char* msg_text) {
extern void show_notification(const char* msg_text) {
UNUserNotificationCenter* center =
UNUserNotificationCenter.currentNotificationCenter;
[center requestAuthorizationWithOptions:UNAuthorizationOptionAlert |
UNAuthorizationOptionProvisional
completionHandler:^(BOOL granted,
NSError* _Nullable error) {
if (error) {
if (error != nil) {
NSLog(@"User notification authorization error: %@",
error.debugDescription);
}
Expand All @@ -63,7 +63,7 @@ void show_notification(const char* msg_text) {
settings.authorizationStatus == UNAuthorizationStatusProvisional) &&
(settings.alertSetting == UNNotificationSettingEnabled)) {
UNMutableNotificationContent* content =
[[UNMutableNotificationContent alloc] init];
UNMutableNotificationContent.alloc.init;
content.title = NSLocalizedString(@"Squirrel", nil);
content.subtitle = NSLocalizedString(@(msg_text), nil);
if (@available(macOS 12.0, *)) {
Expand All @@ -75,7 +75,7 @@ void show_notification(const char* msg_text) {
trigger:nil];
[center addNotificationRequest:request
withCompletionHandler:^(NSError* _Nullable error) {
if (error) {
if (error != nil) {
NSLog(@"User notification request error: %@",
error.debugDescription);
}
Expand All @@ -85,9 +85,9 @@ void show_notification(const char* msg_text) {
}

static void show_status(const char* msg_text_long, const char* msg_text_short) {
NSString* msgLong = msg_text_long ? @(msg_text_long) : nil;
NSString* msgLong = msg_text_long != NULL ? @(msg_text_long) : nil;
NSString* msgShort =
msg_text_short
msg_text_short != NULL
? @(msg_text_short)
: [msgLong substringWithRange:
[msgLong rangeOfComposedCharacterSequenceAtIndex:0]];
Expand All @@ -99,37 +99,34 @@ static void notification_handler(void* context_object,
RimeSessionId session_id,
const char* message_type,
const char* message_value) {
if (!strcmp(message_type, "deploy")) {
if (!strcmp(message_value, "start")) {
if (strcmp(message_type, "deploy") == 0) {
if (strcmp(message_value, "start") == 0) {
show_notification("deploy_start");
} else if (!strcmp(message_value, "success")) {
} else if (strcmp(message_value, "success") == 0) {
show_notification("deploy_success");
} else if (!strcmp(message_value, "failure")) {
} else if (strcmp(message_value, "failure") == 0) {
show_notification("deploy_failure");
}
return;
}
SquirrelApplicationDelegate* app_delegate = (__bridge id)context_object;
// schema change
if (!strcmp(message_type, "schema") &&
if (strcmp(message_type, "schema") == 0 &&
app_delegate.showNotifications != kShowNotificationsNever) {
const char* schema_name = strchr(message_value, '/');
if (schema_name) {
if (schema_name != NULL) {
++schema_name;
show_status(schema_name, schema_name);
}
return;
}
// option change
if (!strcmp(message_type, "option") && app_delegate) {
if (strcmp(message_type, "option") == 0 && app_delegate) {
Bool state = message_value[0] != '!';
const char* option_name = message_value + !state;
BOOL updateScriptVariant = [app_delegate.panel.optionSwitcher
updateCurrentScriptVariant:@(message_value)];
BOOL updateStyleOptions = NO;
BOOL updateScriptVariant = NO;
if ([app_delegate.panel.optionSwitcher
updateCurrentScriptVariant:@(message_value)]) {
updateScriptVariant = YES;
}
if ([app_delegate.panel.optionSwitcher updateGroupState:@(message_value)
ofOption:@(option_name)]) {
updateStyleOptions = YES;
Expand All @@ -148,7 +145,7 @@ static void notification_handler(void* context_object,
RimeStringSlice state_label_short =
rime_get_api()->get_state_label_abbreviated(session_id, option_name,
state, True);
if (state_label_long.str || state_label_short.str) {
if (state_label_long.str != NULL || state_label_short.str != NULL) {
const char* short_message =
state_label_short.length < strlen(state_label_short.str)
? NULL
Expand Down Expand Up @@ -208,7 +205,7 @@ - (void)loadSettings {
if ([defaultConfig openWithConfigId:@"default"]) {
NSString* hotkey =
[defaultConfig getStringForOption:@"switcher/hotkeys/@0"];
if (hotkey) {
if (hotkey != nil) {
NSArray<NSString*>* keys = [hotkey componentsSeparatedByString:@"+"];
for (NSUInteger i = 0; i < keys.count - 1; ++i) {
_switcherKeyModifierMask |=
Expand Down Expand Up @@ -285,7 +282,7 @@ - (BOOL)problematicLaunchDetected {
NSData* archive = [NSData dataWithContentsOfURL:logfile
options:NSDataReadingUncached
error:nil];
if (archive) {
if (archive != nil) {
NSDate* previousLaunch =
[NSKeyedUnarchiver unarchivedObjectOfClass:NSDate.class
fromData:archive
Expand All @@ -294,8 +291,7 @@ - (BOOL)problematicLaunchDetected {
detected = YES;
}
}
NSDate* now = [NSDate date];
NSData* record = [NSKeyedArchiver archivedDataWithRootObject:now
NSData* record = [NSKeyedArchiver archivedDataWithRootObject:NSDate.date
requiringSecureCoding:NO
error:nil];
[record writeToURL:logfile atomically:NO];
Expand Down Expand Up @@ -325,20 +321,19 @@ - (NSApplicationTerminateReply)applicationShouldTerminate:
}

- (void)inputSourceChanged:(NSNotification*)aNotification {
CFStringRef inputSource = (CFStringRef)TISGetInputSourceProperty(
TISCopyCurrentKeyboardInputSource(), kTISPropertyInputSourceID);
CFStringRef bundleId = CFBundleGetIdentifier(CFBundleGetMainBundle());
if (!CFStringHasPrefix(inputSource, bundleId)) {
_isCurrentInputMethod = NO;
if (CFStringRef inputSource = (CFStringRef)TISGetInputSourceProperty(
TISCopyCurrentKeyboardInputSource(), kTISPropertyInputSourceID)) {
if (!CFStringHasPrefix(inputSource, kBundleId)) {
_isCurrentInputMethod = NO;
}
}
}

// add an awakeFromNib item so that we can set the action method. Note that
// any menuItems without an action will be disabled when displayed in the Text
// Input Menu.
- (void)awakeFromNib {
NSNotificationCenter* center =
[NSWorkspace sharedWorkspace].notificationCenter;
NSNotificationCenter* center = NSWorkspace.sharedWorkspace.notificationCenter;
[center addObserver:self
selector:@selector(workspaceWillPowerOff:)
name:NSWorkspaceWillPowerOffNotification
Expand All @@ -365,8 +360,8 @@ - (void)awakeFromNib {
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
[NSNotificationCenter.defaultCenter removeObserver:self];
[NSDistributedNotificationCenter.defaultCenter removeObserver:self];
[_panel hide];
}

Expand Down
22 changes: 14 additions & 8 deletions SquirrelConfig.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#import <Cocoa/Cocoa.h>

typedef uintptr_t RimeSessionId;
#import <rime_api.h>

__attribute__((objc_direct_members))
@interface SquirrelOptionSwitcher : NSObject
Expand Down Expand Up @@ -39,11 +38,18 @@ __attribute__((objc_direct_members))
@end // SquirrelOptionSwitcher

__attribute__((objc_direct_members))
@interface SquirrelConfig : NSObject
@interface SquirrelAppOptions : NSDictionary<NSString*, NSNumber*>

typedef NSDictionary<NSString*, NSNumber*> SquirrelAppOptions;
- (BOOL)boolValueForKey:(NSString* _Nonnull)key;
- (int)intValueForKey:(NSString* _Nonnull)key;
- (double)doubleValueForKey:(NSString* _Nonnull)key;

@property(nonatomic, strong, readonly, nonnull) NSString* schemaId;
@end // SquirrelAppOptions

__attribute__((objc_direct_members))
@interface SquirrelConfig : NSObject

@property(nonatomic, strong, readonly, nullable) NSString* schemaId;
@property(nonatomic, strong, nonnull) NSString* colorSpace;

- (BOOL)openBaseConfig;
Expand All @@ -55,13 +61,13 @@ typedef NSDictionary<NSString*, NSNumber*> SquirrelAppOptions;

- (BOOL)hasSection:(NSString* _Nonnull)section;

- (BOOL)setOption:(NSString* _Nonnull)option withBool:(bool)value;
- (BOOL)setOption:(NSString* _Nonnull)option withBool:(BOOL)value;
- (BOOL)setOption:(NSString* _Nonnull)option withInt:(int)value;
- (BOOL)setOption:(NSString* _Nonnull)option withDouble:(double)value;
- (BOOL)setOption:(NSString* _Nonnull)option
withString:(NSString* _Nonnull)value;

- (bool)getBoolForOption:(NSString* _Nonnull)option;
- (BOOL)getBoolForOption:(NSString* _Nonnull)option;
- (int)getIntForOption:(NSString* _Nonnull)option;
- (double)getDoubleForOption:(NSString* _Nonnull)option;
- (double)getDoubleForOption:(NSString* _Nonnull)option
Expand Down Expand Up @@ -101,7 +107,7 @@ typedef NSDictionary<NSString*, NSNumber*> SquirrelAppOptions;
- (NSUInteger)getListSizeForOption:(NSString* _Nonnull)option;
- (NSArray<NSString*>* _Nullable)getListForOption:(NSString* _Nonnull)option;

- (SquirrelOptionSwitcher* _Nullable)getOptionSwitcher;
- (SquirrelOptionSwitcher* _Nonnull)getOptionSwitcher;
- (SquirrelAppOptions* _Nonnull)getAppOptions:(NSString* _Nonnull)appName;

@end // SquirrelConfig
Loading

0 comments on commit b209906

Please sign in to comment.