aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-01-03 00:31:34 +0800
committercrupest <crupest@outlook.com>2020-01-03 00:31:34 +0800
commitd5ff69096a3f56052b30d8ef827845473d4aa5ea (patch)
tree8faf7466ea173b66e81da83bdc4e0be1a159e263 /src
parent26c3b5c7a509d0123719f2a6537399c332a48011 (diff)
downloadcru-d5ff69096a3f56052b30d8ef827845473d4aa5ea.tar.gz
cru-d5ff69096a3f56052b30d8ef827845473d4aa5ea.tar.bz2
cru-d5ff69096a3f56052b30d8ef827845473d4aa5ea.zip
...
Diffstat (limited to 'src')
-rw-r--r--src/ui/window.cpp90
-rw-r--r--src/win/native/ui_application.cpp8
-rw-r--r--src/win/native/window.cpp130
3 files changed, 119 insertions, 109 deletions
diff --git a/src/ui/window.cpp b/src/ui/window.cpp
index 6ff44b4b..35463778 100644
--- a/src/ui/window.cpp
+++ b/src/ui/window.cpp
@@ -11,6 +11,8 @@
namespace cru::ui {
using cru::platform::native::FocusChangeType;
+using cru::platform::native::INativeWindow;
+using cru::platform::native::INativeWindowResolver;
using cru::platform::native::IUiApplication;
using cru::platform::native::MouseEnterLeaveType;
@@ -88,11 +90,12 @@ Window* Window::CreateOverlapped() {
namespace {
template <typename T>
-void BindNativeEvent(Window* window, IEvent<T>* event,
- void (Window::*handler)(typename IEvent<T>::EventArgs),
- std::vector<EventRevokerGuard>& guard_pool) {
- guard_pool.push_back(EventRevokerGuard(
- event->AddHandler(std::bind(handler, window, std::placeholders::_1))));
+inline void BindNativeEvent(
+ Window* window, INativeWindow* native_window, IEvent<T>* event,
+ void (Window::*handler)(INativeWindow*, typename IEvent<T>::EventArgs),
+ std::vector<EventRevokerGuard>& guard_pool) {
+ guard_pool.push_back(EventRevokerGuard(event->AddHandler(
+ std::bind(handler, window, native_window, std::placeholders::_1))));
}
} // namespace
@@ -101,30 +104,34 @@ Window::Window(tag_overlapped_constructor)
focus_control_(this),
mouse_captured_control_(nullptr) {
window_ = this;
- native_window_ = IUiApplication::GetInstance()->CreateWindow(nullptr);
+ native_window_resolver_ =
+ IUiApplication::GetInstance()->CreateWindow(nullptr);
+
+ const auto native_window = native_window_resolver_->Resolve();
+
render_object_.reset(new render::WindowRenderObject(this));
render_object_->SetAttachedControl(this);
- BindNativeEvent(this, native_window_->DestroyEvent(),
+ BindNativeEvent(this, native_window, native_window->DestroyEvent(),
&Window::OnNativeDestroy, event_revoker_guards_);
- BindNativeEvent(this, native_window_->PaintEvent(), &Window::OnNativePaint,
- event_revoker_guards_);
- BindNativeEvent(this, native_window_->ResizeEvent(), &Window::OnNativeResize,
- event_revoker_guards_);
- BindNativeEvent(this, native_window_->FocusEvent(), &Window::OnNativeFocus,
- event_revoker_guards_);
- BindNativeEvent(this, native_window_->MouseEnterLeaveEvent(),
+ BindNativeEvent(this, native_window, native_window->PaintEvent(),
+ &Window::OnNativePaint, event_revoker_guards_);
+ BindNativeEvent(this, native_window, native_window->ResizeEvent(),
+ &Window::OnNativeResize, event_revoker_guards_);
+ BindNativeEvent(this, native_window, native_window->FocusEvent(),
+ &Window::OnNativeFocus, event_revoker_guards_);
+ BindNativeEvent(this, native_window, native_window->MouseEnterLeaveEvent(),
&Window::OnNativeMouseEnterLeave, event_revoker_guards_);
- BindNativeEvent(this, native_window_->MouseMoveEvent(),
+ BindNativeEvent(this, native_window, native_window->MouseMoveEvent(),
&Window::OnNativeMouseMove, event_revoker_guards_);
- BindNativeEvent(this, native_window_->MouseDownEvent(),
+ BindNativeEvent(this, native_window, native_window->MouseDownEvent(),
&Window::OnNativeMouseDown, event_revoker_guards_);
- BindNativeEvent(this, native_window_->MouseUpEvent(),
+ BindNativeEvent(this, native_window, native_window->MouseUpEvent(),
&Window::OnNativeMouseUp, event_revoker_guards_);
- BindNativeEvent(this, native_window_->KeyDownEvent(),
+ BindNativeEvent(this, native_window, native_window->KeyDownEvent(),
&Window::OnNativeKeyDown, event_revoker_guards_);
- BindNativeEvent(this, native_window_->KeyUpEvent(), &Window::OnNativeKeyUp,
- event_revoker_guards_);
+ BindNativeEvent(this, native_window, native_window->KeyUpEvent(),
+ &Window::OnNativeKeyUp, event_revoker_guards_);
}
Window::~Window() {
@@ -138,6 +145,10 @@ render::RenderObject* Window::GetRenderObject() const {
return render_object_.get();
}
+platform::native::INativeWindow* Window::GetNativeWindow() {
+ return native_window_resolver_->Resolve();
+}
+
bool Window::RequestFocusFor(Control* control) {
assert(control != nullptr); // The control to request focus can't be null.
// You can set it as the window.
@@ -194,21 +205,27 @@ Control* Window::HitTest(const Point& point) {
return render_object_->HitTest(point)->GetAttachedControl();
}
-void Window::OnNativeDestroy(std::nullptr_t) { delete this; }
+void Window::OnNativeDestroy(INativeWindow* window, std::nullptr_t) {
+ CRU_UNUSED(window)
+ delete this;
+}
-void Window::OnNativePaint(std::nullptr_t) {
- auto painter = native_window_->BeginPaint();
+void Window::OnNativePaint(INativeWindow* window, std::nullptr_t) {
+ auto painter = window->BeginPaint();
render_object_->Draw(painter.get());
painter->EndDraw();
}
-void Window::OnNativeResize(const Size& size) {
+void Window::OnNativeResize(INativeWindow* window, const Size& size) {
+ CRU_UNUSED(window)
CRU_UNUSED(size)
render_object_->GetRenderHost()->InvalidateLayout();
}
-void Window::OnNativeFocus(FocusChangeType focus) {
+void Window::OnNativeFocus(INativeWindow* window, FocusChangeType focus) {
+ CRU_UNUSED(window)
+
focus == FocusChangeType::Gain
? DispatchEvent(event_names::GainFocus, focus_control_,
&Control::GainFocusEvent, nullptr, true)
@@ -216,7 +233,10 @@ void Window::OnNativeFocus(FocusChangeType focus) {
&Control::LoseFocusEvent, nullptr, true);
}
-void Window::OnNativeMouseEnterLeave(MouseEnterLeaveType type) {
+void Window::OnNativeMouseEnterLeave(INativeWindow* window,
+ MouseEnterLeaveType type) {
+ CRU_UNUSED(window)
+
if (type == MouseEnterLeaveType::Leave) {
DispatchEvent(event_names::MouseLeave, mouse_hover_control_,
&Control::MouseLeaveEvent, nullptr);
@@ -224,7 +244,9 @@ void Window::OnNativeMouseEnterLeave(MouseEnterLeaveType type) {
}
}
-void Window::OnNativeMouseMove(const Point& point) {
+void Window::OnNativeMouseMove(INativeWindow* window, const Point& point) {
+ CRU_UNUSED(window)
+
// Find the first control that hit test succeed.
const auto new_mouse_hover_control = HitTest(point);
const auto old_mouse_hover_control = mouse_hover_control_;
@@ -256,7 +278,10 @@ void Window::OnNativeMouseMove(const Point& point) {
}
void Window::OnNativeMouseDown(
+ INativeWindow* window,
const platform::native::NativeMouseButtonEventArgs& args) {
+ CRU_UNUSED(window)
+
Control* control =
mouse_captured_control_ ? mouse_captured_control_ : HitTest(args.point);
DispatchEvent(event_names::MouseDown, control, &Control::MouseDownEvent,
@@ -264,19 +289,26 @@ void Window::OnNativeMouseDown(
}
void Window::OnNativeMouseUp(
+ INativeWindow* window,
const platform::native::NativeMouseButtonEventArgs& args) {
+ CRU_UNUSED(window)
+
Control* control =
mouse_captured_control_ ? mouse_captured_control_ : HitTest(args.point);
DispatchEvent(event_names::MouseUp, control, &Control::MouseUpEvent, nullptr,
args.point, args.button);
}
-void Window::OnNativeKeyDown(int virtual_code) {
+void Window::OnNativeKeyDown(INativeWindow* window, int virtual_code) {
+ CRU_UNUSED(window)
+
DispatchEvent(event_names::KeyDown, focus_control_, &Control::KeyDownEvent,
nullptr, virtual_code);
}
-void Window::OnNativeKeyUp(int virtual_code) {
+void Window::OnNativeKeyUp(INativeWindow* window, int virtual_code) {
+ CRU_UNUSED(window)
+
DispatchEvent(event_names::KeyUp, focus_control_, &Control::KeyUpEvent,
nullptr, virtual_code);
}
diff --git a/src/win/native/ui_application.cpp b/src/win/native/ui_application.cpp
index 75fce6ce..9ab61551 100644
--- a/src/win/native/ui_application.cpp
+++ b/src/win/native/ui_application.cpp
@@ -103,13 +103,15 @@ std::vector<INativeWindow*> WinUiApplication::GetAllWindow() {
return result;
}
-INativeWindow* WinUiApplication::CreateWindow(INativeWindow* parent) {
+std::shared_ptr<INativeWindowResolver> 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);
+ return (new WinNativeWindow(this, window_manager_->GetGeneralWindowClass(),
+ WS_OVERLAPPEDWINDOW, p))
+ ->GetResolver();
}
cru::platform::graph::IGraphFactory* WinUiApplication::GetGraphFactory() {
diff --git a/src/win/native/window.cpp b/src/win/native/window.cpp
index a8016676..2e99a5cb 100644
--- a/src/win/native/window.cpp
+++ b/src/win/native/window.cpp
@@ -23,10 +23,12 @@ inline Point PiToDip(const POINT& pi_point) {
WinNativeWindow::WinNativeWindow(WinUiApplication* application,
WindowClass* window_class, DWORD window_style,
WinNativeWindow* parent)
- : application_(application), parent_window_(parent) {
+ : application_(application),
+ resolver_(std::make_shared<WinNativeWindowResolver>(this)),
+ parent_window_(parent) {
assert(application); // application can't be null.
- if (parent != nullptr && !parent->IsValid()) {
+ if (parent != nullptr) {
throw new std::runtime_error("Can't use a invalid window as parent.");
}
@@ -51,63 +53,45 @@ WinNativeWindow::WinNativeWindow(WinUiApplication* application,
}
WinNativeWindow::~WinNativeWindow() {
- if (IsValid()) {
- SetDeleteThisOnDestroy(false); // avoid double delete.
+ if (!sync_flag_) {
+ sync_flag_ = true;
Close();
}
+ resolver_->Reset();
}
-bool WinNativeWindow::IsValid() { return hwnd_ != nullptr; }
+void WinNativeWindow::Close() { ::DestroyWindow(hwnd_); }
-void WinNativeWindow::SetDeleteThisOnDestroy(bool value) {
- delete_this_on_destroy_ = value;
-}
-
-void WinNativeWindow::Close() {
- if (IsValid()) DestroyWindow(hwnd_);
-}
-
-bool WinNativeWindow::IsVisible() {
- if (IsValid()) return ::IsWindowVisible(hwnd_);
- return false;
-}
+bool WinNativeWindow::IsVisible() { return ::IsWindowVisible(hwnd_); }
void WinNativeWindow::SetVisible(bool is_visible) {
- if (!IsValid()) return;
is_visible ? ShowWindow(hwnd_, SW_SHOWNORMAL) : ShowWindow(hwnd_, SW_HIDE);
}
Size WinNativeWindow::GetClientSize() {
- if (!IsValid()) return Size{};
-
const auto pixel_rect = GetClientRectPixel();
return Size(PixelToDipX(pixel_rect.right), PixelToDipY(pixel_rect.bottom));
}
void WinNativeWindow::SetClientSize(const Size& size) {
- if (IsValid()) {
- const auto window_style =
- static_cast<DWORD>(GetWindowLongPtr(hwnd_, GWL_STYLE));
- const auto window_ex_style =
- static_cast<DWORD>(GetWindowLongPtr(hwnd_, GWL_EXSTYLE));
-
- RECT rect;
- rect.left = 0;
- rect.top = 0;
- rect.right = DipToPixelX(size.width);
- rect.bottom = DipToPixelY(size.height);
- if (!AdjustWindowRectEx(&rect, window_style, FALSE, window_ex_style))
- throw Win32Error(::GetLastError(),
- "Failed to invoke AdjustWindowRectEx.");
-
- if (!SetWindowPos(hwnd_, nullptr, 0, 0, rect.right - rect.left,
- rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE))
- throw Win32Error(::GetLastError(), "Failed to invoke SetWindowPos.");
- }
+ const auto window_style =
+ static_cast<DWORD>(GetWindowLongPtr(hwnd_, GWL_STYLE));
+ const auto window_ex_style =
+ static_cast<DWORD>(GetWindowLongPtr(hwnd_, GWL_EXSTYLE));
+
+ RECT rect;
+ rect.left = 0;
+ rect.top = 0;
+ rect.right = DipToPixelX(size.width);
+ rect.bottom = DipToPixelY(size.height);
+ if (!AdjustWindowRectEx(&rect, window_style, FALSE, window_ex_style))
+ throw Win32Error(::GetLastError(), "Failed to invoke AdjustWindowRectEx.");
+
+ if (!SetWindowPos(hwnd_, nullptr, 0, 0, rect.right - rect.left,
+ rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE))
+ throw Win32Error(::GetLastError(), "Failed to invoke SetWindowPos.");
}
Rect WinNativeWindow::GetWindowRect() {
- if (!IsValid()) return Rect{};
-
RECT rect;
if (!::GetWindowRect(hwnd_, &rect))
throw Win32Error(::GetLastError(), "Failed to invoke GetWindowRect.");
@@ -117,50 +101,37 @@ Rect WinNativeWindow::GetWindowRect() {
}
void WinNativeWindow::SetWindowRect(const Rect& rect) {
- if (IsValid()) {
- if (!SetWindowPos(hwnd_, nullptr, DipToPixelX(rect.left),
- DipToPixelY(rect.top), DipToPixelX(rect.GetRight()),
- DipToPixelY(rect.GetBottom()), SWP_NOZORDER))
- throw Win32Error(::GetLastError(), "Failed to invoke SetWindowPos.");
- }
+ if (!SetWindowPos(hwnd_, nullptr, DipToPixelX(rect.left),
+ DipToPixelY(rect.top), DipToPixelX(rect.GetRight()),
+ DipToPixelY(rect.GetBottom()), SWP_NOZORDER))
+ throw Win32Error(::GetLastError(), "Failed to invoke SetWindowPos.");
}
Point WinNativeWindow::GetMousePosition() {
- if (IsValid()) {
- POINT p;
- if (!::GetCursorPos(&p))
- throw Win32Error(::GetLastError(), "Failed to get cursor position.");
- if (!::ScreenToClient(hwnd_, &p))
- throw Win32Error(::GetLastError(), "Failed to call ScreenToClient.");
- return PiToDip(p);
- }
- return Point{};
+ POINT p;
+ if (!::GetCursorPos(&p))
+ throw Win32Error(::GetLastError(), "Failed to get cursor position.");
+ if (!::ScreenToClient(hwnd_, &p))
+ throw Win32Error(::GetLastError(), "Failed to call ScreenToClient.");
+ return PiToDip(p);
}
bool WinNativeWindow::CaptureMouse() {
- if (IsValid()) {
- ::SetCapture(hwnd_);
- return true;
- }
- return false;
+ ::SetCapture(hwnd_);
+ return true;
}
bool WinNativeWindow::ReleaseMouse() {
- if (IsValid()) {
- const auto result = ::ReleaseCapture();
- return result != 0;
- }
- return false;
+ const auto result = ::ReleaseCapture();
+ return result != 0;
}
void WinNativeWindow::RequestRepaint() {
- if (IsValid()) {
- log::Debug("A repaint is requested.");
- if (!::InvalidateRect(hwnd_, nullptr, FALSE))
- throw Win32Error(::GetLastError(), "Failed to invalidate window.");
- if (!::UpdateWindow(hwnd_))
- throw Win32Error(::GetLastError(), "Failed to update window.");
- }
+ log::Debug("A repaint is requested.");
+ if (!::InvalidateRect(hwnd_, nullptr, FALSE))
+ throw Win32Error(::GetLastError(), "Failed to invalidate window.");
+ if (!::UpdateWindow(hwnd_))
+ throw Win32Error(::GetLastError(), "Failed to update window.");
}
std::unique_ptr<graph::IPainter> WinNativeWindow::BeginPaint() {
@@ -172,8 +143,6 @@ void WinNativeWindow::SetCursor(std::shared_ptr<ICursor> cursor) {
throw std::runtime_error("Can't use a nullptr as cursor.");
}
- if (!IsValid()) return;
-
cursor_ = CheckPlatform<WinCursor>(cursor, GetPlatformId());
if (!::SetClassLongPtrW(hwnd_, GCLP_HCURSOR,
@@ -358,8 +327,10 @@ void WinNativeWindow::OnDestroyInternal() {
application_->GetWindowManager()->UnregisterWindow(hwnd_);
hwnd_ = nullptr;
destroy_event_.Raise(nullptr);
- if (delete_this_on_destroy_)
- application_->InvokeLater([this] { delete this; });
+ if (!sync_flag_) {
+ sync_flag_ = true;
+ delete this;
+ }
}
void WinNativeWindow::OnPaintInternal() {
@@ -438,4 +409,9 @@ void WinNativeWindow::OnCharInternal(wchar_t c) { CRU_UNUSED(c) }
void WinNativeWindow::OnActivatedInternal() {}
void WinNativeWindow::OnDeactivatedInternal() {}
+
+void WinNativeWindowResolver::Reset() {
+ assert(window_); // already reset, can't reset again
+ window_ = nullptr;
+}
} // namespace cru::platform::native::win