fix: synchronize macOS window theme on flutter theme changed.

This commit is contained in:
Kingtous
2023-02-02 13:57:20 +08:00
parent 76adc8de20
commit 6119e04006
6 changed files with 94 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ import 'package:back_button_interceptor/back_button_interceptor.dart';
import 'package:desktop_multi_window/desktop_multi_window.dart';
import 'package:ffi/ffi.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_hbb/utils/platform_channel.dart';
import 'package:win32/win32.dart' as win32;
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
@@ -235,6 +236,12 @@ class MyTheme {
}
bind.mainChangeTheme(dark: mode.toShortString());
}
// Synchronize the window theme of the system.
if (Platform.isMacOS) {
final isDark = mode == ThemeMode.dark;
RdPlatformChannel.instance.changeSystemWindowTheme(
isDark ? SystemWindowTheme.dark : SystemWindowTheme.light);
}
}
static ThemeMode currentThemeMode() {
@@ -1686,3 +1693,16 @@ String getWindowName({WindowType? overrideType}) {
String getWindowNameWithId(String id, {WindowType? overrideType}) {
return "${DesktopTab.labelGetterAlias(id).value} - ${getWindowName(overrideType: overrideType)}";
}
void updateSystemWindowTheme() {
// Set system window theme for macOS
final userPreference = MyTheme.getThemeModePreference();
if (userPreference != ThemeMode.system) {
if (Platform.isMacOS) {
RdPlatformChannel.instance.changeSystemWindowTheme(
userPreference == ThemeMode.light
? SystemWindowTheme.light
: SystemWindowTheme.dark);
}
}
}

View File

@@ -1,9 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter_hbb/common.dart';
import 'package:flutter_hbb/main.dart';
import 'package:get/get.dart';
class RefreshWrapper extends StatefulWidget {
final Widget Function(BuildContext context) builder;
const RefreshWrapper({super.key, required this.builder});
@override
@@ -30,6 +32,8 @@ class RefreshWrapperState extends State<RefreshWrapper> {
if (Get.context != null) {
(context as Element).visitChildren(_rebuildElement);
}
// Synchronize the window theme of the system.
updateSystemWindowTheme();
setState(() {});
}

View File

@@ -108,6 +108,8 @@ Future<void> initEnv(String appType) async {
await initGlobalFFI();
// await Firebase.initializeApp();
_registerEventHandler();
// Update the system theme.
updateSystemWindowTheme();
}
void runMainApp(bool startService) async {
@@ -327,6 +329,8 @@ class _AppState extends State<App> {
to = ThemeMode.light;
}
Get.changeThemeMode(to);
// Synchronize the window theme of the system.
updateSystemWindowTheme();
if (desktopType == DesktopType.main) {
bind.mainChangeTheme(dark: to.toShortString());
}

View File

@@ -0,0 +1,34 @@
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hbb/main.dart';
enum SystemWindowTheme { light, dark }
/// The platform channel for RustDesk
class RdPlatformChannel {
RdPlatformChannel._();
static final RdPlatformChannel _windowUtil = RdPlatformChannel._();
static RdPlatformChannel get instance => _windowUtil;
final MethodChannel _osxMethodChannel =
MethodChannel("org.rustdesk.rustdesk/macos");
final MethodChannel _winMethodChannel =
MethodChannel("org.rustdesk.rustdesk/windows");
final MethodChannel _linuxMethodChannel =
MethodChannel("org.rustdesk.rustdesk/linux");
/// Change the theme of the system window
Future<void> changeSystemWindowTheme(SystemWindowTheme theme) {
assert(Platform.isMacOS);
if (kDebugMode) {
print(
"[Window ${kWindowId ?? 'Main'}] change system window theme to ${theme.name}");
}
return _osxMethodChannel
.invokeMethod("setWindowTheme", {"themeName": theme.name});
}
}