-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindowsize.go
More file actions
103 lines (94 loc) · 2.78 KB
/
Copy pathwindowsize.go
File metadata and controls
103 lines (94 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"context"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
const (
fallbackWindowWidth = 1200
fallbackWindowHeight = 800
minWindowWidth = 720
minWindowHeight = 480
windowScreenRatio = 0.80
windowScreenMaxRatio = 0.92
)
// screenGetAll / window* are seams so applyInitialWindowSize can be tested
// without a WebView. Production points them at the Wails runtime.
var (
screenGetAll = runtime.ScreenGetAll
windowSetSize = runtime.WindowSetSize
windowSetMinSize = runtime.WindowSetMinSize
windowCenter = runtime.WindowCenter
windowShow = runtime.WindowShow
)
// initialWindowSize returns a window size that is 80% of the logical screen,
// clamped to [min, 92% of screen]. When the screen is smaller than the design
// minimum, the result never exceeds the 92% cap (stay on-screen).
func initialWindowSize(screenW, screenH int) (width, height int) {
if screenW <= 0 || screenH <= 0 {
return fallbackWindowWidth, fallbackWindowHeight
}
maxW := max(1, int(float64(screenW)*windowScreenMaxRatio))
maxH := max(1, int(float64(screenH)*windowScreenMaxRatio))
minW := min(minWindowWidth, maxW)
minH := min(minWindowHeight, maxH)
w := int(float64(screenW) * windowScreenRatio)
h := int(float64(screenH) * windowScreenRatio)
return clampInt(w, minW, maxW), clampInt(h, minH, maxH)
}
func clampInt(v, lo, hi int) int {
if v < lo {
return lo
}
if v > hi {
return hi
}
return v
}
// screenLogicalSize picks the current screen (else primary, else first) and
// returns its logical Size, falling back to the deprecated Width/Height.
func screenLogicalSize(screens []runtime.Screen) (w, h int, ok bool) {
s, ok := pickScreen(screens)
if !ok {
return 0, 0, false
}
w, h = s.Size.Width, s.Size.Height
if w <= 0 || h <= 0 {
w, h = s.Width, s.Height
}
if w <= 0 || h <= 0 {
return 0, 0, false
}
return w, h, true
}
func pickScreen(screens []runtime.Screen) (runtime.Screen, bool) {
if len(screens) == 0 {
return runtime.Screen{}, false
}
var primary *runtime.Screen
for i := range screens {
if screens[i].IsCurrent {
return screens[i], true
}
if screens[i].IsPrimary && primary == nil {
primary = &screens[i]
}
}
if primary != nil {
return *primary, true
}
return screens[0], true
}
// applyInitialWindowSize sizes and centers the hidden window from the current
// screen, then always shows it — ScreenGetAll failures fall back to 1200x800.
func applyInitialWindowSize(ctx context.Context) {
defer windowShow(ctx)
w, h := fallbackWindowWidth, fallbackWindowHeight
if screens, err := screenGetAll(ctx); err == nil {
if sw, sh, ok := screenLogicalSize(screens); ok {
w, h = initialWindowSize(sw, sh)
}
}
windowSetMinSize(ctx, min(minWindowWidth, w), min(minWindowHeight, h))
windowSetSize(ctx, w, h)
windowCenter(ctx)
}