window.go RawHistoryFolder
1package webkit
2
3import (
4 "fmt"
5
6 "github.com/bnema/dumber/internal/logging"
7 "github.com/diamondburned/gotk4/pkg/gtk/v4"
8)
9
10// Window wraps a GTK Window for the browser
11type Window struct {
12 win *gtk.Window
13}
14
15// NewWindow creates a new GTK window with the given title
16func NewWindow(title string) (*Window, error) {
17 InitMainThread()
18
19 win := gtk.NewWindow()
20 if win == nil {
21 return nil, ErrWebViewNotInitialized
22 }
23
24 win.SetTitle(title)
25 win.SetDefaultSize(1024, 768)
26
27 // Connect close-request signal to handle Cmd+Q and window close button
28 // Returning true prevents the default close behavior and allows graceful shutdown
29 win.ConnectCloseRequest(func() bool {
30 logging.Info(fmt.Sprintf("[window] Close request received (Cmd+Q or close button) - initiating graceful shutdown"))
31 // Quit the main loop gracefully, which will trigger cleanup
32 QuitMainLoop()
33 // Return true to prevent GTK from destroying the window immediately
34 // The cleanup process will handle window destruction
35 return true
36 })
37
38 return &Window{win: win}, nil
39}
40
41// SetTitle updates the window title
42func (w *Window) SetTitle(title string) {
43 if w == nil || w.win == nil {
44 return
45 }
46 w.win.SetTitle(title)
47}
48
49// SetChild sets the child widget of the window
50func (w *Window) SetChild(child gtk.Widgetter) {
51 if w == nil || w.win == nil {
52 return
53 }
54 w.win.SetChild(child)
55}
56
57// Show makes the window visible
58// Note: In GTK4, Show() is deprecated. Use SetVisible(true) instead.
59func (w *Window) Show() {
60 if w == nil || w.win == nil {
61 return
62 }
63 w.win.SetVisible(true)
64}
65
66// Present brings the window to the front
67func (w *Window) Present() {
68 if w == nil || w.win == nil {
69 return
70 }
71 w.win.Present()
72}
73
74// Close destroys the window
75func (w *Window) Close() {
76 if w == nil || w.win == nil {
77 return
78 }
79 w.win.Close()
80}
81
82// AsWindow returns the underlying gtk.Window for advanced operations
83func (w *Window) AsWindow() *gtk.Window {
84 if w == nil {
85 return nil
86 }
87 return w.win
88}
89
90// SetDefaultSize sets the default window size
91func (w *Window) SetDefaultSize(width, height int) {
92 if w == nil || w.win == nil {
93 return
94 }
95 w.win.SetDefaultSize(width, height)
96}
97
98// Destroy destroys the window
99func (w *Window) Destroy() {
100 if w == nil || w.win == nil {
101 return
102 }
103 w.win.Destroy()
104}
105
106// InitializeGlobalShortcuts is defined in shortcuts.go and implemented there