diff --git a/v2/internal/frontend/desktop/windows/win32/window.go b/v2/internal/frontend/desktop/windows/win32/window.go index 3bd3f2399..c5db085ff 100644 --- a/v2/internal/frontend/desktop/windows/win32/window.go +++ b/v2/internal/frontend/desktop/windows/win32/window.go @@ -38,6 +38,27 @@ const ( GCLP_HBRBACKGROUND int32 = -10 ) +// Power +const ( + // WM_POWERBROADCAST - Notifies applications that a power-management event has occurred. + WM_POWERBROADCAST = 536 + + // PBT_APMPOWERSTATUSCHANGE - Power status has changed. + PBT_APMPOWERSTATUSCHANGE = 10 + + // PBT_APMRESUMEAUTOMATIC -Operation is resuming automatically from a low-power state. This message is sent every time the system resumes. + PBT_APMRESUMEAUTOMATIC = 18 + + // PBT_APMRESUMESUSPEND - Operation is resuming from a low-power state. This message is sent after PBT_APMRESUMEAUTOMATIC if the resume is triggered by user input, such as pressing a key. + PBT_APMRESUMESUSPEND = 7 + + // PBT_APMSUSPEND - System is suspending operation. + PBT_APMSUSPEND = 4 + + // PBT_POWERSETTINGCHANGE - A power setting change event has been received. + PBT_POWERSETTINGCHANGE = 32787 +) + // http://msdn.microsoft.com/en-us/library/windows/desktop/bb773244.aspx type MARGINS struct { CxLeftWidth, CxRightWidth, CyTopHeight, CyBottomHeight int32 diff --git a/v2/internal/frontend/desktop/windows/window.go b/v2/internal/frontend/desktop/windows/window.go index 386863a65..3d530a96c 100644 --- a/v2/internal/frontend/desktop/windows/window.go +++ b/v2/internal/frontend/desktop/windows/window.go @@ -24,6 +24,8 @@ type Window struct { isDarkMode bool isActive bool hasBeenShown bool + OnSuspend func() + OnResume func() } func NewWindow(parent winc.Controller, appoptions *options.App, versionInfo *operatingsystem.WindowsVersionInfo) *Window { @@ -83,6 +85,8 @@ func NewWindow(parent winc.Controller, appoptions *options.App, versionInfo *ope result.updateTheme() if appoptions.Windows != nil { + result.OnSuspend = appoptions.Windows.OnSuspend + result.OnResume = appoptions.Windows.OnResume if appoptions.Windows.WindowIsTranslucent { // TODO: Migrate to win32 package if !win32.IsWindowsVersionAtLeast(10, 0, 22579) { @@ -144,6 +148,18 @@ func (w *Window) SetMaxSize(maxWidth int, maxHeight int) { func (w *Window) WndProc(msg uint32, wparam, lparam uintptr) uintptr { switch msg { + case win32.WM_POWERBROADCAST: + switch wparam { + case win32.PBT_APMSUSPEND: + if w.OnSuspend != nil { + w.OnSuspend() + } + case win32.PBT_APMRESUMEAUTOMATIC: + if w.OnResume != nil { + w.OnResume() + } + } + case w32.WM_SETTINGCHANGE: settingChanged := w32.UTF16PtrToString((*uint16)(unsafe.Pointer(lparam))) if settingChanged == "ImmersiveColorSet" { @@ -154,7 +170,7 @@ func (w *Window) WndProc(msg uint32, wparam, lparam uintptr) uintptr { w32.SetFocus(w.Handle()) case w32.WM_MOVE, w32.WM_MOVING: if w.notifyParentWindowPositionChanged != nil { - w.notifyParentWindowPositionChanged() + _ = w.notifyParentWindowPositionChanged() } case w32.WM_ACTIVATE: //if !w.frontendOptions.Frameless { diff --git a/v2/pkg/options/windows/windows.go b/v2/pkg/options/windows/windows.go index da1db640d..146d23d3a 100644 --- a/v2/pkg/options/windows/windows.go +++ b/v2/pkg/options/windows/windows.go @@ -86,6 +86,11 @@ type Options struct { // ResizeDebounceMS is the amount of time to debounce redraws of webview2 // when resizing the window ResizeDebounceMS uint16 + + // OnSuspend is called when Windows enters low power mode + OnSuspend func() + // OnResume is called when Windows resumes from low power mode + OnResume func() } func DefaultMessages() *Messages { diff --git a/website/docs/reference/options.mdx b/website/docs/reference/options.mdx index a15a468d2..96b4d0f17 100644 --- a/website/docs/reference/options.mdx +++ b/website/docs/reference/options.mdx @@ -520,6 +520,24 @@ Type: uint16 ResizeDebounceMS is the amount of time to debounce redraws of webview2 when resizing the window. The default value (0) will perform redraws as fast as it can. +### OnSuspend + +Name: OnSuspend + +Type: func() + +If set, this function will be called when windows initiates a switch to low power mode (suspend/hibernate) + +### OnResume + +Name: OnResume + +Type: func() + +If set, this function will be called when windows resumes from low power mode (suspend/hibernate) + + + ## Mac Specific Options ### TitleBar