bulk.go RawHistoryFolder
1package content
2
3import (
4 "context"
5
6 "github.com/bnema/dumber/internal/application/port"
7 "github.com/bnema/dumber/internal/domain/entity"
8 "github.com/bnema/dumber/internal/logging"
9)
10
11// ApplyFiltersToAll applies content filters to all active webviews.
12// Called when filters become available after webviews were already created.
13func (c *Coordinator) ApplyFiltersToAll(ctx context.Context) {
14 log := logging.FromContext(ctx)
15 if c.filterApplier == nil {
16 log.Debug().Msg("ApplyFiltersToAll: no filter applier configured")
17 return
18 }
19
20 c.webViewsMu.RLock()
21 snapshot := make([]port.WebView, 0, len(c.webViews))
22 for _, wv := range c.webViews {
23 snapshot = append(snapshot, wv)
24 }
25 c.webViewsMu.RUnlock()
26
27 c.filterApplier.ApplyToAll(ctx, snapshot)
28 log.Debug().Int("count", len(snapshot)).Msg("applied filters to all webviews")
29}
30
31// ApplySettingsToAll reapplies engine settings to all active WebViews.
32func (c *Coordinator) ApplySettingsToAll(ctx context.Context) {
33 log := logging.FromContext(ctx)
34 if c.settingsApplier == nil {
35 log.Debug().Msg("ApplySettingsToAll: no settings applier configured")
36 return
37 }
38
39 c.webViewsMu.RLock()
40 snapshot := make([]port.WebView, 0, len(c.webViews))
41 for _, wv := range c.webViews {
42 snapshot = append(snapshot, wv)
43 }
44 c.webViewsMu.RUnlock()
45
46 c.settingsApplier.ApplyToAll(ctx, snapshot)
47 log.Debug().Int("count", len(snapshot)).Msg("applied settings to all webviews")
48}
49
50// RefreshInjectedScriptsToAll clears and re-injects user scripts into all active WebViews.
51//
52// WebKit user scripts are snapshotted when added to a WebKitUserContentManager, so when
53// appearance settings change at runtime (dark mode, palettes, UI scale), we must refresh
54// the scripts so future navigations pick up the latest values.
55// Script refresh is deferred for any WebView that is currently loading to avoid
56// removing scripts mid-navigation.
57func (c *Coordinator) RefreshInjectedScriptsToAll(ctx context.Context) {
58 log := logging.FromContext(ctx)
59 if c.injector == nil {
60 return
61 }
62
63 // Snapshot webViews under lock to avoid data race with concurrent popup create/close.
64 c.webViewsMu.RLock()
65 snapshot := make(map[entity.PaneID]port.WebView, len(c.webViews))
66 for k, v := range c.webViews {
67 snapshot[k] = v
68 }
69 c.webViewsMu.RUnlock()
70
71 for paneID, wv := range snapshot {
72 if wv == nil || wv.IsDestroyed() {
73 continue
74 }
75 if c.shouldDeferAppearance(wv) {
76 c.queueScriptRefresh(paneID)
77 log.Debug().Str("pane_id", string(paneID)).Msg("deferred script refresh until load finished")
78 continue
79 }
80
81 c.refreshInjectedScripts(ctx, c.injector, paneID, wv)
82 }
83}