widgets.go RawHistoryFolder
1// Package layout provides GTK widget abstractions and layout management for the pane system.
2// It defines interfaces that wrap GTK types, enabling unit testing without GTK runtime.
3package layout
4
5import (
6 "github.com/jwijenbergh/puregotk/v4/gtk"
7)
8
9// Orientation represents the orientation for layout widgets.
10type Orientation = gtk.Orientation
11
12// Orientation constants matching GTK values.
13const (
14 OrientationHorizontal = gtk.OrientationHorizontalValue
15 OrientationVertical = gtk.OrientationVerticalValue
16)
17
18// Widget is the base interface that all GTK widgets implement.
19// It provides common widget operations needed for layout management.
20type Widget interface {
21 // Visibility
22 Show()
23 Hide()
24 SetVisible(visible bool)
25 IsVisible() bool
26
27 // Focus
28 GrabFocus() bool
29 HasFocus() bool
30 SetCanFocus(canFocus bool)
31 SetFocusable(focusable bool) // GTK4: makes widget able to receive keyboard focus
32 SetFocusOnClick(focusOnClick bool)
33
34 // Pointer events
35 SetCanTarget(canTarget bool) // If false, widget won't receive pointer events
36
37 // Layout
38 SetHexpand(expand bool)
39 SetVexpand(expand bool)
40 GetHexpand() bool
41 GetVexpand() bool
42 SetHalign(align gtk.Align)
43 SetValign(align gtk.Align)
44 SetSizeRequest(width, height int)
45
46 // Geometry - for focus navigation
47 GetAllocatedWidth() int
48 GetAllocatedHeight() int
49 // ComputePoint returns the position of this widget's origin (0,0) relative to
50 // the target widget's coordinate space. If target is nil, returns position
51 // relative to the native window. Returns ok=false if computation fails.
52 ComputePoint(target Widget) (x, y float64, ok bool)
53
54 // CSS styling
55 AddCssClass(cssClass string)
56 RemoveCssClass(cssClass string)
57 HasCssClass(cssClass string) bool
58
59 // Parent management
60 Unparent()
61 GetParent() Widget
62
63 // GTK interop - returns the underlying GTK widget for embedding
64 GtkWidget() *gtk.Widget
65
66 // AddController adds an event controller to the widget
67 AddController(controller *gtk.EventController)
68}
69
70// PanedWidget wraps gtk.Paned for creating split views.
71// It manages two child widgets separated by a draggable divider.
72type PanedWidget interface {
73 Widget
74
75 // Child management
76 SetStartChild(child Widget)
77 SetEndChild(child Widget)
78 GetStartChild() Widget
79 GetEndChild() Widget
80
81 // Divider position
82 SetPosition(position int)
83 GetPosition() int
84
85 // Resize behavior
86 SetResizeStartChild(resize bool)
87 SetResizeEndChild(resize bool)
88 GetResizeStartChild() bool
89 GetResizeEndChild() bool
90
91 // Shrink behavior (whether child can be smaller than its minimum size)
92 SetShrinkStartChild(shrink bool)
93 SetShrinkEndChild(shrink bool)
94 GetShrinkStartChild() bool
95 GetShrinkEndChild() bool
96
97 // Signals
98 ConnectMap(callback func()) uint32
99 ConnectNotifyPosition(callback func()) uint32
100
101 // Tick callback for frame-based updates (returns signal ID)
102 // Callback returns true to continue, false to stop
103 AddTickCallback(callback func() bool) uint
104
105 // Handle appearance
106 SetWideHandle(wide bool)
107 GetWideHandle() bool
108}
109
110// BoxWidget wraps gtk.Box for linear layouts.
111// It arranges children in a single row or column.
112type BoxWidget interface {
113 Widget
114
115 // Child management
116 Append(child Widget)
117 Prepend(child Widget)
118 Remove(child Widget)
119 InsertChildAfter(child Widget, sibling Widget)
120 ReorderChildAfter(child Widget, sibling Widget)
121
122 // Configuration
123 SetHomogeneous(homogeneous bool)
124 GetHomogeneous() bool
125 SetSpacing(spacing int)
126 GetSpacing() int
127 SetOrientation(orientation Orientation)
128 GetOrientation() Orientation
129}
130
131// OverlayWidget wraps gtk.Overlay for layered content.
132// It displays overlay widgets on top of a main child widget.
133type OverlayWidget interface {
134 Widget
135
136 // Main child
137 SetChild(child Widget)
138 GetChild() Widget
139
140 // Overlay management
141 AddOverlay(overlay Widget)
142 RemoveOverlay(overlay Widget)
143
144 // Overlay configuration
145 SetClipOverlay(overlay Widget, clip bool)
146 GetClipOverlay(overlay Widget) bool
147 SetMeasureOverlay(overlay Widget, measure bool)
148 GetMeasureOverlay(overlay Widget) bool
149}
150
151// EllipsizeMode represents pango ellipsize modes.
152type EllipsizeMode = int
153
154// Ellipsize mode constants.
155const (
156 EllipsizeNone EllipsizeMode = 0
157 EllipsizeStart EllipsizeMode = 1
158 EllipsizeMiddle EllipsizeMode = 2
159 EllipsizeEnd EllipsizeMode = 3
160)
161
162// LabelWidget wraps gtk.Label for text display.
163type LabelWidget interface {
164 Widget
165
166 SetText(text string)
167 GetText() string
168 SetMarkup(markup string)
169 SetEllipsize(mode EllipsizeMode)
170 SetMaxWidthChars(nChars int)
171 SetXalign(xalign float32)
172}
173
174// ButtonWidget wraps gtk.Button for clickable elements.
175type ButtonWidget interface {
176 Widget
177
178 SetLabel(label string)
179 GetLabel() string
180 SetChild(child Widget)
181 GetChild() Widget
182 SetIconName(iconName string)
183
184 // Connect click handler, returns signal ID for disconnection
185 ConnectClicked(callback func()) uint32
186}
187
188// Paintable represents a graphics texture that can be displayed in an image.
189// This interface abstracts over gdk.Paintable/gdk.Texture.
190type Paintable interface {
191 GoPointer() uintptr
192}
193
194// ImageWidget wraps gtk.Image for displaying images.
195type ImageWidget interface {
196 Widget
197
198 SetFromIconName(iconName string)
199 SetFromFile(filename string)
200 SetFromPaintable(paintable Paintable)
201 SetPixelSize(pixelSize int)
202 Clear()
203}
204
205// ProgressBarWidget wraps gtk.ProgressBar for displaying loading progress.
206// Implementations may include smooth animation behavior.
207type ProgressBarWidget interface {
208 Widget
209
210 // SetFraction sets the progress value (0.0 to 1.0).
211 SetFraction(fraction float64)
212 // GetFraction returns the current progress value.
213 GetFraction() float64
214}
215
216// SpinnerWidget wraps gtk.Spinner for displaying indefinite loading.
217type SpinnerWidget interface {
218 Widget
219
220 Start()
221 Stop()
222 SetSpinning(spinning bool)
223 GetSpinning() bool
224}
225
226// WidgetFactory creates widget instances.
227// This abstraction allows tests to inject mock factories.
228type WidgetFactory interface {
229 // Container widgets
230 NewPaned(orientation Orientation) PanedWidget
231 NewBox(orientation Orientation, spacing int) BoxWidget
232 NewOverlay() OverlayWidget
233
234 // Display widgets
235 NewLabel(text string) LabelWidget
236 NewButton() ButtonWidget
237 NewImage() ImageWidget
238 NewProgressBar() ProgressBarWidget
239 NewSpinner() SpinnerWidget
240
241 // Wrap existing GTK widget
242 WrapWidget(w *gtk.Widget) Widget
243}
244
245// PaneRenderer creates pane view widgets from domain entities.
246// This decouples the tree renderer from concrete PaneView implementation.
247type PaneRenderer interface {
248 // RenderPane creates a widget for a pane node.
249 // The webViewWidget is the GTK widget from the WebView.
250 RenderPane(paneID string, webViewWidget Widget) Widget
251}