From 4225133ab6b472c73493a0e519a772a91fc61b07 Mon Sep 17 00:00:00 2001 From: Tu Dinh Date: Sun, 30 May 2021 21:33:26 +0200 Subject: [PATCH] refactor language bar, add tooltip --- VietTypeATL/EngineController.cpp | 7 ++- VietTypeATL/EngineController.h | 7 ++- VietTypeATL/LanguageBarButton.cpp | 15 ++++-- VietTypeATL/LanguageBarButton.h | 1 + VietTypeATL/LanguageBarHandlers.cpp | 83 +++++------------------------ VietTypeATL/LanguageBarHandlers.h | 49 ++++------------- 6 files changed, 40 insertions(+), 122 deletions(-) diff --git a/VietTypeATL/EngineController.cpp b/VietTypeATL/EngineController.cpp index 727030e..fe66c8c 100644 --- a/VietTypeATL/EngineController.cpp +++ b/VietTypeATL/EngineController.cpp @@ -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 { @@ -225,7 +224,7 @@ HRESULT EngineController::UpdateStates() { _Check_return_ HRESULT EngineController::InitLanguageBar() { HRESULT hr; - _indicatorButton = std::make_unique(); + _indicatorButton = std::make_unique(); hr = _indicatorButton->Initialize( this, _langBarItemMgr, @@ -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 = std::make_unique(); hr = _langBarButton->Initialize( this, _langBarItemMgr, diff --git a/VietTypeATL/EngineController.h b/VietTypeATL/EngineController.h index ec41bb6..b9808e5 100644 --- a/VietTypeATL/EngineController.h +++ b/VietTypeATL/EngineController.h @@ -10,8 +10,7 @@ namespace VietType { -class IndicatorButton; -class LangBarButton; +class RefreshableButton; class CompositionManager; class EngineController; class SettingsDialog; @@ -85,8 +84,8 @@ class EngineController : SinkAdvisor _openCloseCompartmentEventSink; // unique_ptr is not necessary but used just to break include cycle - std::unique_ptr _indicatorButton; - std::unique_ptr _langBarButton; + std::unique_ptr _indicatorButton; + std::unique_ptr _langBarButton; CComPtr _settings; diff --git a/VietTypeATL/LanguageBarButton.cpp b/VietTypeATL/LanguageBarButton.cpp index 4c0dc3b..d26eef1 100644 --- a/VietTypeATL/LanguageBarButton.cpp +++ b/VietTypeATL/LanguageBarButton.cpp @@ -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); @@ -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, diff --git a/VietTypeATL/LanguageBarButton.h b/VietTypeATL/LanguageBarButton.h index 06c2828..98f94bf 100644 --- a/VietTypeATL/LanguageBarButton.h +++ b/VietTypeATL/LanguageBarButton.h @@ -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 : diff --git a/VietTypeATL/LanguageBarHandlers.cpp b/VietTypeATL/LanguageBarHandlers.cpp index 427ba37..793ed8e 100644 --- a/VietTypeATL/LanguageBarHandlers.cpp +++ b/VietTypeATL/LanguageBarHandlers.cpp @@ -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) { @@ -258,7 +254,7 @@ 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; } @@ -266,11 +262,11 @@ HRESULT IndicatorButton::InitMenu(__RPC__in_opt ITfMenu* 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"); @@ -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(LoadImage(Globals::DllInstance, MAKEINTRESOURCE(light ? IDI_ICONXL : IDI_ICONXD), IMAGE_ICON, 16, 16, 0)); - } else if (_controller->IsEnabled()) { - *hicon = static_cast(LoadImage(Globals::DllInstance, MAKEINTRESOURCE(light ? IDI_ICONVL : IDI_ICONVD), IMAGE_ICON, 16, 16, 0)); + return std::wstring(L"Paused"); } else { - *hicon = static_cast(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; } diff --git a/VietTypeATL/LanguageBarHandlers.h b/VietTypeATL/LanguageBarHandlers.h index ebfc1dc..e65e029 100644 --- a/VietTypeATL/LanguageBarHandlers.h +++ b/VietTypeATL/LanguageBarHandlers.h @@ -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( @@ -37,42 +46,4 @@ class RefreshableButton : public ILanguageBarCallbacks { CComPtr _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; -}; - }