Skip to content

Commit

Permalink
formatting, nullability & speed up with C array
Browse files Browse the repository at this point in the history
Update SquirrelPanel.m
  • Loading branch information
groverlynn committed Mar 8, 2024
1 parent 4cc3894 commit 0e829df
Show file tree
Hide file tree
Showing 24 changed files with 2,056 additions and 1,982 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"symbols" : [
{
"filename" : "chevron.down.svg",
"filename" : "arrow.down.and.line.horizontal.and.arrow.up.svg",
"idiom" : "universal"
}
]
Expand Down
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.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"symbols" : [
{
"filename" : "chevron.up.svg",
"filename" : "arrow.up.and.line.horizontal.and.arrow.down.svg",
"idiom" : "universal"
}
]
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 0 additions & 160 deletions Assets.xcassets/Symbols/chevron.down.symbolset/chevron.down.svg

This file was deleted.

160 changes: 0 additions & 160 deletions Assets.xcassets/Symbols/chevron.up.symbolset/chevron.up.svg

This file was deleted.

7 changes: 5 additions & 2 deletions Squirrel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@
7BDB21211C6EF1BE0025E351 /* SquirrelConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SquirrelConfig.h; sourceTree = "<group>"; };
7BDB21221C6EF1BE0025E351 /* SquirrelConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SquirrelConfig.m; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
A44571AB0DBF42C200F793F9 /* macos_keycode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = macos_keycode.h; sourceTree = "<group>"; };
A44571AB0DBF42C200F793F9 /* macos_keycode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = macos_keycode.h; sourceTree = "<group>"; usesTabs = 0; };
A47C48DE105E8CE8006D528B /* macos_keycode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = macos_keycode.m; sourceTree = "<group>"; };
A4B8E1B20F645B870094E08B /* Carbon.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Carbon.framework; path = /System/Library/Frameworks/Carbon.framework; sourceTree = "<absolute>"; };
A4FC48CA0F6530EF0069BE81 /* en */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -507,7 +507,8 @@
29B97313FDCFA39411CA2CEA /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1220;
BuildIndependentTargetsInParallel = YES;
LastUpgradeCheck = 1530;
};
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "Squirrel" */;
compatibilityVersion = "Xcode 10.0";
Expand Down Expand Up @@ -704,6 +705,7 @@
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_APPICON_NAME = RimeIcon;
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
Expand Down Expand Up @@ -760,6 +762,7 @@
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_APPICON_NAME = RimeIcon;
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
Expand Down
25 changes: 12 additions & 13 deletions SquirrelApplicationDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,34 @@ typedef NS_ENUM(NSUInteger, SquirrelNotificationPolicy) {
kShowNotificationsAlways = 2
};

@property(nonatomic, copy) IBOutlet NSMenu* menu;
@property(nonatomic, strong) IBOutlet SquirrelPanel* panel;
@property(nonatomic, strong) IBOutlet id updater;
@property(nonatomic, weak, nullable) IBOutlet NSMenu *menu;
@property(nonatomic, weak, nullable) IBOutlet SquirrelPanel *panel;
@property(nonatomic, weak, nullable) IBOutlet id updater;

@property(nonatomic, strong, readonly) SquirrelConfig* config;
@property(nonatomic, strong, readonly, nullable) SquirrelConfig *config;
@property(nonatomic, readonly) SquirrelNotificationPolicy showNotifications;

- (IBAction)deploy:(id)sender;
- (IBAction)syncUserData:(id)sender;
- (IBAction)configure:(id)sender;
- (IBAction)openWiki:(id)sender;
- (IBAction)deploy:(id _Nullable)sender;
- (IBAction)syncUserData:(id _Nullable)sender;
- (IBAction)configure:(id _Nullable)sender;
- (IBAction)openWiki:(id _Nullable)sender;

- (void)setupRime;
- (void)startRimeWithFullCheck:(BOOL)fullCheck;
- (void)loadSettings;
- (void)loadSchemaSpecificSettings:(NSString*)schemaId
- (void)loadSchemaSpecificSettings:(NSString * _Nonnull)schemaId
withRimeSession:(RimeSessionId)sessionId;
- (void)loadSchemaSpecificLabels:(NSString*)schemaId;
- (void)loadSchemaSpecificLabels:(NSString * _Nonnull)schemaId;

@property(nonatomic, readonly) BOOL problematicLaunchDetected;

