Skip to content

Commit

Permalink
refactor language bar, add tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhngtu committed May 30, 2021
1 parent 0aedc66 commit 4225133
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 122 deletions.
7 changes: 3 additions & 4 deletions VietTypeATL/EngineController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,9 @@ HRESULT EngineController::ToggleUserEnabled() {

long EngineController::IsEnabled() const {
long enabled;
#pragma warning(suppress: 4189)
HRESULT hr = this->IsUserEnabled(&enabled);
HRESULT_CHECK(hr, L"%s", L"this->IsUserEnabled failed");
return enabled && _blocked == BlockedKind::Free;
return !!enabled && _blocked == BlockedKind::Free;
}

EngineController::BlockedKind EngineController::GetBlocked() const {
Expand Down Expand Up @@ -225,7 +224,7 @@ HRESULT EngineController::UpdateStates() {
_Check_return_ HRESULT EngineController::InitLanguageBar() {
HRESULT hr;

_indicatorButton = std::make_unique<IndicatorButton>();
_indicatorButton = std::make_unique<RefreshableButton>();
hr = _indicatorButton->Initialize(
this,
_langBarItemMgr,
Expand All @@ -235,7 +234,7 @@ _Check_return_ HRESULT EngineController::InitLanguageBar() {
Globals::TextServiceDescription);
HRESULT_CHECK_RETURN(hr, L"%s", L"_indicatorButton->Initialize failed");

_langBarButton = std::make_unique<LangBarButton>();
_langBarButton = std::make_unique<RefreshableButton>();
hr = _langBarButton->Initialize(
this,
_langBarItemMgr,
Expand Down
7 changes: 3 additions & 4 deletions VietTypeATL/EngineController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

namespace VietType {

class IndicatorButton;
class LangBarButton;
class RefreshableButton;
class CompositionManager;
class EngineController;
class SettingsDialog;
Expand Down Expand Up @@ -85,8 +84,8 @@ class EngineController :
SinkAdvisor<ITfCompartmentEventSink> _openCloseCompartmentEventSink;

// unique_ptr is not necessary but used just to break include cycle
std::unique_ptr<IndicatorButton> _indicatorButton;
std::unique_ptr<LangBarButton> _langBarButton;
std::unique_ptr<RefreshableButton> _indicatorButton;
std::unique_ptr<RefreshableButton> _langBarButton;

CComPtr<EngineSettingsController> _settings;

Expand Down
15 changes: 11 additions & 4 deletions VietTypeATL/LanguageBarButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ STDMETHODIMP LanguageBarButton::Show(_In_ BOOL fShow) {
return E_NOTIMPL;
}

STDMETHODIMP LanguageBarButton::GetTooltipString(__RPC__deref_out_opt BSTR* pbstrToolTip) {
return E_NOTIMPL;
}

STDMETHODIMP LanguageBarButton::OnClick(_In_ TfLBIClick click, _In_ POINT pt, __RPC__in const RECT* prcArea) {
if (_callbacks) {
return _callbacks->OnClick(click, pt, prcArea);
Expand Down Expand Up @@ -102,10 +98,21 @@ STDMETHODIMP LanguageBarButton::GetIcon(__RPC__deref_out_opt HICON* phIcon) {
}

STDMETHODIMP LanguageBarButton::GetText(__RPC__deref_out_opt BSTR* pbstrText) {
if (!pbstrText) {
return E_INVALIDARG;
}
*pbstrText = SysAllocString(_callbacks->GetText().c_str());
return *pbstrText ? S_OK : E_OUTOFMEMORY;
}

STDMETHODIMP LanguageBarButton::GetTooltipString(__RPC__deref_out_opt BSTR* pbstrText) {
if (!pbstrText) {
return E_INVALIDARG;
}
*pbstrText = SysAllocString(_callbacks->GetTooltipString().c_str());
return *pbstrText ? S_OK : E_OUTOFMEMORY;
}

_Check_return_ HRESULT LanguageBarButton::Initialize(
_In_ const GUID& guidItem,
_In_ DWORD style,
Expand Down
1 change: 1 addition & 0 deletions VietTypeATL/LanguageBarButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ILanguageBarCallbacks {
virtual HRESULT GetIcon(__RPC__deref_out_opt HICON* hicon) = 0;
virtual DWORD GetStatus() = 0;
virtual std::wstring GetText() = 0;
virtual std::wstring GetTooltipString() = 0;
};

class LanguageBarButton :
Expand Down
83 changes: 12 additions & 71 deletions VietTypeATL/LanguageBarHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,7 @@ HRESULT RefreshableButton::Uninitialize() {
return S_OK;
}

////////////////////////////////////////////////////////////////////////////////
// IndicatorButton
////////////////////////////////////////////////////////////////////////////////

HRESULT IndicatorButton::OnClick(_In_ TfLBIClick click, _In_ POINT pt, __RPC__in const RECT* area) {
HRESULT RefreshableButton::OnClick(_In_ TfLBIClick click, _In_ POINT pt, __RPC__in const RECT* area) {
if (click == TF_LBI_CLK_LEFT) {
return _controller->ToggleUserEnabled();
} else if (click == TF_LBI_CLK_RIGHT) {
Expand All @@ -258,19 +254,19 @@ HRESULT IndicatorButton::OnClick(_In_ TfLBIClick click, _In_ POINT pt, __RPC__in
return S_OK;
}

HRESULT IndicatorButton::InitMenu(__RPC__in_opt ITfMenu* menu) {
HRESULT RefreshableButton::InitMenu(__RPC__in_opt ITfMenu* menu) {
if (!menu) {
return E_INVALIDARG;
}
CopyTfMenu(menu);
return S_OK;
}

HRESULT IndicatorButton::OnMenuSelect(_In_ UINT id) {
HRESULT RefreshableButton::OnMenuSelect(_In_ UINT id) {
return OnMenuSelectAll(id, _controller);
}

HRESULT IndicatorButton::GetIcon(__RPC__deref_out_opt HICON* hicon) {
HRESULT RefreshableButton::GetIcon(__RPC__deref_out_opt HICON* hicon) {
// Windows docs is a liar, icons are mandatory
if (!Globals::DllInstance) {
DBG_DPRINT(L"%s", L"cannot obtain instance");
Expand All @@ -288,82 +284,27 @@ HRESULT IndicatorButton::GetIcon(__RPC__deref_out_opt HICON* hicon) {
return *hicon ? S_OK : E_FAIL;
}

HRESULT IndicatorButton::Refresh() {
HRESULT hr;

hr = _button->NotifyUpdate(TF_LBI_ICON);
HRESULT_CHECK_RETURN(hr, L"%s", L"button->NotifyUpdate failed");

return hr;
}

DWORD IndicatorButton::GetStatus() {
DWORD RefreshableButton::GetStatus() {
return 0;
}

std::wstring IndicatorButton::GetText() {
std::wstring RefreshableButton::GetText() {
return _controller->IsEnabled() ? std::wstring(L"VIE") : std::wstring(L"ENG");
}

////////////////////////////////////////////////////////////////////////////////
// LangBarButton
////////////////////////////////////////////////////////////////////////////////

HRESULT LangBarButton::OnClick(_In_ TfLBIClick click, _In_ POINT pt, __RPC__in const RECT* area) {
if (click == TF_LBI_CLK_LEFT) {
return _controller->ToggleUserEnabled();
} else if (click == TF_LBI_CLK_RIGHT) {
int itemId = PopMenu(pt, area);
if (itemId) {
return OnMenuSelectAll(itemId, _controller);
}
}
return S_OK;
}

HRESULT LangBarButton::InitMenu(__RPC__in_opt ITfMenu* menu) {
if (!menu) {
return E_INVALIDARG;
}
CopyTfMenu(menu);
return S_OK;
}

HRESULT LangBarButton::OnMenuSelect(_In_ UINT id) {
return OnMenuSelectAll(id, _controller);
}

HRESULT LangBarButton::GetIcon(__RPC__deref_out_opt HICON* hicon) {
// Windows docs is a liar, icons are mandatory
if (!Globals::DllInstance) {
DBG_DPRINT(L"%s", L"cannot obtain instance");
return E_FAIL;
}

DWORD light = GetSystemLightTheme();
std::wstring RefreshableButton::GetTooltipString() {
if (_controller->GetBlocked() == EngineController::BlockedKind::Blocked) {
*hicon = static_cast<HICON>(LoadImage(Globals::DllInstance, MAKEINTRESOURCE(light ? IDI_ICONXL : IDI_ICONXD), IMAGE_ICON, 16, 16, 0));
} else if (_controller->IsEnabled()) {
*hicon = static_cast<HICON>(LoadImage(Globals::DllInstance, MAKEINTRESOURCE(light ? IDI_ICONVL : IDI_ICONVD), IMAGE_ICON, 16, 16, 0));
return std::wstring(L"Paused");
} else {
*hicon = static_cast<HICON>(LoadImage(Globals::DllInstance, MAKEINTRESOURCE(light ? IDI_ICONEL : IDI_ICONED), IMAGE_ICON, 16, 16, 0));
return _controller->IsEnabled() ? std::wstring(L"Vietnamese") : std::wstring(L"English");
}
return *hicon ? S_OK : E_FAIL;
}

DWORD LangBarButton::GetStatus() {
return 0;
}

std::wstring LangBarButton::GetText() {
return _controller->IsEnabled() ? std::wstring(L"VIE") : std::wstring(L"ENG");
}

HRESULT LangBarButton::Refresh() {
HRESULT RefreshableButton::Refresh() {
HRESULT hr;

hr = _button->NotifyUpdate(TF_LBI_ICON);
HRESULT_CHECK_RETURN(hr, L"%s", L"_button->NotifyUpdate failed");
hr = _button->NotifyUpdate(TF_LBI_ICON | TF_LBI_TEXT | TF_LBI_TOOLTIP);
HRESULT_CHECK_RETURN(hr, L"%s", L"button->NotifyUpdate failed");

return hr;
}
Expand Down
49 changes: 10 additions & 39 deletions VietTypeATL/LanguageBarHandlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ class RefreshableButton : public ILanguageBarCallbacks {
RefreshableButton& operator=(const RefreshableButton&) = delete;
~RefreshableButton() = default;

virtual HRESULT Refresh() = 0;
virtual HRESULT Refresh();

// Inherited via ILanguageBarCallbacks
virtual HRESULT OnClick(_In_ TfLBIClick click, _In_ POINT pt, __RPC__in const RECT* area) override;
virtual HRESULT InitMenu(__RPC__in_opt ITfMenu* menu) override;
virtual HRESULT OnMenuSelect(_In_ UINT id) override;
virtual HRESULT GetIcon(__RPC__deref_out_opt HICON* hicon) override;
virtual DWORD GetStatus() override;
virtual std::wstring GetText() override;
virtual std::wstring GetTooltipString() override;

// ensure that langbar init does not read engine state
_Check_return_ HRESULT Initialize(
Expand All @@ -37,42 +46,4 @@ class RefreshableButton : public ILanguageBarCallbacks {
CComPtr<ITfLangBarItemMgr> _langBarItemMgr;
};

class IndicatorButton : public RefreshableButton {
public:
IndicatorButton() = default;
IndicatorButton(const IndicatorButton&) = delete;
IndicatorButton& operator=(const IndicatorButton&) = delete;
~IndicatorButton() = default;

// Inherited via ILanguageBarCallbacks
virtual HRESULT OnClick(_In_ TfLBIClick click, _In_ POINT pt, __RPC__in const RECT* area) override;
virtual HRESULT InitMenu(__RPC__in_opt ITfMenu* menu) override;
virtual HRESULT OnMenuSelect(_In_ UINT id) override;
virtual HRESULT GetIcon(__RPC__deref_out_opt HICON* hicon) override;
virtual DWORD GetStatus() override;
virtual std::wstring GetText() override;

// Inherited via RefreshableButton
virtual HRESULT Refresh() override;
};

class LangBarButton : public RefreshableButton {
public:
LangBarButton() = default;
LangBarButton(const LangBarButton&) = delete;
LangBarButton& operator=(const LangBarButton&) = delete;
~LangBarButton() = default;

// Inherited via ILanguageBarCallbacks
virtual HRESULT OnClick(_In_ TfLBIClick click, _In_ POINT pt, __RPC__in const RECT* area) override;
virtual HRESULT InitMenu(__RPC__in_opt ITfMenu* menu) override;
virtual HRESULT OnMenuSelect(_In_ UINT id) override;
virtual HRESULT GetIcon(__RPC__deref_out_opt HICON* hicon) override;
virtual DWORD GetStatus() override;
virtual std::wstring GetText() override;

// Inherited via RefreshableButton
virtual HRESULT Refresh() override;
};

}

0 comments on commit 4225133

Please sign in to comment.