From 225241081c60ff1c688cbb3d7415d6e25d8e8ef5 Mon Sep 17 00:00:00 2001 From: Hiraku Date: Mon, 10 Jan 2022 16:18:42 +0800 Subject: [PATCH] PR#1: macOS Notification when Kanji Conversation switch changes. --- Source/AppDelegate.h | 2 +- Source/AppDelegate.m | 5 +++++ Source/InputMethodController.h | 3 +++ Source/InputMethodController.mm | 24 ++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Source/AppDelegate.h b/Source/AppDelegate.h index 66d758bf..5e3de301 100644 --- a/Source/AppDelegate.h +++ b/Source/AppDelegate.h @@ -36,7 +36,7 @@ @class PreferencesWindowController; -@interface AppDelegate : NSObject +@interface AppDelegate : NSObject { @private NSURLConnection *_updateCheckConnection; diff --git a/Source/AppDelegate.m b/Source/AppDelegate.m index eb7dc496..46d1ed3f 100644 --- a/Source/AppDelegate.m +++ b/Source/AppDelegate.m @@ -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 diff --git a/Source/InputMethodController.h b/Source/InputMethodController.h index 9ba86602..7cb01be8 100644 --- a/Source/InputMethodController.h +++ b/Source/InputMethodController.h @@ -75,6 +75,9 @@ // if Chinese conversion is enabled BOOL _chineseConversionEnabled; + + // if Chinese conversion status has been changed + BOOL _previousChineseConversionEnabledStatus; } @end diff --git a/Source/InputMethodController.mm b/Source/InputMethodController.mm index 0f7b46d3..b995b2b9 100644 --- a/Source/InputMethodController.mm +++ b/Source/InputMethodController.mm @@ -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)