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

Add a windows 10 workaround to force dark mode #2216

Merged
merged 9 commits into from May 22, 2021
1 change: 1 addition & 0 deletions cmd/fyne/internal/commands/package.go
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"flag"
"fmt"

// import image encodings
_ "image/jpeg"
_ "image/png"
Expand Down
7 changes: 4 additions & 3 deletions internal/driver/glfw/window.go
@@ -1,6 +1,5 @@
package glfw

import "C"
import (
"bytes"
"context"
Expand All @@ -10,14 +9,14 @@ import (
"sync"
"time"

"github.com/go-gl/glfw/v3.3/glfw"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/internal"
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/internal/driver"
"fyne.io/fyne/v2/internal/painter/gl"

"github.com/go-gl/glfw/v3.3/glfw"
)

const (
Expand Down Expand Up @@ -1449,6 +1448,8 @@ func (w *window) create() {
})

runOnMain(func() {
w.setDarkMode()

win := w.view()
win.SetCloseCallback(w.closed)
win.SetPosCallback(w.moved)
Expand Down
6 changes: 6 additions & 0 deletions internal/driver/glfw/window_notwindows.go
@@ -0,0 +1,6 @@
// +build !windows

package glfw

func (w *window) setDarkMode() {
}
43 changes: 43 additions & 0 deletions internal/driver/glfw/window_windows.go
@@ -0,0 +1,43 @@
package glfw

import (
"runtime"
"syscall"
"unsafe"

"fyne.io/fyne/v2"
"golang.org/x/sys/windows/registry"
)

func (w *window) setDarkMode() {
if runtime.GOOS == "windows" {
hwnd := w.view().GetWin32Window()
dark := isDark()

dwm := syscall.NewLazyDLL("dwmapi.dll")
setAtt := dwm.NewProc("DwmSetWindowAttribute")
ret, _, err := setAtt.Call(uintptr(unsafe.Pointer(hwnd)), // window handle
20, // DWMWA_USE_IMMERSIVE_DARK_MODE
uintptr(unsafe.Pointer(&dark)), // on or off
8) // sizeof(darkMode)

if ret != 0 { // err is always non-nil, we check return value
fyne.LogError("Failed to set dark mode", err)
}
}
}

func isDark() bool {
k, err := registry.OpenKey(registry.CURRENT_USER, `SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize`, registry.QUERY_VALUE)
if err != nil { // older version of Windows will not have this key
return false
}
defer k.Close()

useLight, _, err := k.GetIntegerValue("AppsUseLightTheme")
if err != nil { // older version of Windows will not have this value
return false
}

return useLight == 0
}