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 cdd45ee
Show file tree
Hide file tree
Showing 23 changed files with 1,660 additions and 1,129 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
24 changes: 12 additions & 12 deletions SquirrelApplicationDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,35 @@ 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)
@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);
89 changes: 63 additions & 26 deletions SquirrelApplicationDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,64 @@ - (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];
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];
}
}

Expand Down Expand Up @@ -241,26 +275,29 @@ - (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) {
error:nil];
if (previousLaunch.timeIntervalSinceNow >= -2) {
detected = YES;
}
}
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
89 changes: 48 additions & 41 deletions SquirrelConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,33 @@

@interface SquirrelOptionSwitcher : NSObject

@property(nonatomic, strong, readonly) NSString* schemaId;
@property(nonatomic, strong, readonly) NSArray<NSString*>* optionNames;
@property(nonatomic, strong, readonly) NSArray<NSString*>* optionStates;
@property(nonatomic, strong, readonly)
@property(nonatomic, strong, readonly, nonnull) NSString* schemaId;
@property(nonatomic, strong, readonly, nullable)
NSArray<NSString*>* optionNames;
@property(nonatomic, strong, readonly, nullable)
NSArray<NSString*>* optionStates;
@property(nonatomic, strong, readonly, nullable)
NSDictionary<NSString*, NSArray<NSString*>*>* optionGroups;
@property(nonatomic, strong, readonly)
@property(nonatomic, strong, readonly, nullable)
NSDictionary<NSString*, NSString*>* switcher;

- (instancetype)initWithSchemaId:(NSString*)schemaId
switcher:(NSDictionary<NSString*, NSString*>*)switcher
optionGroups:(NSDictionary<NSString*, NSArray<NSString*>*>*)
optionGroups;
- (instancetype _Nonnull)
initWithSchemaId:(NSString* _Nonnull)schemaId
switcher:(NSDictionary<NSString*, NSString*>* _Nullable)switcher
optionGroups:(NSDictionary<NSString*, NSArray<NSString*>*>* _Nullable)
optionGroups;

- (instancetype)initWithSchemaId:(NSString*)schemaId;
- (instancetype _Nonnull)initWithSchemaId:(NSString* _Nonnull)schemaId;

// return whether switcher options has been successfully updated
- (BOOL)updateSwitcher:(NSDictionary<NSString*, NSString*>*)switcher;
- (BOOL)updateSwitcher:(NSDictionary<NSString*, NSString*>* _Nullable)switcher;

- (BOOL)updateGroupState:(NSString*)optionState ofOption:(NSString*)optionName;
- (BOOL)updateGroupState:(NSString* _Nullable)optionState
ofOption:(NSString* _Nullable)optionName;

- (BOOL)containsOption:(NSString*)optionName;
- (BOOL)containsOption:(NSString* _Nonnull)optionName;

- (NSMutableDictionary<NSString*, NSString*>*)mutableSwitcher;
- (NSMutableDictionary<NSString*, NSString*>* _Nullable)mutableSwitcher;

@end // SquirrelOptionSwitcher

Expand All @@ -34,44 +38,47 @@ typedef NSDictionary<NSString*, NSNumber*> SquirrelAppOptions;
typedef NSMutableDictionary<NSString*, NSNumber*> SquirrelMutableAppOptions;

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

- (BOOL)openBaseConfig;
- (BOOL)openWithSchemaId:(NSString*)schemaId baseConfig:(SquirrelConfig*)config;
- (BOOL)openUserConfig:(NSString*)configId;
- (BOOL)openWithConfigId:(NSString*)configId;
- (BOOL)openWithSchemaId:(NSString* _Nonnull)schemaId
baseConfig:(SquirrelConfig* _Nullable)config;
- (BOOL)openUserConfig:(NSString* _Nonnull)configId;
- (BOOL)openWithConfigId:(NSString* _Nonnull)configId;
- (void)close;

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

- (BOOL)setBool:(bool)value forOption:(NSString*)option;
- (BOOL)setInt:(int)value forOption:(NSString*)option;
- (BOOL)setDouble:(double)value forOption:(NSString*)option;
- (BOOL)setString:(NSString*)value forOption:(NSString*)option;
- (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*)option;
- (int)getIntForOption:(NSString*)option;
- (double)getDoubleForOption:(NSString*)option;
- (double)getDoubleForOption:(NSString*)option
applyConstraint:(double (*)(double param))func;
- (BOOL)getBoolForOption:(NSString* _Nonnull)option;
- (int)getIntForOption:(NSString* _Nonnull)option;
- (double)getDoubleForOption:(NSString* _Nonnull)option;
- (double)getDoubleForOption:(NSString* _Nonnull)option
applyConstraint:(double (*_Nonnull)(double param))func;

- (NSNumber*)getOptionalBoolForOption:(NSString*)option;
- (NSNumber*)getOptionalIntForOption:(NSString*)option;
- (NSNumber*)getOptionalDoubleForOption:(NSString*)option;
- (NSNumber*)getOptionalDoubleForOption:(NSString*)option
applyConstraint:(double (*)(double param))func;
- (NSNumber* _Nullable)getOptionalBoolForOption:(NSString* _Nonnull)option;
- (NSNumber* _Nullable)getOptionalIntForOption:(NSString* _Nonnull)option;
- (NSNumber* _Nullable)getOptionalDoubleForOption:(NSString* _Nonnull)option;
- (NSNumber* _Nullable)getOptionalDoubleForOption:(NSString* _Nonnull)option
applyConstraint:
(double (*_Nonnull)(double param))func;

- (NSString*)getStringForOption:(NSString*)option;
- (NSString* _Nullable)getStringForOption:(NSString* _Nonnull)option;
// 0xaabbggrr or 0xbbggrr
- (NSColor*)getColorForOption:(NSString*)option;
- (NSColor* _Nullable)getColorForOption:(NSString* _Nonnull)option;
// file path (absolute or relative to ~/Library/Rime)
- (NSImage*)getImageForOption:(NSString*)option;
- (NSImage* _Nullable)getImageForOption:(NSString* _Nonnull)option;

- (NSUInteger)getListSizeForOption:(NSString*)option;
- (NSArray<NSString*>*)getListForOption:(NSString*)option;
- (NSUInteger)getListSizeForOption:(NSString* _Nonnull)option;
- (NSArray<NSString*>* _Nullable)getListForOption:(NSString* _Nonnull)option;

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

@end // SquirrelConfig
3 changes: 1 addition & 2 deletions SquirrelConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,8 @@ - (NSUInteger)getListSizeForOption:(NSString*)option {
return nil;
}
NSMutableArray* strList = [[NSMutableArray alloc] init];
while (rime_get_api()->config_next(&iterator)) {
while (rime_get_api()->config_next(&iterator))
[strList addObject:[self getStringForOption:@(iterator.path)]];
}
rime_get_api()->config_end(&iterator);
return strList;
}
Expand Down
Loading

0 comments on commit cdd45ee

Please sign in to comment.