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
64func (pw *PopupWindow) Show() {
65 if pw == nil || pw.window == nil {
66 return
67 }
68 pw.window.Present()
69}
70
71func (pw *PopupWindow) Close() {
72 if pw == nil || pw.window == nil {
73 return
74 }
75 pw.window.Close()
76}
77
78func (pw *PopupWindow) Destroy() {
79 if pw == nil || pw.window == nil {
80 return
81 }
82 pw.window.Destroy()
83 pw.window = nil
84 pw.content = nil
85 pw.currentContent = nil
86}
87
88func (pw *PopupWindow) Window() *gtk.ApplicationWindow {
89 if pw == nil {
90 return nil
91 }
92 return pw.window
93}
94
95func (pw *PopupWindow) SetTitle(title string) {
96 if pw == nil || pw.window == nil {
97 return
98 }
99 pw.window.SetTitle(&title)
100}