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