-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor_geometry.go
More file actions
103 lines (84 loc) · 2.31 KB
/
monitor_geometry.go
File metadata and controls
103 lines (84 loc) · 2.31 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 devbrowser
import (
"context"
"fmt"
"strconv"
"time"
"github.com/tinywasm/devbrowser/cdproto/browser"
"github.com/tinywasm/devbrowser/chromedp"
)
// monitorBrowserGeometry monitors changes in browser window position and size
// and automatically saves them to the database
func (b *DevBrowser) monitorBrowserGeometry() {
b.Mu.Lock()
ctx := b.Ctx
isOpen := b.IsOpenFlag
b.Mu.Unlock()
if ctx == nil || !isOpen {
return
}
// Wait a bit before starting to monitor to allow Chrome to stabilize
// after initial window creation and avoid capturing transient sizes
time.Sleep(3 * time.Second)
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
// Browser context closed, stop monitoring
return
case <-ticker.C:
// Check and save current geometry if changed
b.checkAndSaveGeometry()
}
}
}
// checkAndSaveGeometry checks current browser geometry and saves if changed
func (b *DevBrowser) checkAndSaveGeometry() {
b.Mu.Lock()
ctx := b.Ctx
b.Mu.Unlock()
if ctx == nil {
return
}
var x, y, width, height int64
// Get window bounds using Chrome DevTools Protocol
err := chromedp.Run(ctx,
chromedp.ActionFunc(func(ctx context.Context) error {
// Get the window ID for the current target
t := chromedp.FromContext(ctx).Target
// Get window bounds for target
windowID, bounds, err := browser.GetWindowForTarget().WithTargetID(t.TargetID).Do(ctx)
if err != nil {
return err
}
// windowID is returned but we use bounds directly
_ = windowID
x = bounds.Left
y = bounds.Top
width = bounds.Width
height = bounds.Height
return nil
}),
)
if err != nil {
// Silently ignore errors - geometry monitoring is non-critical
return
}
// Check if position changed
newPosition := strconv.FormatInt(x, 10) + "," + strconv.FormatInt(y, 10)
if newPosition != b.Position {
b.Position = newPosition
b.DB.Set(StoreKeyBrowserPosition, b.Position)
}
// Check if width or height changed
newWidth := int(width)
newHeight := int(height)
if (newWidth != b.Width && newWidth > 0) || (newHeight != b.Height && newHeight > 0) {
b.Width = newWidth
b.Height = newHeight
b.SizeConfigured = true // Mark as manually configured
size := fmt.Sprintf("%d,%d", b.Width, b.Height)
b.DB.Set(StoreKeyBrowserSize, size)
}
}