feat: add Flutter USB mirroring and single-instance desktop clients
This commit is contained in:
1 parent
3a7bb071f0
commit
a339465392
30 files changed
+1688
-104
No files matched your search
@@ -0,0 +1,188 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:material_ui/material_ui.dart';
|
||||
|
||||
import '../data/manager_repository.dart';
|
||||
|
||||
/// A native child window occupies this rectangle; controls stay outside it.
|
||||
class EmbeddedScreen extends StatefulWidget {
|
||||
final int pid;
|
||||
const EmbeddedScreen({super.key, required this.pid});
|
||||
static const channel = MethodChannel('ads/scrcpy_host');
|
||||
@override
|
||||
State<EmbeddedScreen> createState() => _EmbeddedScreenState();
|
||||
}
|
||||
|
||||
class _EmbeddedScreenState extends State<EmbeddedScreen> {
|
||||
final area = GlobalKey();
|
||||
Timer? timer;
|
||||
bool attached = false, updating = false;
|
||||
String? error;
|
||||
int attempts = 0;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
timer = Timer.periodic(const Duration(milliseconds: 150), (_) => update());
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => update());
|
||||
}
|
||||
|
||||
Future<void> update() async {
|
||||
if (!mounted || updating || error != null) {
|
||||
return;
|
||||
}
|
||||
updating = true;
|
||||
try {
|
||||
if (!attached) {
|
||||
attached =
|
||||
await EmbeddedScreen.channel.invokeMethod<bool>('attach', {
|
||||
'pid': widget.pid,
|
||||
}) ??
|
||||
false;
|
||||
if (!mounted) {
|
||||
await EmbeddedScreen.channel.invokeMethod<void>('hide', {
|
||||
'pid': widget.pid,
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!attached) {
|
||||
if (++attempts > 100) {
|
||||
throw StateError('等待 scrcpy 窗口超时,请检查运行日志');
|
||||
}
|
||||
return;
|
||||
}
|
||||
setState(() {});
|
||||
}
|
||||
final box = area.currentContext?.findRenderObject();
|
||||
if (box is! RenderBox || !box.hasSize) {
|
||||
return;
|
||||
}
|
||||
final point = box.localToGlobal(Offset.zero);
|
||||
final scale = MediaQuery.devicePixelRatioOf(context);
|
||||
await EmbeddedScreen.channel.invokeMethod<void>('layout', {
|
||||
'pid': widget.pid,
|
||||
'x': (point.dx * scale).round(),
|
||||
'y': (point.dy * scale).round(),
|
||||
'width': (box.size.width * scale).floor(),
|
||||
'height': (box.size.height * scale).floor(),
|
||||
});
|
||||
} catch (e) {
|
||||
await EmbeddedScreen.channel
|
||||
.invokeMethod<void>('hide', {'pid': widget.pid})
|
||||
.catchError((Object _) {});
|
||||
if (mounted) {
|
||||
setState(() => error = e.toString());
|
||||
}
|
||||
} finally {
|
||||
updating = false;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
timer?.cancel();
|
||||
unawaited(
|
||||
EmbeddedScreen.channel
|
||||
.invokeMethod<void>('hide', {'pid': widget.pid})
|
||||
.catchError((Object _) {}),
|
||||
);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => ColoredBox(
|
||||
key: area,
|
||||
color: const Color(0xFF111827),
|
||||
child: Center(
|
||||
child: error != null
|
||||
? Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Text(
|
||||
'投屏嵌入失败\n$error\n请停止投屏后重试。',
|
||||
style: const TextStyle(color: Colors.white70),
|
||||
),
|
||||
)
|
||||
: attached
|
||||
? const SizedBox.expand()
|
||||
: const Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
CircularProgressIndicator(),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
'正在连接 scrcpy 画面…',
|
||||
style: TextStyle(color: Colors.white70),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class ScreenPage extends StatelessWidget {
|
||||
final Session? session;
|
||||
final VoidCallback onBack;
|
||||
final Future<void> Function() onStop;
|
||||
final Future<void> Function() onToggle;
|
||||
final bool busy;
|
||||
const ScreenPage({
|
||||
super.key,
|
||||
required this.session,
|
||||
required this.onBack,
|
||||
required this.onStop,
|
||||
required this.onToggle,
|
||||
this.busy = false,
|
||||
});
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final s = session;
|
||||
final pid = s?.screen?.pid;
|
||||
return Column(
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
OutlinedButton.icon(
|
||||
onPressed: onBack,
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
label: const Text('返回设备'),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Text(
|
||||
s == null ? '设备连接已释放' : '${s.id} · ${s.endpoint}',
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
OutlinedButton.icon(
|
||||
onPressed: pid == null || busy ? null : onToggle,
|
||||
icon: Icon(
|
||||
s?.screenEmbedded == true
|
||||
? Icons.open_in_new
|
||||
: Icons.picture_in_picture_alt,
|
||||
),
|
||||
label: Text(s?.screenEmbedded == true ? '独立投屏' : '返回内嵌'),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
TextButton(
|
||||
onPressed: pid == null || busy ? null : onStop,
|
||||
child: const Text('停止投屏'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Expanded(
|
||||
child: pid == null
|
||||
? const Center(child: Text('投屏已停止,请返回设备重新打开。'))
|
||||
: s?.screenEmbedded == true
|
||||
? EmbeddedScreen(key: ValueKey(pid), pid: pid)
|
||||
: const Center(child: Text('投屏已在独立窗口运行,设备连接保持不变。')),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
'点击画面后可用鼠标和键盘控制 · 切换页面后投屏保留 · 暂不支持在内嵌画面使用全屏快捷键',
|
||||
style: TextStyle(fontSize: 11, color: Colors.blueGrey),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,14 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:material_ui/material_ui.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../data/windows_usb.dart';
|
||||
|
||||
import '../data/manager_repository.dart';
|
||||
import 'manager_model.dart';
|
||||
import 'agent_view.dart';
|
||||
import 'embedded_screen.dart';
|
||||
|
||||
class ManagerView extends StatefulWidget {
|
||||
final ManagerModel model;
|
||||
@@ -15,6 +20,7 @@ class ManagerView extends StatefulWidget {
|
||||
|
||||
class _ManagerViewState extends State<ManagerView> {
|
||||
int page = 0;
|
||||
String? screenDevice;
|
||||
String search = '';
|
||||
ManagerModel get m => widget.model;
|
||||
@override
|
||||
@@ -125,6 +131,7 @@ class _ManagerViewState extends State<ManagerView> {
|
||||
'连接配置',
|
||||
'运行日志',
|
||||
'Agent 管理',
|
||||
'内嵌投屏',
|
||||
][page],
|
||||
style: const TextStyle(
|
||||
fontSize: 24,
|
||||
@@ -163,6 +170,28 @@ class _ManagerViewState extends State<ManagerView> {
|
||||
2 => adb(),
|
||||
3 => SettingsView(model: m),
|
||||
5 => AgentView(model: m.agents),
|
||||
6 => ScreenPage(
|
||||
busy: m.pending.contains(screenDevice),
|
||||
onToggle: () =>
|
||||
m.deviceAction(screenDevice!, () async {
|
||||
final s = m.repository.sessions[screenDevice];
|
||||
if (s != null) {
|
||||
await m.repository.screen(
|
||||
s,
|
||||
embedded: !s.screenEmbedded,
|
||||
);
|
||||
}
|
||||
}),
|
||||
session: m.repository.sessions[screenDevice],
|
||||
onBack: () => setState(() => page = 0),
|
||||
onStop: () => m.deviceAction(screenDevice!, () async {
|
||||
final s = m.repository.sessions[screenDevice];
|
||||
if (s != null) {
|
||||
await m.repository.stop(s.screen);
|
||||
s.screen = null;
|
||||
}
|
||||
}),
|
||||
),
|
||||
_ => logs(),
|
||||
},
|
||||
),
|
||||
@@ -349,14 +378,19 @@ class _ManagerViewState extends State<ManagerView> {
|
||||
spacing: 8,
|
||||
children: [
|
||||
FilledButton.icon(
|
||||
onPressed: pending || !s.ready || s.screen != null
|
||||
onPressed: pending || !s.ready
|
||||
? null
|
||||
: () => m.deviceAction(
|
||||
d.id,
|
||||
() => m.repository.screen(s),
|
||||
),
|
||||
: () => m.deviceAction(d.id, () async {
|
||||
await m.repository.screen(s, embedded: true);
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
screenDevice = s.id;
|
||||
page = 6;
|
||||
});
|
||||
}
|
||||
}),
|
||||
icon: const Icon(Icons.open_in_new, size: 16),
|
||||
label: Text(s.screen == null ? '打开 scrcpy' : '投屏运行中'),
|
||||
label: Text(s.screen == null ? '内嵌投屏' : '查看投屏'),
|
||||
),
|
||||
OutlinedButton(
|
||||
onPressed: pending ? null : () => m.disconnect(d.id),
|
||||
@@ -372,7 +406,7 @@ class _ManagerViewState extends State<ManagerView> {
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
'网络 ADB ${s.endpoint} · ${s.route}',
|
||||
'${s.mode} ${s.endpoint} · ${s.route}',
|
||||
style: const TextStyle(fontSize: 12),
|
||||
),
|
||||
),
|
||||
@@ -494,14 +528,16 @@ class _ManagerViewState extends State<ManagerView> {
|
||||
|
||||
class SettingsView extends StatefulWidget {
|
||||
final ManagerModel model;
|
||||
const SettingsView({super.key, required this.model});
|
||||
final Future<bool> Function()? driverInstalled;
|
||||
const SettingsView({super.key, required this.model, this.driverInstalled});
|
||||
@override
|
||||
State<SettingsView> createState() => _SettingsViewState();
|
||||
}
|
||||
|
||||
class _SettingsViewState extends State<SettingsView> {
|
||||
late final Map<String, TextEditingController> fields;
|
||||
bool relayOnly = false;
|
||||
bool relayOnly = false, usbMode = false, installingUsb = false;
|
||||
String usbStatus = '尚未检测';
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -514,10 +550,93 @@ class _SettingsViewState extends State<SettingsView> {
|
||||
'client': s.client,
|
||||
'adb': s.adb,
|
||||
'scrcpy': s.scrcpy,
|
||||
'usbip': s.usbip,
|
||||
}.entries)
|
||||
entry.key: TextEditingController(text: entry.value),
|
||||
};
|
||||
relayOnly = s.relayOnly;
|
||||
usbMode = s.usbMode;
|
||||
}
|
||||
|
||||
Future<void> changeUsb(bool value, {bool detectOnly = false}) async {
|
||||
if (!value && !detectOnly) {
|
||||
setState(() => usbMode = false);
|
||||
return;
|
||||
}
|
||||
await widget.model.action(() async {
|
||||
final repo = widget.model.repository;
|
||||
final driver =
|
||||
await (widget.driverInstalled?.call() ??
|
||||
UsbDriver.installed(repo.run));
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
if (!driver) {
|
||||
setState(() {
|
||||
usbMode = false;
|
||||
usbStatus = '未安装 USBip 虚拟控制器';
|
||||
});
|
||||
final install = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('需要安装 USBip 驱动'),
|
||||
content: const Text(
|
||||
'USB 挂载需要已签名的 USBip 虚拟控制器。安装会请求管理员权限,可能短暂重启 USB 集线器并要求重启 Windows。安装后请重新检测;网络 ADB 仍可使用。',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('暂不安装'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('安装驱动'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (install == true && mounted) {
|
||||
await installUsb();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!await File(fields['usbip']!.text.trim()).exists()) {
|
||||
throw StateError('驱动已安装,但找不到 usbip.exe,请检查路径');
|
||||
}
|
||||
await repo.run(repo.findTool('ads-usb'), ['--version']);
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
usbStatus = '组件可用';
|
||||
if (!detectOnly) {
|
||||
usbMode = value;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> installUsb() async {
|
||||
if (installingUsb) {
|
||||
return;
|
||||
}
|
||||
setState(() => installingUsb = true);
|
||||
await widget.model.action(() async {
|
||||
final repo = widget.model.repository;
|
||||
final code = await UsbDriver.install(
|
||||
UsbDriver.installer(repo.root?.path),
|
||||
);
|
||||
if (mounted) {
|
||||
setState(
|
||||
() => usbStatus = code == 3010
|
||||
? '安装完成,需要重启 Windows 后检测'
|
||||
: '安装程序已结束,请重新检测组件',
|
||||
);
|
||||
}
|
||||
widget.model.notice = usbStatus;
|
||||
});
|
||||
if (mounted) {
|
||||
setState(() => installingUsb = false);
|
||||
}
|
||||
}
|
||||
|
||||
void apply() {
|
||||
@@ -529,6 +648,8 @@ class _SettingsViewState extends State<SettingsView> {
|
||||
s.adb = fields['adb']!.text.trim();
|
||||
s.scrcpy = fields['scrcpy']!.text.trim();
|
||||
s.relayOnly = relayOnly;
|
||||
s.usbMode = usbMode;
|
||||
s.usbip = fields['usbip']!.text.trim();
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -565,6 +686,7 @@ class _SettingsViewState extends State<SettingsView> {
|
||||
('client', 'ads-client.exe 路径'),
|
||||
('adb', 'adb.exe 路径'),
|
||||
('scrcpy', 'scrcpy.exe 路径(需保留完整发行目录)'),
|
||||
('usbip', 'usbip.exe 路径'),
|
||||
])
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 18),
|
||||
@@ -573,6 +695,29 @@ class _SettingsViewState extends State<SettingsView> {
|
||||
decoration: InputDecoration(labelText: label),
|
||||
),
|
||||
),
|
||||
SwitchListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
value: usbMode,
|
||||
title: const Text('挂载为 USB 设备 · Windows'),
|
||||
subtitle: Text('仅模拟 ADB;修改在下次连接生效。$usbStatus'),
|
||||
onChanged: installingUsb ? null : changeUsb,
|
||||
),
|
||||
Wrap(
|
||||
spacing: 12,
|
||||
children: [
|
||||
OutlinedButton(
|
||||
onPressed: installingUsb
|
||||
? null
|
||||
: () => changeUsb(usbMode, detectOnly: true),
|
||||
child: const Text('检测 USB 组件'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: installingUsb ? null : installUsb,
|
||||
child: Text(installingUsb ? '等待驱动安装完成…' : '安装 USBip 驱动'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
SwitchListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
value: relayOnly,
|
||||
|
||||
Reference in new issue
Block a user