Skip to content

Commit

Permalink
refactor: avoid cyclical reference between buttons and text service
Browse files Browse the repository at this point in the history
  • Loading branch information
kanru committed Dec 25, 2024
1 parent 29d4159 commit 71b2c5c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
39 changes: 26 additions & 13 deletions chewing_tip/ChewingTextService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,21 @@ TextService::TextService():

// add preserved keys
addPreservedKey(VK_SPACE, TF_MOD_SHIFT, g_shiftSpaceGuid); // shift + space
}

TextService::~TextService(void) {
if(popupMenu_)
::DestroyMenu(popupMenu_);

if(messageWindow_)
hideMessage();

freeChewingContext();
OutputDebugStringW(L"[chewing] Unloaded\n");
}

// virtual
void TextService::onActivate() {
// add language bar buttons
// siwtch Chinese/English modes
TF_LANGBARITEMINFO info = {
Expand Down Expand Up @@ -171,20 +185,7 @@ TextService::TextService():
);
addButton(imeModeIcon_.get());
}
}

TextService::~TextService(void) {
if(popupMenu_)
::DestroyMenu(popupMenu_);

if(messageWindow_)
hideMessage();

freeChewingContext();
}

// virtual
void TextService::onActivate() {
config().reloadIfNeeded();
initChewingContext();
updateLangButtons();
Expand All @@ -195,6 +196,18 @@ 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;
}

lastKeyDownCode_ = 0;
freeChewingContext();

Expand Down
17 changes: 17 additions & 0 deletions chewing_tip/TextService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ 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
1 change: 1 addition & 0 deletions chewing_tip/TextService.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class TextService:

// language bar buttons
void addButton(ITfLangBarItemButton* button);
void removeButton(ITfLangBarItemButton* button);

// preserved keys
void addPreservedKey(UINT keyCode, UINT modifiers, const GUID& guid);
Expand Down
7 changes: 5 additions & 2 deletions chewing_tip/src/lang_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ pub(crate) unsafe trait ILangBarButton: ITfLangBarItemButton {
fn set_enabled(&self, enabled: bool) -> Result<()>;
}

#[derive(Debug)]
#[implement(ITfLangBarItem, ITfLangBarItemButton, ITfSource, ILangBarButton)]
struct LangBarButton {
info: TF_LANGBARITEMINFO,
Expand Down Expand Up @@ -69,9 +68,13 @@ unsafe extern "C" fn CreateLangBarButton(
icon: HICON,
menu: HMENU,
command_id: u32,
run_command: IRunCommand,
run_command: *mut IRunCommand,
ret: *mut *mut c_void,
) {
let binding = run_command.cast();
let run_command_ref =
IRunCommand::from_raw_borrowed(&binding).expect("invalid IRunCommand pointer");
let run_command: IRunCommand = run_command_ref.cast().expect("invalid IRunCommand pointer");
let lang_bar_btn = LangBarButton {
info,
status: Cell::new(0),
Expand Down

0 comments on commit 71b2c5c

Please sign in to comment.