-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathdisplay.go
More file actions
167 lines (142 loc) · 4.78 KB
/
display.go
File metadata and controls
167 lines (142 loc) · 4.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package workspacesdk
import "math"
const (
// DesktopNativeWidth is the default native desktop width in pixels used for
// computer-use desktop sessions.
DesktopNativeWidth = 1920
// DesktopNativeHeight is the default native desktop height in pixels used for
// computer-use desktop sessions.
DesktopNativeHeight = 1080
desktopDeclaredMaxLongEdge = 1568
desktopDeclaredMaxTotalPixels = 1_150_000
)
var preferredDeclaredDesktopWidths = []int{1280, 1024}
// DesktopGeometry describes the native workspace desktop and the declared
// model-facing geometry used for screenshots and coordinates.
type DesktopGeometry struct {
NativeWidth int
NativeHeight int
DeclaredWidth int
DeclaredHeight int
}
// DefaultDesktopGeometry returns the default native desktop geometry together
// with the declared model-facing geometry derived from it.
func DefaultDesktopGeometry() DesktopGeometry {
return NewDesktopGeometry(DesktopNativeWidth, DesktopNativeHeight)
}
// NewDesktopGeometry derives a declared model-facing geometry from the native
// desktop size.
func NewDesktopGeometry(nativeWidth, nativeHeight int) DesktopGeometry {
nativeWidth = sanitizeDesktopDimension(nativeWidth)
nativeHeight = sanitizeDesktopDimension(nativeHeight)
declaredWidth, declaredHeight := computeDeclaredDesktopSize(
nativeWidth,
nativeHeight,
)
return DesktopGeometry{
NativeWidth: nativeWidth,
NativeHeight: nativeHeight,
DeclaredWidth: declaredWidth,
DeclaredHeight: declaredHeight,
}
}
// NewDesktopGeometryWithDeclared returns a geometry that preserves the native
// desktop size while using the provided declared model-facing dimensions.
func NewDesktopGeometryWithDeclared(
nativeWidth,
nativeHeight,
declaredWidth,
declaredHeight int,
) DesktopGeometry {
nativeWidth = sanitizeDesktopDimension(nativeWidth)
nativeHeight = sanitizeDesktopDimension(nativeHeight)
if declaredWidth <= 0 {
declaredWidth = nativeWidth
}
if declaredHeight <= 0 {
declaredHeight = nativeHeight
}
return DesktopGeometry{
NativeWidth: nativeWidth,
NativeHeight: nativeHeight,
DeclaredWidth: sanitizeDesktopDimension(declaredWidth),
DeclaredHeight: sanitizeDesktopDimension(declaredHeight),
}
}
// DeclaredPointToNative maps a point from declared model-facing coordinates to
// native desktop coordinates using the existing pixel-center truncation rule.
func (g DesktopGeometry) DeclaredPointToNative(x, y int) (nativeX, nativeY int) {
return scaleDesktopCoordinate(x, g.DeclaredWidth, g.NativeWidth),
scaleDesktopCoordinate(y, g.DeclaredHeight, g.NativeHeight)
}
// NativePointToDeclared maps a point from native desktop coordinates to the
// declared model-facing coordinate space using the same truncating transform.
func (g DesktopGeometry) NativePointToDeclared(x, y int) (declaredX, declaredY int) {
return scaleDesktopCoordinate(x, g.NativeWidth, g.DeclaredWidth),
scaleDesktopCoordinate(y, g.NativeHeight, g.DeclaredHeight)
}
func computeDeclaredDesktopSize(nativeWidth, nativeHeight int) (declaredWidth, declaredHeight int) {
if desktopSizeFitsDeclaredLimits(nativeWidth, nativeHeight) {
return nativeWidth, nativeHeight
}
if nativeWidth >= nativeHeight {
for _, declaredWidth := range preferredDeclaredDesktopWidths {
if declaredWidth > nativeWidth {
continue
}
declaredHeight := max(1, declaredWidth*nativeHeight/nativeWidth)
if desktopSizeFitsDeclaredLimits(declaredWidth, declaredHeight) {
return declaredWidth, declaredHeight
}
}
}
return computeGenericDeclaredDesktopSize(nativeWidth, nativeHeight)
}
func desktopSizeFitsDeclaredLimits(width, height int) bool {
return max(width, height) <= desktopDeclaredMaxLongEdge &&
width*height <= desktopDeclaredMaxTotalPixels
}
func computeGenericDeclaredDesktopSize(width, height int) (scaledWidth, scaledHeight int) {
longEdge := max(width, height)
totalPixels := width * height
longEdgeScale := float64(desktopDeclaredMaxLongEdge) / float64(longEdge)
totalPixelsScale := math.Sqrt(
float64(desktopDeclaredMaxTotalPixels) / float64(totalPixels),
)
scale := min(1.0, longEdgeScale, totalPixelsScale)
if scale >= 1.0 {
return width, height
}
return max(1, int(float64(width)*scale)),
max(1, int(float64(height)*scale))
}
func scaleDesktopCoordinate(coord, fromDim, toDim int) int {
if toDim <= 0 {
return 0
}
if fromDim <= 0 || fromDim == toDim {
return clampDesktopCoordinate(coord, toDim)
}
scaled := (float64(coord)+0.5)*float64(toDim)/float64(fromDim) - 0.5
scaled = math.Max(scaled, 0)
scaled = math.Min(scaled, float64(toDim-1))
return int(math.Round(scaled))
}
func clampDesktopCoordinate(coord, dim int) int {
if dim <= 0 {
return 0
}
if coord < 0 {
return 0
}
if coord >= dim {
return dim - 1
}
return coord
}
func sanitizeDesktopDimension(dim int) int {
if dim <= 0 {
return 1
}
return dim
}