Files
Reims-Go/internal/host/fullscreen.go

162 lines
4.2 KiB
Go

package host
import (
"errors"
"maps"
"sync"
)
// ErrDisplayWindowNotFound indicates that the process has no matching display window.
var ErrDisplayWindowNotFound = errors.New("no display window found")
const (
wsCaption = 0x00C00000
wsThickFrame = 0x00040000
wsMinimizeBox = 0x00020000
wsMaximizeBox = 0x00010000
wsSysMenu = 0x00080000
wsVisible = 0x10000000
wsExDlgModalFrame = 0x00000001
wsExWindowEdge = 0x00000100
wsExClientEdge = 0x00000200
wsExStaticEdge = 0x00020000
wsExToolWindow = 0x00000080
swShowMaximized = 3
windowFrameStyles = wsCaption | wsThickFrame | wsMinimizeBox | wsMaximizeBox | wsSysMenu
windowFrameExStyles = wsExDlgModalFrame | wsExWindowEdge | wsExClientEdge | wsExStaticEdge
)
// WindowRect uses Win32 virtual-screen coordinates.
type WindowRect struct {
Left, Top, Right, Bottom int32
}
func isBorderlessFullscreenRect(style uintptr, winRect, monitorRect WindowRect) bool {
const tolerance = 2
return style&(wsCaption|wsThickFrame) == 0 &&
winRect.Left <= monitorRect.Left+tolerance &&
winRect.Top <= monitorRect.Top+tolerance &&
winRect.Right >= monitorRect.Right-tolerance &&
winRect.Bottom >= monitorRect.Bottom-tolerance
}
func computeFallbackWindowRect(workRect WindowRect) WindowRect {
workWidth := workRect.Right - workRect.Left
workHeight := workRect.Bottom - workRect.Top
width := min(workWidth, max(640, workWidth*85/100))
height := min(workHeight, max(480, workHeight*85/100))
left := workRect.Left + (workWidth-width)/2
top := workRect.Top + (workHeight-height)/2
return WindowRect{Left: left, Top: top, Right: left + width, Bottom: top + height}
}
type windowPlacement struct {
Size, Flags, ShowCmd uint32
MinX, MinY int32
MaxX, MaxY int32
Normal WindowRect
}
type savedWindowState struct {
pid uint32
style, exStyle uintptr
placement windowPlacement
fullscreenRect WindowRect
wasFullscreen bool
}
func minimizedWindowRect(normal WindowRect, exStyle uintptr, monitor, work WindowRect, saved savedWindowState) WindowRect {
if saved.placement.ShowCmd == swShowMaximized && normal == saved.placement.Normal {
// A window maximized before entering full screen can retain its older
// normal position. Use the last bounds set by the full-screen toggle.
return saved.fullscreenRect
}
rect := normal
if exStyle&wsExToolWindow == 0 {
// WINDOWPLACEMENT uses workspace coordinates for ordinary top-level windows.
dx, dy := work.Left-monitor.Left, work.Top-monitor.Top
rect.Left += dx
rect.Right += dx
rect.Top += dy
rect.Bottom += dy
}
return rect
}
type fullscreenWindowLock struct {
mu sync.Mutex
refs int
}
type fullscreenWindows struct {
mu sync.Mutex
locks map[uintptr]*fullscreenWindowLock
states map[uintptr]savedWindowState
}
func (windows *fullscreenWindows) lock(hwnd uintptr) func() {
windows.mu.Lock()
lock := windows.locks[hwnd]
if lock == nil {
lock = &fullscreenWindowLock{}
windows.locks[hwnd] = lock
}
lock.refs++
windows.mu.Unlock()
lock.mu.Lock()
return func() {
lock.mu.Unlock()
windows.mu.Lock()
lock.refs--
if lock.refs == 0 {
delete(windows.locks, hwnd)
}
windows.mu.Unlock()
}
}
func (windows *fullscreenWindows) lookup(hwnd uintptr, pid uint32) (savedWindowState, bool) {
windows.mu.Lock()
defer windows.mu.Unlock()
state, ok := windows.states[hwnd]
if !ok || state.pid != pid {
return savedWindowState{}, false
}
return state, true
}
func (windows *fullscreenWindows) remember(hwnd uintptr, original savedWindowState, bounds WindowRect) {
windows.mu.Lock()
defer windows.mu.Unlock()
if saved, ok := windows.states[hwnd]; ok && saved.pid == original.pid {
original = saved
}
original.fullscreenRect = bounds
windows.states[hwnd] = original
}
func (windows *fullscreenWindows) forget(hwnd uintptr) {
windows.mu.Lock()
delete(windows.states, hwnd)
windows.mu.Unlock()
}
func (windows *fullscreenWindows) prune(livePID func(uintptr) uint32) {
windows.mu.Lock()
states := maps.Clone(windows.states)
windows.mu.Unlock()
for hwnd, state := range states {
if livePID(hwnd) == state.pid {
continue
}
windows.mu.Lock()
if windows.states[hwnd] == state {
delete(windows.states, hwnd)
}
windows.mu.Unlock()
}
}