@end // SquirrelApplicationDelegate

@interface NSApplication (SquirrelApp)

@property(nonatomic, strong, readonly)
SquirrelApplicationDelegate* squirrelAppDelegate;
@property(nonatomic, strong, readonly, nonnull) SquirrelApplicationDelegate *squirrelAppDelegate;

@end // NSApplication (SquirrelApp)

// also used in main.m
extern void show_notification(const char* msg_text);
extern void show_notification(const char * _Nonnull msg_text);
137 changes: 78 additions & 59 deletions SquirrelApplicationDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,63 @@ - (IBAction)syncUserData:(id)sender {
}

- (IBAction)configure:(id)sender {
[[NSWorkspace sharedWorkspace]
openURL:[NSURL URLWithString:[@"file://"
stringByAppendingString:
(@"~/Library/Rime")
.stringByStandardizingPath]]];
[NSWorkspace.sharedWorkspace openURL:
[NSURL fileURLWithPath:@"~/Library/Rime/".stringByExpandingTildeInPath
isDirectory:YES]];
}

- (IBAction)openWiki:(id)sender {
[NSWorkspace.sharedWorkspace openURL:[NSURL URLWithString:kRimeWikiURL]];
}

void show_notification(const char* msg_text) {
@autoreleasepool {
id notification = [[NSClassFromString(@"NSUserNotification") alloc] init];
[notification performSelector:@selector(setTitle:)
withObject:NSLocalizedString(@"Squirrel", nil)];
[notification performSelector:@selector(setSubtitle:)
withObject:NSLocalizedString(@(msg_text), nil)];
id notificationCenter = [(id)NSClassFromString(@"NSUserNotificationCenter")
performSelector:@selector(defaultUserNotificationCenter)];
[notificationCenter
performSelector:@selector(removeAllDeliveredNotifications)];
[notificationCenter performSelector:@selector(deliverNotification:)
withObject:notification];
void show_notification(const char *msg_text) {
if (@available(macOS 10.14, *)) {
UNUserNotificationCenter *center = UNUserNotificationCenter.currentNotificationCenter;
[center requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionProvisional
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (error) {
NSLog(@"User notification authorization error: %@", error.debugDescription);
}
}];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if ((settings.authorizationStatus == UNAuthorizationStatusAuthorized ||
settings.authorizationStatus == UNAuthorizationStatusProvisional) &&
(settings.alertSetting == UNNotificationSettingEnabled)) {
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = NSLocalizedString(@"Squirrel", nil);
content.subtitle = NSLocalizedString(@(msg_text), nil);
if (@available(macOS 12.0, *)) {
content.interruptionLevel = UNNotificationInterruptionLevelActive;
}
UNNotificationRequest *request =
[UNNotificationRequest requestWithIdentifier:@"SquirrelNotification"
content:content
trigger:nil];
[center addNotificationRequest:request
withCompletionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"User notification request error: %@", error.debugDescription);
}
}];
}
}];
} else {
NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = NSLocalizedString(@"Squirrel", nil);
notification.subtitle = NSLocalizedString(@(msg_text), nil);

NSUserNotificationCenter *notificationCenter =
NSUserNotificationCenter.defaultUserNotificationCenter;
[notificationCenter removeAllDeliveredNotifications];
[notificationCenter deliverNotification:notification];
}
}

static void show_status(const char* msg_text_long, const char* msg_text_short) {
NSString* msgLong = msg_text_long ? @(msg_text_long) : nil;
NSString* msgShort =
msg_text_short
? @(msg_text_short)
: [msgLong substringWithRange:
[msgLong rangeOfComposedCharacterSequenceAtIndex:0]];
static void show_status(const char *msg_text_long, const char *msg_text_short) {
NSString *msgLong = msg_text_long ? @(msg_text_long) : nil;
NSString *msgShort = msg_text_short ? @(msg_text_short) :
[msgLong substringWithRange:
[msgLong rangeOfComposedCharacterSequenceAtIndex:0]];
[NSApp.squirrelAppDelegate.panel updateStatusLong:msgLong
statusShort:msgShort];
}
Expand Down Expand Up @@ -98,17 +121,13 @@ static void notification_handler(void* context_object,
}
}
if (app_delegate.showNotifications != kShowNotificationsNever) {
RimeStringSlice state_label_long =
rime_get_api()->get_state_label_abbreviated(session_id, option_name,
state, False);
RimeStringSlice state_label_long =
rime_get_api()->get_state_label_abbreviated(session_id, option_name, state, False);
RimeStringSlice state_label_short =
rime_get_api()->get_state_label_abbreviated(session_id, option_name,
state, True);
rime_get_api()->get_state_label_abbreviated(session_id, option_name, state, True);
if (state_label_long.str || state_label_short.str) {
const char* short_message =
state_label_short.length < strlen(state_label_short.str)
? NULL
: state_label_short.str;
const char *short_message =
state_label_short.length < strlen(state_label_short.str) ? NULL : state_label_short.str;
show_status(state_label_long.str, short_message);
}
}
Expand Down Expand Up @@ -155,15 +174,14 @@ - (void)shutdownRime {
rime_get_api()->finalize();
}

