feat: desktop_multi_window Support

This commit is contained in:
2025-03-16 17:14:45 +08:00
parent b18024a8ce
commit f5f3e4753c
17 changed files with 588 additions and 163 deletions

View File

@ -7,16 +7,14 @@ import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:starcitizen_doctor/generated/l10n.dart';
Future showToast(BuildContext context, String msg,
{BoxConstraints? constraints, String? title}) async {
Future showToast(BuildContext context, String msg, {BoxConstraints? constraints, String? title}) async {
return showBaseDialog(context,
title: title ?? S.current.app_common_tip,
content: Text(msg),
actions: [
FilledButton(
child: Padding(
padding:
const EdgeInsets.only(top: 2, bottom: 2, left: 8, right: 8),
padding: const EdgeInsets.only(top: 2, bottom: 2, left: 8, right: 8),
child: Text(S.current.app_common_tip_i_know),
),
onPressed: () => Navigator.pop(context),
@ -25,11 +23,8 @@ Future showToast(BuildContext context, String msg,
constraints: constraints);
}
Future<bool> showConfirmDialogs(
BuildContext context, String title, Widget content,
{String confirm = "",
String cancel = "",
BoxConstraints? constraints}) async {
Future<bool> showConfirmDialogs(BuildContext context, String title, Widget content,
{String confirm = "", String cancel = "", BoxConstraints? constraints}) async {
if (confirm.isEmpty) confirm = S.current.app_common_tip_confirm;
if (cancel.isEmpty) cancel = S.current.app_common_tip_cancel;
@ -40,8 +35,7 @@ Future<bool> showConfirmDialogs(
if (confirm.isNotEmpty)
FilledButton(
child: Padding(
padding:
const EdgeInsets.only(top: 2, bottom: 2, left: 8, right: 8),
padding: const EdgeInsets.only(top: 2, bottom: 2, left: 8, right: 8),
child: Text(confirm),
),
onPressed: () => Navigator.pop(context, true),
@ -49,8 +43,7 @@ Future<bool> showConfirmDialogs(
if (cancel.isNotEmpty)
Button(
child: Padding(
padding:
const EdgeInsets.only(top: 2, bottom: 2, left: 8, right: 8),
padding: const EdgeInsets.only(top: 2, bottom: 2, left: 8, right: 8),
child: Text(cancel),
),
onPressed: () => Navigator.pop(context, false),
@ -62,13 +55,15 @@ Future<bool> showConfirmDialogs(
Future<String?> showInputDialogs(BuildContext context,
{required String title,
required String content,
BoxConstraints? constraints,
String? initialValue,
List<TextInputFormatter>? inputFormatters}) async {
required String content,
BoxConstraints? constraints,
String? initialValue,
List<TextInputFormatter>? inputFormatters}) async {
String? userInput;
constraints ??=
BoxConstraints(maxWidth: MediaQuery.of(context).size.width * .38);
constraints ??= BoxConstraints(maxWidth: MediaQuery
.of(context)
.size
.width * .38);
final ok = await showConfirmDialogs(
context,
title,
@ -97,22 +92,20 @@ Future<String?> showInputDialogs(BuildContext context,
}
Future showBaseDialog(BuildContext context,
{required String title,
required Widget content,
List<Widget>? actions,
BoxConstraints? constraints}) async {
{required String title, required Widget content, List<Widget>? actions, BoxConstraints? constraints}) async {
return await showDialog(
context: context,
builder: (context) => ContentDialog(
title: Text(title),
content: content,
constraints: constraints ??
const BoxConstraints(
maxWidth: 512,
maxHeight: 756.0,
),
actions: actions,
),
builder: (context) =>
ContentDialog(
title: Text(title),
content: content,
constraints: constraints ??
const BoxConstraints(
maxWidth: 512,
maxHeight: 756.0,
),
actions: actions,
),
);
}
@ -120,10 +113,8 @@ bool stringIsNotEmpty(String? s) {
return s != null && (s.isNotEmpty);
}
Future<Uint8List?> widgetToPngImage(GlobalKey repaintBoundaryKey,
{double pixelRatio = 3.0}) async {
RenderRepaintBoundary? boundary = repaintBoundaryKey.currentContext
?.findRenderObject() as RenderRepaintBoundary?;
Future<Uint8List?> widgetToPngImage(GlobalKey repaintBoundaryKey, {double pixelRatio = 3.0}) async {
RenderRepaintBoundary? boundary = repaintBoundaryKey.currentContext?.findRenderObject() as RenderRepaintBoundary?;
if (boundary == null) return null;
ui.Image image = await boundary.toImage(pixelRatio: pixelRatio);
@ -133,11 +124,25 @@ Future<Uint8List?> widgetToPngImage(GlobalKey repaintBoundaryKey,
return pngBytes;
}
double roundDoubleTo(double value, double precision) =>
(value * precision).round() / precision;
double roundDoubleTo(double value, double precision) => (value * precision).round() / precision;
int getMinNumber(List<int> list) {
if (list.isEmpty) return 0;
list.sort((a, b) => a.compareTo(b));
return list.first;
}
String colorToHexCode(Color color, {ignoreTransparency = false}) {
final colorValue = color.toARGB32();
final colorAlpha = ((0xff000000 & colorValue) >> 24);
final r = ((0x00ff0000 & colorValue) >> 16).toRadixString(16).padLeft(2, '0');
final g = ((0x0000ff00 & colorValue) >> 8).toRadixString(16).padLeft(2, '0');
final b = ((0x000000ff & colorValue) >> 0).toRadixString(16).padLeft(2, '0');
final a = colorAlpha.toRadixString(16).padLeft(2, '0');
if (ignoreTransparency || colorAlpha == 255) {
return '#$r$g$b';
} else {
return '#$a$r$g$b';
}
}

View File

@ -0,0 +1,108 @@
import 'dart:convert';
import 'package:desktop_multi_window/desktop_multi_window.dart';
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:starcitizen_doctor/app.dart';
import 'package:starcitizen_doctor/ui/tools/log_analyze_ui/log_analyze_ui.dart';
import 'base_utils.dart';
part 'multi_window_manager.freezed.dart';
part 'multi_window_manager.g.dart';
@freezed
class MultiWindowAppState with _$MultiWindowAppState {
const factory MultiWindowAppState({
required String backgroundColor,
required String menuColor,
required String micaColor,
String? languageCode,
String? countryCode,
}) = _MultiWindowAppState;
factory MultiWindowAppState.fromJson(Map<String, dynamic> json) => _$MultiWindowAppStateFromJson(json);
}
class MultiWindowManager {
static Future<void> launchSubWindow(String type, AppGlobalState appGlobalState) async {
final window = await DesktopMultiWindow.createWindow(jsonEncode({
'window_type': type,
'app_state': _appStateToWindowState(appGlobalState).toJson(),
}));
window.setTitle("Log 分析器");
await window.center();
await window.show();
// sendAppStateBroadcast(appGlobalState);
}
static sendAppStateBroadcast(AppGlobalState appGlobalState) {
DesktopMultiWindow.invokeMethod(
0,
'app_state_broadcast',
_appStateToWindowState(appGlobalState).toJson(),
);
}
static MultiWindowAppState _appStateToWindowState(AppGlobalState appGlobalState) {
return MultiWindowAppState(
backgroundColor: colorToHexCode(appGlobalState.themeConf.backgroundColor),
menuColor: colorToHexCode(appGlobalState.themeConf.menuColor),
micaColor: colorToHexCode(appGlobalState.themeConf.micaColor),
languageCode: appGlobalState.appLocale?.languageCode,
countryCode: appGlobalState.appLocale?.countryCode,
);
}
static void runSubWindowApp(List<String> args) {
final argument = args[2].isEmpty ? const {} : jsonDecode(args[2]) as Map<String, dynamic>;
Widget? windowWidget;
switch (argument["window_type"]) {
case "log_analyze":
windowWidget = const ToolsLogAnalyzeDialogUI();
break;
default:
throw Exception('Unknown window type');
}
final windowAppState = MultiWindowAppState.fromJson(argument['app_state'] ?? {});
return runApp(ProviderScope(
child: FluentApp(
title: "StarCitizenToolBox",
restorationScopeId: "StarCitizenToolBox",
themeMode: ThemeMode.dark,
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
FluentLocalizations.delegate,
],
supportedLocales: const [Locale('en', 'US')],
home: windowWidget,
theme: FluentThemeData(
brightness: Brightness.dark,
fontFamily: "SourceHanSansCN-Regular",
navigationPaneTheme: NavigationPaneThemeData(
backgroundColor: HexColor(windowAppState.backgroundColor),
),
menuColor: HexColor(windowAppState.menuColor),
micaBackgroundColor: HexColor(windowAppState.micaColor),
buttonTheme: ButtonThemeData(
defaultButtonStyle: ButtonStyle(
shape: WidgetStateProperty.all(RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4),
side: BorderSide(color: Colors.white.withValues(alpha: .01)))),
))),
locale: windowAppState.languageCode != null
? Locale(windowAppState.languageCode!, windowAppState.countryCode)
: null,
debugShowCheckedModeBanner: false,
),
));
}
}

View File

@ -0,0 +1,255 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
part of 'multi_window_manager.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
T _$identity<T>(T value) => value;
final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');
MultiWindowAppState _$MultiWindowAppStateFromJson(Map<String, dynamic> json) {
return _MultiWindowAppState.fromJson(json);
}
/// @nodoc
mixin _$MultiWindowAppState {
String get backgroundColor => throw _privateConstructorUsedError;
String get menuColor => throw _privateConstructorUsedError;
String get micaColor => throw _privateConstructorUsedError;
String? get languageCode => throw _privateConstructorUsedError;
String? get countryCode => throw _privateConstructorUsedError;
/// Serializes this MultiWindowAppState to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
/// Create a copy of MultiWindowAppState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$MultiWindowAppStateCopyWith<MultiWindowAppState> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $MultiWindowAppStateCopyWith<$Res> {
factory $MultiWindowAppStateCopyWith(
MultiWindowAppState value, $Res Function(MultiWindowAppState) then) =
_$MultiWindowAppStateCopyWithImpl<$Res, MultiWindowAppState>;
@useResult
$Res call(
{String backgroundColor,
String menuColor,
String micaColor,
String? languageCode,
String? countryCode});
}
/// @nodoc
class _$MultiWindowAppStateCopyWithImpl<$Res, $Val extends MultiWindowAppState>
implements $MultiWindowAppStateCopyWith<$Res> {
_$MultiWindowAppStateCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of MultiWindowAppState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? backgroundColor = null,
Object? menuColor = null,
Object? micaColor = null,
Object? languageCode = freezed,
Object? countryCode = freezed,
}) {
return _then(_value.copyWith(
backgroundColor: null == backgroundColor
? _value.backgroundColor
: backgroundColor // ignore: cast_nullable_to_non_nullable
as String,
menuColor: null == menuColor
? _value.menuColor
: menuColor // ignore: cast_nullable_to_non_nullable
as String,
micaColor: null == micaColor
? _value.micaColor
: micaColor // ignore: cast_nullable_to_non_nullable
as String,
languageCode: freezed == languageCode
? _value.languageCode
: languageCode // ignore: cast_nullable_to_non_nullable
as String?,
countryCode: freezed == countryCode
? _value.countryCode
: countryCode // ignore: cast_nullable_to_non_nullable
as String?,
) as $Val);
}
}
/// @nodoc
abstract class _$$MultiWindowAppStateImplCopyWith<$Res>
implements $MultiWindowAppStateCopyWith<$Res> {
factory _$$MultiWindowAppStateImplCopyWith(_$MultiWindowAppStateImpl value,
$Res Function(_$MultiWindowAppStateImpl) then) =
__$$MultiWindowAppStateImplCopyWithImpl<$Res>;
@override
@useResult
$Res call(
{String backgroundColor,
String menuColor,
String micaColor,
String? languageCode,
String? countryCode});
}
/// @nodoc
class __$$MultiWindowAppStateImplCopyWithImpl<$Res>
extends _$MultiWindowAppStateCopyWithImpl<$Res, _$MultiWindowAppStateImpl>
implements _$$MultiWindowAppStateImplCopyWith<$Res> {
__$$MultiWindowAppStateImplCopyWithImpl(_$MultiWindowAppStateImpl _value,
$Res Function(_$MultiWindowAppStateImpl) _then)
: super(_value, _then);
/// Create a copy of MultiWindowAppState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? backgroundColor = null,
Object? menuColor = null,
Object? micaColor = null,
Object? languageCode = freezed,
Object? countryCode = freezed,
}) {
return _then(_$MultiWindowAppStateImpl(
backgroundColor: null == backgroundColor
? _value.backgroundColor
: backgroundColor // ignore: cast_nullable_to_non_nullable
as String,
menuColor: null == menuColor
? _value.menuColor
: menuColor // ignore: cast_nullable_to_non_nullable
as String,
micaColor: null == micaColor
? _value.micaColor
: micaColor // ignore: cast_nullable_to_non_nullable
as String,
languageCode: freezed == languageCode
? _value.languageCode
: languageCode // ignore: cast_nullable_to_non_nullable
as String?,
countryCode: freezed == countryCode
? _value.countryCode
: countryCode // ignore: cast_nullable_to_non_nullable
as String?,
));
}
}
/// @nodoc
@JsonSerializable()
class _$MultiWindowAppStateImpl implements _MultiWindowAppState {
const _$MultiWindowAppStateImpl(
{required this.backgroundColor,
required this.menuColor,
required this.micaColor,
this.languageCode,
this.countryCode});
factory _$MultiWindowAppStateImpl.fromJson(Map<String, dynamic> json) =>
_$$MultiWindowAppStateImplFromJson(json);
@override
final String backgroundColor;
@override
final String menuColor;
@override
final String micaColor;
@override
final String? languageCode;
@override
final String? countryCode;
@override
String toString() {
return 'MultiWindowAppState(backgroundColor: $backgroundColor, menuColor: $menuColor, micaColor: $micaColor, languageCode: $languageCode, countryCode: $countryCode)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$MultiWindowAppStateImpl &&
(identical(other.backgroundColor, backgroundColor) ||
other.backgroundColor == backgroundColor) &&
(identical(other.menuColor, menuColor) ||
other.menuColor == menuColor) &&
(identical(other.micaColor, micaColor) ||
other.micaColor == micaColor) &&
(identical(other.languageCode, languageCode) ||
other.languageCode == languageCode) &&
(identical(other.countryCode, countryCode) ||
other.countryCode == countryCode));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType, backgroundColor, menuColor,
micaColor, languageCode, countryCode);
/// Create a copy of MultiWindowAppState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$MultiWindowAppStateImplCopyWith<_$MultiWindowAppStateImpl> get copyWith =>
__$$MultiWindowAppStateImplCopyWithImpl<_$MultiWindowAppStateImpl>(
this, _$identity);
@override
Map<String, dynamic> toJson() {
return _$$MultiWindowAppStateImplToJson(
this,
);
}
}
abstract class _MultiWindowAppState implements MultiWindowAppState {
const factory _MultiWindowAppState(
{required final String backgroundColor,
required final String menuColor,
required final String micaColor,
final String? languageCode,
final String? countryCode}) = _$MultiWindowAppStateImpl;
factory _MultiWindowAppState.fromJson(Map<String, dynamic> json) =
_$MultiWindowAppStateImpl.fromJson;
@override
String get backgroundColor;
@override
String get menuColor;
@override
String get micaColor;
@override
String? get languageCode;
@override
String? get countryCode;
/// Create a copy of MultiWindowAppState
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$MultiWindowAppStateImplCopyWith<_$MultiWindowAppStateImpl> get copyWith =>
throw _privateConstructorUsedError;
}

View File

@ -0,0 +1,27 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'multi_window_manager.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
_$MultiWindowAppStateImpl _$$MultiWindowAppStateImplFromJson(
Map<String, dynamic> json) =>
_$MultiWindowAppStateImpl(
backgroundColor: json['backgroundColor'] as String,
menuColor: json['menuColor'] as String,
micaColor: json['micaColor'] as String,
languageCode: json['languageCode'] as String?,
countryCode: json['countryCode'] as String?,
);
Map<String, dynamic> _$$MultiWindowAppStateImplToJson(
_$MultiWindowAppStateImpl instance) =>
<String, dynamic>{
'backgroundColor': instance.backgroundColor,
'menuColor': instance.menuColor,
'micaColor': instance.micaColor,
'languageCode': instance.languageCode,
'countryCode': instance.countryCode,
};