2022-05-29 17:19:50 +08:00
|
|
|
import 'dart:convert';
|
2022-05-31 16:27:54 +08:00
|
|
|
import 'dart:math';
|
2022-05-29 17:19:50 +08:00
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2022-06-28 22:04:10 +08:00
|
|
|
import 'package:flutter_hbb/common.dart';
|
2022-08-03 15:31:19 +08:00
|
|
|
import 'package:flutter_hbb/consts.dart';
|
2022-05-29 17:19:50 +08:00
|
|
|
import 'package:flutter_hbb/desktop/pages/remote_page.dart';
|
2022-08-06 17:08:48 +08:00
|
|
|
import 'package:flutter_hbb/desktop/widgets/tabbar_widget.dart';
|
2022-05-29 17:19:50 +08:00
|
|
|
import 'package:flutter_hbb/utils/multi_window_manager.dart';
|
2022-06-28 22:04:10 +08:00
|
|
|
import 'package:get/get.dart';
|
2022-08-09 09:01:06 +08:00
|
|
|
import 'package:window_manager/window_manager.dart';
|
2022-06-28 22:04:10 +08:00
|
|
|
|
|
|
|
import '../../models/model.dart';
|
2022-05-29 17:19:50 +08:00
|
|
|
|
|
|
|
class ConnectionTabPage extends StatefulWidget {
|
|
|
|
final Map<String, dynamic> params;
|
|
|
|
|
|
|
|
const ConnectionTabPage({Key? key, required this.params}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<ConnectionTabPage> createState() => _ConnectionTabPageState(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ConnectionTabPageState extends State<ConnectionTabPage>
|
2022-08-05 10:27:06 +08:00
|
|
|
with TickerProviderStateMixin {
|
2022-05-29 17:19:50 +08:00
|
|
|
// refactor List<int> when using multi-tab
|
|
|
|
// this singleton is only for test
|
2022-08-06 17:08:48 +08:00
|
|
|
var connectionIds = RxList<String>.empty(growable: true);
|
2022-05-31 16:27:54 +08:00
|
|
|
var initialIndex = 0;
|
2022-08-05 10:27:06 +08:00
|
|
|
late Rx<TabController> tabController;
|
2022-08-06 17:08:48 +08:00
|
|
|
static final Rx<int> _selected = 0.obs;
|
2022-08-05 10:27:06 +08:00
|
|
|
|
|
|
|
var connectionMap = RxList<Widget>.empty(growable: true);
|
2022-05-29 17:19:50 +08:00
|
|
|
|
|
|
|
_ConnectionTabPageState(Map<String, dynamic> params) {
|
2022-05-31 16:27:54 +08:00
|
|
|
if (params['id'] != null) {
|
|
|
|
connectionIds.add(params['id']);
|
|
|
|
}
|
2022-05-29 17:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2022-08-05 10:27:06 +08:00
|
|
|
tabController =
|
|
|
|
TabController(length: connectionIds.length, vsync: this).obs;
|
2022-05-29 17:19:50 +08:00
|
|
|
rustDeskWinManager.setMethodHandler((call, fromWindowId) async {
|
|
|
|
print(
|
|
|
|
"call ${call.method} with args ${call.arguments} from window ${fromWindowId}");
|
|
|
|
// for simplify, just replace connectionId
|
|
|
|
if (call.method == "new_remote_desktop") {
|
2022-08-09 09:01:06 +08:00
|
|
|
window_on_top();
|
2022-08-05 10:27:06 +08:00
|
|
|
final args = jsonDecode(call.arguments);
|
|
|
|
final id = args['id'];
|
|
|
|
final indexOf = connectionIds.indexOf(id);
|
|
|
|
if (indexOf >= 0) {
|
|
|
|
initialIndex = indexOf;
|
|
|
|
tabController.value.animateTo(initialIndex, duration: Duration.zero);
|
|
|
|
} else {
|
|
|
|
connectionIds.add(id);
|
|
|
|
initialIndex = connectionIds.length - 1;
|
|
|
|
tabController.value = TabController(
|
|
|
|
length: connectionIds.length,
|
|
|
|
vsync: this,
|
|
|
|
initialIndex: initialIndex);
|
|
|
|
}
|
2022-08-06 17:08:48 +08:00
|
|
|
_selected.value = initialIndex;
|
2022-06-28 22:04:10 +08:00
|
|
|
} else if (call.method == "onDestroy") {
|
|
|
|
print("executing onDestroy hook, closing ${connectionIds}");
|
|
|
|
connectionIds.forEach((id) {
|
|
|
|
final tag = '${id}';
|
|
|
|
ffi(tag).close().then((_) {
|
|
|
|
Get.delete<FFI>(tag: tag);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
Get.back();
|
2022-05-29 17:19:50 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-05-31 16:27:54 +08:00
|
|
|
return Scaffold(
|
2022-08-05 10:27:06 +08:00
|
|
|
body: Column(
|
|
|
|
children: [
|
2022-08-06 17:08:48 +08:00
|
|
|
Obx(() => DesktopTabBar(
|
|
|
|
controller: tabController,
|
|
|
|
tabs: connectionIds.toList(),
|
|
|
|
onTabClose: onRemoveId,
|
|
|
|
tabIcon: Icons.desktop_windows_sharp,
|
|
|
|
selected: _selected,
|
|
|
|
)),
|
2022-08-05 10:27:06 +08:00
|
|
|
Expanded(
|
|
|
|
child: Obx(() => TabBarView(
|
|
|
|
controller: tabController.value,
|
|
|
|
children: connectionIds
|
|
|
|
.map((e) => RemotePage(
|
|
|
|
key: ValueKey(e),
|
|
|
|
id: e,
|
|
|
|
tabBarHeight: kDesktopRemoteTabBarHeight,
|
|
|
|
)) //RemotePage(key: ValueKey(e), id: e))
|
|
|
|
.toList()))),
|
|
|
|
],
|
2022-05-31 16:27:54 +08:00
|
|
|
),
|
2022-05-29 17:19:50 +08:00
|
|
|
);
|
|
|
|
}
|
2022-05-31 16:27:54 +08:00
|
|
|
|
|
|
|
void onRemoveId(String id) {
|
|
|
|
final indexOf = connectionIds.indexOf(id);
|
|
|
|
if (indexOf == -1) {
|
|
|
|
return;
|
|
|
|
}
|
2022-08-05 10:27:06 +08:00
|
|
|
connectionIds.removeAt(indexOf);
|
|
|
|
initialIndex = max(0, initialIndex - 1);
|
|
|
|
tabController.value = TabController(
|
|
|
|
length: connectionIds.length, vsync: this, initialIndex: initialIndex);
|
2022-08-09 09:01:06 +08:00
|
|
|
if (connectionIds.length == 0) {
|
|
|
|
windowManager.close();
|
|
|
|
}
|
2022-05-31 16:27:54 +08:00
|
|
|
}
|
2022-05-29 17:19:50 +08:00
|
|
|
}
|