mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 17:22:01 +08:00
[Feature/1149] Dark mode: functions for manually switching theme (#1291)
* [Feature/1149] Dark mode: functions for manually switching theme
This commit is contained in:
parent
584f88f727
commit
7dd3f96915
@ -125,6 +125,18 @@ func (f *Frontend) WindowReload() {
|
|||||||
f.ExecJS("runtime.WindowReload();")
|
f.ExecJS("runtime.WindowReload();")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *Frontend) WindowSetSystemDefaultTheme() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Frontend) WindowSetLightTheme() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Frontend) WindowSetDarkTheme() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Frontend) Run(ctx context.Context) error {
|
func (f *Frontend) Run(ctx context.Context) error {
|
||||||
|
|
||||||
f.ctx = context.WithValue(ctx, "frontend", f)
|
f.ctx = context.WithValue(ctx, "frontend", f)
|
||||||
|
@ -114,6 +114,18 @@ func (f *Frontend) WindowReload() {
|
|||||||
f.ExecJS("runtime.WindowReload();")
|
f.ExecJS("runtime.WindowReload();")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *Frontend) WindowSetSystemDefaultTheme() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Frontend) WindowSetLightTheme() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Frontend) WindowSetDarkTheme() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Frontend) Run(ctx context.Context) error {
|
func (f *Frontend) Run(ctx context.Context) error {
|
||||||
|
|
||||||
f.ctx = context.WithValue(ctx, "frontend", f)
|
f.ctx = context.WithValue(ctx, "frontend", f)
|
||||||
|
@ -13,6 +13,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/wailsapp/wails/v2/internal/system/operatingsystem"
|
||||||
|
|
||||||
"github.com/wailsapp/wails/v2/internal/binding"
|
"github.com/wailsapp/wails/v2/internal/binding"
|
||||||
"github.com/wailsapp/wails/v2/internal/frontend"
|
"github.com/wailsapp/wails/v2/internal/frontend"
|
||||||
"github.com/wailsapp/wails/v2/internal/frontend/assetserver"
|
"github.com/wailsapp/wails/v2/internal/frontend/assetserver"
|
||||||
@ -23,6 +25,7 @@ import (
|
|||||||
"github.com/wailsapp/wails/v2/internal/logger"
|
"github.com/wailsapp/wails/v2/internal/logger"
|
||||||
"github.com/wailsapp/wails/v2/internal/system/operatingsystem"
|
"github.com/wailsapp/wails/v2/internal/system/operatingsystem"
|
||||||
"github.com/wailsapp/wails/v2/pkg/options"
|
"github.com/wailsapp/wails/v2/pkg/options"
|
||||||
|
"github.com/wailsapp/wails/v2/pkg/options/windows"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Frontend struct {
|
type Frontend struct {
|
||||||
@ -100,6 +103,30 @@ func (f *Frontend) WindowReload() {
|
|||||||
f.ExecJS("runtime.WindowReload();")
|
f.ExecJS("runtime.WindowReload();")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *Frontend) WindowSetSystemDefaultTheme() {
|
||||||
|
runtime.LockOSThread()
|
||||||
|
f.mainWindow.frontendOptions.Windows.Theme = windows.SystemDefault
|
||||||
|
f.mainWindow.Invoke(func() {
|
||||||
|
f.mainWindow.updateTheme()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Frontend) WindowSetLightTheme() {
|
||||||
|
runtime.LockOSThread()
|
||||||
|
f.mainWindow.frontendOptions.Windows.Theme = windows.Light
|
||||||
|
f.mainWindow.Invoke(func() {
|
||||||
|
f.mainWindow.updateTheme()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Frontend) WindowSetDarkTheme() {
|
||||||
|
runtime.LockOSThread()
|
||||||
|
f.mainWindow.frontendOptions.Windows.Theme = windows.Dark
|
||||||
|
f.mainWindow.Invoke(func() {
|
||||||
|
f.mainWindow.updateTheme()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Frontend) Run(ctx context.Context) error {
|
func (f *Frontend) Run(ctx context.Context) error {
|
||||||
|
|
||||||
f.ctx = context.WithValue(ctx, "frontend", f)
|
f.ctx = context.WithValue(ctx, "frontend", f)
|
||||||
|
@ -47,6 +47,18 @@ type DevWebServer struct {
|
|||||||
devServerAddr string
|
devServerAddr string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *DevWebServer) WindowSetSystemDefaultTheme() {
|
||||||
|
d.desktopFrontend.WindowSetSystemDefaultTheme()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DevWebServer) WindowSetLightTheme() {
|
||||||
|
d.desktopFrontend.WindowSetLightTheme()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DevWebServer) WindowSetDarkTheme() {
|
||||||
|
d.desktopFrontend.WindowSetDarkTheme()
|
||||||
|
}
|
||||||
|
|
||||||
func (d *DevWebServer) Run(ctx context.Context) error {
|
func (d *DevWebServer) Run(ctx context.Context) error {
|
||||||
d.ctx = ctx
|
d.ctx = ctx
|
||||||
|
|
||||||
|
@ -24,6 +24,15 @@ func (d *Dispatcher) processWindowMessage(message string, sender frontend.Fronte
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch message[1] {
|
switch message[1] {
|
||||||
|
case 'A':
|
||||||
|
switch message[2:] {
|
||||||
|
case "SDT":
|
||||||
|
go sender.WindowSetSystemDefaultTheme()
|
||||||
|
case "LT":
|
||||||
|
go sender.WindowSetLightTheme()
|
||||||
|
case "DT":
|
||||||
|
go sender.WindowSetDarkTheme()
|
||||||
|
}
|
||||||
case 'c':
|
case 'c':
|
||||||
go sender.WindowCenter()
|
go sender.WindowCenter()
|
||||||
case 'T':
|
case 'T':
|
||||||
|
@ -87,6 +87,9 @@ type Frontend interface {
|
|||||||
WindowUnfullscreen()
|
WindowUnfullscreen()
|
||||||
WindowSetRGBA(col *options.RGBA)
|
WindowSetRGBA(col *options.RGBA)
|
||||||
WindowReload()
|
WindowReload()
|
||||||
|
WindowSetSystemDefaultTheme()
|
||||||
|
WindowSetLightTheme()
|
||||||
|
WindowSetDarkTheme()
|
||||||
|
|
||||||
// Menus
|
// Menus
|
||||||
MenuSetApplicationMenu(menu *menu.Menu)
|
MenuSetApplicationMenu(menu *menu.Menu)
|
||||||
|
@ -17,6 +17,18 @@ export function WindowReload() {
|
|||||||
window.location.reload();
|
window.location.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function WindowSetSystemDefaultTheme() {
|
||||||
|
window.WailsInvoke('WASDT');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function WindowSetLightTheme() {
|
||||||
|
window.WailsInvoke('WALT');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function WindowSetDarkTheme() {
|
||||||
|
window.WailsInvoke('WADT');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Place the window in the center of the screen
|
* Place the window in the center of the screen
|
||||||
*
|
*
|
||||||
|
@ -235,6 +235,9 @@
|
|||||||
WindowMaximise: () => WindowMaximise,
|
WindowMaximise: () => WindowMaximise,
|
||||||
WindowMinimise: () => WindowMinimise,
|
WindowMinimise: () => WindowMinimise,
|
||||||
WindowReload: () => WindowReload,
|
WindowReload: () => WindowReload,
|
||||||
|
WindowSetSystemDefaultTheme: () => WindowSetSystemDefaultTheme,
|
||||||
|
WindowSetLightTheme: () => WindowSetLightTheme,
|
||||||
|
WindowSetDarkTheme: () => WindowSetDarkTheme,
|
||||||
WindowSetMaxSize: () => WindowSetMaxSize,
|
WindowSetMaxSize: () => WindowSetMaxSize,
|
||||||
WindowSetMinSize: () => WindowSetMinSize,
|
WindowSetMinSize: () => WindowSetMinSize,
|
||||||
WindowSetPosition: () => WindowSetPosition,
|
WindowSetPosition: () => WindowSetPosition,
|
||||||
@ -250,6 +253,15 @@
|
|||||||
function WindowReload() {
|
function WindowReload() {
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
}
|
}
|
||||||
|
function WindowSetSystemDefaultTheme() {
|
||||||
|
window.WailsInvoke('WASDT');
|
||||||
|
}
|
||||||
|
function WindowSetLightTheme() {
|
||||||
|
window.WailsInvoke('WALT');
|
||||||
|
}
|
||||||
|
function WindowSetDarkTheme() {
|
||||||
|
window.WailsInvoke('WADT');
|
||||||
|
}
|
||||||
function WindowCenter() {
|
function WindowCenter() {
|
||||||
window.WailsInvoke("Wc");
|
window.WailsInvoke("Wc");
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,12 @@ export interface runtime {
|
|||||||
|
|
||||||
WindowReload(): void;
|
WindowReload(): void;
|
||||||
|
|
||||||
|
WindowSetSystemDefaultTheme(): void;
|
||||||
|
|
||||||
|
WindowSetLightTheme(): void;
|
||||||
|
|
||||||
|
WindowSetDarkTheme(): void;
|
||||||
|
|
||||||
WindowCenter(): void;
|
WindowCenter(): void;
|
||||||
|
|
||||||
WindowSetTitle(title: string): void;
|
WindowSetTitle(title: string): void;
|
||||||
|
@ -19,6 +19,18 @@ export function WindowReload() {
|
|||||||
window.runtime.WindowReload();
|
window.runtime.WindowReload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function WindowSetSystemDefaultTheme() {
|
||||||
|
window.runtime.WindowSetSystemDefaultTheme();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function WindowSetLightTheme() {
|
||||||
|
window.runtime.WindowSetLightTheme();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function WindowSetDarkTheme() {
|
||||||
|
window.runtime.WindowSetDarkTheme();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Place the window in the center of the screen
|
* Place the window in the center of the screen
|
||||||
*
|
*
|
||||||
|
@ -36,6 +36,21 @@ func WindowReload(ctx context.Context) {
|
|||||||
appFrontend.WindowReload()
|
appFrontend.WindowReload()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WindowSetSystemDefaultTheme(ctx context.Context) {
|
||||||
|
appFrontend := getFrontend(ctx)
|
||||||
|
appFrontend.WindowSetSystemDefaultTheme()
|
||||||
|
}
|
||||||
|
|
||||||
|
func WindowSetLightTheme(ctx context.Context) {
|
||||||
|
appFrontend := getFrontend(ctx)
|
||||||
|
appFrontend.WindowSetLightTheme()
|
||||||
|
}
|
||||||
|
|
||||||
|
func WindowSetDarkTheme(ctx context.Context) {
|
||||||
|
appFrontend := getFrontend(ctx)
|
||||||
|
appFrontend.WindowSetDarkTheme()
|
||||||
|
}
|
||||||
|
|
||||||
// WindowShow shows the window if hidden
|
// WindowShow shows the window if hidden
|
||||||
func WindowShow(ctx context.Context) {
|
func WindowShow(ctx context.Context) {
|
||||||
appFrontend := getFrontend(ctx)
|
appFrontend := getFrontend(ctx)
|
||||||
|
@ -43,6 +43,33 @@ JS Signature: `WindowReload()`
|
|||||||
|
|
||||||
Performs a "reload" (Reloads index.html)
|
Performs a "reload" (Reloads index.html)
|
||||||
|
|
||||||
|
### WindowSetSystemDefaultTheme
|
||||||
|
Go Signature: `WindowSetSystemDefaultTheme(ctx context.Context)`
|
||||||
|
|
||||||
|
JS Signature: `WindowSetSystemDefaultTheme()`
|
||||||
|
|
||||||
|
Windows only.
|
||||||
|
|
||||||
|
Sets window theme to system default (dark/light).
|
||||||
|
|
||||||
|
### WindowSetLightTheme
|
||||||
|
Go Signature: `WindowSetLightTheme(ctx context.Context)`
|
||||||
|
|
||||||
|
JS Signature: `WindowSetLightTheme()`
|
||||||
|
|
||||||
|
Windows only.
|
||||||
|
|
||||||
|
Sets window theme to light.
|
||||||
|
|
||||||
|
### WindowSetDarkTheme
|
||||||
|
Go Signature: `WindowSetDarkTheme(ctx context.Context)`
|
||||||
|
|
||||||
|
JS Signature: `WindowSetDarkTheme()`
|
||||||
|
|
||||||
|
Windows only.
|
||||||
|
|
||||||
|
Sets window theme to dark.
|
||||||
|
|
||||||
### WindowShow
|
### WindowShow
|
||||||
Go Signature: `WindowShow(ctx context.Context)`
|
Go Signature: `WindowShow(ctx context.Context)`
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user