forked from richardwilkes/unison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_windows.go
92 lines (80 loc) · 2.29 KB
/
app_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright ©2021-2022 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import (
"sync/atomic"
"syscall"
"time"
"github.com/richardwilkes/toolbox/errs"
"github.com/richardwilkes/toolbox/xio"
"github.com/richardwilkes/unison/enums/thememode"
"github.com/richardwilkes/unison/internal/w32"
"golang.org/x/sys/windows/registry"
)
var appUsesLightThemeValue = uint32(1)
func platformEarlyInit() {
AttachConsole()
}
func platformLateInit() {
keyPath := `Software\Microsoft\Windows\CurrentVersion\Themes\Personalize`
k, err := registry.OpenKey(registry.CURRENT_USER, keyPath, syscall.KEY_NOTIFY|registry.QUERY_VALUE)
if err != nil {
errs.Log(errs.NewWithCause("unable to open dark mode key", err), "key", "CURRENT_USER", "path", keyPath)
return
}
if err = updateTheme(k, true); err != nil {
errs.Log(err)
xio.CloseIgnoringErrors(k)
return
}
go func() {
for {
w32.RegNotifyChangeKeyValue(k, false, w32.RegNotifyChangeName|w32.RegNotifyChangeLastSet, 0, false)
if err = updateTheme(k, false); err != nil {
errs.Log(err)
xio.CloseIgnoringErrors(k)
return
}
}
}()
}
func platformFinishedStartup() {
}
func platformBeep() {
w32.MessageBeep(w32.MBDefault)
}
func platformIsDarkModeTrackingPossible() bool {
return true
}
func platformIsDarkModeEnabled() bool {
return atomic.LoadUint32(&appUsesLightThemeValue) == 0
}
func platformDoubleClickInterval() time.Duration {
return w32.GetDoubleClickTime()
}
func updateTheme(k registry.Key, sync bool) error {
val, _, err := k.GetIntegerValue("AppsUseLightTheme")
if err != nil {
return errs.NewWithCause("unable to retrieve current dark mode value", err)
}
var swapped bool
if val == 0 {
swapped = atomic.CompareAndSwapUint32(&appUsesLightThemeValue, 1, 0)
} else {
swapped = atomic.CompareAndSwapUint32(&appUsesLightThemeValue, 0, 1)
}
if swapped && currentThemeMode == thememode.Auto {
if sync {
ThemeChanged()
} else {
InvokeTask(ThemeChanged)
}
}
return nil
}