Skip to content

Commit

Permalink
api
Browse files Browse the repository at this point in the history
  • Loading branch information
groverlynn committed Oct 17, 2023
1 parent 4bddc4f commit e8c984d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 94 deletions.
20 changes: 10 additions & 10 deletions SquirrelApplicationDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ - (IBAction)deploy:(id)sender
- (IBAction)syncUserData:(id)sender
{
NSLog(@"Sync user data");
rime_get_api()->sync_user_data();
RimeSyncUserData();
}

- (IBAction)configure:(id)sender
Expand Down Expand Up @@ -90,8 +90,8 @@ void notification_handler(void *context_object, RimeSessionId session_id,
}
}
if ([app_delegate enableNotifications]) {
RimeStringSlice state_label_long = rime_get_api()->get_state_label_abbreviated(session_id, option_name, state, NO);
RimeStringSlice state_label_short = rime_get_api()->get_state_label_abbreviated(session_id, option_name, state, YES);
RimeStringSlice state_label_long = RimeGetStateLabelAbbreviated(session_id, option_name, state, False);
RimeStringSlice state_label_short = RimeGetStateLabelAbbreviated(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;
show_status_message(state_label_long.str, short_message, message_type);
Expand All @@ -112,7 +112,7 @@ - (void)setupRime
NSLog(@"Error creating user data directory: %@", userDataDir);
}
}
rime_get_api()->set_notification_handler(notification_handler, (__bridge void *)(self));
RimeSetNotificationHandler(notification_handler, (__bridge void *)(self));
RIME_STRUCT(RimeTraits, squirrel_traits);
squirrel_traits.shared_data_dir = [NSBundle mainBundle].sharedSupportPath.UTF8String;
squirrel_traits.user_data_dir = userDataDir.UTF8String;
Expand All @@ -121,23 +121,23 @@ - (void)setupRime
squirrel_traits.distribution_version =
[[NSBundle mainBundle].infoDictionary[@"CFBundleVersion"] UTF8String];
squirrel_traits.app_name = "rime.squirrel";
rime_get_api()->setup(&squirrel_traits);
RimeSetup(&squirrel_traits);
}

