mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-18 22:33:42 +00:00
feat: new ui (#1455)
This commit is contained in:
742 files changed
+59951
-5934
No files matched your search
@@ -0,0 +1,30 @@
|
||||
class AppWindowAppearance {
|
||||
const AppWindowAppearance({required this.themeMode, required this.themeColor, required this.locale});
|
||||
|
||||
const AppWindowAppearance.defaults() : themeMode = 'system', themeColor = 'green', locale = '';
|
||||
|
||||
factory AppWindowAppearance.fromJson(Map<String, dynamic> json) {
|
||||
return AppWindowAppearance(
|
||||
themeMode: (json['themeMode'] ?? 'system').toString(),
|
||||
themeColor: (json['themeColor'] ?? 'green').toString(),
|
||||
locale: (json['locale'] ?? '').toString(),
|
||||
);
|
||||
}
|
||||
|
||||
final String themeMode;
|
||||
final String themeColor;
|
||||
final String locale;
|
||||
|
||||
Map<String, dynamic> toJson() => {'themeMode': themeMode, 'themeColor': themeColor, 'locale': locale};
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is AppWindowAppearance &&
|
||||
other.themeMode == themeMode &&
|
||||
other.themeColor == themeColor &&
|
||||
other.locale == locale;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(themeMode, themeColor, locale);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
import 'package:flutter/foundation.dart' show TargetPlatform, defaultTargetPlatform;
|
||||
import 'package:flutter/material.dart' show Colors;
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:window_manager/window_manager.dart';
|
||||
|
||||
import '../../database/database.dart';
|
||||
import '../../l10n/l10n.dart';
|
||||
import 'app_window_chrome.dart';
|
||||
import 'app_window_payload.dart';
|
||||
|
||||
class AppWindowBootstrap {
|
||||
static Future<void> setupMainWindow({bool hidden = false}) async {
|
||||
if (!AppWindowChrome.isDesktopWindow) {
|
||||
return;
|
||||
}
|
||||
|
||||
await windowManager.ensureInitialized();
|
||||
|
||||
final windowState = Database.instance.getWindowState();
|
||||
final windowOptions = WindowOptions(
|
||||
size: Size(windowState?.width ?? 1024, windowState?.height ?? 768),
|
||||
center: true,
|
||||
skipTaskbar: false,
|
||||
backgroundColor: Colors.transparent,
|
||||
titleBarStyle: TitleBarStyle.hidden,
|
||||
);
|
||||
|
||||
windowManager.waitUntilReadyToShow(windowOptions, () async {
|
||||
if (AppWindowChrome.usesCustomChrome) {
|
||||
await windowManager.setAsFrameless();
|
||||
}
|
||||
await windowManager.setHasShadow(true);
|
||||
if (!hidden) {
|
||||
await windowManager.show();
|
||||
await windowManager.focus();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static Future<void> setupSubWindow(AppWindowType type, {String localeConfig = ''}) async {
|
||||
if (!AppWindowChrome.isDesktopWindow) {
|
||||
return;
|
||||
}
|
||||
|
||||
await windowManager.ensureInitialized();
|
||||
final title = subWindowTitle(type, appLocalizationsFor(localeConfig));
|
||||
|
||||
final options = switch (type) {
|
||||
AppWindowType.createTask => WindowOptions(
|
||||
size: Size(820, 620),
|
||||
center: true,
|
||||
skipTaskbar: false,
|
||||
backgroundColor: Colors.transparent,
|
||||
title: title,
|
||||
titleBarStyle: TitleBarStyle.hidden,
|
||||
),
|
||||
_ => WindowOptions(
|
||||
size: Size(900, 640),
|
||||
center: true,
|
||||
skipTaskbar: false,
|
||||
backgroundColor: Colors.transparent,
|
||||
title: title,
|
||||
titleBarStyle: TitleBarStyle.hidden,
|
||||
),
|
||||
};
|
||||
|
||||
windowManager.waitUntilReadyToShow(options, () async {
|
||||
if (AppWindowChrome.usesCustomChrome) {
|
||||
await windowManager.setAsFrameless();
|
||||
}
|
||||
await windowManager.setTitle(title);
|
||||
await windowManager.setHasShadow(true);
|
||||
await _showSubWindowInForeground();
|
||||
});
|
||||
}
|
||||
|
||||
static String subWindowTitle(AppWindowType type, AppLocalizations l10n) {
|
||||
return switch (type) {
|
||||
AppWindowType.createTask => '${l10n.create} - Gopeed',
|
||||
_ => l10n.appTitle,
|
||||
};
|
||||
}
|
||||
|
||||
static Future<void> _showSubWindowInForeground() async {
|
||||
if (defaultTargetPlatform != TargetPlatform.windows) {
|
||||
await windowManager.show();
|
||||
await windowManager.focus();
|
||||
return;
|
||||
}
|
||||
|
||||
// SetForegroundWindow may be rejected after the browser regains focus
|
||||
// while the child Flutter engine is starting. A brief topmost pulse keeps
|
||||
// the user-initiated create window visible without making it permanently
|
||||
// always-on-top.
|
||||
var pulsing = false;
|
||||
try {
|
||||
await windowManager.setAlwaysOnTop(true);
|
||||
pulsing = true;
|
||||
await windowManager.show();
|
||||
await windowManager.focus();
|
||||
await Future<void>.delayed(const Duration(milliseconds: 180));
|
||||
} finally {
|
||||
if (pulsing) {
|
||||
await windowManager.setAlwaysOnTop(false);
|
||||
}
|
||||
}
|
||||
await windowManager.focus();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
abstract final class AppWindowChrome {
|
||||
static bool get isDesktopWindow =>
|
||||
!kIsWeb &&
|
||||
(defaultTargetPlatform == TargetPlatform.windows ||
|
||||
defaultTargetPlatform == TargetPlatform.macOS ||
|
||||
defaultTargetPlatform == TargetPlatform.linux);
|
||||
|
||||
static bool get usesCustomChrome =>
|
||||
!kIsWeb && (defaultTargetPlatform == TargetPlatform.windows || defaultTargetPlatform == TargetPlatform.linux);
|
||||
|
||||
static bool get reservesHeaderInset => isDesktopWindow;
|
||||
|
||||
static bool get clipsRoundedCorners => !kIsWeb && defaultTargetPlatform == TargetPlatform.windows;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import 'package:flutter/material.dart' show Colors;
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:window_manager/window_manager.dart';
|
||||
|
||||
import '../../shared/widgets/window/desktop_window_header.dart';
|
||||
import 'app_window_chrome.dart';
|
||||
|
||||
const double kWindowCornerRadius = 12;
|
||||
|
||||
class AppWindowFrame extends StatelessWidget {
|
||||
const AppWindowFrame({super.key, required this.child});
|
||||
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget framedChild = child;
|
||||
|
||||
if (AppWindowChrome.usesCustomChrome) {
|
||||
framedChild = Stack(
|
||||
children: [
|
||||
Positioned.fill(child: child),
|
||||
const Positioned(top: 0, left: 0, right: 0, child: DesktopWindowHeader()),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
if (AppWindowChrome.clipsRoundedCorners) {
|
||||
framedChild = ClipRRect(borderRadius: BorderRadius.circular(kWindowCornerRadius), child: framedChild);
|
||||
}
|
||||
|
||||
if (AppWindowChrome.clipsRoundedCorners) {
|
||||
framedChild = DecoratedBox(
|
||||
decoration: const BoxDecoration(color: Colors.transparent),
|
||||
child: framedChild,
|
||||
);
|
||||
}
|
||||
|
||||
if (AppWindowChrome.usesCustomChrome) {
|
||||
framedChild = DragToResizeArea(resizeEdgeSize: 6, child: framedChild);
|
||||
}
|
||||
|
||||
return framedChild;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import 'package:desktop_multi_window/desktop_multi_window.dart';
|
||||
|
||||
import 'app_window_payload.dart';
|
||||
|
||||
class AppWindowLaunchContext {
|
||||
const AppWindowLaunchContext._({required this.isSubWindow, required this.payload, this.windowId});
|
||||
|
||||
factory AppWindowLaunchContext.main() {
|
||||
return AppWindowLaunchContext._(isSubWindow: false, payload: AppWindowPayload.main());
|
||||
}
|
||||
|
||||
factory AppWindowLaunchContext.subWindow({required String windowId, required AppWindowPayload payload}) {
|
||||
return AppWindowLaunchContext._(isSubWindow: true, windowId: windowId, payload: payload);
|
||||
}
|
||||
|
||||
factory AppWindowLaunchContext.fromArgs(List<String> args) {
|
||||
if (args.length >= 2 && args.first == 'multi_window') {
|
||||
final windowId = args[1];
|
||||
final payload = args.length >= 3 ? AppWindowPayload.fromRaw(args[2]) : AppWindowPayload.main();
|
||||
return AppWindowLaunchContext.subWindow(windowId: windowId, payload: payload);
|
||||
}
|
||||
|
||||
return AppWindowLaunchContext.main();
|
||||
}
|
||||
|
||||
final bool isSubWindow;
|
||||
final String? windowId;
|
||||
final AppWindowPayload payload;
|
||||
|
||||
WindowController? buildController() {
|
||||
if (!isSubWindow || windowId == null) {
|
||||
return null;
|
||||
}
|
||||
return WindowController.fromWindowId(windowId!);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:desktop_multi_window/desktop_multi_window.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../api/model/create_task.dart';
|
||||
import '../../util/log_util.dart';
|
||||
import '../capabilities/app_capabilities.dart';
|
||||
import 'app_window_payload.dart';
|
||||
import 'window_capability_transport.dart';
|
||||
|
||||
class AppWindowLauncher {
|
||||
static Future<bool> openCreateTaskWindow({CreateTask? createTask}) async {
|
||||
if (kIsWeb || !(Platform.isWindows || Platform.isMacOS || Platform.isLinux)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
await AppWindowCapabilityHost.instance.start(LocalAppCapabilities.instance.registry);
|
||||
final payload = AppWindowPayload.createTask(createTask: createTask?.toJson());
|
||||
// Every request deliberately creates a new child window. Create-task
|
||||
// windows are independent and are never reused.
|
||||
await WindowController.create(WindowConfiguration(arguments: payload.toRaw(), hiddenAtLaunch: true));
|
||||
// Keep the native window hidden until the child engine has applied its
|
||||
// final size, centered position, chrome, and foreground behavior in
|
||||
// AppWindowBootstrap.setupSubWindow.
|
||||
return true;
|
||||
} catch (error, stackTrace) {
|
||||
try {
|
||||
logger.w('create task window failed', error, stackTrace);
|
||||
} catch (_) {
|
||||
debugPrint('create task window failed: $error');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import 'dart:convert';
|
||||
|
||||
enum AppWindowType { main, createTask }
|
||||
|
||||
class AppWindowPayload {
|
||||
const AppWindowPayload({required this.type, this.data = const {}});
|
||||
|
||||
factory AppWindowPayload.main() => const AppWindowPayload(type: AppWindowType.main);
|
||||
|
||||
factory AppWindowPayload.createTask({Map<String, dynamic>? createTask}) => AppWindowPayload(
|
||||
type: AppWindowType.createTask,
|
||||
data: {
|
||||
...?createTask == null ? null : {'createTask': createTask},
|
||||
},
|
||||
);
|
||||
|
||||
factory AppWindowPayload.fromRaw(String raw) {
|
||||
if (raw.trim().isEmpty) {
|
||||
return AppWindowPayload.main();
|
||||
}
|
||||
|
||||
try {
|
||||
final decoded = jsonDecode(raw);
|
||||
if (decoded is Map<String, dynamic>) {
|
||||
final type = switch ((decoded['type'] ?? '').toString()) {
|
||||
'create_task' => AppWindowType.createTask,
|
||||
_ => AppWindowType.main,
|
||||
};
|
||||
return AppWindowPayload(type: type, data: decoded);
|
||||
}
|
||||
} catch (_) {
|
||||
// Ignore invalid payload and fallback to main.
|
||||
}
|
||||
|
||||
return AppWindowPayload.main();
|
||||
}
|
||||
|
||||
final AppWindowType type;
|
||||
final Map<String, dynamic> data;
|
||||
|
||||
Map<String, dynamic>? get createTask {
|
||||
final raw = data['createTask'];
|
||||
return raw is Map<String, dynamic> ? raw : null;
|
||||
}
|
||||
|
||||
String toRaw() {
|
||||
return jsonEncode({
|
||||
'type': switch (type) {
|
||||
AppWindowType.main => 'main',
|
||||
AppWindowType.createTask => 'create_task',
|
||||
},
|
||||
...data,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:desktop_multi_window/desktop_multi_window.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../capabilities/app_capabilities.dart';
|
||||
import '../capabilities/capability_rpc.dart';
|
||||
import 'app_window_appearance.dart';
|
||||
|
||||
abstract final class AppWindowRpcProtocol {
|
||||
static const channelName = 'gopeed.app.capabilities.v1';
|
||||
static const call = 'capability.call';
|
||||
static const bootstrap = 'window.bootstrap';
|
||||
static const subscribe = 'window.subscribe';
|
||||
static const unsubscribe = 'window.unsubscribe';
|
||||
static const event = 'window.event';
|
||||
static const appearanceChanged = 'appearance.changed';
|
||||
}
|
||||
|
||||
const _hostChannel = WindowMethodChannel(AppWindowRpcProtocol.channelName, mode: ChannelMode.unidirectional);
|
||||
|
||||
class WindowCapabilityInvoker implements CapabilityInvoker {
|
||||
const WindowCapabilityInvoker(this.codecs);
|
||||
|
||||
final RpcCodecRegistry codecs;
|
||||
|
||||
@override
|
||||
Future<R> invoke<P, R>(RpcMethod<P, R> method, P params) async {
|
||||
final raw = await _hostChannel.invokeMethod<dynamic>(AppWindowRpcProtocol.call, {
|
||||
'operation': method.name,
|
||||
'payload': codecs.encode(params),
|
||||
});
|
||||
final response = codecs.decode<Map<String, dynamic>>(raw);
|
||||
if (response['ok'] != true) {
|
||||
final error = codecs.decode<Map<String, dynamic>>(response['error']);
|
||||
throw CapabilityException(
|
||||
(error['code'] ?? 'remote_error').toString(),
|
||||
(error['message'] ?? 'Remote capability failed').toString(),
|
||||
error['details'],
|
||||
);
|
||||
}
|
||||
return codecs.decode<R>(response['data']);
|
||||
}
|
||||
}
|
||||
|
||||
class AppWindowCapabilityHost {
|
||||
AppWindowCapabilityHost._();
|
||||
|
||||
static final instance = AppWindowCapabilityHost._();
|
||||
|
||||
CapabilityRegistry? _registry;
|
||||
AppWindowAppearance _appearance = const AppWindowAppearance.defaults();
|
||||
final Map<String, WindowController> _subscribers = {};
|
||||
|
||||
bool get started => _registry != null;
|
||||
|
||||
Future<void> start(CapabilityRegistry registry) async {
|
||||
if (_registry != null) return;
|
||||
_registry = registry;
|
||||
await _hostChannel.setMethodCallHandler(_handleCall);
|
||||
}
|
||||
|
||||
Future<void> stop() async {
|
||||
_registry = null;
|
||||
_subscribers.clear();
|
||||
await _hostChannel.setMethodCallHandler(null);
|
||||
}
|
||||
|
||||
void updateAppearance(AppWindowAppearance appearance) {
|
||||
if (_appearance == appearance) return;
|
||||
_appearance = appearance;
|
||||
unawaited(_broadcastAppearance());
|
||||
}
|
||||
|
||||
Future<dynamic> _handleCall(MethodCall call) async {
|
||||
try {
|
||||
switch (call.method) {
|
||||
case AppWindowRpcProtocol.call:
|
||||
final args = _map(call.arguments);
|
||||
final registry = _registry;
|
||||
if (registry == null) {
|
||||
throw const CapabilityException('host_unavailable', 'Main window capabilities are not ready');
|
||||
}
|
||||
final data = await registry.invoke((args['operation'] ?? '').toString(), args['payload']);
|
||||
return {'ok': true, 'data': data};
|
||||
case AppWindowRpcProtocol.bootstrap:
|
||||
return _appearance.toJson();
|
||||
case AppWindowRpcProtocol.subscribe:
|
||||
final windowId = (_map(call.arguments)['windowId'] ?? '').toString();
|
||||
if (windowId.isEmpty) {
|
||||
throw const CapabilityException('invalid_window', 'Missing child window id');
|
||||
}
|
||||
final controller = WindowController.fromWindowId(windowId);
|
||||
_subscribers[windowId] = controller;
|
||||
scheduleMicrotask(() => _sendAppearance(windowId, controller));
|
||||
return null;
|
||||
case AppWindowRpcProtocol.unsubscribe:
|
||||
final windowId = (_map(call.arguments)['windowId'] ?? '').toString();
|
||||
_subscribers.remove(windowId);
|
||||
return null;
|
||||
default:
|
||||
throw CapabilityException('method_not_found', 'Window RPC method not found: ${call.method}');
|
||||
}
|
||||
} on CapabilityException catch (error) {
|
||||
if (call.method == AppWindowRpcProtocol.call) {
|
||||
return {
|
||||
'ok': false,
|
||||
'error': {'code': error.code, 'message': error.message, 'details': error.details},
|
||||
};
|
||||
}
|
||||
rethrow;
|
||||
} catch (error) {
|
||||
if (call.method == AppWindowRpcProtocol.call) {
|
||||
return {
|
||||
'ok': false,
|
||||
'error': {'code': 'internal_error', 'message': error.toString()},
|
||||
};
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _broadcastAppearance() async {
|
||||
final subscribers = Map<String, WindowController>.of(_subscribers);
|
||||
await Future.wait([for (final entry in subscribers.entries) _sendAppearance(entry.key, entry.value)]);
|
||||
}
|
||||
|
||||
Future<void> _sendAppearance(String windowId, WindowController controller) async {
|
||||
try {
|
||||
await controller.invokeMethod<void>(AppWindowRpcProtocol.event, {
|
||||
'name': AppWindowRpcProtocol.appearanceChanged,
|
||||
'data': _appearance.toJson(),
|
||||
});
|
||||
} catch (_) {
|
||||
_subscribers.remove(windowId);
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, dynamic> _map(Object? value) {
|
||||
if (value is! Map) return const {};
|
||||
return {for (final entry in value.entries) entry.key.toString(): entry.value};
|
||||
}
|
||||
}
|
||||
|
||||
class ChildWindowSession {
|
||||
ChildWindowSession._(this.controller, this.codecs)
|
||||
: capabilities = AppCapabilities(WindowCapabilityInvoker(codecs)),
|
||||
appearance = ValueNotifier(const AppWindowAppearance.defaults());
|
||||
|
||||
static Future<ChildWindowSession> connect(WindowController controller) async {
|
||||
final session = ChildWindowSession._(controller, createAppCapabilityCodecs());
|
||||
await session._initialize();
|
||||
return session;
|
||||
}
|
||||
|
||||
final WindowController controller;
|
||||
final RpcCodecRegistry codecs;
|
||||
final AppCapabilities capabilities;
|
||||
final ValueNotifier<AppWindowAppearance> appearance;
|
||||
|
||||
Future<void> _initialize() async {
|
||||
await controller.setWindowMethodHandler(_handleWindowCall);
|
||||
final initial = await _hostChannel.invokeMethod<dynamic>(AppWindowRpcProtocol.bootstrap);
|
||||
appearance.value = codecs.decode<AppWindowAppearance>(initial);
|
||||
await _hostChannel.invokeMethod<void>(AppWindowRpcProtocol.subscribe, {'windowId': controller.windowId});
|
||||
}
|
||||
|
||||
Future<dynamic> _handleWindowCall(MethodCall call) async {
|
||||
if (call.method != AppWindowRpcProtocol.event) return null;
|
||||
final args = codecs.decode<Map<String, dynamic>>(call.arguments);
|
||||
if (args['name'] == AppWindowRpcProtocol.appearanceChanged) {
|
||||
appearance.value = codecs.decode<AppWindowAppearance>(args['data']);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
unawaited(controller.setWindowMethodHandler(null));
|
||||
unawaited(_hostChannel.invokeMethod<void>(AppWindowRpcProtocol.unsubscribe, {'windowId': controller.windowId}));
|
||||
appearance.dispose();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user