5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 17:39:58 +08:00

Merge pull request #1400 from wailsapp/feature/native-translucency

Initial support for new native translucency in Windows Preview
This commit is contained in:
Lea Anthony 2022-05-18 18:52:45 +10:00 committed by GitHub
parent 935f6871ab
commit 953bc09343
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 8 deletions

View File

@ -1,11 +1,8 @@
//go:build windows
package win32
import (
"unsafe"
"golang.org/x/sys/windows/registry"
"unsafe"
)
type DWMWINDOWATTRIBUTE int32
@ -15,10 +12,19 @@ const DwmwaUseImmersiveDarkMode DWMWINDOWATTRIBUTE = 20
const DwmwaBorderColor DWMWINDOWATTRIBUTE = 34
const DwmwaCaptionColor DWMWINDOWATTRIBUTE = 35
const DwmwaTextColor DWMWINDOWATTRIBUTE = 36
const DwmwaSystemBackdropType DWMWINDOWATTRIBUTE = 38
const SPI_GETHIGHCONTRAST = 0x0042
const HCF_HIGHCONTRASTON = 0x00000001
type BackdropType int32
const DwmsbtAuto BackdropType = 0
const DwmsbtDisable = 1 // None
const DwmsbtMainWindow = 2 // Mica
const DwmsbtTransientWindow = 3 // Acrylic
const DwmsbtTabbedWindow = 4 // Tabbed
func dwmSetWindowAttribute(hwnd uintptr, dwAttribute DWMWINDOWATTRIBUTE, pvAttribute unsafe.Pointer, cbAttribute uintptr) {
ret, _, err := procDwmSetWindowAttribute.Call(
hwnd,
@ -46,7 +52,19 @@ func SetTheme(hwnd uintptr, useDarkMode bool) {
if IsWindowsVersionAtLeast(10, 0, 18985) {
attr = DwmwaUseImmersiveDarkMode
}
dwmSetWindowAttribute(hwnd, attr, unsafe.Pointer(&useDarkMode), unsafe.Sizeof(&useDarkMode))
var winDark int32
if useDarkMode {
winDark = 1
}
dwmSetWindowAttribute(hwnd, attr, unsafe.Pointer(&winDark), unsafe.Sizeof(winDark))
}
}
func EnableTranslucency(hwnd uintptr, backdrop BackdropType) {
if IsWindowsVersionAtLeast(10, 0, 22579) {
dwmSetWindowAttribute(hwnd, DwmwaSystemBackdropType, unsafe.Pointer(&backdrop), unsafe.Sizeof(backdrop))
} else {
println("Warning: Translucency unavailable on Windows < 22579")
}
}
@ -73,7 +91,6 @@ func IsCurrentlyDarkMode() bool {
if err != nil {
return false
}
return AppsUseLightTheme == 0
}
@ -91,5 +108,6 @@ func IsCurrentlyHighContrastMode() bool {
_ = err
return false
}
return result.DwFlags&HCF_HIGHCONTRASTON == HCF_HIGHCONTRASTON
r := result.DwFlags&HCF_HIGHCONTRASTON == HCF_HIGHCONTRASTON
return r
}

View File

@ -78,7 +78,12 @@ func NewWindow(parent winc.Controller, appoptions *options.App, versionInfo *ope
if appoptions.Windows != nil {
if appoptions.Windows.WindowIsTranslucent {
// TODO: Migrate to win32 package
if !win32.IsWindowsVersionAtLeast(10, 0, 22579) {
result.SetTranslucentBackground()
} else {
win32.EnableTranslucency(result.Handle(), win32.BackdropType(appoptions.Windows.TranslucencyType))
}
}
if appoptions.Windows.DisableWindowIcon {

View File

@ -23,6 +23,16 @@ const (
Light Theme = 2
)
type BackdropType int32
const (
Auto BackdropType = 0
Disable BackdropType = 1 // None
MainWindow BackdropType = 2 // Mica
TransientWindow BackdropType = 3 // Acrylic
TabbedWindow BackdropType = 4 // Tabbed
)
func RGB(r, g, b uint8) int32 {
var col = int32(b)
col = col<<8 | int32(g)
@ -67,6 +77,9 @@ type Options struct {
// Custom settings for dark/light mode
CustomTheme *ThemeSettings
// Windows 11 22579 minimum
TranslucencyType BackdropType
// User messages that can be customised
Messages *Messages
}