From 827d21576922842a34583cd9a7f9361936fde9b0 Mon Sep 17 00:00:00 2001 From: Hiroyuki Komatsu Date: Tue, 24 Sep 2024 00:53:17 +0000 Subject: [PATCH] Use the "com.apple" prefix instead of the product specific one (e.g. "org.mozc") * This could fix the issue that the composing mode is not changed. * Simplified the code by removing a global variable. #codehealth PiperOrigin-RevId: 678009827 --- src/mac/GoogleJapaneseInputController.mm | 66 +++++++++---------- src/mac/GoogleJapaneseInputController_test.mm | 6 +- 2 files changed, 33 insertions(+), 39 deletions(-) diff --git a/src/mac/GoogleJapaneseInputController.mm b/src/mac/GoogleJapaneseInputController.mm index 8a0943c34..51f6eec1e 100644 --- a/src/mac/GoogleJapaneseInputController.mm +++ b/src/mac/GoogleJapaneseInputController.mm @@ -79,9 +79,6 @@ namespace { // set of bundle IDs of applications on which Mozc should not open urls. const SetOfString *gNoOpenLinkApps = nullptr; -// The mapping from the CompositionMode enum to the actual id string -// of composition modes. -const std::map *gModeIdMap = nullptr; const SetOfString *gNoSelectedRangeApps = nullptr; const SetOfString *gNoDisplayModeSwitchApps = nullptr; const SetOfString *gNoSurroundingTextApps = nullptr; @@ -95,10 +92,11 @@ // surrounding text takes too much time. So we set this limitation. const int kGetSurroundingTextClientLengthLimit = 1000; -NSString *GetLabelForSuffix(const absl::string_view suffix) { - std::string label = mozc::MacUtil::GetLabelForSuffix(suffix); - return [NSString stringWithUTF8String:label.c_str()]; -} +constexpr absl::string_view kRomanModeId = "com.apple.inputmethod.Roman"; +constexpr absl::string_view kKatakanaModeId = "com.apple.inputmethod.Japanese.Katakana"; +constexpr absl::string_view kHalfWidthKanaModeId = "com.apple.inputmethod.Japanese.HalfWidthKana"; +constexpr absl::string_view kFullWidthRomanModeId = "com.apple.inputmethod.Japanese.FullWidthRoman"; +constexpr absl::string_view kHiraganaModeId = "com.apple.inputmethod.Japanese"; CompositionMode GetCompositionMode(absl::string_view mode_id) { if (mode_id.empty()) { @@ -111,19 +109,19 @@ CompositionMode GetCompositionMode(absl::string_view mode_id) { // /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/ // Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/TextServices.h // These IDs are also defined in Info.plist. - if (mode_id == "com.apple.inputmethod.Roman") { + if (mode_id == kRomanModeId) { return mozc::commands::HALF_ASCII; } - if (mode_id == "com.apple.inputmethod.Japanese.Katakana") { + if (mode_id == kKatakanaModeId) { return mozc::commands::FULL_KATAKANA; } - if (mode_id == "com.apple.inputmethod.Japanese.HalfWidthKana") { + if (mode_id == kHalfWidthKanaModeId) { return mozc::commands::HALF_KATAKANA; } - if (mode_id == "com.apple.inputmethod.Japanese.FullWidthRoman") { + if (mode_id == kFullWidthRomanModeId) { return mozc::commands::FULL_ASCII; } - if (mode_id == "com.apple.inputmethod.Japanese") { + if (mode_id == kHiraganaModeId) { return mozc::commands::HIRAGANA; } @@ -131,6 +129,25 @@ CompositionMode GetCompositionMode(absl::string_view mode_id) { return mozc::commands::DIRECT; } +absl::string_view GetModeId(CompositionMode mode) { + switch (mode) { + case mozc::commands::DIRECT: + case mozc::commands::HALF_ASCII: + return kRomanModeId; + case mozc::commands::FULL_KATAKANA: + return kKatakanaModeId; + case mozc::commands::HALF_KATAKANA: + return kHalfWidthKanaModeId; + case mozc::commands::FULL_ASCII: + return kFullWidthRomanModeId; + case mozc::commands::HIRAGANA: + return kHiraganaModeId; + default: + LOG(ERROR) << "The code should not reach here."; + return kRomanModeId; + } +} + bool IsBannedApplication(const SetOfString *bundleIdSet, const absl::string_view bundleId) { return bundleIdSet == nullptr || bundleIdSet->find(bundleId) != bundleIdSet->end(); } @@ -235,18 +252,6 @@ + (void)initializeConstants { gNoOpenLinkApps = noOpenlinkApps; } - std::map *newMap = - new (std::nothrow) std::map; - if (newMap) { - (*newMap)[mozc::commands::DIRECT] = GetLabelForSuffix("Roman"); - (*newMap)[mozc::commands::HIRAGANA] = GetLabelForSuffix("base"); - (*newMap)[mozc::commands::FULL_KATAKANA] = GetLabelForSuffix("Katakana"); - (*newMap)[mozc::commands::HALF_ASCII] = GetLabelForSuffix("Roman"); - (*newMap)[mozc::commands::FULL_ASCII] = GetLabelForSuffix("FullWidthRoman"); - (*newMap)[mozc::commands::HALF_KATAKANA] = GetLabelForSuffix("FullWidthRoman"); - gModeIdMap = newMap; - } - SetOfString *noSelectedRangeApps = new (std::nothrow) SetOfString; if (noSelectedRangeApps) { // Do not call selectedRange: method for the following @@ -439,21 +444,12 @@ - (void)switchMode:(CompositionMode)new_mode client:(id)sender { } - (void)switchDisplayMode { - if (gModeIdMap == nullptr) { - LOG(ERROR) << "gModeIdMap is not initialized correctly."; - return; - } if (IsBannedApplication(gNoDisplayModeSwitchApps, clientBundle_)) { return; } - std::map::const_iterator it = gModeIdMap->find(mode_); - if (it == gModeIdMap->end()) { - LOG(ERROR) << "mode: " << mode_ << " is invalid"; - return; - } - - [[self client] selectInputMode:it->second]; + absl::string_view mode_id = GetModeId(mode_); + [[self client] selectInputMode:[NSString stringWithUTF8String:mode_id.data()]]; } - (void)commitText:(const char *)text client:(id)sender { diff --git a/src/mac/GoogleJapaneseInputController_test.mm b/src/mac/GoogleJapaneseInputController_test.mm index d6b4c7fda..51142decd 100644 --- a/src/mac/GoogleJapaneseInputController_test.mm +++ b/src/mac/GoogleJapaneseInputController_test.mm @@ -564,8 +564,7 @@ BOOL SendKeyEvent(unsigned short keyCode, GoogleJapaneseInputController *control EXPECT_EQ(controller_.mode, commands::DIRECT); [controller_ switchDisplayMode]; EXPECT_EQ([mock_client_ getCounter:"selectInputMode:"], 1); - std::string expected = MacUtil::GetLabelForSuffix("Roman"); - EXPECT_EQ(mock_client_.selectedMode, expected); + EXPECT_EQ(mock_client_.selectedMode, "com.apple.inputmethod.Roman"); // Does not change the display mode for MS Word. See // GoogleJapaneseInputController.mm for the detailed information. @@ -575,8 +574,7 @@ BOOL SendKeyEvent(unsigned short keyCode, GoogleJapaneseInputController *control [controller_ switchDisplayMode]; // still remains 1 and display mode does not change. EXPECT_EQ([mock_client_ getCounter:"selectInputMode:"], 1); - expected = MacUtil::GetLabelForSuffix("Roman"); - EXPECT_EQ(mock_client_.selectedMode, expected); + EXPECT_EQ(mock_client_.selectedMode, "com.apple.inputmethod.Roman"); } TEST_F(GoogleJapaneseInputControllerTest, commitText) {