- (void)startRimeWithFullCheck:(BOOL)fullCheck
{
NSLog(@"Initializing la rime...");
rime_get_api()->initialize(NULL);
RimeInitialize(NULL);
// check for configuration updates
if (rime_get_api()->start_maintenance((Bool)fullCheck)) {
if (RimeStartMaintenance((Bool)fullCheck)) {
// update squirrel config
rime_get_api()->deploy_config_file("squirrel.yaml", "config_version");
RimeDeployConfigFile("squirrel.yaml", "config_version");
}
}

- (void)shutdownRime {
[_config close];
rime_get_api()->finalize();
RimeFinalize();
}

- (void)loadSettings {
Expand Down Expand Up @@ -235,7 +235,7 @@ - (void)rimeNeedsSync:(NSNotification *)aNotification
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
NSLog(@"Squirrel is quitting.");
rime_get_api()->cleanup_all_sessions();
RimeCleanupAllSessions();
return NSTerminateNow;
}

Expand Down
46 changes: 23 additions & 23 deletions SquirrelConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ - (NSString *)schemaId {

- (BOOL)openBaseConfig {
[self close];
_isOpen = !!rime_get_api()->config_open("squirrel", &_config);
_isOpen = !!RimeConfigOpen("squirrel", &_config);
return _isOpen;
}

- (BOOL)openWithSchemaId:(NSString *)schemaId
baseConfig:(SquirrelConfig *)baseConfig {
[self close];
_isOpen = !!rime_get_api()->schema_open(schemaId.UTF8String, &_config);
_isOpen = !!RimeSchemaOpen(schemaId.UTF8String, &_config);
if (_isOpen) {
_schemaId = schemaId;
_baseConfig = baseConfig;
Expand All @@ -121,19 +121,19 @@ - (BOOL)openWithSchemaId:(NSString *)schemaId

- (BOOL)openUserConfig:(NSString *)configId {
[self close];
_isOpen = !!rime_get_api()->user_config_open(configId.UTF8String, &_config);
_isOpen = !!RimeUserConfigOpen(configId.UTF8String, &_config);
return _isOpen;
}

- (BOOL)openWithConfigId:(NSString *)configId {
[self close];
_isOpen = !!rime_get_api()->config_open(configId.UTF8String, &_config);
_isOpen = !!RimeConfigOpen(configId.UTF8String, &_config);
return _isOpen;
}

- (void)close {
if (_isOpen) {
rime_get_api()->config_close(&_config);
RimeConfigClose(&_config);
_baseConfig = nil;
_isOpen = NO;
}
Expand All @@ -142,8 +142,8 @@ - (void)close {
- (BOOL)hasSection:(NSString *)section {
if (_isOpen) {
RimeConfigIterator iterator = {0};
if (rime_get_api()->config_begin_map(&iterator, &_config, section.UTF8String)) {
rime_get_api()->config_end(&iterator);
if (RimeConfigBeginMap(&iterator, &_config, section.UTF8String)) {
RimeConfigEnd(&iterator);
return YES;
}
}
Expand All @@ -168,7 +168,7 @@ - (NSNumber *)getOptionalBool:(NSString *)option {
return cachedValue;
}
Bool value;
if (_isOpen && rime_get_api()->config_get_bool(&_config, option.UTF8String, &value)) {
if (_isOpen && RimeConfigGetBool(&_config, option.UTF8String, &value)) {
return _cache[option] = @(!!value);
}
return [_baseConfig getOptionalBool:option];
Expand All @@ -180,7 +180,7 @@ - (NSNumber *)getOptionalInt:(NSString *)option {
return cachedValue;
}
int value;
if (_isOpen && rime_get_api()->config_get_int(&_config, option.UTF8String, &value)) {
if (_isOpen && RimeConfigGetInt(&_config, option.UTF8String, &value)) {
return _cache[option] = @(value);
}
return [_baseConfig getOptionalInt:option];
Expand All @@ -192,7 +192,7 @@ - (NSNumber *)getOptionalDouble:(NSString *)option {
return cachedValue;
}
double value;
if (_isOpen && rime_get_api()->config_get_double(&_config, option.UTF8String, &value)) {
if (_isOpen && RimeConfigGetDouble(&_config, option.UTF8String, &value)) {
return _cache[option] = @(value);
}
return [_baseConfig getOptionalDouble:option];
Expand All @@ -204,7 +204,7 @@ - (NSString *)getString:(NSString *)option {
return cachedValue;
}
const char *value =
_isOpen ? rime_get_api()->config_get_cstring(&_config, option.UTF8String) : NULL;
_isOpen ? RimeConfigGetCString(&_config, option.UTF8String) : NULL;
if (value) {
return _cache[option] = @(value);
}
Expand Down Expand Up @@ -240,20 +240,20 @@ - (NSColor *)getPattern:(NSString *)option {
- (NSArray<NSString *> *)getList:(NSString *)option {
NSMutableArray<NSString *> *strList = [[NSMutableArray alloc] init];
RimeConfigIterator iterator;
rime_get_api()->config_begin_list(&iterator, &_config, option.UTF8String);
while (rime_get_api()->config_next(&iterator)) {
RimeConfigBeginList(&iterator, &_config, option.UTF8String);
while (RimeConfigNext(&iterator)) {
[strList addObject:[self getString:@(iterator.path)]];
}
rime_get_api()->config_end(&iterator);
RimeConfigEnd(&iterator);
return strList;
}

- (SquirrelOptionSwitcher *)getOptionSwitcher {
NSMutableDictionary<NSString *, NSString*> *switcher = [[NSMutableDictionary alloc] init];
NSMutableDictionary<NSString *, NSArray<NSString *> *> *optionGroups = [[NSMutableDictionary alloc] init];
RimeConfigIterator switchIter;
rime_get_api()->config_begin_list(&switchIter, &_config, "switches");
while (rime_get_api()->config_next(&switchIter)) {
RimeConfigBeginList(&switchIter, &_config, "switches");
while (RimeConfigNext(&switchIter)) {
int reset = [self getInt:[@(switchIter.path) stringByAppendingString:@"/reset"]];
NSString *name = [self getString:[@(switchIter.path) stringByAppendingString:@"/name"]];
if (name) {
Expand All @@ -266,13 +266,13 @@ - (SquirrelOptionSwitcher *)getOptionSwitcher {
NSMutableArray *optionGroup = [[NSMutableArray alloc] init];
BOOL hasStyleSection = NO;
RimeConfigIterator optionIter;
rime_get_api()->config_begin_list(&optionIter, &_config, [@(switchIter.path) stringByAppendingString:@"/options"].UTF8String);
while (rime_get_api()->config_next(&optionIter)) {
RimeConfigBeginList(&optionIter, &_config, [@(switchIter.path) stringByAppendingString:@"/options"].UTF8String);
while (RimeConfigNext(&optionIter)) {
NSString *option = [self getString:@(optionIter.path)];
[optionGroup addObject:option];
hasStyleSection |= [self hasSection:[@"style/" stringByAppendingString:option]];
}
rime_get_api()->config_end(&optionIter);
RimeConfigEnd(&optionIter);
if (hasStyleSection) {
for (NSUInteger i = 0; i < optionGroup.count; ++i) {
switcher[optionGroup[i]] = optionGroup[reset];
Expand All @@ -281,7 +281,7 @@ - (SquirrelOptionSwitcher *)getOptionSwitcher {
}
}
}
rime_get_api()->config_end(&switchIter);
RimeConfigEnd(&switchIter);
return [[SquirrelOptionSwitcher alloc] initWithSchemaId:_schemaId
switcher:switcher
optionGroups:optionGroups];
Expand All @@ -291,13 +291,13 @@ - (SquirrelAppOptions *)getAppOptions:(NSString *)appName {
NSString *rootKey = [@"app_options/" stringByAppendingString:appName];
SquirrelMutableAppOptions *appOptions = [[SquirrelMutableAppOptions alloc] init];
RimeConfigIterator iterator;
rime_get_api()->config_begin_map(&iterator, &_config, rootKey.UTF8String);
while (rime_get_api()->config_next(&iterator)) {
RimeConfigBeginMap(&iterator, &_config, rootKey.UTF8String);
while (RimeConfigNext(&iterator)) {
//NSLog(@"DEBUG option[%d]: %s (%s)", iterator.index, iterator.key, iterator.path);
BOOL value = [self getBool:@(iterator.path)];
appOptions[@(iterator.key)] = @(value);
}
rime_get_api()->config_end(&iterator);
RimeConfigEnd(&iterator);
return [appOptions copy];
}

Expand Down
Loading

0 comments on commit e8c984d

Please sign in to comment.