SquirrelOptionSwitcher* updateOptionSwitcher(
SquirrelOptionSwitcher* optionSwitcher,
RimeSessionId sessionId) {
NSMutableDictionary* switcher = optionSwitcher.mutableSwitcher;
NSSet* prevStates = [NSSet setWithArray:optionSwitcher.optionStates];
for (NSString* state in prevStates) {
NSString* updatedState;
NSArray* optionGroup = [optionSwitcher.switcher allKeysForObject:state];
for (NSString* option in optionGroup) {
SquirrelOptionSwitcher *updateOptionSwitcher(SquirrelOptionSwitcher *optionSwitcher,
RimeSessionId sessionId) {
NSMutableDictionary *switcher = optionSwitcher.mutableSwitcher;
NSSet *prevStates = [NSSet setWithArray:optionSwitcher.optionStates];
for (NSString *state in prevStates) {
NSString *updatedState;
NSArray *optionGroup = [optionSwitcher.switcher allKeysForObject:state];
for (NSString *option in optionGroup) {
if (rime_get_api()->get_option(sessionId, option.UTF8String)) {
updatedState = option;
break;
Expand Down Expand Up @@ -212,8 +230,8 @@ - (void)loadSchemaSpecificSettings:(NSString*)schemaId
self.panel.optionSwitcher = updateOptionSwitcher(optionSwitcher, sessionId);
[self.panel loadConfig:schema];
} else {
self.panel.optionSwitcher =
[[SquirrelOptionSwitcher alloc] initWithSchemaId:schemaId];
self.panel.optionSwitcher = [[SquirrelOptionSwitcher alloc]
initWithSchemaId:schemaId];
[self.panel loadConfig:self.config];
}
[schema close];
Expand Down Expand Up @@ -241,26 +259,27 @@ - (void)loadSchemaSpecificLabels:(NSString*)schemaId {
// prevent freezing the system
- (BOOL)problematicLaunchDetected {
BOOL detected = NO;
NSString* logfile = [NSTemporaryDirectory()
stringByAppendingPathComponent:@"squirrel_launch.dat"];
NSURL *logfile = [[NSURL fileURLWithPath:NSTemporaryDirectory()
isDirectory:YES]
URLByAppendingPathComponent:@"squirrel_launch.dat"];
// NSLog(@"[DEBUG] archive: %@", logfile);
NSData* archive = [NSData dataWithContentsOfFile:logfile
options:NSDataReadingUncached
error:nil];
NSData *archive = [NSData dataWithContentsOfURL:logfile
options:NSDataReadingUncached
error:nil];
if (archive) {
NSDate* previousLaunch =
[NSKeyedUnarchiver unarchivedObjectOfClass:NSDate.class
fromData:archive
error:NULL];
if (previousLaunch && previousLaunch.timeIntervalSinceNow >= -2) {
NSDate *previousLaunch = [NSKeyedUnarchiver unarchivedObjectOfClass:NSDate.class
fromData:archive
error:nil];
if (previousLaunch.timeIntervalSinceNow >= -2) {
detected = YES;
}
}
NSDate* now = [NSDate date];
NSData* record = [NSKeyedArchiver archivedDataWithRootObject:now
NSDate *now = [NSDate date];
NSData *record = [NSKeyedArchiver archivedDataWithRootObject:now
requiringSecureCoding:NO
error:NULL];
[record writeToFile:logfile atomically:NO];
error:nil];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingToURL:logfile error:nil];
[fileHandle writeData:record];
return detected;
}

Expand Down
Loading

0 comments on commit 0e829df

Please sign in to comment.