widgets.go RawHistoryFolder
1package webkit
2
3import (
4 "github.com/diamondburned/gotk4/pkg/gtk/v4"
5 "github.com/diamondburned/gotk4/pkg/pango"
6)
7
8// Paned wraps gtk.Paned for split pane layouts
9type Paned struct {
10 paned *gtk.Paned
11}
12
13// NewPaned creates a new Paned widget with the given orientation
14func NewPaned(orientation gtk.Orientation) *Paned {
15 return &Paned{
16 paned: gtk.NewPaned(orientation),
17 }
18}
19
20// SetStartChild sets the first child of the paned
21func (p *Paned) SetStartChild(child gtk.Widgetter) {
22 if p == nil || p.paned == nil {
23 return
24 }
25 p.paned.SetStartChild(child)
26}
27
28// SetEndChild sets the second child of the paned
29func (p *Paned) SetEndChild(child gtk.Widgetter) {
30 if p == nil || p.paned == nil {
31 return
32 }
33 p.paned.SetEndChild(child)
34}
35
36// SetPosition sets the divider position
37func (p *Paned) SetPosition(pos int) {
38 if p == nil || p.paned == nil {
39 return
40 }
41 p.paned.SetPosition(pos)
42}
43
44// GetPosition returns the divider position
45func (p *Paned) GetPosition() int {
46 if p == nil || p.paned == nil {
47 return 0
48 }
49 return p.paned.Position()
50}
51
52// SetResizeStartChild sets whether the start child should resize
53func (p *Paned) SetResizeStartChild(resize bool) {
54 if p == nil || p.paned == nil {
55 return
56 }
57 p.paned.SetResizeStartChild(resize)
58}
59
60// SetResizeEndChild sets whether the end child should resize
61func (p *Paned) SetResizeEndChild(resize bool) {
62 if p == nil || p.paned == nil {
63 return
64 }
65 p.paned.SetResizeEndChild(resize)
66}
67
68// AsWidget returns the paned as a Widgetter
69func (p *Paned) AsWidget() gtk.Widgetter {
70 if p == nil || p.paned == nil {
71 return nil
72 }
73 return p.paned
74}
75
76// StartChild returns the start child
77func (p *Paned) StartChild() gtk.Widgetter {
78 if p == nil || p.paned == nil {
79 return nil
80 }
81 return p.paned.StartChild()
82}
83
84// EndChild returns the end child
85func (p *Paned) EndChild() gtk.Widgetter {
86 if p == nil || p.paned == nil {
87 return nil
88 }
89 return p.paned.EndChild()
90}
91
92// Box wraps gtk.Box for layout management
93type Box struct {
94 box *gtk.Box
95}
96
97// NewBox creates a new Box with the given orientation and spacing
98func NewBox(orientation gtk.Orientation, spacing int) *Box {
99 return &Box{
100 box: gtk.NewBox(orientation, spacing),
101 }
102}
103
104// Append adds a child to the end of the box
105func (b *Box) Append(child gtk.Widgetter) {
106 if b == nil || b.box == nil {
107 return
108 }
109 b.box.Append(child)
110}
111
112// Prepend adds a child to the beginning of the box
113func (b *Box) Prepend(child gtk.Widgetter) {
114 if b == nil || b.box == nil {
115 return
116 }
117 b.box.Prepend(child)
118}
119
120// Remove removes a child from the box
121func (b *Box) Remove(child gtk.Widgetter) {
122 if b == nil || b.box == nil {
123 return
124 }
125 b.box.Remove(child)
126}
127
128// InsertChildAfter inserts a child after the specified sibling
129func (b *Box) InsertChildAfter(child, sibling gtk.Widgetter) {
130 if b == nil || b.box == nil {
131 return
132 }
133 b.box.InsertChildAfter(child, sibling)
134}
135
136// AsWidget returns the box as a Widgetter
137func (b *Box) AsWidget() gtk.Widgetter {
138 if b == nil || b.box == nil {
139 return nil
140 }
141 return b.box
142}
143
144// Label wraps gtk.Label for text display
145type Label struct {
146 label *gtk.Label
147}
148
149// NewLabel creates a new Label with the given text
150func NewLabel(text string) *Label {
151 return &Label{
152 label: gtk.NewLabel(text),
153 }
154}
155
156// SetText updates the label text
157func (l *Label) SetText(text string) {
158 if l == nil || l.label == nil {
159 return
160 }
161 l.label.SetText(text)
162}
163
164// GetText returns the current label text
165func (l *Label) GetText() string {
166 if l == nil || l.label == nil {
167 return ""
168 }
169 return l.label.Text()
170}
171
172// SetEllipsize sets the ellipsization mode
173func (l *Label) SetEllipsize(mode pango.EllipsizeMode) {
174 if l == nil || l.label == nil {
175 return
176 }
177 l.label.SetEllipsize(mode)
178}
179
180// SetMaxWidthChars sets the maximum width in characters
181func (l *Label) SetMaxWidthChars(nChars int) {
182 if l == nil || l.label == nil {
183 return
184 }
185 l.label.SetMaxWidthChars(nChars)
186}
187
188// AsWidget returns the label as a Widgetter
189func (l *Label) AsWidget() gtk.Widgetter {
190 if l == nil || l.label == nil {
191 return nil
192 }
193 return l.label
194}
195
196// Image wraps gtk.Image for displaying images (e.g., favicons)
197type Image struct {
198 image *gtk.Image
199}
200
201// NewImage creates a new empty Image
202func NewImage() *Image {
203 return &Image{
204 image: gtk.NewImage(),
205 }
206}
207
208// NewImageFromFile creates a new Image from a file
209func NewImageFromFile(filename string) *Image {
210 return &Image{
211 image: gtk.NewImageFromFile(filename),
212 }
213}
214
215// SetFromFile loads an image from a file
216func (i *Image) SetFromFile(filename string) {
217 if i == nil || i.image == nil {
218 return
219 }
220 i.image.SetFromFile(filename)
221}
222
223// SetPixelSize sets the pixel size for the image
224func (i *Image) SetPixelSize(size int) {
225 if i == nil || i.image == nil {
226 return
227 }
228 i.image.SetPixelSize(size)
229}
230
231// AsWidget returns the image as a Widgetter
232func (i *Image) AsWidget() gtk.Widgetter {
233 if i == nil || i.image == nil {
234 return nil
235 }
236 return i.image
237}
238
239// Widget helper functions for common operations
240
241// getWidget extracts the underlying *gtk.Widget from a Widgetter
242// This is needed because some methods are only available on *gtk.Widget
243func getWidget(w gtk.Widgetter) *gtk.Widget {
244 if w == nil {
245 return nil
246 }
247 return gtk.BaseWidget(w)
248}
249
250// WidgetShow makes a widget visible
251func WidgetShow(w gtk.Widgetter) {
252 if widget := getWidget(w); widget != nil {
253 widget.SetVisible(true)
254 }
255}
256
257// WidgetHide makes a widget invisible
258func WidgetHide(w gtk.Widgetter) {
259 if widget := getWidget(w); widget != nil {
260 widget.SetVisible(false)
261 }
262}
263
264// WidgetSetVisible sets the visibility of a widget
265func WidgetSetVisible(w gtk.Widgetter, visible bool) {
266 if widget := getWidget(w); widget != nil {
267 widget.SetVisible(visible)
268 }
269}
270
271// WidgetSetSizeRequest sets the minimum size request
272func WidgetSetSizeRequest(w gtk.Widgetter, width, height int) {
273 if widget := getWidget(w); widget != nil {
274 widget.SetSizeRequest(width, height)
275 }
276}
277
278// WidgetQueueResize queues a resize operation
279func WidgetQueueResize(w gtk.Widgetter) {
280 if widget := getWidget(w); widget != nil {
281 widget.QueueResize()
282 }
283}
284
285// WidgetGrabFocus gives focus to a widget
286func WidgetGrabFocus(w gtk.Widgetter) {
287 if widget := getWidget(w); widget != nil {
288 widget.GrabFocus()
289 }
290}