Skip to content

Commit c01f59c

Browse files
committed
Merge remote-tracking branch 'upstream/master' into UI
2 parents 2356165 + cc0aea0 commit c01f59c

File tree

3 files changed

+95
-33
lines changed

3 files changed

+95
-33
lines changed

input_source.m

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66
static const CFStringRef kHantInputModeID =
77
CFSTR("im.rime.inputmethod.Squirrel.Hant");
88

9-
typedef NS_OPTIONS(int, RimeInputMode) {
10-
DEFAULT_INPUT_MODE = 1 << 0,
11-
HANS_INPUT_MODE = 1 << 0,
12-
HANT_INPUT_MODE = 1 << 1
13-
};
9+
#define HANS_INPUT_MODE (1 << 0)
10+
#define HANT_INPUT_MODE (1 << 1)
11+
12+
#define DEFAULT_INPUT_MODE HANS_INPUT_MODE
13+
14+
int GetEnabledInputModes(void);
1415

1516
void RegisterInputSource(void) {
17+
int enabled_input_modes = GetEnabledInputModes();
18+
if (enabled_input_modes) {
19+
// Already registered.
20+
return;
21+
}
1622
CFURLRef installedLocationURL = CFURLCreateFromFileSystemRepresentation(
1723
NULL, (UInt8*)kInstallLocation, (CFIndex)strlen(kInstallLocation), false);
1824
if (installedLocationURL) {
@@ -22,7 +28,14 @@ void RegisterInputSource(void) {
2228
}
2329
}
2430

25-
void ActivateInputSource(int enabled_modes) {
31+
void EnableInputSource(void) {
32+
int enabled_input_modes = GetEnabledInputModes();
33+
if (enabled_input_modes) {
34+
// keep user's manually enabled input modes.
35+
return;
36+
}
37+
// neither is enabled, enable the default input mode.
38+
int input_modes_to_enable = DEFAULT_INPUT_MODE;
2639
CFArrayRef sourceList = TISCreateInputSourceList(NULL, true);
2740
for (CFIndex i = 0; i < CFArrayGetCount(sourceList); ++i) {
2841
TISInputSourceRef inputSource =
@@ -31,23 +44,61 @@ void ActivateInputSource(int enabled_modes) {
3144
inputSource, kTISPropertyInputSourceID);
3245
// NSLog(@"Examining input source: %@", sourceID);
3346
if ((!CFStringCompare(sourceID, kHansInputModeID, 0) &&
34-
((enabled_modes & HANS_INPUT_MODE) != 0)) ||
47+
((input_modes_to_enable & HANS_INPUT_MODE) != 0)) ||
3548
(!CFStringCompare(sourceID, kHantInputModeID, 0) &&
36-
((enabled_modes & HANT_INPUT_MODE) != 0))) {
37-
TISEnableInputSource(inputSource);
38-
NSLog(@"Enabled input source: %@", sourceID);
49+
((input_modes_to_enable & HANT_INPUT_MODE) != 0))) {
50+
CFBooleanRef isEnabled = (CFBooleanRef)TISGetInputSourceProperty(
51+
inputSource, kTISPropertyInputSourceIsEnabled);
52+
if (!CFBooleanGetValue(isEnabled)) {
53+
TISEnableInputSource(inputSource);
54+
NSLog(@"Enabled input source: %@", sourceID);
55+
}
56+
}
57+
}
58+
CFRelease(sourceList);
59+
}
60+
61+
void SelectInputSource(void) {
62+
int enabled_input_modes = GetEnabledInputModes();
63+
int input_modes_to_select = ((enabled_input_modes & DEFAULT_INPUT_MODE) != 0)
64+
? DEFAULT_INPUT_MODE
65+
: enabled_input_modes;
66+
if (!input_modes_to_select) {
67+
NSLog(@"No enabled input sources.");
68+
return;
69+
}
70+
CFArrayRef sourceList = TISCreateInputSourceList(NULL, true);
71+
for (CFIndex i = 0; i < CFArrayGetCount(sourceList); ++i) {
72+
TISInputSourceRef inputSource =
73+
(TISInputSourceRef)CFArrayGetValueAtIndex(sourceList, i);
74+
CFStringRef sourceID = (CFStringRef)TISGetInputSourceProperty(
75+
inputSource, kTISPropertyInputSourceID);
76+
// NSLog(@"Examining input source: %@", sourceID);
77+
if ((!CFStringCompare(sourceID, kHansInputModeID, 0) &&
78+
((input_modes_to_select & HANS_INPUT_MODE) != 0)) ||
79+
(!CFStringCompare(sourceID, kHantInputModeID, 0) &&
80+
((input_modes_to_select & HANT_INPUT_MODE) != 0))) {
81+
// select the first enabled input mode in Squirrel.
82+
CFBooleanRef isEnabled = (CFBooleanRef)TISGetInputSourceProperty(
83+
inputSource, kTISPropertyInputSourceIsEnabled);
84+
if (!CFBooleanGetValue(isEnabled)) {
85+
continue;
86+
}
3987
CFBooleanRef isSelectable = (CFBooleanRef)TISGetInputSourceProperty(
4088
inputSource, kTISPropertyInputSourceIsSelectCapable);
41-
if (CFBooleanGetValue(isSelectable)) {
89+
CFBooleanRef isSelected = (CFBooleanRef)TISGetInputSourceProperty(
90+
inputSource, kTISPropertyInputSourceIsSelected);
91+
if (!CFBooleanGetValue(isSelected) && CFBooleanGetValue(isSelectable)) {
4292
TISSelectInputSource(inputSource);
4393
NSLog(@"Selected input source: %@", sourceID);
4494
}
95+
break;
4596
}
4697
}
4798
CFRelease(sourceList);
4899
}
49100

50-
void DeactivateInputSource(void) {
101+
void DisableInputSource(void) {
51102
CFArrayRef sourceList = TISCreateInputSourceList(NULL, true);
52103
for (CFIndex i = CFArrayGetCount(sourceList); i > 0; --i) {
53104
TISInputSourceRef inputSource =
@@ -68,8 +119,8 @@ void DeactivateInputSource(void) {
68119
CFRelease(sourceList);
69120
}
70121

71-
RimeInputMode GetEnabledInputModes(void) {
72-
RimeInputMode input_modes = 0;
122+
int GetEnabledInputModes(void) {
123+
int input_modes = 0;
73124
CFArrayRef sourceList = TISCreateInputSourceList(NULL, true);
74125
for (CFIndex i = 0; i < CFArrayGetCount(sourceList); ++i) {
75126
TISInputSourceRef inputSource =

main.m

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@
33
#import <Cocoa/Cocoa.h>
44
#import <InputMethodKit/InputMethodKit.h>
55
#import <rime_api.h>
6-
7-
typedef NS_OPTIONS(int, RimeInputMode) {
8-
DEFAULT_INPUT_MODE = 1 << 0,
9-
HANS_INPUT_MODE = 1 << 0,
10-
HANT_INPUT_MODE = 1 << 1
11-
};
6+
#import <string.h>
127

138
void RegisterInputSource(void);
14-
RimeInputMode GetEnabledInputModes(void);
15-
void DeactivateInputSource(void);
16-
void ActivateInputSource(RimeInputMode input_modes);
9+
void DisableInputSource(void);
10+
void EnableInputSource(void);
11+
void SelectInputSource(void);
1712

1813
// Each input method needs a unique connection name.
1914
// Note that periods and spaces are not allowed in the connection name.
@@ -37,12 +32,24 @@ int main(int argc, char* argv[]) {
3732
return 0;
3833
}
3934

40-
if (argc > 1 && !strcmp("--install", argv[1])) {
41-
// register and enable Squirrel
35+
if (argc > 1 && (!strcmp("--register-input-source", argv[1]) ||
36+
!strcmp("--install", argv[1]))) {
4237
RegisterInputSource();
43-
int input_modes = GetEnabledInputModes();
44-
DeactivateInputSource();
45-
ActivateInputSource(input_modes ?: DEFAULT_INPUT_MODE);
38+
return 0;
39+
}
40+
41+
if (argc > 1 && !strcmp("--enable-input-source", argv[1])) {
42+
EnableInputSource();
43+
return 0;
44+
}
45+
46+
if (argc > 1 && !strcmp("--disable-input-source", argv[1])) {
47+
DisableInputSource();
48+
return 0;
49+
}
50+
51+
if (argc > 1 && !strcmp("--select-input-source", argv[1])) {
52+
SelectInputSource();
4653
return 0;
4754
}
4855

@@ -74,7 +81,7 @@ int main(int argc, char* argv[]) {
7481
// load the bundle explicitly because in this case the input method is a
7582
// background only application
7683
[main loadNibNamed:@"MainMenu"
77-
owner:NSApplication.sharedApplication
84+
owner:[NSApplication sharedApplication]
7885
topLevelObjects:nil];
7986

8087
// opencc will be configured with relative dictionary paths

scripts/postinstall

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#!/bin/bash
2+
set -e
23

34
login_user=`/usr/bin/stat -f%Su /dev/console`
45
squirrel_app_root="${DSTROOT}/Squirrel.app"
56
squirrel_executable="${squirrel_app_root}/Contents/MacOS/Squirrel"
67
rime_package_installer="${squirrel_app_root}/Contents/MacOS/rime-install"
78
rime_shared_data_path="${squirrel_app_root}/Contents/SharedSupport"
89

9-
/usr/bin/sudo -u "${login_user}" /usr/bin/killall Squirrel > /dev/null
10+
/usr/bin/sudo -u "${login_user}" /usr/bin/killall Squirrel > /dev/null || true
11+
12+
"${squirrel_executable}" --register-input-source
1013

1114
if [ -z "${RIME_NO_PREBUILD}" ]; then
1215
pushd "${rime_shared_data_path}" > /dev/null
1316
"${squirrel_executable}" --build
1417
popd > /dev/null
15-
fi
16-
17-
/usr/bin/sudo -u "${login_user}" "${squirrel_executable}" --install
18+
fi && (
19+
/usr/bin/sudo -u "${login_user}" "${squirrel_executable}" --enable-input-source
20+
/usr/bin/sudo -u "${login_user}" "${squirrel_executable}" --select-input-source
21+
)

0 commit comments

Comments
 (0)