5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-21 19:39:29 +08:00

[v3 windows] Fix window lose focus events.

This commit is contained in:
Lea Anthony 2023-07-04 20:43:21 +10:00
parent 3efab5ba23
commit 10144adf61
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
2 changed files with 22 additions and 48 deletions

View File

@ -46,54 +46,31 @@ func (s *windowsSystemTray) positionWindow(window *WebviewWindow, offset int) er
screenBounds := currentScreen.WorkArea screenBounds := currentScreen.WorkArea
newX := screenBounds.Width - window.Width()
newY := screenBounds.Height - window.Height()
taskbarBounds := w32.GetTaskbarPosition() taskbarBounds := w32.GetTaskbarPosition()
switch taskbarBounds.UEdge { switch taskbarBounds.UEdge {
case w32.ABE_LEFT: case w32.ABE_LEFT:
if trayBounds == nil { if trayBounds != nil && trayBounds.Y-(window.Height()/2) >= 0 {
// Move it to the bottom left corner of the screen newY = trayBounds.Y - (window.Height() / 2)
window.SetRelativePosition(offset, screenBounds.Height-window.Height())
return nil
} }
newHeight := trayBounds.Y - (window.Height() / 2) window.SetRelativePosition(offset, newY)
if newHeight < 0 {
newHeight = 0
}
// Move it to the top left corner of the screen
window.SetRelativePosition(offset, newHeight)
case w32.ABE_TOP: case w32.ABE_TOP:
if trayBounds == nil { if trayBounds != nil && trayBounds.X-(window.Width()/2) <= newX {
// Move it to the top right corner of the screen newX = trayBounds.X - (window.Width() / 2)
window.SetRelativePosition(screenBounds.Width-window.Width(), offset)
return nil
} }
newWidth := trayBounds.X - (window.Width() / 2) window.SetRelativePosition(newX, offset)
if newWidth > screenBounds.Width-window.Width() {
newWidth = screenBounds.Width - window.Width()
}
// Move it to the top left corner of the screen
window.SetRelativePosition(newWidth, offset)
case w32.ABE_RIGHT: case w32.ABE_RIGHT:
if trayBounds == nil { if trayBounds != nil && trayBounds.Y-(window.Height()/2) <= newY {
// Move it to the bottom right corner of the screen newY = trayBounds.Y - (window.Height() / 2)
window.SetRelativePosition(screenBounds.Width-window.Width()-offset, screenBounds.Height-window.Height())
return nil
} }
newHeight := trayBounds.Y - (window.Height() / 2) window.SetRelativePosition(screenBounds.Width-window.Width()-offset, newY)
if newHeight > screenBounds.Height-window.Height() {
newHeight = screenBounds.Height - window.Height()
}
window.SetRelativePosition(screenBounds.Width-window.Width()-offset, newHeight)
case w32.ABE_BOTTOM: case w32.ABE_BOTTOM:
if trayBounds == nil { if trayBounds != nil && trayBounds.X-(window.Width()/2) <= newX {
// Move it to the bottom right corner of the screen newX = trayBounds.X - (window.Width() / 2)
window.SetRelativePosition(screenBounds.Width-window.Width(), screenBounds.Height-window.Height()-offset)
return nil
} }
newWidth := trayBounds.X - (window.Width() / 2) window.SetRelativePosition(newX, screenBounds.Height-window.Height()-offset)
if newWidth > screenBounds.Width-window.Width() {
newWidth = screenBounds.Width - window.Width()
}
window.SetRelativePosition(newWidth, screenBounds.Height-window.Height()-offset)
} }
return nil return nil
} }

View File

@ -61,10 +61,7 @@ type windowsWebviewWindow struct {
// resizeBorder* is the width/height of the resize border in pixels. // resizeBorder* is the width/height of the resize border in pixels.
resizeBorderWidth int32 resizeBorderWidth int32
resizeBorderHeight int32 resizeBorderHeight int32
focusingChromium bool
// Internal flag to prevent closing the window almost immediately after opening it.
// This happens when the window is opened after clicking a notification icon
justOpened bool
} }
func (w *windowsWebviewWindow) setAbsolutePosition(x int, y int) { func (w *windowsWebviewWindow) setAbsolutePosition(x int, y int) {
@ -566,7 +563,9 @@ func (w *windowsWebviewWindow) focus() {
func (w *windowsWebviewWindow) show() { func (w *windowsWebviewWindow) show() {
w32.ShowWindow(w.hwnd, w32.SW_SHOW) w32.ShowWindow(w.hwnd, w32.SW_SHOW)
w.focusingChromium = true
w.chromium.Focus() w.chromium.Focus()
w.focusingChromium = false
} }
func (w *windowsWebviewWindow) hide() { func (w *windowsWebviewWindow) hide() {
@ -740,6 +739,9 @@ func (w *windowsWebviewWindow) isActive() bool {
func (w *windowsWebviewWindow) WndProc(msg uint32, wparam, lparam uintptr) uintptr { func (w *windowsWebviewWindow) WndProc(msg uint32, wparam, lparam uintptr) uintptr {
switch msg { switch msg {
case w32.WM_ACTIVATE: case w32.WM_ACTIVATE:
if int(wparam&0xffff) == w32.WA_INACTIVE {
w.parent.emit(events.Common.WindowLostFocus)
}
if wparam == w32.WA_ACTIVE || wparam == w32.WA_CLICKACTIVE { if wparam == w32.WA_ACTIVE || wparam == w32.WA_CLICKACTIVE {
getNativeApplication().currentWindowID = w.parent.id getNativeApplication().currentWindowID = w.parent.id
w.parent.emit(events.Common.WindowFocus) w.parent.emit(events.Common.WindowFocus)
@ -751,16 +753,11 @@ func (w *windowsWebviewWindow) WndProc(msg uint32, wparam, lparam uintptr) uintp
if w.framelessWithDecorations() { if w.framelessWithDecorations() {
w32.ExtendFrameIntoClientArea(w.hwnd, true) w32.ExtendFrameIntoClientArea(w.hwnd, true)
} }
w.justOpened = true
go func() {
time.Sleep(100 * time.Millisecond)
w.justOpened = false
}()
case w32.WM_CLOSE: case w32.WM_CLOSE:
w.parent.emit(events.Common.WindowClosing) w.parent.emit(events.Common.WindowClosing)
return 0 return 0
case w32.WM_KILLFOCUS: case w32.WM_KILLFOCUS:
if w.justOpened { if w.focusingChromium {
return 0 return 0
} }
w.parent.emit(events.Common.WindowLostFocus) w.parent.emit(events.Common.WindowLostFocus)