1package content
2
3import (
4 "context"
5 "strings"
6
7 "github.com/bnema/dumber/internal/application/port"
8 "github.com/bnema/dumber/internal/domain/entity"
9 "github.com/bnema/dumber/internal/logging"
10 "github.com/bnema/dumber/internal/ui/adapter"
11 "github.com/bnema/dumber/internal/ui/layout"
12 "github.com/bnema/puregotk/v4/gdk"
13 "github.com/bnema/puregotk/v4/glib"
14)
15
16// GetTitle returns the current title for a pane.
17func (c *Coordinator) GetTitle(paneID entity.PaneID) string {
18 c.titleMu.RLock()
19 defer c.titleMu.RUnlock()
20 return c.paneTitles[paneID]
21}
22
23// onTitleChanged updates title tracking when a WebView's title changes.
24func (c *Coordinator) onTitleChanged(ctx context.Context, paneID entity.PaneID, title string) {
25 log := logging.FromContext(ctx)
26
27 // Update title map
28 c.titleMu.Lock()
29 c.paneTitles[paneID] = title
30 c.titleMu.Unlock()
31
32 // Update domain model and stacked title if available.
33 if c.getActiveWS != nil {
34 ws, wsView := c.getActiveWS()
35 if ws != nil {
36 paneNode := ws.FindPane(paneID)
37 if paneNode != nil && paneNode.Pane != nil {
38 paneNode.Pane.Title = title
39 }
40 }
41
42 if wsView != nil {
43 tr := wsView.TreeRenderer()
44 if tr != nil {
45 if stackedView := tr.GetStackedViewForPane(string(paneID)); stackedView != nil {
46 c.updateStackedPaneTitle(ctx, stackedView, paneID, title)
47 }
48 }
49 }
50 }
51
52 // Notify history persistence (get URL from WebView)
53 if c.onTitleUpdated != nil {
54 if wv := c.getWebViewLocked(paneID); wv != nil {
55 currentURI := wv.URI()
56 if currentURI != "" && title != "" {
57 c.onTitleUpdated(ctx, paneID, currentURI, title)
58 }
59 }
60 }
61
62 // Notify window title updates only for the active pane.
63 if c.onWindowTitleChanged != nil && c.getActiveWS != nil {
64 if ws, _ := c.getActiveWS(); ws != nil && ws.ActivePaneID == paneID {
65 c.onWindowTitleChanged(paneID, title)
66 }
67 }
68
69 log.Debug().
70 Str("pane_id", string(paneID)).
71 Str("title", title).
72 Msg("pane title updated")
73}
74
75// updateStackedPaneTitle updates the title of a pane in a StackedView.
76func (c *Coordinator) updateStackedPaneTitle(
77 ctx context.Context,
78 sv *layout.StackedView,
79 paneID entity.PaneID,
80 title string,
81) {
82 log := logging.FromContext(ctx)
83
84 // Find the pane's index directly in the StackedView
85 index := sv.FindPaneIndex(string(paneID))
86 if index < 0 {
87 log.Debug().
88 Str("pane_id", string(paneID)).
89 Msg("pane not found in StackedView for title update")
90 return
91 }
92
93 if err := sv.UpdateTitle(index, title); err != nil {
94 log.Warn().Err(err).Int("index", index).Msg("failed to update stacked pane title")
95 }
96}
97
98// syncStackedTitle updates the stacked title bar for a pane if it's in a stack.
99// Called from onLoadCommitted to keep titles in sync during navigation.
100func (c *Coordinator) syncStackedTitle(ctx context.Context, paneID entity.PaneID, title string) {
101 if c.getActiveWS == nil {
102 return
103 }
104 _, wsView := c.getActiveWS()
105 if wsView == nil {
106 return
107 }
108 tr := wsView.TreeRenderer()
109 if tr == nil {
110 return
111 }
112 if sv := tr.GetStackedViewForPane(string(paneID)); sv != nil {
113 c.updateStackedPaneTitle(ctx, sv, paneID, title)
114 }
115}
116
117// onFaviconChanged updates favicon tracking when a WebView's favicon changes.
118func (c *Coordinator) onFaviconChanged(ctx context.Context, paneID entity.PaneID, emittingWV port.WebView, favicon *gdk.Texture) {
119 log := logging.FromContext(ctx)
120
121 // Verify this WebView is still bound to the expected pane
122 currentWV := c.getWebViewLocked(paneID)
123 if currentWV == nil || currentWV != emittingWV {
124 log.Debug().
125 Str("pane_id", string(paneID)).
126 Msg("ignoring favicon change from unbound webview")
127 return
128 }
129
130 uri := emittingWV.URI()
131
132 // Update favicon cache with domain key (handles cross-domain redirects)
133 if c.faviconAdapter != nil && favicon != nil && uri != "" {
134 c.navOriginMu.RLock()
135 originURL := c.navOrigins[paneID]
136 c.navOriginMu.RUnlock()
137 c.faviconAdapter.StoreFromWebKitWithOrigin(ctx, uri, originURL, favicon)
138 }
139
140 // Update StackedView favicon if this pane is in a stack
141 c.updateStackedFaviconForPane(ctx, paneID, favicon)
142
143 log.Debug().
144 Str("pane_id", string(paneID)).
145 Str("uri", uri).
146 Bool("has_favicon", favicon != nil).
147 Msg("pane favicon updated")
148}
149
150// updateStackedPaneFavicon updates the favicon of a pane in a StackedView.
151func (c *Coordinator) updateStackedPaneFavicon(
152 ctx context.Context,
153 sv *layout.StackedView,
154 paneID entity.PaneID,
155 favicon *gdk.Texture,
156) {
157 log := logging.FromContext(ctx)
158
159 // Find the pane's index directly in the StackedView
160 index := sv.FindPaneIndex(string(paneID))
161 if index < 0 {
162 log.Debug().
163 Str("pane_id", string(paneID)).
164 Msg("pane not found in StackedView for favicon update")
165 return
166 }
167
168 if err := sv.UpdateFaviconTexture(index, favicon); err != nil {
169 log.Warn().Err(err).Int("index", index).Msg("failed to update stacked pane favicon")
170 }
171}
172
173// updateStackedFaviconForPane updates the stacked title bar favicon for a pane.
174func (c *Coordinator) updateStackedFaviconForPane(ctx context.Context, paneID entity.PaneID, texture *gdk.Texture) {
175 if c.getActiveWS == nil {
176 return
177 }
178 _, wsView := c.getActiveWS()
179 if wsView == nil {
180 return
181 }
182 tr := wsView.TreeRenderer()
183 if tr == nil {
184 return
185 }
186 if sv := tr.GetStackedViewForPane(string(paneID)); sv != nil {
187 c.updateStackedPaneFavicon(ctx, sv, paneID, texture)
188 }
189}
190
191// resolveCommittedFavicon ensures the stacked title bar has a favicon after navigation commits.
192// First checks if WebKit already has a favicon for the page (common for subpath URLs).
193// Falls back to the full GetOrFetch pipeline (cache + WebKit DB + DuckDuckGo API).
194// This closes the gap where title bars relied solely on the notify::favicon signal.
195func (c *Coordinator) resolveCommittedFavicon(ctx context.Context, paneID entity.PaneID, wv port.WebView) {
196 if c.faviconAdapter == nil || wv == nil {
197 return
198 }
199
200 uri := wv.URI()
201 if uri == "" || strings.HasPrefix(uri, "about:") || strings.HasPrefix(uri, "dumb://") {
202 return
203 }
204
205 log := logging.FromContext(ctx)
206
207 // Check if we already have a texture cached for this domain
208 if texture := c.faviconAdapter.GetTextureByURL(uri); texture != nil {
209 // Already cached - just ensure the stacked view is updated
210 c.updateStackedFaviconForPane(ctx, paneID, texture)
211 return
212 }
213
214 // Check if the WebView already has a favicon for this page
215 if icon := wv.Favicon(); icon != nil {
216 if gdkIcon, ok := icon.(*gdk.Texture); ok {
217 log.Debug().Str("pane_id", string(paneID)).Str("uri", uri).Msg("using existing favicon for committed page")
218 // Store and update through the normal path
219 c.navOriginMu.RLock()
220 originURL := c.navOrigins[paneID]
221 c.navOriginMu.RUnlock()
222 c.faviconAdapter.StoreFromWebKitWithOrigin(ctx, uri, originURL, gdkIcon)
223 c.updateStackedFaviconForPane(ctx, paneID, gdkIcon)
224 return
225 }
226 }
227
228 // Fall back to GetOrFetch (checks service cache, engine DB, then DuckDuckGo API)
229 // Capture current generation to guard against stale callbacks
230 gen := wv.Generation()
231 capturedURI := uri
232 c.faviconAdapter.GetOrFetch(ctx, uri, func(texture *gdk.Texture) {
233 // Skip nil results — a nil means "couldn't resolve", not "no favicon".
234 // Without this guard, a late nil callback can overwrite a good favicon
235 // that was already set by an earlier onFaviconChanged signal.
236 if texture == nil {
237 return
238 }
239 // Verify WebView is still bound to pane and hasn't been reused
240 if wv.Generation() != gen {
241 return
242 }
243 currentWV := c.getWebViewLocked(paneID)
244 if currentWV != wv {
245 return
246 }
247 // Verify the WebView hasn't navigated away since we started the fetch
248 if currentWV.URI() != capturedURI {
249 return
250 }
251 c.updateStackedFaviconForPane(ctx, paneID, texture)
252 })
253}
254
255// FaviconAdapter returns the favicon adapter for external use (e.g., omnibox).
256func (c *Coordinator) FaviconAdapter() *adapter.FaviconAdapter {
257 return c.faviconAdapter
258}
259
260// SetNavigationOrigin records the original URL before navigation starts.
261// This allows caching favicons under both original and final domains
262// when cross-domain redirects occur (e.g., google.fr → google.com).
263func (c *Coordinator) SetNavigationOrigin(paneID entity.PaneID, uri string) {
264 c.navOriginMu.Lock()
265 c.navOrigins[paneID] = uri
266 c.navOriginMu.Unlock()
267}
268
269// PreloadCachedFavicon checks the favicon cache and updates the stacked pane
270// title bar immediately if a cached favicon exists for the URL.
271// This provides instant favicon display without waiting for WebKit.
272func (c *Coordinator) PreloadCachedFavicon(ctx context.Context, paneID entity.PaneID, uri string) {
273 if c.faviconAdapter == nil || uri == "" {
274 return
275 }
276 select {
277 case <-ctx.Done():
278 return
279 default:
280 }
281
282 // Check memory and disk cache (no external fetch)
283 texture := c.faviconAdapter.PreloadFromCache(ctx, uri)
284 select {
285 case <-ctx.Done():
286 return
287 default:
288 }
289
290 // Update stacked pane favicon on GTK main loop.
291 cb := glib.SourceFunc(func(_ uintptr) bool {
292 select {
293 case <-ctx.Done():
294 return false
295 default:
296 }
297 if c.getActiveWS == nil {
298 return false
299 }
300 // Only update if we found a cached texture.
301 // If nil, let resolveCommittedFavicon handle it after load commits.
302 if texture == nil {
303 return false
304 }
305 _, wsView := c.getActiveWS()
306 if wsView == nil {
307 return false
308 }
309 tr := wsView.TreeRenderer()
310 if tr == nil {
311 return false
312 }
313 stackedView := tr.GetStackedViewForPane(string(paneID))
314 if stackedView == nil {
315 return false
316 }
317 c.updateStackedPaneFavicon(ctx, stackedView, paneID, texture)
318 return false
319 })
320 glib.IdleAdd(&cb, 0)
321}