From 277ebfa4c3efa8afaac61e4e8036842369ff960e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=85=E6=88=8E=E6=B0=8F?= Date: Fri, 15 Mar 2024 22:52:49 +0800 Subject: [PATCH 1/3] fix(SquirrelPanel): text shown in top-left corner (#856) it happens when the panel is displayed for the first time. usually it's the ascii mode status tooltip. calling setTextContainerInset: in drawRect: has no effect for what's being drawn. fixed by setting the inset on an ealier occasion. --- SquirrelPanel.m | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/SquirrelPanel.m b/SquirrelPanel.m index a7ec23945..37f056797 100644 --- a/SquirrelPanel.m +++ b/SquirrelPanel.m @@ -829,10 +829,6 @@ - (void)drawRect:(NSRect)dirtyRect { CGPathRef preeditPath = CGPathCreateMutable(); SquirrelTheme* theme = self.currentTheme; - NSPoint textFieldOrigin = dirtyRect.origin; - textFieldOrigin.y += theme.edgeInset.height; - textFieldOrigin.x += theme.edgeInset.width; - // Draw preedit Rect NSRect backgroundRect = dirtyRect; NSRect containingRect = dirtyRect; @@ -1030,8 +1026,6 @@ - (void)drawRect:(NSRect)dirtyRect { } [panelLayer addSublayer:layer]; } - [_textView - setTextContainerInset:NSMakeSize(textFieldOrigin.x, textFieldOrigin.y)]; } - (BOOL)clickAtPoint:(NSPoint)_point index:(NSInteger*)_index { @@ -1465,9 +1459,12 @@ - (void)show { [self.contentView setBoundsOrigin:NSMakePoint(0, 0)]; [_view.textView setBoundsOrigin:NSMakePoint(0, 0)]; } - BOOL translucency = theme.translucency; [_view setFrame:self.contentView.bounds]; [_view.textView setFrame:self.contentView.bounds]; + [_view.textView setTextContainerInset:NSMakeSize(theme.edgeInset.width, + theme.edgeInset.height)]; + + BOOL translucency = theme.translucency; if (translucency) { [_back setFrame:self.contentView.bounds]; _back.appearance = NSApp.effectiveAppearance; From d45b9a648811eac683e82a030a7c9dffb7c4b216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=85=E6=88=8E=E6=B0=8F?= Date: Thu, 28 Mar 2024 23:59:35 +0800 Subject: [PATCH 2/3] fix: install input source usable, but with issues switching imes before log out. --- input_source.m | 68 ++++++++++++++++++++++++++++++++++++++++----- main.m | 30 +++++++++++++------- scripts/postinstall | 12 +++++--- 3 files changed, 89 insertions(+), 21 deletions(-) diff --git a/input_source.m b/input_source.m index 6b00be271..11b82e5e1 100644 --- a/input_source.m +++ b/input_source.m @@ -9,7 +9,16 @@ #define HANS_INPUT_MODE (1 << 0) #define HANT_INPUT_MODE (1 << 1) +#define DEFAULT_INPUT_MODE HANS_INPUT_MODE + +int GetEnabledInputModes(void); + void RegisterInputSource(void) { + int enabled_input_modes = GetEnabledInputModes(); + if (enabled_input_modes) { + // Already registered. + return; + } CFURLRef installedLocationURL = CFURLCreateFromFileSystemRepresentation( NULL, (UInt8*)kInstallLocation, (CFIndex)strlen(kInstallLocation), false); if (installedLocationURL) { @@ -19,7 +28,14 @@ void RegisterInputSource(void) { } } -void ActivateInputSource(int enabled_modes) { +void EnableInputSource(void) { + int enabled_input_modes = GetEnabledInputModes(); + if (enabled_input_modes) { + // keep user's manually enabled input modes. + return; + } + // neither is enabled, enable the default input mode. + int input_modes_to_enable = DEFAULT_INPUT_MODE; CFArrayRef sourceList = TISCreateInputSourceList(NULL, true); for (CFIndex i = 0; i < CFArrayGetCount(sourceList); ++i) { TISInputSourceRef inputSource = @@ -28,23 +44,61 @@ void ActivateInputSource(int enabled_modes) { inputSource, kTISPropertyInputSourceID); // NSLog(@"Examining input source: %@", sourceID); if ((!CFStringCompare(sourceID, kHansInputModeID, 0) && - ((enabled_modes & HANS_INPUT_MODE) != 0)) || + ((input_modes_to_enable & HANS_INPUT_MODE) != 0)) || (!CFStringCompare(sourceID, kHantInputModeID, 0) && - ((enabled_modes & HANT_INPUT_MODE) != 0))) { - TISEnableInputSource(inputSource); - NSLog(@"Enabled input source: %@", sourceID); + ((input_modes_to_enable & HANT_INPUT_MODE) != 0))) { + CFBooleanRef isEnabled = (CFBooleanRef)TISGetInputSourceProperty( + inputSource, kTISPropertyInputSourceIsEnabled); + if (!CFBooleanGetValue(isEnabled)) { + TISEnableInputSource(inputSource); + NSLog(@"Enabled input source: %@", sourceID); + } + } + } + CFRelease(sourceList); +} + +void SelectInputSource(void) { + int enabled_input_modes = GetEnabledInputModes(); + int input_modes_to_select = + ((enabled_input_modes & DEFAULT_INPUT_MODE) != 0) + ? DEFAULT_INPUT_MODE : enabled_input_modes; + if (!input_modes_to_select) { + NSLog(@"No enabled input sources."); + return; + } + CFArrayRef sourceList = TISCreateInputSourceList(NULL, true); + for (CFIndex i = 0; i < CFArrayGetCount(sourceList); ++i) { + TISInputSourceRef inputSource = + (TISInputSourceRef)CFArrayGetValueAtIndex(sourceList, i); + CFStringRef sourceID = (CFStringRef)TISGetInputSourceProperty( + inputSource, kTISPropertyInputSourceID); + // NSLog(@"Examining input source: %@", sourceID); + if ((!CFStringCompare(sourceID, kHansInputModeID, 0) && + ((input_modes_to_select & HANS_INPUT_MODE) != 0)) || + (!CFStringCompare(sourceID, kHantInputModeID, 0) && + ((input_modes_to_select & HANT_INPUT_MODE) != 0))) { + // select the first enabled input mode in Squirrel. + CFBooleanRef isEnabled = (CFBooleanRef)TISGetInputSourceProperty( + inputSource, kTISPropertyInputSourceIsEnabled); + if (!CFBooleanGetValue(isEnabled)) { + continue; + } CFBooleanRef isSelectable = (CFBooleanRef)TISGetInputSourceProperty( inputSource, kTISPropertyInputSourceIsSelectCapable); - if (CFBooleanGetValue(isSelectable)) { + CFBooleanRef isSelected = (CFBooleanRef)TISGetInputSourceProperty( + inputSource, kTISPropertyInputSourceIsSelected); + if (!CFBooleanGetValue(isSelected) && CFBooleanGetValue(isSelectable)) { TISSelectInputSource(inputSource); NSLog(@"Selected input source: %@", sourceID); } + break; } } CFRelease(sourceList); } -void DeactivateInputSource(void) { +void DisableInputSource(void) { CFArrayRef sourceList = TISCreateInputSourceList(NULL, true); for (CFIndex i = CFArrayGetCount(sourceList); i > 0; --i) { TISInputSourceRef inputSource = diff --git a/main.m b/main.m index 53b6491c0..4a85398bd 100644 --- a/main.m +++ b/main.m @@ -6,11 +6,9 @@ #import void RegisterInputSource(void); -int GetEnabledInputModes(void); -void DeactivateInputSource(void); -void ActivateInputSource(int input_modes); - -#define DEFAULT_INPUT_MODE 1 +void DisableInputSource(void); +void EnableInputSource(void); +void SelectInputSource(void); // Each input method needs a unique connection name. // Note that periods and spaces are not allowed in the connection name. @@ -34,12 +32,24 @@ int main(int argc, char* argv[]) { return 0; } - if (argc > 1 && !strcmp("--install", argv[1])) { - // register and enable Squirrel + if (argc > 1 && (!strcmp("--register-input-source", argv[1]) || + !strcmp("--install", argv[1]))) { RegisterInputSource(); - int input_modes = GetEnabledInputModes(); - DeactivateInputSource(); - ActivateInputSource(input_modes ?: DEFAULT_INPUT_MODE); + return 0; + } + + if (argc > 1 && !strcmp("--enable-input-source", argv[1])) { + EnableInputSource(); + return 0; + } + + if (argc > 1 && !strcmp("--disable-input-source", argv[1])) { + DisableInputSource(); + return 0; + } + + if (argc > 1 && !strcmp("--select-input-source", argv[1])) { + SelectInputSource(); return 0; } diff --git a/scripts/postinstall b/scripts/postinstall index cbfc6e198..7ef6cb9e1 100755 --- a/scripts/postinstall +++ b/scripts/postinstall @@ -1,4 +1,5 @@ #!/bin/bash +set -e login_user=`/usr/bin/stat -f%Su /dev/console` squirrel_app_root="${DSTROOT}/Squirrel.app" @@ -6,12 +7,15 @@ squirrel_executable="${squirrel_app_root}/Contents/MacOS/Squirrel" rime_package_installer="${squirrel_app_root}/Contents/MacOS/rime-install" rime_shared_data_path="${squirrel_app_root}/Contents/SharedSupport" -/usr/bin/sudo -u "${login_user}" /usr/bin/killall Squirrel > /dev/null +/usr/bin/sudo -u "${login_user}" /usr/bin/killall Squirrel > /dev/null || true + +"${squirrel_executable}" --register-input-source if [ -z "${RIME_NO_PREBUILD}" ]; then pushd "${rime_shared_data_path}" > /dev/null "${squirrel_executable}" --build popd > /dev/null -fi - -/usr/bin/sudo -u "${login_user}" "${squirrel_executable}" --install +fi && ( + /usr/bin/sudo -u "${login_user}" "${squirrel_executable}" --enable-input-source + /usr/bin/sudo -u "${login_user}" "${squirrel_executable}" --select-input-source +) From cc0aea086f978011ce614e46a68ae51d3af90042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=85=E6=88=8E=E6=B0=8F?= Date: Fri, 29 Mar 2024 00:09:53 +0800 Subject: [PATCH 3/3] chore: format code --- input_source.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/input_source.m b/input_source.m index 11b82e5e1..535b7cff4 100644 --- a/input_source.m +++ b/input_source.m @@ -60,9 +60,9 @@ void EnableInputSource(void) { void SelectInputSource(void) { int enabled_input_modes = GetEnabledInputModes(); - int input_modes_to_select = - ((enabled_input_modes & DEFAULT_INPUT_MODE) != 0) - ? DEFAULT_INPUT_MODE : enabled_input_modes; + int input_modes_to_select = ((enabled_input_modes & DEFAULT_INPUT_MODE) != 0) + ? DEFAULT_INPUT_MODE + : enabled_input_modes; if (!input_modes_to_select) { NSLog(@"No enabled input sources."); return;