tab_bar.go RawHistoryFolder
1package component
2
3import (
4 "sync"
5
6 "github.com/bnema/dumber/internal/domain/entity"
7 "github.com/bnema/puregotk/v4/gtk"
8)
9
10// TabBar represents the horizontal tab bar widget.
11type TabBar struct {
12 box *gtk.Box
13 buttons map[entity.TabID]*TabButton
14
15 explicitlyVisible bool
16 autoHidden bool
17
18 // Active tab tracking
19 activeTabID entity.TabID
20
21 // Callbacks
22 onSwitch func(tabID entity.TabID)
23 onClose func(tabID entity.TabID)
24 onCreate func()
25
26 mu sync.RWMutex
27}
28
29// NewTabBar creates a new tab bar widget.
30func NewTabBar() *TabBar {
31 tb := &TabBar{
32 buttons: make(map[entity.TabID]*TabButton),
33 explicitlyVisible: true,
34 }
35
36 // Create horizontal box container
37 tb.box = gtk.NewBox(gtk.OrientationHorizontalValue, 0)
38 if tb.box == nil {
39 return nil
40 }
41
42 // Configure the box
43 tb.box.SetHexpand(true)
44 tb.box.SetVexpand(false)
45 tb.box.SetVisible(true)
46 tb.box.AddCssClass("tab-bar")
47
48 return tb
49}
50
51// Widget returns the underlying GTK widget for embedding.
52func (tb *TabBar) Widget() *gtk.Widget {
53 return &tb.box.Widget
54}
55
56// Box returns the underlying GTK box.
57func (tb *TabBar) Box() *gtk.Box {
58 return tb.box
59}
60
61// AddTab adds a new tab button to the bar.
62func (tb *TabBar) AddTab(tab *entity.Tab) {
63 tb.mu.Lock()
64 defer tb.mu.Unlock()
65
66 // Check if already exists
67 if _, exists := tb.buttons[tab.ID]; exists {
68 return
69 }
70
71 // Create the button
72 button := NewTabButton(tab)
73 if button == nil {
74 return
75 }
76
77 // Set click handler
78 button.SetOnClick(func(tabID entity.TabID) {
79 if tb.onSwitch != nil {
80 tb.onSwitch(tabID)
81 }
82 })
83
84 // Add to container
85 tb.box.Append(button.Widget())
86 tb.buttons[tab.ID] = button
87}
88
89// RemoveTab removes a tab button from the bar.
90func (tb *TabBar) RemoveTab(tabID entity.TabID) {
91 tb.mu.Lock()
92 defer tb.mu.Unlock()
93
94 button, exists := tb.buttons[tabID]
95 if !exists {
96 return
97 }
98
99 // Remove from container
100 tb.box.Remove(button.Widget())
101 button.Destroy()
102 delete(tb.buttons, tabID)
103}
104
105// SetActive updates which tab is shown as active.
106func (tb *TabBar) SetActive(tabID entity.TabID) {
107 tb.mu.Lock()
108 defer tb.mu.Unlock()
109
110 // Deactivate previous
111 if tb.activeTabID != "" && tb.activeTabID != tabID {
112 if prevButton, exists := tb.buttons[tb.activeTabID]; exists {
113 prevButton.SetActive(false)
114 }
115 }
116
117 // Activate new
118 if button, exists := tb.buttons[tabID]; exists {
119 button.SetActive(true)
120 }
121
122 tb.activeTabID = tabID
123}
124
125// UpdateTitle updates the title of a specific tab button.
126func (tb *TabBar) UpdateTitle(tabID entity.TabID, title string) {
127 tb.mu.RLock()
128 defer tb.mu.RUnlock()
129
130 if button, exists := tb.buttons[tabID]; exists {
131 button.SetTitle(title)
132 }
133}
134
135// SetOnSwitch sets the callback for tab switch events.
136func (tb *TabBar) SetOnSwitch(fn func(tabID entity.TabID)) {
137 tb.onSwitch = fn
138}
139
140// SetOnClose sets the callback for tab close events.
141func (tb *TabBar) SetOnClose(fn func(tabID entity.TabID)) {
142 tb.onClose = fn
143}
144
145// SetOnCreate sets the callback for new tab creation.
146func (tb *TabBar) SetOnCreate(fn func()) {
147 tb.onCreate = fn
148}
149
150// Count returns the number of tabs in the bar.
151func (tb *TabBar) Count() int {
152 tb.mu.RLock()
153 defer tb.mu.RUnlock()
154 return len(tb.buttons)
155}
156
157// ActiveTabID returns the currently active tab ID.
158func (tb *TabBar) ActiveTabID() entity.TabID {
159 tb.mu.RLock()
160 defer tb.mu.RUnlock()
161 return tb.activeTabID
162}
163
164// SetVisible explicitly shows or hides the tab bar.
165func (tb *TabBar) SetVisible(visible bool) {
166 if tb == nil || tb.box == nil {
167 return
168 }
169 tb.explicitlyVisible = visible
170 tb.applyVisibilityState()
171}
172
173// SetAutoHidden hides the tab bar visually while keeping the overlay widget
174// visible to GTK when the bar has not been explicitly hidden. It is used for
175// hide_tab_bar_when_single_tab; WebView viewport height stays stable because the
176// tab bar is mounted as a non-measured overlay above the content.
177func (tb *TabBar) SetAutoHidden(hidden bool) {
178 if tb == nil || tb.box == nil {
179 return
180 }
181
182 tb.autoHidden = hidden
183 tb.applyVisibilityState()
184}
185
186func (tb *TabBar) applyVisibilityState() {
187 if tb == nil || tb.box == nil {
188 return
189 }
190
191 interactive := tb.explicitlyVisible && !tb.autoHidden
192 tb.box.SetVisible(tb.explicitlyVisible)
193 if interactive {
194 tb.box.SetOpacity(1.0)
195 } else {
196 tb.box.SetOpacity(0.0)
197 }
198 tb.box.SetCanTarget(interactive)
199 tb.box.SetFocusable(interactive)
200}
201
202// Destroy cleans up all tab bar resources.
203func (tb *TabBar) Destroy() {
204 tb.mu.Lock()
205 defer tb.mu.Unlock()
206
207 for _, button := range tb.buttons {
208 button.Destroy()
209 }
210 tb.buttons = nil
211
212 if tb.box != nil {
213 tb.box.Unref()
214 tb.box = nil
215 }
216}