-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeometry_utils.go
More file actions
140 lines (115 loc) · 4.57 KB
/
geometry_utils.go
File metadata and controls
140 lines (115 loc) · 4.57 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package devbrowser
import (
"fmt"
)
// calculateConstrainedSize determines the best window size based on available monitor space
// and previous configuration.
//
// logic:
// 1. If monitor size is unknown (0x0), return requested size
// 2. If valid monitor size:
// - If !sizeConfigured (first run or auto):
// Accept requested size BUT ensure it fits within monitor (clamping)
// - If sizeConfigured (user manually set size):
// Check if it fits. If not, scale down or clamp. To be safe, we clamp to available area.
//
// In all cases, we ensure the window is not larger than the available screen area.
func (b *DevBrowser) CalculateConstrainedSize(reqW, reqH, monW, monH int) (int, int) {
if monW <= 0 || monH <= 0 {
return reqW, reqH
}
finalW, finalH := reqW, reqH
// Constrain width
if finalW > monW {
finalW = monW
}
// Constrain height
if finalH > monH {
finalH = monH
}
// If we modified dimensions, we might want to log it (caller handles logging)
return finalW, finalH
}
// startWithDetectedSize updates the browser size if it hasn't been configured yet
// and we have detected a monitor size.
func (b *DevBrowser) StartWithDetectedSize() {
b.Mu.Lock()
defer b.Mu.Unlock()
// If user already has a saved config, respect it (but apply constraints later)
if b.SizeConfigured {
return
}
// If no monitor detected, keep defaults
if b.MonitorWidth == 0 || b.MonitorHeight == 0 {
return
}
// Logic for default size when nothing is configured:
// We want a reasonable default that fits.
// Default is 1024x768 in New().
// If monitor is smaller (e.g. mobile 375x...), default might be too big.
// So strictly speaking, we just run the constraint logic on the default.
// If the default 1024x768 fits, we keep it. Use cases:
// - Desktop (1920x1080): 1024x768 fits -> use default.
// - Tablet/Phone (e.g. 800x600 screen?): 1024x768 -> becomes 800x600.
// However, the user request says:
// "se necestia que se sepa de forma automatizada las medidas del monitor para tener
// como base el alto y ancho cuando este no ha sido configurado [...] asi arrancar con esa medida"
// This implies we might want to start MAXIMIZED or close to full screen if not configured?
// Or maybe just ensure it fits.
// "tomar las medidas del monitor como base":
// Let's interpret this as: define a sensible default based on monitor size.
// If it's a large screen, maybe we stick to a sensible "desktop" default (like 1280x800 or 1440x900).
// If it's small, we take the max available.
// Let's stick to the safer "ensure it fits" approach for now, which satisfies "adjust to them".
// But let's verify if "arrancar con esa medida" means full screen.
// Usually devs don't want full screen 4k browser on startup.
// Let's assume they want the Configured Default (1024x768) BUT guaranteed to fit.
newW, newH := b.CalculateConstrainedSize(b.Width, b.Height, b.MonitorWidth, b.MonitorHeight)
// If monitor is really big, maybe we can be bolder than 1024x768?
// The prompt says: "las medidas desktop,mobile, tablet no puedes superar ninguna de estas deben ser proporcional o ajaustarce a ellas"
// This confirms the constraint logic is the priority.
// Special case: If the "default" hasn't been touched, maybe we upgrade it to a better desktop size
// if the screen allows? 1024x768 is a bit old school.
// But let's safely just constrain for now.
b.Width = newW
b.Height = newH
b.Log(fmt.Sprintf("Browser size auto-adjusted to monitor: %dx%d", newW, newH))
}
// getPresetSize calculates the optimal dimensions for a requested preset mode.
// It uses predefined base sizes but ensures they fit within the current monitor constraints.
func (b *DevBrowser) GetPresetSize(mode string) (int, int, error) {
var baseW, baseH int
// Base definitions
switch mode {
case "desktop":
baseW, baseH = 1440, 900
case "mobile":
baseW, baseH = 375, 812
case "tablet":
baseW, baseH = 768, 1024
default:
return 0, 0, fmt.Errorf("unknown mode: %s", mode)
}
b.Mu.Lock()
monW := b.MonitorWidth
monH := b.MonitorHeight
b.Mu.Unlock()
// If monitor size not detected yet (lazy load), try to detect it now
if monW == 0 || monH == 0 {
b.DetectMonitorSize()
// Re-read
b.Mu.Lock()
monW = b.MonitorWidth
monH = b.MonitorHeight
b.Mu.Unlock()
}
// If still not detected, return base size
if monW == 0 || monH == 0 {
return baseW, baseH, nil
}
// Calculate constrained size
// We currently use clamping ("adjust to them") which is standard for maximizing space
// while ensuring it fits.
finalW, finalH := b.CalculateConstrainedSize(baseW, baseH, monW, monH)
return finalW, finalH, nil
}