From 33a9c052133e35deee1d4c86827c3d0cbdfd130e Mon Sep 17 00:00:00 2001 From: ALMAS Date: Fri, 25 Jul 2025 19:46:05 +0800 Subject: [PATCH] [V3] feat: add Windows getAccentColor implementation (#4427) * [V3] feat: add Windows getAccentColor implementation --------- Co-authored-by: Lea Anthony --- docs/src/content/docs/changelog.mdx | 7 +++-- mkdocs-website/docs/en/changelog.md | 6 ++--- v3/UNRELEASED_CHANGELOG.md | 1 + v3/pkg/application/application_windows.go | 10 ++++--- v3/pkg/w32/theme.go | 33 ++++++++++++++--------- 5 files changed, 35 insertions(+), 22 deletions(-) diff --git a/docs/src/content/docs/changelog.mdx b/docs/src/content/docs/changelog.mdx index b74d02fb1..a2330334b 100644 --- a/docs/src/content/docs/changelog.mdx +++ b/docs/src/content/docs/changelog.mdx @@ -31,7 +31,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Windows dark theme menus + menubar. By @leaanthony in [a29b4f0861b1d0a700e9eb213c6f1076ec40efd5](https://github.com/wailsapp/wails/commit/a29b4f0861b1d0a700e9eb213c6f1076ec40efd5) - Rename built-in services for clearer JS/TS bindings by @popaprozac in [PR](https://github.com/wailsapp/wails/pull/4405) - ## v3.0.0-alpha.12 - 2025-07-15 ### Added @@ -482,9 +481,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - 🐧 add task `run:linux` by [@marcus-crane](https://github.com/marcus-crane) in [#3146](https://github.com/wailsapp/wails/pull/3146) -- Export `SetIcon` method by @almas1992 in +- Export `SetIcon` method by [@almas-x](https://github.com/almas-x) in [PR](https://github.com/wailsapp/wails/pull/3147) -- Improve `OnShutdown` by @almas1992 in +- Improve `OnShutdown` by [@almas-x](https://github.com/almas-x) in [PR](https://github.com/wailsapp/wails/pull/3189) - Restore `ToggleMaximise` method in `Window` interface by [@fbbdev](https://github.com/fbbdev) in @@ -555,7 +554,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed bug for linux in doctor in the event user doesn't have proper drivers installed. Added by [@pylotlight](https://github.com/pylotlight) in [PR](https://github.com/wailsapp/wails/pull/3032) -- Fix dpi scaling on start up (windows). Changed by @almas1992 in +- Fix dpi scaling on start up (windows). Changed by [@almas-x](https://github.com/almas-x) in [PR](https://github.com/wailsapp/wails/pull/3145) - Fix replace line in `go.mod` to use relative paths. Fixes Windows paths with spaces - @leaanthony. diff --git a/mkdocs-website/docs/en/changelog.md b/mkdocs-website/docs/en/changelog.md index 9350cdac1..0023f38ca 100644 --- a/mkdocs-website/docs/en/changelog.md +++ b/mkdocs-website/docs/en/changelog.md @@ -23,8 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [darwin] add option for showing the toolbar in fullscreen mode on macOS by [@fbbdev](https://github.com/fbbdev) in [#3282](https://github.com/wailsapp/wails/pull/3282) - [linux] add onKeyPress logic to convert linux keypress into an accelerator @[Atterpac](https://github.com/Atterpac) in[#3022](https://github.com/wailsapp/wails/pull/3022]) - [linux] add task `run:linux` by [@marcus-crane](https://github.com/marcus-crane) in [#3146](https://github.com/wailsapp/wails/pull/3146) -- Export `SetIcon` method by @almas1992 in [PR](https://github.com/wailsapp/wails/pull/3147) -- Improve `OnShutdown` by @almas1992 in [PR](https://github.com/wailsapp/wails/pull/3189) +- Export `SetIcon` method by [@almas-x](https://github.com/almas-x) in [PR](https://github.com/wailsapp/wails/pull/3147) +- Improve `OnShutdown` by [@almas-x](https://github.com/almas-x) in [PR](https://github.com/wailsapp/wails/pull/3189) - Restore `ToggleMaximise` method in `Window` interface by [@fbbdev](https://github.com/fbbdev) in [#3281](https://github.com/wailsapp/wails/pull/3281) - Added more information to `Environment()`. By @leaanthony in [aba82cc](https://github.com/wailsapp/wails/commit/aba82cc52787c97fb99afa58b8b63a0004b7ff6c) based on [PR](https://github.com/wailsapp/wails/pull/2044) by @Mai-Lapyst - Expose the `WebviewWindow.IsFocused` method on the `Window` interface by [@fbbdev](https://github.com/fbbdev) in [#3295](https://github.com/wailsapp/wails/pull/3295) @@ -49,7 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed hex values for arrow keys on Darwin by [jaybeecave](https://github.com/jaybeecave) in [#3052](https://github.com/wailsapp/wails/pull/3052). - Set drag-n-drop for windows to working. Added by [@pylotlight](https://github.com/pylotlight) in [PR](https://github.com/wailsapp/wails/pull/3039) - Fixed bug for linux in doctor in the event user doesn't have proper drivers installed. Added by [@pylotlight](https://github.com/pylotlight) in [PR](https://github.com/wailsapp/wails/pull/3032) -- Fix dpi scaling on start up (windows). Changed by @almas1992 in [PR](https://github.com/wailsapp/wails/pull/3145) +- Fix dpi scaling on start up (windows). Changed by [@almas-x](https://github.com/almas-x) in [PR](https://github.com/wailsapp/wails/pull/3145) - Fix replace line in `go.mod` to use relative paths. Fixes Windows paths with spaces - @leaanthony. - Fix MacOS systray click handling when no attached window by [thomas-senechal](https://github.com/thomas-senechal) in PR [#3207](https://github.com/wailsapp/wails/pull/3207) - Fix failing Windows build due to unknown option by [thomas-senechal](https://github.com/thomas-senechal) in PR [#3208](https://github.com/wailsapp/wails/pull/3208) diff --git a/v3/UNRELEASED_CHANGELOG.md b/v3/UNRELEASED_CHANGELOG.md index 8e4648038..8fc834092 100644 --- a/v3/UNRELEASED_CHANGELOG.md +++ b/v3/UNRELEASED_CHANGELOG.md @@ -17,6 +17,7 @@ After processing, the content will be moved to the main changelog and this file ## Added +- Add Windows `getAccentColor` implementation by [@almas-x](https://github.com/almas-x) in [PR](https://github.com/wailsapp/wails/pull/4427) ## Changed diff --git a/v3/pkg/application/application_windows.go b/v3/pkg/application/application_windows.go index 7caf4f5c4..f72abf963 100644 --- a/v3/pkg/application/application_windows.go +++ b/v3/pkg/application/application_windows.go @@ -57,9 +57,13 @@ func (m *windowsApp) isDarkMode() bool { } func (m *windowsApp) getAccentColor() string { - // Windows doesn't have a simple accent color API in the current w32 package - // Return a default blue color - return "rgb(0,122,255)" + accentColor, err := w32.GetAccentColor() + if err != nil { + m.parent.error("failed to get accent color: %w", err) + return "rgb(0,122,255)" + } + + return accentColor } func (m *windowsApp) isOnMainThread() bool { diff --git a/v3/pkg/w32/theme.go b/v3/pkg/w32/theme.go index bbb510ef6..0e8a8ef17 100644 --- a/v3/pkg/w32/theme.go +++ b/v3/pkg/w32/theme.go @@ -3,6 +3,7 @@ package w32 import ( + "fmt" "syscall" "unsafe" @@ -145,18 +146,6 @@ func init() { } } - //procAllowDarkModeForApp, err := windows.GetProcAddressByOrdinal(localUXTheme, uintptr(137)) - //if err == nil { - // procAllowDarkModeForApplication = func(hwnd HWND, allow bool) uintptr { - // var allowInt int32 - // if allow { - // allowInt = 1 - // } - // ret, _, _ := syscall.SyscallN(procAllowDarkModeForApp, uintptr(allowInt)) - // return ret - // } - //} - // Initialize dark mode if SetPreferredAppMode != nil { SetPreferredAppMode(PreferredAppModeAllowDark) @@ -300,6 +289,26 @@ func IsCurrentlyHighContrastMode() bool { return r } +func GetAccentColor() (string, error) { + key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\DWM`, registry.QUERY_VALUE) + if err != nil { + return "", err + } + defer key.Close() + + accentColor, _, err := key.GetIntegerValue("AccentColor") + if err != nil { + return "", err + } + + // Extract RGB components from ABGR format (Alpha, Blue, Green, Red) + red := uint8(accentColor & 0xFF) + green := uint8((accentColor >> 8) & 0xFF) + blue := uint8((accentColor >> 16) & 0xFF) + + return fmt.Sprintf("rgb(%d,%d,%d)", red, green, blue), nil +} + // OpenThemeData opens theme data for a window and its class func OpenThemeData(hwnd HWND, pszClassList string) HTHEME { ret, _, _ := procOpenThemeData.Call(