Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement automatic theme watching #1479

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions core/renderwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,14 +527,10 @@ func (w *renderWindow) handleWindowEvents(e events.Event) {
if DebugSettings.WindowEventTrace {
log.Println("Win: ScreenUpdate", w.name, screenConfig())
}
if !TheApp.Platform().IsMobile() { // native desktop
if TheApp.NScreens() > 0 {
AppearanceSettings.Apply()
UpdateAll()
theWindowGeometrySaver.restoreAll()
}
} else {
w.resized()
if TheApp.NScreens() > 0 {
AppearanceSettings.Apply()
UpdateAll()
theWindowGeometrySaver.restoreAll()
}
}
}
Expand Down
1 change: 0 additions & 1 deletion system/driver/offscreen/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (a *App) NewWindow(opts *system.NewWindowOptions) (system.Window, error) {

w.Event.WindowResize()
w.Event.Window(events.WinShow)
w.Event.Window(events.ScreenUpdate)
w.Event.Window(events.WinFocus)

go w.WinLoop()
Expand Down
2 changes: 1 addition & 1 deletion system/driver/web/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func (a *App) SetSystemWindow() {
a.Resize()
a.InitDrawer()
a.Event.Window(events.WinShow)
a.Event.Window(events.ScreenUpdate)
a.Event.Window(events.WinFocus)
}

Expand Down Expand Up @@ -218,6 +217,7 @@ func (a *App) Cursor(win system.Window) system.Cursor {
}

func (a *App) IsDark() bool {
// See https://stackoverflow.com/a/57795495
return js.Global().Get("matchMedia").Truthy() &&
js.Global().Call("matchMedia", "(prefers-color-scheme: dark)").Get("matches").Truthy()
}
Expand Down
11 changes: 11 additions & 0 deletions system/driver/web/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func (a *App) AddEventListeners() {
AddEventListener(g, "beforeinput", a.OnBeforeInput)
AddEventListener(g.Get("visualViewport"), "resize", a.OnResize)
AddEventListener(g, "blur", a.OnBlur)

// See https://stackoverflow.com/a/57795495
if g.Get("matchMedia").Truthy() {
pcs := g.Call("matchMedia", "(prefers-color-scheme: dark)")
AddEventListener(pcs, "change", a.OnThemeChange)
}
}

func AddEventListener(v js.Value, nm string, fn func(this js.Value, args []js.Value) any, opts ...map[string]any) {
Expand Down Expand Up @@ -270,3 +276,8 @@ func (a *App) OnBlur(this js.Value, args []js.Value) any {
a.KeyMods = 0
return nil
}

func (a *App) OnThemeChange(this js.Value, args []js.Value) any {
a.Event.Window(events.ScreenUpdate)
return nil
}
Loading