progress_bar.go RawHistoryFolder
1// Package component provides UI components for the browser.
2package component
3
4import (
5 "sync"
6
7 "github.com/jwijenbergh/puregotk/v4/glib"
8 "github.com/jwijenbergh/puregotk/v4/gtk"
9
10 "github.com/bnema/dumber/internal/ui/layout"
11)
12
13const (
14 // Animation step size - how much to increment per frame
15 progressStep = 0.02
16 // Animation interval in milliseconds
17 progressIntervalMs = 16 // ~60fps
18 // Timeout to auto-hide progress bar if stuck (30 seconds)
19 progressTimeoutMs = 30000
20)
21
22// ProgressBar displays a slim loading progress indicator at the bottom of a pane.
23// Uses native GtkProgressBar with "osd" styling for overlay appearance.
24// Implements smooth animation by incrementing towards the target value.
25// Includes a 30-second timeout to auto-hide if the page load stalls.
26type ProgressBar struct {
27 progressBar layout.ProgressBarWidget
28
29 visible bool
30 currentValue float64 // Current displayed value
31 targetValue float64 // Target value to animate towards
32 animationTimer uint // Timer source ID for animation
33 timeoutTimer uint // Timer source ID for auto-hide timeout
34
35 mu sync.Mutex
36}
37
38// NewProgressBar creates a new progress bar component using the widget factory.
39func NewProgressBar(factory layout.WidgetFactory) *ProgressBar {
40 progressBar := factory.NewProgressBar()
41
42 // Add "osd" class for on-screen-display overlay styling (like Epiphany)
43 progressBar.AddCssClass("osd")
44
45 // Position at bottom, full width
46 progressBar.SetValign(gtk.AlignEndValue)
47 progressBar.SetHalign(gtk.AlignFillValue)
48 progressBar.SetHexpand(true)
49
50 // Set minimum size to prevent GTK warning about negative minimum width (-2)
51 // The internal "progress" gizmo needs valid dimensions before realization
52 // Using 0 for width (not -1) ensures GTK doesn't calculate negative sizes
53 progressBar.SetSizeRequest(0, 4)
54
55 // Don't intercept pointer events - let clicks pass through to WebView
56 progressBar.SetCanTarget(false)
57 progressBar.SetCanFocus(false)
58
59 // Initialize fraction to 0
60 progressBar.SetFraction(0)
61
62 // Hidden by default
63 progressBar.SetVisible(false)
64
65 return &ProgressBar{
66 progressBar: progressBar,
67 visible: false,
68 currentValue: 0,
69 targetValue: 0,
70 }
71}
72
73// SetProgress sets the target progress value and starts smooth animation.
74// progress should be between 0.0 and 1.0.
75func (pb *ProgressBar) SetProgress(progress float64) {
76 pb.mu.Lock()
77 defer pb.mu.Unlock()
78
79 // Clamp progress to valid range
80 if progress < 0 {
81 progress = 0
82 } else if progress > 1 {
83 progress = 1
84 }
85
86 pb.targetValue = progress
87
88 // For large jumps (>0.3) or completion, update immediately
89 diff := progress - pb.currentValue
90 if diff > 0.3 || progress >= 1.0 {
91 pb.currentValue = progress
92 pb.progressBar.SetFraction(progress)
93 return
94 }
95
96 // Start animation if not already running
97 if pb.animationTimer == 0 {
98 pb.startAnimation()
99 }
100}
101
102// startAnimation begins the smooth progress animation.
103// Must be called with lock held.
104func (pb *ProgressBar) startAnimation() {
105 cb := glib.SourceFunc(func(_ uintptr) bool {
106 pb.mu.Lock()
107 defer pb.mu.Unlock()
108
109 // Check if we've reached the target
110 if pb.currentValue >= pb.targetValue {
111 pb.animationTimer = 0
112 return false // Stop the timer
113 }
114
115 // Increment towards target
116 pb.currentValue += progressStep
117 if pb.currentValue > pb.targetValue {
118 pb.currentValue = pb.targetValue
119 }
120
121 pb.progressBar.SetFraction(pb.currentValue)
122
123 // Continue animation if not at target
124 if pb.currentValue >= pb.targetValue {
125 pb.animationTimer = 0 // Clear before GLib auto-removes
126 return false
127 }
128 return true
129 })
130
131 pb.animationTimer = glib.TimeoutAdd(progressIntervalMs, &cb, 0)
132}
133
134// Show makes the progress bar visible and starts the auto-hide timeout.
135func (pb *ProgressBar) Show() {
136 pb.mu.Lock()
137 defer pb.mu.Unlock()
138
139 if !pb.visible {
140 pb.visible = true
141 pb.progressBar.SetVisible(true)
142 }
143
144 // Reset timeout timer on every Show call
145 pb.resetTimeout()
146}
147
148// resetTimeout cancels any existing timeout and starts a new one.
149// Must be called with lock held.
150func (pb *ProgressBar) resetTimeout() {
151 // Cancel existing timeout
152 if pb.timeoutTimer != 0 {
153 glib.SourceRemove(pb.timeoutTimer)
154 pb.timeoutTimer = 0
155 }
156
157 // Start new timeout
158 cb := glib.SourceFunc(func(_ uintptr) bool {
159 pb.mu.Lock()
160 defer pb.mu.Unlock()
161
162 // Clear timer ID (timer is being removed)
163 pb.timeoutTimer = 0
164
165 // Hide the progress bar inline to avoid race condition
166 pb.hideInternal()
167 return false // Don't repeat
168 })
169
170 pb.timeoutTimer = glib.TimeoutAdd(progressTimeoutMs, &cb, 0)
171}
172
173// Hide makes the progress bar invisible and resets state.
174func (pb *ProgressBar) Hide() {
175 pb.mu.Lock()
176 defer pb.mu.Unlock()
177
178 pb.hideInternal()
179}
180
181// hideInternal performs the actual hide operation.
182// Must be called with lock held.
183func (pb *ProgressBar) hideInternal() {
184 if pb.visible {
185 pb.visible = false
186 pb.progressBar.SetVisible(false)
187
188 // Stop any running animation
189 if pb.animationTimer != 0 {
190 glib.SourceRemove(pb.animationTimer)
191 pb.animationTimer = 0
192 }
193
194 // Stop timeout timer
195 if pb.timeoutTimer != 0 {
196 glib.SourceRemove(pb.timeoutTimer)
197 pb.timeoutTimer = 0
198 }
199
200 // Reset values
201 pb.currentValue = 0
202 pb.targetValue = 0
203 pb.progressBar.SetFraction(0)
204 }
205}
206
207// IsVisible returns whether the progress bar is currently visible.
208func (pb *ProgressBar) IsVisible() bool {
209 pb.mu.Lock()
210 defer pb.mu.Unlock()
211 return pb.visible
212}
213
214// Widget returns the underlying widget for embedding in overlays.
215func (pb *ProgressBar) Widget() layout.Widget {
216 return pb.progressBar
217}