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),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user