Skip to content

Commit

Permalink
Merge branch 'wincmd'
Browse files Browse the repository at this point in the history
  • Loading branch information
benma committed Apr 12, 2023
2 parents e288014 + 782bb28 commit 651133c
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 69 deletions.
39 changes: 39 additions & 0 deletions frontends/qt/server/detectdarktheme_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2023 Shift Crypto AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build darwin
// +build darwin

package main

import (
"os/exec"
"strings"

"github.com/digitalbitbox/bitbox-wallet-app/util/logging"
)

// detect theme used by OS and return true if it's dark
func detectDarkTheme() bool {
log := logging.Get().WithGroup("server")
cmd := exec.Command("defaults", "read", "-g", "AppleInterfaceStyle")
out, err := cmd.Output()
if err == nil {
log.Info("MacOS theme: " + string(out))
if strings.TrimSpace(string(out)) == "Dark" {
return true
}
}
return false
}
69 changes: 69 additions & 0 deletions frontends/qt/server/detectdarktheme_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2023 Shift Crypto AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux
// +build linux

package main

import (
"os"
"os/exec"

"github.com/digitalbitbox/bitbox-wallet-app/util/logging"
)

func detectDarkTheme() bool {
log := logging.Get().WithGroup("server")
// Try KDE first, since Kubuntu can also have `gsettings` and that can lead to wrong results
cmd := exec.Command("kreadconfig5", "--file", os.ExpandEnv("$HOME/.config/kdeglobals"), "--group", "General", "--key", "ColorScheme")
out, err := cmd.Output()
if err == nil {
log.Info("kde theme: " + string(out))
if matchDarkTheme(string(out)) {
return true
}
}

// Try Gnome/Ubuntu
cmd = exec.Command("gsettings", "get", "org.gnome.desktop.interface", "color-scheme")
out, err = cmd.Output()
if err == nil {
log.Info("Gnome/Ubuntu theme: " + string(out))
if matchDarkTheme(string(out)) {
return true
}
}

// Try Cinnamon
cmd = exec.Command("gsettings", "get", "org.cinnamon.desktop.interface", "gtk-theme")
out, err = cmd.Output()
if err == nil {
log.Info("Cinnamon theme: " + string(out))
if matchDarkTheme(string(out)) {
return true
}
}

// Try XFCE4
cmd = exec.Command("xfconf-query", "-c", "xsettings", "-p", "/Net/ThemeName")
out, err = cmd.Output()
if err == nil {
log.Info("xfce theme: " + string(out))
if matchDarkTheme(string(out)) {
return true
}
}
return false
}
43 changes: 43 additions & 0 deletions frontends/qt/server/detectdarktheme_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2023 Shift Crypto AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build windows
// +build windows

package main

import (
"os/exec"
"strings"
"syscall"

"github.com/digitalbitbox/bitbox-wallet-app/util/logging"
)

// detect theme used by OS and return true if it's dark
func detectDarkTheme() bool {
log := logging.Get().WithGroup("server")
const regKey = `HKCU\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize`
const regName = `AppsUseLightTheme`
cmd := exec.Command("reg", "query", regKey, "/v", regName)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
out, err := cmd.Output()
if err == nil {
log.Info("windows theme: " + string(out))
if strings.Contains(string(out), "0x0") {
return true
}
}
return false
}
69 changes: 0 additions & 69 deletions frontends/qt/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import "C"
import (
"flag"
"os"
"os/exec"
"runtime"
"strings"
"unsafe"
Expand Down Expand Up @@ -96,74 +95,6 @@ func matchDarkTheme(themeName string) bool {
return strings.Contains(strings.ToLower(themeName), "dark")
}

// detect theme used by OS and return true if it's dark
func detectDarkTheme() bool {
log := logging.Get().WithGroup("server")
switch myos := runtime.GOOS; myos {
case "darwin":
cmd := exec.Command("defaults", "read", "-g", "AppleInterfaceStyle")
out, err := cmd.Output()
if err == nil {
log.Info("MacOS theme: " + string(out))
if strings.TrimSpace(string(out)) == "Dark" {
return true
}
}
case "linux":
// Try KDE first, since Kubuntu can also have `gsettings` and that can lead to wrong results
cmd := exec.Command("kreadconfig5", "--file", os.ExpandEnv("$HOME/.config/kdeglobals"), "--group", "General", "--key", "ColorScheme")
out, err := cmd.Output()
if err == nil {
log.Info("kde theme: " + string(out))
if matchDarkTheme(string(out)) {
return true
}
}

// Try Gnome/Ubuntu
cmd = exec.Command("gsettings", "get", "org.gnome.desktop.interface", "color-scheme")
out, err = cmd.Output()
if err == nil {
log.Info("Gnome/Ubuntu theme: " + string(out))
if matchDarkTheme(string(out)) {
return true
}
}

// Try Cinnamon
cmd = exec.Command("gsettings", "get", "org.cinnamon.desktop.interface", "gtk-theme")
out, err = cmd.Output()
if err == nil {
log.Info("Cinnamon theme: " + string(out))
if matchDarkTheme(string(out)) {
return true
}
}

// Try XFCE4
cmd = exec.Command("xfconf-query", "-c", "xsettings", "-p", "/Net/ThemeName")
out, err = cmd.Output()
if err == nil {
log.Info("xfce theme: " + string(out))
if matchDarkTheme(string(out)) {
return true
}
}
case "windows":
const regKey = `HKCU\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize`
const regName = `AppsUseLightTheme`
cmd := exec.Command("reg", "query", regKey, "/v", regName)
out, err := cmd.Output()
if err == nil {
log.Info("windows theme: " + string(out))
if strings.Contains(string(out), "0x0") {
return true
}
}
}
return false
}

//export serve
func serve(
cppHeapFreeFn C.cppHeapFree,
Expand Down

0 comments on commit 651133c

Please sign in to comment.