Skip to content

Commit

Permalink
refactor: simplify lang bar buttons handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kanru committed Dec 25, 2024
1 parent 71b2c5c commit ca24d85
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 35 deletions.
8 changes: 1 addition & 7 deletions chewing_tip/ChewingTextService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,10 @@ void TextService::onActivate() {
// virtual
void TextService::onDeactivate() {
// Remove all buttons to avoid reference cycles
removeButton(switchLangButton_.get());
switchLangButton_ = nullptr;
removeButton(switchShapeButton_.get());
switchShapeButton_ = nullptr;
removeButton(settingsMenuButton_.get());
settingsMenuButton_ = nullptr;
if (imeModeIcon_) {
removeButton(imeModeIcon_.get());
imeModeIcon_ = nullptr;
}
imeModeIcon_ = nullptr;

lastKeyDownCode_ = 0;
freeChewingContext();
Expand Down
30 changes: 3 additions & 27 deletions chewing_tip/TextService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ TextService::~TextService(void) {
}
}
}

langBarMgr_ = NULL;
}

void TextService::addButton(ITfLangBarItemButton* button) {
Expand All @@ -99,23 +97,6 @@ void TextService::addButton(ITfLangBarItemButton* button) {
}
}

void TextService::removeButton(ITfLangBarItemButton* button) {
if(button) {
winrt::com_ptr<ITfLangBarItemButton> btn;
btn.copy_from(button);
auto it = find(langBarButtons_.begin(), langBarButtons_.end(), btn);
if(it != langBarButtons_.end()) {
if (threadMgr_) {
winrt::com_ptr<ITfLangBarItemMgr> langBarItemMgr;
if(threadMgr_->QueryInterface(IID_ITfLangBarItemMgr, langBarItemMgr.put_void()) == S_OK) {
langBarItemMgr->RemoveItem(button);
}
}
langBarButtons_.erase(it);
}
}
}

// preserved key
void TextService::addPreservedKey(UINT keyCode, UINT modifiers, const GUID& guid) {
PreservedKey preservedKey;
Expand Down Expand Up @@ -421,9 +402,6 @@ STDMETHODIMP TextService::Activate(ITfThreadMgr *pThreadMgr, TfClientId tfClient
if(!isKeyboardOpened_)
setKeyboardOpen(true);

// initialize language bar
::CoCreateInstance(CLSID_TF_LangBarMgr, NULL, CLSCTX_INPROC_SERVER,
IID_ITfLangBarMgr, (void**)&langBarMgr_);
// Note: language bar has no effects in Win 8 immersive mode
if(!langBarButtons_.empty()) {
winrt::com_ptr<ITfLangBarItemMgr> langBarItemMgr;
Expand Down Expand Up @@ -455,15 +433,13 @@ STDMETHODIMP TextService::Deactivate() {
// uninitialize language bar
if(!langBarButtons_.empty()) {
winrt::com_ptr<ITfLangBarItemMgr> langBarItemMgr;
if(threadMgr_->QueryInterface(IID_ITfLangBarItemMgr, langBarItemMgr.put_void()) == S_OK) {
for(auto& button: langBarButtons_) {
if (threadMgr_->QueryInterface(IID_ITfLangBarItemMgr, langBarItemMgr.put_void()) == S_OK) {
for (auto& button: langBarButtons_) {
langBarItemMgr->RemoveItem(button.get());
}
}
}
if(langBarMgr_) {
langBarMgr_ = NULL;
}
langBarButtons_.clear();

// unadvice event sinks

Expand Down
1 change: 0 additions & 1 deletion chewing_tip/TextService.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ class TextService:
DWORD threadMgrEventSinkCookie_;

ITfComposition* composition_; // acquired when starting composition, released when ending composition
winrt::com_ptr<ITfLangBarMgr> langBarMgr_;
std::vector<winrt::com_ptr<ITfLangBarItemButton>> langBarButtons_;
std::vector<PreservedKey> preservedKeys_;
std::vector<CompartmentMonitor> compartmentMonitors_;
Expand Down

0 comments on commit ca24d85

Please sign in to comment.