mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-15 15:15:51 +01:00
refactor(theme): convert platform-specific window theme methods to standalone functions
This commit is contained in:
parent
6d9e228139
commit
e8a1bf0886
3 changed files with 16 additions and 17 deletions
|
|
@ -4,9 +4,9 @@ package application
|
|||
|
||||
import "fmt"
|
||||
|
||||
// getOppositeAppearance returns the macOS appearance that represents
|
||||
// getOppositeMacAppearance returns the macOS appearance that represents
|
||||
// the opposite light/dark variant.
|
||||
func (w *macosWebviewWindow) getOppositeAppearance(name string) (MacAppearanceType, error) {
|
||||
func getOppositeMacAppearance(name string) (MacAppearanceType, error) {
|
||||
if name == "NSAppearanceNameDarkAqua" {
|
||||
return "NSAppearanceNameAqua", nil
|
||||
}
|
||||
|
|
@ -16,10 +16,9 @@ func (w *macosWebviewWindow) getOppositeAppearance(name string) (MacAppearanceTy
|
|||
return "NSAppearanceNameDarkAqua", err
|
||||
}
|
||||
|
||||
// isAppearanceDark reports whether the current window appearance
|
||||
// isMacAppearanceDark reports whether the current window appearance
|
||||
// corresponds to a dark macOS appearance.
|
||||
func (w *macosWebviewWindow) isAppearanceDark() bool {
|
||||
appr := w.getEffectiveAppearanceName()
|
||||
func isMacAppearanceDark(appr string) bool {
|
||||
// Check if the appearance name contains "Dark"
|
||||
switch appr {
|
||||
case "NSAppearanceNameDarkAqua",
|
||||
|
|
@ -40,7 +39,7 @@ func (w *macosWebviewWindow) syncTheme() {
|
|||
}
|
||||
|
||||
currentAppearance := w.getEffectiveAppearanceName()
|
||||
currentDark := w.isAppearanceDark()
|
||||
currentDark := isMacAppearanceDark(currentAppearance)
|
||||
|
||||
switch globalApplication.theme {
|
||||
case AppSystemDefault:
|
||||
|
|
@ -48,12 +47,12 @@ func (w *macosWebviewWindow) syncTheme() {
|
|||
return
|
||||
case AppDark:
|
||||
if !currentDark {
|
||||
appr, _ := w.getOppositeAppearance(currentAppearance)
|
||||
appr, _ := getOppositeMacAppearance(currentAppearance)
|
||||
w.setAppearanceByName(appr)
|
||||
}
|
||||
case AppLight:
|
||||
if currentDark {
|
||||
appr, _ := w.getOppositeAppearance(currentAppearance)
|
||||
appr, _ := getOppositeMacAppearance(currentAppearance)
|
||||
w.setAppearanceByName(appr)
|
||||
}
|
||||
}
|
||||
|
|
@ -74,18 +73,18 @@ func (w *macosWebviewWindow) setTheme(theme WinTheme) {
|
|||
}
|
||||
|
||||
currentAppearance := w.getEffectiveAppearanceName()
|
||||
isDark := w.isAppearanceDark()
|
||||
isDark := isMacAppearanceDark(currentAppearance)
|
||||
w.parent.followApplicationTheme = false
|
||||
|
||||
switch theme {
|
||||
case WinThemeDark:
|
||||
if !isDark {
|
||||
appr, _ := w.getOppositeAppearance(currentAppearance)
|
||||
appr, _ := getOppositeMacAppearance(currentAppearance)
|
||||
w.setAppearanceByName(appr)
|
||||
}
|
||||
case WinThemeLight:
|
||||
if isDark {
|
||||
appr, _ := w.getOppositeAppearance(currentAppearance)
|
||||
appr, _ := getOppositeMacAppearance(currentAppearance)
|
||||
w.setAppearanceByName(appr)
|
||||
}
|
||||
}
|
||||
|
|
@ -103,7 +102,7 @@ func (w *macosWebviewWindow) getTheme() WinTheme {
|
|||
return WinThemeSystem
|
||||
}
|
||||
|
||||
if w.isAppearanceDark() {
|
||||
if isMacAppearanceDark(w.getEffectiveAppearanceName()) {
|
||||
return WinThemeDark
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,10 +4,10 @@ package application
|
|||
|
||||
import "github.com/wailsapp/wails/v3/pkg/w32"
|
||||
|
||||
// resolveTheme determines the realized Theme for the window by resolving
|
||||
// resolveWindowsEffectiveTheme determines the realized Theme for the window by resolving
|
||||
// application-level and window-level theme settings.
|
||||
func (w *windowsWebviewWindow) resolveTheme() Theme {
|
||||
switch w.parent.options.Windows.Theme {
|
||||
func resolveWindowsEffectiveTheme(winTheme WinTheme, appTheme AppTheme) Theme {
|
||||
switch winTheme {
|
||||
case WinThemeDark:
|
||||
return Dark
|
||||
case WinThemeLight:
|
||||
|
|
@ -16,7 +16,7 @@ func (w *windowsWebviewWindow) resolveTheme() Theme {
|
|||
return SystemDefault
|
||||
default:
|
||||
// For WinThemeApplication and/or Unset values we default to following
|
||||
switch globalApplication.theme {
|
||||
switch appTheme {
|
||||
case AppDark:
|
||||
return Dark
|
||||
case AppLight:
|
||||
|
|
|
|||
|
|
@ -515,7 +515,7 @@ func (w *windowsWebviewWindow) run() {
|
|||
}
|
||||
|
||||
// System, Dark, Light - Resolved Theme to Apply
|
||||
w.theme = w.resolveTheme()
|
||||
w.theme = resolveWindowsEffectiveTheme(options.Windows.Theme, globalApplication.theme)
|
||||
w.syncTheme()
|
||||
|
||||
// Always listen to OS theme changes but only update the theme if we are following the application theme
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue