diff options
Diffstat (limited to 'src/win')
-rw-r--r-- | src/win/native/InputMethod.cpp | 65 | ||||
-rw-r--r-- | src/win/native/UiApplication.cpp | 8 | ||||
-rw-r--r-- | src/win/native/Window.cpp | 26 |
3 files changed, 27 insertions, 72 deletions
diff --git a/src/win/native/InputMethod.cpp b/src/win/native/InputMethod.cpp index d976a8ba..45c5f8da 100644 --- a/src/win/native/InputMethod.cpp +++ b/src/win/native/InputMethod.cpp @@ -145,30 +145,23 @@ CompositionText GetCompositionInfo(HIMC imm_context) { WinInputMethodContext::WinInputMethodContext( gsl::not_null<WinNativeWindow*> window) - : native_window_resolver_(window->GetResolver()) { - event_revoker_guards_.push_back( - EventRevokerGuard(window->NativeMessageEvent()->AddHandler( - std::bind(&WinInputMethodContext::OnWindowNativeMessage, this, - std::placeholders::_1)))); + : native_window_(window) { + event_guard_ += window->NativeMessageEvent()->AddHandler( + std::bind(&WinInputMethodContext::OnWindowNativeMessage, this, + std::placeholders::_1)); } WinInputMethodContext::~WinInputMethodContext() {} void WinInputMethodContext::EnableIME() { - const auto native_window = Resolve(native_window_resolver_.get()); - if (native_window == nullptr) return; - const auto hwnd = native_window->GetWindowHandle(); - + const auto hwnd = native_window_->GetWindowHandle(); if (::ImmAssociateContextEx(hwnd, nullptr, IACE_DEFAULT) == FALSE) { log::TagWarn(log_tag, u"Failed to enable ime."); } } void WinInputMethodContext::DisableIME() { - const auto native_window = Resolve(native_window_resolver_.get()); - if (native_window == nullptr) return; - const auto hwnd = native_window->GetWindowHandle(); - + const auto hwnd = native_window_->GetWindowHandle(); AutoHIMC himc{hwnd}; if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_COMPLETE, 0)) { @@ -182,46 +175,32 @@ void WinInputMethodContext::DisableIME() { } void WinInputMethodContext::CompleteComposition() { - auto optional_himc = TryGetHIMC(); - if (!optional_himc.has_value()) return; - auto himc = *std::move(optional_himc); - + auto himc = GetHIMC(); if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_COMPLETE, 0)) { log::TagWarn(log_tag, u"Failed to complete composition."); } } void WinInputMethodContext::CancelComposition() { - auto optional_himc = TryGetHIMC(); - if (!optional_himc.has_value()) return; - auto himc = *std::move(optional_himc); - + auto himc = GetHIMC(); if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_CANCEL, 0)) { log::TagWarn(log_tag, u"Failed to complete composition."); } } CompositionText WinInputMethodContext::GetCompositionText() { - auto optional_himc = TryGetHIMC(); - if (!optional_himc.has_value()) return CompositionText{}; - auto himc = *std::move(optional_himc); - + auto himc = GetHIMC(); return GetCompositionInfo(himc.Get()); } void WinInputMethodContext::SetCandidateWindowPosition(const Point& point) { - auto optional_himc = TryGetHIMC(); - if (!optional_himc.has_value()) return; - auto himc = *std::move(optional_himc); + auto himc = GetHIMC(); ::CANDIDATEFORM form; form.dwIndex = 1; form.dwStyle = CFS_CANDIDATEPOS; - auto window = - dynamic_cast<WinNativeWindow*>(this->native_window_resolver_->Resolve()); - form.ptCurrentPos = - window == nullptr ? POINT{0, 0} : window->DipToPixel(point); + form.ptCurrentPos = native_window_->DipToPixel(point); if (!::ImmSetCandidateWindow(himc.Get(), &form)) log::TagDebug(log_tag, @@ -287,29 +266,13 @@ void WinInputMethodContext::OnWindowNativeMessage( } std::u16string WinInputMethodContext::GetResultString() { - auto optional_himc = TryGetHIMC(); - if (!optional_himc.has_value()) return u""; - auto himc = *std::move(optional_himc); - + auto himc = GetHIMC(); auto result = win::GetResultString(himc.Get()); return result; } -std::optional<AutoHIMC> WinInputMethodContext::TryGetHIMC() { - const auto native_window = Resolve(native_window_resolver_.get()); - if (native_window == nullptr) return std::nullopt; - const auto hwnd = native_window->GetWindowHandle(); +AutoHIMC WinInputMethodContext::GetHIMC() { + const auto hwnd = native_window_->GetWindowHandle(); return AutoHIMC{hwnd}; } - -WinInputMethodManager::WinInputMethodManager(WinUiApplication*) {} - -WinInputMethodManager::~WinInputMethodManager() {} - -std::unique_ptr<IInputMethodContext> WinInputMethodManager::GetContext( - INativeWindow* window) { - Expects(window); - const auto w = CheckPlatform<WinNativeWindow>(window, GetPlatformId()); - return std::make_unique<WinInputMethodContext>(w); -} } // namespace cru::platform::native::win diff --git a/src/win/native/UiApplication.cpp b/src/win/native/UiApplication.cpp index 3f7a0cf5..60ff8e8c 100644 --- a/src/win/native/UiApplication.cpp +++ b/src/win/native/UiApplication.cpp @@ -100,15 +100,13 @@ std::vector<INativeWindow*> WinUiApplication::GetAllWindow() { return result; } -std::shared_ptr<INativeWindowResolver> WinUiApplication::CreateWindow( - INativeWindow* parent) { +INativeWindow* WinUiApplication::CreateWindow(INativeWindow* parent) { WinNativeWindow* p = nullptr; if (parent != nullptr) { p = CheckPlatform<WinNativeWindow>(parent, GetPlatformId()); } - return (new WinNativeWindow(this, window_manager_->GetGeneralWindowClass(), - WS_OVERLAPPEDWINDOW, p)) - ->GetResolver(); + return new WinNativeWindow(this, window_manager_->GetGeneralWindowClass(), + WS_OVERLAPPEDWINDOW, p); } cru::platform::graph::IGraphFactory* WinUiApplication::GetGraphFactory() { diff --git a/src/win/native/Window.cpp b/src/win/native/Window.cpp index 735221ca..d9237c4f 100644 --- a/src/win/native/Window.cpp +++ b/src/win/native/Window.cpp @@ -3,23 +3,24 @@ #include "WindowManager.hpp" #include "cru/common/Logger.hpp" #include "cru/platform/Check.hpp" +#include "cru/platform/native/Base.hpp" #include "cru/win/graph/direct/WindowPainter.hpp" #include "cru/win/native/Cursor.hpp" #include "cru/win/native/Exception.hpp" +#include "cru/win/native/InputMethod.hpp" #include "cru/win/native/Keyboard.hpp" #include "cru/win/native/UiApplication.hpp" #include "cru/win/native/WindowClass.hpp" #include <imm.h> #include <windowsx.h> +#include <memory> namespace cru::platform::native::win { WinNativeWindow::WinNativeWindow(WinUiApplication* application, WindowClass* window_class, DWORD window_style, WinNativeWindow* parent) - : application_(application), - resolver_(std::make_shared<WinNativeWindowResolver>(this)), - parent_window_(parent) { + : application_(application), parent_window_(parent) { Expects(application); // application can't be null. if (parent != nullptr) { @@ -52,6 +53,8 @@ WinNativeWindow::WinNativeWindow(WinUiApplication* application, std::make_unique<graph::win::direct::D2DWindowRenderTarget>( application->GetDirectFactory(), hwnd_); window_render_target_->SetDpi(dpi_, dpi_); + + input_method_context_ = std::make_unique<WinInputMethodContext>(this); } WinNativeWindow::~WinNativeWindow() { @@ -59,7 +62,6 @@ WinNativeWindow::~WinNativeWindow() { sync_flag_ = true; Close(); } - resolver_->Reset(); } void WinNativeWindow::Close() { ::DestroyWindow(hwnd_); } @@ -197,6 +199,10 @@ void WinNativeWindow::SetCursor(std::shared_ptr<ICursor> cursor) { } } +IInputMethodContext* WinNativeWindow::GetInputMethodContext() { + return static_cast<IInputMethodContext*>(input_method_context_.get()); +} + bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param, LRESULT* result) { @@ -443,16 +449,4 @@ void WinNativeWindow::OnKeyUpInternal(int virtual_code) { void WinNativeWindow::OnActivatedInternal() {} void WinNativeWindow::OnDeactivatedInternal() {} - -void WinNativeWindowResolver::Reset() { - Expects(window_); // already reset, can't reset again - window_ = nullptr; -} - -WinNativeWindow* Resolve(gsl::not_null<INativeWindowResolver*> resolver) { - const auto window = resolver->Resolve(); - return window == nullptr ? nullptr - : CheckPlatform<WinNativeWindow>( - window, WinNativeResource::k_platform_id); -} // namespace cru::platform::native::win } // namespace cru::platform::native::win |