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
22 changes: 19 additions & 3 deletions internal/driver/glfw/window.go
@@ -1,6 +1,5 @@
package glfw

import "C"
import (
"bytes"
"context"
Expand All @@ -10,14 +9,15 @@ 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"
"fyne.io/fyne/v2/theme"
)

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

runOnMain(func() {
w.setDarkMode(fyne.CurrentApp().Settings().ThemeVariant() == theme.VariantDark)

win := w.view()
win.SetCloseCallback(w.closed)
win.SetPosCallback(w.moved)
Expand Down Expand Up @@ -1533,3 +1535,17 @@ func isKeyModifier(keyName fyne.KeyName) bool {
keyName == desktop.KeyAltLeft || keyName == desktop.KeyAltRight ||
keyName == desktop.KeySuperLeft || keyName == desktop.KeySuperRight
}

//lint:ignore U1000 This is fine, just a helper function for some platforms
Jacalz marked this conversation as resolved.
Show resolved Hide resolved
func standardResize(w *window, size fyne.Size) {
d, ok := fyne.CurrentApp().Driver().(*gLDriver)
if !ok { // don't wait to redraw in this way if we are running on test
w.canvas.Resize(size)
return
}

runOnDraw(w, func() {
w.canvas.Resize(size)
d.repaintWindow(w)
})
}
3 changes: 3 additions & 0 deletions internal/driver/glfw/window_linux.go
Expand Up @@ -6,3 +6,6 @@ func (w *window) platformResize(canvasSize fyne.Size) {
w.canvas.Resize(canvasSize)
w.canvas.Refresh(w.canvas.content)
}

func (w *window) setDarkMode(dark bool) {
}
14 changes: 4 additions & 10 deletions internal/driver/glfw/window_other.go
@@ -1,18 +1,12 @@
// +build !linux
// +build !linux,!windows

package glfw

import "fyne.io/fyne/v2"

func (w *window) platformResize(canvasSize fyne.Size) {
d, ok := fyne.CurrentApp().Driver().(*gLDriver)
if !ok { // don't wait to redraw in this way if we are running on test
w.canvas.Resize(canvasSize)
return
}
standardResize(w, canvasSize)
}

runOnDraw(w, func() {
w.canvas.Resize(canvasSize)
d.repaintWindow(w)
})
func (w *window) setDarkMode(dark bool) {
}
30 changes: 30 additions & 0 deletions internal/driver/glfw/window_windows.go
@@ -0,0 +1,30 @@
package glfw

import (
"runtime"
"syscall"
"unsafe"

"fyne.io/fyne/v2"
)

func (w *window) platformResize(canvasSize fyne.Size) {
standardResize(w, canvasSize)
}

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

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)
}
}
}