detached 2553067

refactor(webkit): use ColorSchemeResolver in ContentInjector for dynamic dark mode

commit
25530676611e9afabecd84b34e2df6610e7a4e28
author
bnema · · 2025-12-29 20:51 UTC
parent
34930dd
signature
GPG Unverified2B82207D370127A0 The key is unknown
1 file · +12 -16
M internal/infrastructure/webkit/injector.go +12 -16
@@ -5,6 +5,7 @@ import (
55 "fmt"
66 "strings"
77
8+ "github.com/bnema/dumber/internal/application/port"
89 "github.com/bnema/dumber/internal/logging"
910 "github.com/bnema/puregotk-webkit/webkit"
1011 )
@@ -118,16 +119,16 @@ var internalPageAllowList = []string{"dumb://*"}
118119 // External pages receive dark mode preference via libadwaita's StyleManager.
119120 // Implements port.ContentInjector interface.
120121 type ContentInjector struct {
121- prefersDark bool
122- themeCSSVars string // CSS custom property declarations for WebUI
123- findCSS string // CSS for find-in-page highlight styling
122+ colorResolver port.ColorSchemeResolver
123+ themeCSSVars string // CSS custom property declarations for WebUI
124+ findCSS string // CSS for find-in-page highlight styling
124125 }
125126
126127 // NewContentInjector creates a new injector instance.
127-// The prefersDark parameter should come from ThemeManager.PrefersDark().
128-func NewContentInjector(prefersDark bool) *ContentInjector {
128+// The resolver is used to dynamically determine dark mode preference.
129+func NewContentInjector(resolver port.ColorSchemeResolver) *ContentInjector {
129130 return &ContentInjector{
130- prefersDark: prefersDark,
131+ colorResolver: resolver,
131132 }
132133 }
133134
@@ -149,15 +150,9 @@ func (ci *ContentInjector) InjectFindHighlightCSS(ctx context.Context, css strin
149150 return nil
150151 }
151152
152-// PrefersDark returns the current dark mode preference.
153+// PrefersDark returns the current dark mode preference from the resolver.
153154 func (ci *ContentInjector) PrefersDark() bool {
154- return ci.prefersDark
155-}
156-
157-// SetPrefersDark updates the dark mode preference.
158-// Call this when theme changes at runtime.
159-func (ci *ContentInjector) SetPrefersDark(prefersDark bool) {
160- ci.prefersDark = prefersDark
155+ return ci.colorResolver.Resolve().PrefersDark
161156 }
162157
163158 // InjectScripts adds scripts to the given content manager.
@@ -186,7 +181,8 @@ func (ci *ContentInjector) InjectScripts(ctx context.Context, ucm *webkit.UserCo
186181 }
187182
188183 // 1. Inject GTK dark mode preference for internal pages only
189- darkModePrefScript := fmt.Sprintf("window.__dumber_gtk_prefers_dark=%t;", ci.prefersDark)
184+ prefersDark := ci.PrefersDark()
185+ darkModePrefScript := fmt.Sprintf("window.__dumber_gtk_prefers_dark=%t;", prefersDark)
190186 addScript(
191187 webkit.NewUserScript(
192188 darkModePrefScript,
@@ -263,5 +259,5 @@ func (ci *ContentInjector) InjectScripts(ctx context.Context, ucm *webkit.UserCo
263259 }
264260 }
265261
266- log.Debug().Bool("prefers_dark", ci.prefersDark).Msg("scripts injected")
262+ log.Debug().Bool("prefers_dark", prefersDark).Msg("scripts injected")
267263 }
@@ -5,6 +5,7 @@ import (
5 "fmt"5 "fmt"
6 "strings"6 "strings"
7 7
8+ "github.com/bnema/dumber/internal/application/port"
8 "github.com/bnema/dumber/internal/logging"9 "github.com/bnema/dumber/internal/logging"
9 "github.com/bnema/puregotk-webkit/webkit"10 "github.com/bnema/puregotk-webkit/webkit"
10 )11 )
@@ -118,16 +119,16 @@ var internalPageAllowList = []string{"dumb://*"}
118 // External pages receive dark mode preference via libadwaita's StyleManager.119 // External pages receive dark mode preference via libadwaita's StyleManager.
119 // Implements port.ContentInjector interface.120 // Implements port.ContentInjector interface.
120 type ContentInjector struct {121 type ContentInjector struct {
121- prefersDark bool122+ colorResolver port.ColorSchemeResolver
122- themeCSSVars string // CSS custom property declarations for WebUI123+ themeCSSVars string // CSS custom property declarations for WebUI
123- findCSS string // CSS for find-in-page highlight styling124+ findCSS string // CSS for find-in-page highlight styling
124 }125 }
125 126
126 // NewContentInjector creates a new injector instance.127 // NewContentInjector creates a new injector instance.
127-// The prefersDark parameter should come from ThemeManager.PrefersDark().128+// The resolver is used to dynamically determine dark mode preference.
128-func NewContentInjector(prefersDark bool) *ContentInjector {129+func NewContentInjector(resolver port.ColorSchemeResolver) *ContentInjector {
129 return &ContentInjector{130 return &ContentInjector{
130- prefersDark: prefersDark,131+ colorResolver: resolver,
131 }132 }
132 }133 }
133 134
@@ -149,15 +150,9 @@ func (ci *ContentInjector) InjectFindHighlightCSS(ctx context.Context, css strin
149 return nil150 return nil
150 }151 }
151 152
152-// PrefersDark returns the current dark mode preference.153+// PrefersDark returns the current dark mode preference from the resolver.
153 func (ci *ContentInjector) PrefersDark() bool {154 func (ci *ContentInjector) PrefersDark() bool {
154- return ci.prefersDark155+ return ci.colorResolver.Resolve().PrefersDark
155-}
156-
157-// SetPrefersDark updates the dark mode preference.
158-// Call this when theme changes at runtime.
159-func (ci *ContentInjector) SetPrefersDark(prefersDark bool) {
160- ci.prefersDark = prefersDark
161 }156 }
162 157
163 // InjectScripts adds scripts to the given content manager.158 // InjectScripts adds scripts to the given content manager.
@@ -186,7 +181,8 @@ func (ci *ContentInjector) InjectScripts(ctx context.Context, ucm *webkit.UserCo
186 }181 }
187 182
188 // 1. Inject GTK dark mode preference for internal pages only183 // 1. Inject GTK dark mode preference for internal pages only
189- darkModePrefScript := fmt.Sprintf("window.__dumber_gtk_prefers_dark=%t;", ci.prefersDark)184+ prefersDark := ci.PrefersDark()
185+ darkModePrefScript := fmt.Sprintf("window.__dumber_gtk_prefers_dark=%t;", prefersDark)
190 addScript(186 addScript(
191 webkit.NewUserScript(187 webkit.NewUserScript(
192 darkModePrefScript,188 darkModePrefScript,
@@ -263,5 +259,5 @@ func (ci *ContentInjector) InjectScripts(ctx context.Context, ucm *webkit.UserCo
263 }259 }
264 }260 }
265 261
266- log.Debug().Bool("prefers_dark", ci.prefersDark).Msg("scripts injected")262+ log.Debug().Bool("prefers_dark", prefersDark).Msg("scripts injected")
267 }263 }

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 8

Tags 42

↑↓ moveenter openesc close