From 163be2ffd4d03f031d56aec41be8e074ab1d35de Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Tue, 18 May 2021 14:25:12 +0100 Subject: [PATCH] app/internal/wm: [Windows] handle WM_DESTROY properly After handling WM_DESTROY, the system will destroy the window for us. This change makes sure destroy events are sent and handled before the window can no longer be used. Signed-off-by: Elias Naur --- app/internal/wm/os_windows.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/app/internal/wm/os_windows.go b/app/internal/wm/os_windows.go index 71170498..5e6dbc6c 100644 --- a/app/internal/wm/os_windows.go +++ b/app/internal/wm/os_windows.go @@ -110,13 +110,11 @@ func NewWindow(window Callbacks, opts *Options) error { cerr <- err return } - defer w.destroy() cerr <- nil winMap.Store(w.hwnd, w) defer winMap.Delete(w.hwnd) w.w = window w.w.SetDriver(w) - defer w.w.Event(system.DestroyEvent{}) w.Option(opts) windows.ShowWindow(w.hwnd, windows.SW_SHOWDEFAULT) windows.SetForegroundWindow(w.hwnd) @@ -293,6 +291,13 @@ func windowProc(hwnd syscall.Handle, msg uint32, wParam, lParam uintptr) uintptr case windows.WM_MOUSEHWHEEL: w.scrollEvent(wParam, lParam, true) case windows.WM_DESTROY: + w.w.Event(system.DestroyEvent{}) + if w.hdc != 0 { + windows.ReleaseDC(w.hdc) + w.hdc = 0 + } + // The system destroys the HWND for us. + w.hwnd = 0 windows.PostQuitMessage(0) case windows.WM_PAINT: w.draw(true) @@ -475,17 +480,6 @@ func (w *window) draw(sync bool) { }) } -func (w *window) destroy() { - if w.hdc != 0 { - windows.ReleaseDC(w.hdc) - w.hdc = 0 - } - if w.hwnd != 0 { - windows.DestroyWindow(w.hwnd) - w.hwnd = 0 - } -} - func (w *window) NewContext() (Context, error) { sort.Slice(drivers, func(i, j int) bool { return drivers[i].priority < drivers[j].priority