mirror of
https://github.com/wavetermdev/wails.git
synced 2026-08-05 13:53:43 -07:00
[v3 mac] Add IsDarkMode to application and JS runtime. Add Common.ThemeChanged event
This commit is contained in:
@@ -136,6 +136,12 @@ explicitly set with `--default-contextmenu: show`.
|
||||
| GetPrimary | Y | Y | Y | |
|
||||
| GetCurrent | Y | Y | Y | |
|
||||
|
||||
### System
|
||||
|
||||
| Feature | Windows | Linux | Mac | Notes |
|
||||
|------------|---------|-------|-----|-------|
|
||||
| IsDarkMode | | | Y | |
|
||||
|
||||
### Window
|
||||
|
||||
Y = Supported
|
||||
|
||||
@@ -68,4 +68,5 @@ require (
|
||||
mvdan.cc/sh/v3 v3.7.0 // indirect
|
||||
)
|
||||
|
||||
replace github.com/wailsapp/wails/v3 => D:\GolandProjects\wails\v3
|
||||
replace github.com/wailsapp/wails/v3 => ../..
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"embed"
|
||||
_ "embed"
|
||||
"github.com/wailsapp/wails/v3/pkg/events"
|
||||
"log"
|
||||
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
@@ -35,6 +36,14 @@ func main() {
|
||||
URL: "/",
|
||||
})
|
||||
|
||||
app.On(events.Common.ThemeChanged, func(e *application.Event) {
|
||||
if app.IsDarkMode() {
|
||||
log.Println("Dark mode!")
|
||||
} else {
|
||||
log.Println("Light mode!")
|
||||
}
|
||||
})
|
||||
|
||||
err := app.Run()
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -43,6 +43,7 @@ export const EventTypes = {
|
||||
ApplicationWillTerminate: "mac:ApplicationWillTerminate",
|
||||
ApplicationWillUnhide: "mac:ApplicationWillUnhide",
|
||||
ApplicationWillUpdate: "mac:ApplicationWillUpdate",
|
||||
ApplicationDidChangeTheme: "mac:ApplicationDidChangeTheme!",
|
||||
WindowDidBecomeKey: "mac:WindowDidBecomeKey",
|
||||
WindowDidBecomeMain: "mac:WindowDidBecomeMain",
|
||||
WindowDidBeginSheet: "mac:WindowDidBeginSheet",
|
||||
@@ -165,5 +166,6 @@ export const EventTypes = {
|
||||
WindowShow: "common:WindowShow",
|
||||
WindowHide: "common:WindowHide",
|
||||
WindowDPIChanged: "common:WindowDPIChanged",
|
||||
ThemeChanged: "common:ThemeChanged",
|
||||
},
|
||||
};
|
||||
|
||||
@@ -13,6 +13,7 @@ The electron alternative for Go
|
||||
import * as Clipboard from './clipboard';
|
||||
import * as Application from './application';
|
||||
import * as Screens from './screens';
|
||||
import * as System from './system';
|
||||
import {Plugin, Call, callErrorCallback, callCallback} from "./calls";
|
||||
import {newWindow} from "./window";
|
||||
import {dispatchWailsEvent, Emit, Off, OffAll, On, Once, OnMultiple} from "./events";
|
||||
@@ -53,6 +54,7 @@ export function newRuntime(windowName) {
|
||||
return newRuntime(windowName);
|
||||
}
|
||||
},
|
||||
System,
|
||||
Screens,
|
||||
Call,
|
||||
Plugin,
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
_ __ _ __
|
||||
| | / /___ _(_) /____
|
||||
| | /| / / __ `/ / / ___/
|
||||
| |/ |/ / /_/ / / (__ )
|
||||
|__/|__/\__,_/_/_/____/
|
||||
The electron alternative for Go
|
||||
(c) Lea Anthony 2019-present
|
||||
*/
|
||||
|
||||
/* jshint esversion: 9 */
|
||||
|
||||
import {newRuntimeCaller} from "./runtime";
|
||||
|
||||
let call = newRuntimeCaller("system");
|
||||
|
||||
/**
|
||||
* Determines if the system is currently using dark mode
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
export function IsDarkMode() {
|
||||
return call("IsDarkMode");
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -132,6 +132,7 @@ type (
|
||||
getScreens() ([]*Screen, error)
|
||||
GetFlags(options Options) map[string]any
|
||||
isOnMainThread() bool
|
||||
isDarkMode() bool
|
||||
}
|
||||
|
||||
runnable interface {
|
||||
@@ -653,6 +654,13 @@ func (a *App) dispatchEventToWindows(event *WailsEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) IsDarkMode() bool {
|
||||
if a.impl == nil {
|
||||
return false
|
||||
}
|
||||
return a.impl.isDarkMode()
|
||||
}
|
||||
|
||||
func (a *App) Hide() {
|
||||
if a.impl != nil {
|
||||
a.impl.hide()
|
||||
|
||||
@@ -52,6 +52,24 @@ static void init(void) {
|
||||
}
|
||||
return event;
|
||||
}];
|
||||
|
||||
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
|
||||
[center addObserver:appDelegate selector:@selector(themeChanged:) name:@"AppleInterfaceThemeChangedNotification" object:nil];
|
||||
|
||||
}
|
||||
|
||||
static bool isDarkMode(void) {
|
||||
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
|
||||
if (userDefaults == nil) {
|
||||
return false;
|
||||
}
|
||||
|
||||
NSString *interfaceStyle = [userDefaults stringForKey:@"AppleInterfaceStyle"];
|
||||
if (interfaceStyle == nil) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return [interfaceStyle isEqualToString:@"Dark"];
|
||||
}
|
||||
|
||||
static void setApplicationShouldTerminateAfterLastWindowClosed(bool shouldTerminate) {
|
||||
@@ -138,6 +156,10 @@ type macosApp struct {
|
||||
parent *App
|
||||
}
|
||||
|
||||
func (m *macosApp) isDarkMode() bool {
|
||||
return bool(C.isDarkMode())
|
||||
}
|
||||
|
||||
func getNativeApplication() *macosApp {
|
||||
return globalApplication.impl.(*macosApp)
|
||||
}
|
||||
|
||||
@@ -12,6 +12,11 @@ extern bool hasListeners(unsigned int);
|
||||
{
|
||||
return self.shouldTerminateWhenLastWindowClosed;
|
||||
}
|
||||
- (void)themeChanged:(NSNotification *)notification {
|
||||
if( hasListeners(EventApplicationDidChangeTheme) ) {
|
||||
processApplicationEvent(EventApplicationDidChangeTheme);
|
||||
}
|
||||
}
|
||||
// GENERATED EVENTS START
|
||||
- (void)applicationDidBecomeActive:(NSNotification *)notification {
|
||||
if( hasListeners(EventApplicationDidBecomeActive) ) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import "github.com/wailsapp/wails/v3/pkg/events"
|
||||
|
||||
var commonApplicationEventMap = map[events.ApplicationEventType]events.ApplicationEventType{
|
||||
events.Mac.ApplicationDidFinishLaunching: events.Common.ApplicationStarted,
|
||||
events.Mac.ApplicationDidChangeTheme: events.Common.ThemeChanged,
|
||||
}
|
||||
|
||||
func (m *macosApp) setupCommonEvents() {
|
||||
|
||||
@@ -94,6 +94,8 @@ func (m *MessageProcessor) HandleRuntimeCall(rw http.ResponseWriter, r *http.Req
|
||||
m.processScreensMethod(method, rw, r, targetWindow, params)
|
||||
case "call":
|
||||
m.processCallMethod(method, rw, r, targetWindow, params)
|
||||
case "system":
|
||||
m.processSystemMethod(method, rw, r, targetWindow, params)
|
||||
default:
|
||||
m.httpError(rw, "Unknown runtime call: %s", object)
|
||||
}
|
||||
|
||||
@@ -16,8 +16,10 @@ func (m *MessageProcessor) processApplicationMethod(method string, rw http.Respo
|
||||
case "Show":
|
||||
globalApplication.Show()
|
||||
m.ok(rw)
|
||||
case "IsDarkMode":
|
||||
m.json(rw, globalApplication.IsDarkMode())
|
||||
default:
|
||||
m.httpError(rw, "Unknown event method: %s", method)
|
||||
m.httpError(rw, "Unknown application method: %s", method)
|
||||
}
|
||||
|
||||
m.Info("Runtime:", "method", "Application."+method)
|
||||
|
||||
@@ -63,7 +63,7 @@ func (m *MessageProcessor) processCallMethod(method string, rw http.ResponseWrit
|
||||
}()
|
||||
m.ok(rw)
|
||||
default:
|
||||
m.httpError(rw, "Unknown dialog method: %s", method)
|
||||
m.httpError(rw, "Unknown call method: %s", method)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func (m *MessageProcessor) processContextMenuMethod(method string, rw http.Respo
|
||||
window.openContextMenu(&data)
|
||||
m.ok(rw)
|
||||
default:
|
||||
m.httpError(rw, "Unknown clipboard method: %s", method)
|
||||
m.httpError(rw, "Unknown contextmenu method: %s", method)
|
||||
}
|
||||
|
||||
m.Info("Runtime:", "method", "ContextMenu."+method)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user