settings.go RawHistoryFolder
1package webkit
2
3import (
4 "context"
5 "sync"
6
7 "github.com/bnema/dumber/internal/infrastructure/config"
8 "github.com/bnema/dumber/internal/logging"
9 "github.com/bnema/puregotk/v4/webkit"
10 "github.com/rs/zerolog"
11)
12
13// SettingsManager creates and manages WebKit Settings instances from config.
14type SettingsManager struct {
15 cfg *config.Config
16 mu sync.RWMutex
17}
18
19// NewSettingsManager creates a new SettingsManager with the given config.
20func NewSettingsManager(ctx context.Context, cfg *config.Config) *SettingsManager {
21 log := logging.FromContext(ctx)
22 log.Debug().Msg("creating settings manager")
23 return &SettingsManager{
24 cfg: cfg,
25 }
26}
27
28// CreateSettings creates a new webkit.Settings instance configured from the current config.
29func (sm *SettingsManager) CreateSettings(ctx context.Context) *webkit.Settings {
30 log := logging.FromContext(ctx)
31
32 sm.mu.RLock()
33 cfg := sm.cfg
34 sm.mu.RUnlock()
35
36 settings := webkit.NewSettings()
37 if settings == nil {
38 log.Error().Msg("failed to create webkit settings")
39 return nil
40 }
41
42 sm.applySettings(ctx, settings, cfg)
43 return settings
44}
45
46// applySettings applies configuration to a webkit.Settings instance.
47func (sm *SettingsManager) applySettings(ctx context.Context, settings *webkit.Settings, cfg *config.Config) {
48 if sm == nil {
49 return
50 }
51 log := logging.FromContext(ctx)
52 applyJavaScriptSettings(settings)
53 applyFontSettings(settings, cfg)
54 applyDebugSettings(settings, cfg)
55 applyBrowsingSettings(settings)
56 applyMediaSettings(settings, cfg, log)
57 applyStorageSettings(settings)
58 applyUISettings(settings)
59 applyCanvasSettings(settings)
60 applyWebRTCSettings(settings)
61
62 webrtcEnabled := settings.GetPropertyEnableWebrtc()
63 mediaStreamEnabled := settings.GetPropertyEnableMediaStream()
64
65 log.Debug().
66 Str("sans_font", cfg.Appearance.SansFont).
67 Bool("developer_extras", cfg.Debug.EnableDevTools).
68 Bool("webrtc_enabled", webrtcEnabled).
69 Bool("media_stream_enabled", mediaStreamEnabled).
70 Msg("settings applied")
71}
72
73func applyJavaScriptSettings(settings *webkit.Settings) {
74 settings.SetEnableJavascript(true)
75 settings.SetEnableJavascriptMarkup(true)
76}
77
78func applyFontSettings(settings *webkit.Settings, cfg *config.Config) {
79 if cfg.Appearance.SansFont != "" {
80 settings.SetDefaultFontFamily(cfg.Appearance.SansFont)
81 settings.SetSansSerifFontFamily(cfg.Appearance.SansFont)
82 }
83 if cfg.Appearance.SerifFont != "" {
84 settings.SetSerifFontFamily(cfg.Appearance.SerifFont)
85 }
86 if cfg.Appearance.MonospaceFont != "" {
87 settings.SetMonospaceFontFamily(cfg.Appearance.MonospaceFont)
88 }
89 if cfg.Appearance.DefaultFontSize > 0 {
90 settings.SetDefaultFontSize(uint32(cfg.Appearance.DefaultFontSize))
91 }
92}
93
94func applyDebugSettings(settings *webkit.Settings, cfg *config.Config) {
95 settings.SetEnableDeveloperExtras(cfg.Debug.EnableDevTools)
96 settings.SetEnableWriteConsoleMessagesToStdout(cfg.Logging.CaptureConsole)
97 settings.SetDrawCompositingIndicators(cfg.Engine.WebKit.DrawCompositingIndicators)
98}
99
100func applyBrowsingSettings(settings *webkit.Settings) {
101 settings.SetEnableSmoothScrolling(true)
102 settings.SetEnablePageCache(true)
103 settings.SetEnableSiteSpecificQuirks(true)
104}
105
106func applyMediaSettings(settings *webkit.Settings, cfg *config.Config, log *zerolog.Logger) {
107 settings.SetEnableWebaudio(true)
108 settings.SetEnableWebgl(true)
109 settings.SetEnableMedia(true)
110 settings.SetEnableMediasource(true)
111 settings.SetEnableMediaCapabilities(true)
112 settings.SetEnableEncryptedMedia(true)
113 settings.SetMediaPlaybackRequiresUserGesture(true)
114 settings.SetMediaPlaybackAllowsInline(true)
115
116 switch cfg.Media.HardwareDecodingMode {
117 case config.HardwareDecodingForce:
118 hwTypes := "video/av01;video/mp4;video/webm;video/x-h264;video/x-h265"
119 settings.SetMediaContentTypesRequiringHardwareSupport(&hwTypes)
120 log.Debug().Msg("hardware decoding: forced (may fail without hw support)")
121 case config.HardwareDecodingDisable:
122 settings.SetHardwareAccelerationPolicy(webkit.HardwareAccelerationPolicyNeverValue)
123 log.Debug().Msg("hardware decoding: disabled (software only)")
124 default:
125 emptyTypes := ""
126 settings.SetMediaContentTypesRequiringHardwareSupport(&emptyTypes)
127 log.Debug().Msg("hardware decoding: auto (hw preferred, software fallback)")
128 }
129}
130
131func applyStorageSettings(settings *webkit.Settings) {
132 settings.SetEnableHtml5LocalStorage(true)
133 settings.SetEnableHtml5Database(true)
134}
135
136func applyUISettings(settings *webkit.Settings) {
137 settings.SetEnableBackForwardNavigationGestures(true)
138 settings.SetEnableFullscreen(true)
139}
140
141func applyCanvasSettings(settings *webkit.Settings) {
142 settings.SetEnable2dCanvasAcceleration(true)
143}
144
145func applyWebRTCSettings(settings *webkit.Settings) {
146 // Set both direct API and gobject properties for robustness with generated bindings.
147 settings.SetPropertyEnableMediaStream(true)
148 settings.SetPropertyEnableWebrtc(true)
149
150 settings.SetEnableMediaStream(true)
151 settings.SetEnableWebrtc(true)
152}
153
154// UpdateFromConfig updates the manager with a new config (for hot-reload).
155// Note: This doesn't update already-created Settings instances.
156// New WebViews will use the updated config.
157func (sm *SettingsManager) UpdateFromConfig(ctx context.Context, cfg *config.Config) {
158 log := logging.FromContext(ctx)
159 sm.mu.Lock()
160 defer sm.mu.Unlock()
161 sm.cfg = cfg
162 log.Debug().Msg("settings config updated")
163}
164
165// ApplyToWebView applies current settings to an existing WebView.
166// This can be used to update a WebView's settings after config hot-reload.
167func (sm *SettingsManager) ApplyToWebView(ctx context.Context, wv *webkit.WebView) {
168 if sm == nil {
169 return
170 }
171 if wv == nil {
172 return
173 }
174
175 // Apply to the existing settings object attached to the WebView
176 existingSettings := wv.GetSettings()
177 if existingSettings != nil {
178 sm.mu.RLock()
179 cfg := sm.cfg
180 sm.mu.RUnlock()
181 sm.applySettings(ctx, existingSettings, cfg)
182 }
183}