PR#1: macOS Notification when Kanji Conversation switch changes.

This commit is contained in:
Hiraku 2022-01-10 16:18:42 +08:00 committed by ShikiSuen
parent 78f35efec1
commit 225241081c
4 changed files with 33 additions and 1 deletions

View File

@ -36,7 +36,7 @@
@class PreferencesWindowController;
@interface AppDelegate : NSObject <NSApplicationDelegate>
@interface AppDelegate : NSObject <NSApplicationDelegate, NSUserNotificationCenterDelegate>
{
@private
NSURLConnection *_updateCheckConnection;

View File

@ -67,6 +67,7 @@ static const NSTimeInterval kTimeoutInterval = 60.0;
}
[self checkForUpdate];
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
}
- (void)checkForUpdate
@ -268,4 +269,8 @@ static const NSTimeInterval kTimeoutInterval = 60.0;
_updateNextStepURL = nil;
}
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification{
return YES;
}
@end

View File

@ -75,6 +75,9 @@
// if Chinese conversion is enabled
BOOL _chineseConversionEnabled;
// if Chinese conversion status has been changed
BOOL _previousChineseConversionEnabledStatus;
}
@end

View File

@ -165,6 +165,8 @@ public:
}
// the two client pointers are weak pointers (i.e. we don't retain them)
// therefore we don't do anything about it
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ChineseConversionStatusChanged" object:nil];
}
- (id)initWithServer:(IMKServer *)server delegate:(id)delegate client:(id)client
@ -196,6 +198,10 @@ public:
_inputMode = kBopomofoModeIdentifier;
_chineseConversionEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:kChineseConversionEnabledKey];
_previousChineseConversionEnabledStatus = _chineseConversionEnabled;
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ChineseConversionStatusChanged" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleChineseConversionStatusDidChanged:) name:@"ChineseConversionStatusChanged" object:nil];
}
return self;
@ -1466,6 +1472,7 @@ public:
{
_chineseConversionEnabled = !_chineseConversionEnabled;
[[NSUserDefaults standardUserDefaults] setBool:_chineseConversionEnabled forKey:kChineseConversionEnabledKey];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChineseConversionStatusChanged" object:nil];
}
- (void)clearLearningDictionary:(id)sender
@ -1517,6 +1524,23 @@ public:
}
}
- (void)handleChineseConversionStatusDidChanged:(NSNotification *)notification
{
// Do not post the notification if status doesn't change.
// This is because the input method can be initiated by multiple applications, then all of them would post the notification.
if (_previousChineseConversionEnabledStatus == _chineseConversionEnabled) {
return;
}
NSUserNotification *userNotification = [[NSUserNotification alloc] init];
userNotification.title = @"vChewing";
userNotification.informativeText = [NSString stringWithFormat:@"%@%@", NSLocalizedString(@"Chinese Conversion", @""), _chineseConversionEnabled ? @" Enabled" : @" Disabled"];
userNotification.soundName = NSUserNotificationDefaultSoundName;
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];
_previousChineseConversionEnabledStatus = _chineseConversionEnabled;
}
@end
static void LTLoadLanguageModelFile(NSString *filenameWithoutExtension, FastLM &lm)