popup_window.go RawHistoryFolder
1package window
2
3import (
4 "context"
5
6 "github.com/bnema/dumber/internal/logging"
7 "github.com/bnema/puregotk/v4/gtk"
8 "github.com/rs/zerolog"
9)
10
11const (
12 popupWindowDefaultWidth = 900
13 popupWindowDefaultHeight = 800
14)
15
16// PopupWindow is a lightweight top-level GTK host for native-required popup flows.
17type PopupWindow struct {
18 window *gtk.ApplicationWindow
19 content *gtk.Box
20 currentContent *gtk.Widget
21 logger zerolog.Logger
22}
23
24func NewPopup(ctx context.Context, app *gtk.Application) (*PopupWindow, error) {
25 log := logging.FromContext(ctx)
26 pw := &PopupWindow{logger: log.With().Str("component", "popup-window").Logger()}
27
28 pw.window = gtk.NewApplicationWindow(app)
29 if pw.window == nil {
30 return nil, ErrWindowCreationFailed
31 }
32 pw.window.SetDefaultSize(popupWindowDefaultWidth, popupWindowDefaultHeight)
33 title := windowTitle
34 pw.window.SetTitle(&title)
35
36 pw.content = gtk.NewBox(gtk.OrientationVerticalValue, 0)
37 if pw.content == nil {
38 pw.window.Unref()
39 return nil, ErrWidgetCreationFailed("popupWindow.content")
40 }
41 pw.content.SetHexpand(true)
42 pw.content.SetVexpand(true)
43 pw.content.SetVisible(true)
44 pw.window.SetChild(&pw.content.Widget)
45 return pw, nil
46}
47
48func (pw *PopupWindow) SetContent(widget *gtk.Widget) {
49 if pw == nil || pw.content == nil {
50 return
51 }
52 if pw.currentContent != nil {
53 pw.content.Remove(pw.currentContent)
54 pw.currentContent = nil
55 }
56 if widget == nil {
57 return
58 }
59 widget.SetVisible(true)
60 pw.content.Append(widget)
61 pw.currentContent = widget
62}
63
64// DetachContent removes and returns the hosted widget without destroying it.
65// Callers must detach before reparenting the widget into another GTK host.
66func (pw *PopupWindow) DetachContent() *gtk.Widget {
67 if pw == nil || pw.content == nil || pw.currentContent == nil {
68 return nil
69 }
70 widget := pw.currentContent
71 pw.content.Remove(widget)
72 pw.currentContent = nil
73 return widget
74}
75
76func (pw *PopupWindow) Show() {
77 if pw == nil || pw.window == nil {
78 return
79 }
80 pw.window.Present()
81}
82
83func (pw *PopupWindow) Close() {
84 if pw == nil || pw.window == nil {
85 return
86 }
87 pw.window.Close()
88}
89
90func (pw *PopupWindow) Destroy() {
91 if pw == nil || pw.window == nil {
92 return
93 }
94 pw.window.Destroy()
95 pw.window = nil
96 pw.content = nil
97 pw.currentContent = nil
98}
99
100func (pw *PopupWindow) Window() *gtk.ApplicationWindow {
101 if pw == nil {
102 return nil
103 }
104 return pw.window
105}
106
107func (pw *PopupWindow) SetTitle(title string) {
108 if pw == nil || pw.window == nil {
109 return
110 }
111 pw.window.SetTitle(&title)
112}