file_manager_tab_page.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import 'dart:convert';
  2. import 'package:desktop_multi_window/desktop_multi_window.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_hbb/common.dart';
  5. import 'package:flutter_hbb/consts.dart';
  6. import 'package:flutter_hbb/models/state_model.dart';
  7. import 'package:flutter_hbb/desktop/pages/file_manager_page.dart';
  8. import 'package:flutter_hbb/desktop/widgets/tabbar_widget.dart';
  9. import 'package:flutter_hbb/utils/multi_window_manager.dart';
  10. import 'package:get/get.dart';
  11. import '../../models/platform_model.dart';
  12. /// File Transfer for multi tabs
  13. class FileManagerTabPage extends StatefulWidget {
  14. final Map<String, dynamic> params;
  15. const FileManagerTabPage({Key? key, required this.params}) : super(key: key);
  16. @override
  17. State<FileManagerTabPage> createState() => _FileManagerTabPageState(params);
  18. }
  19. class _FileManagerTabPageState extends State<FileManagerTabPage> {
  20. DesktopTabController get tabController => Get.find<DesktopTabController>();
  21. static const IconData selectedIcon = Icons.file_copy_sharp;
  22. static const IconData unselectedIcon = Icons.file_copy_outlined;
  23. _FileManagerTabPageState(Map<String, dynamic> params) {
  24. Get.put(DesktopTabController(tabType: DesktopTabType.fileTransfer));
  25. tabController.onSelected = (id) {
  26. WindowController.fromWindowId(windowId())
  27. .setTitle(getWindowNameWithId(id));
  28. };
  29. tabController.onRemoved = (_, id) => onRemoveId(id);
  30. tabController.add(TabInfo(
  31. key: params['id'],
  32. label: params['id'],
  33. selectedIcon: selectedIcon,
  34. unselectedIcon: unselectedIcon,
  35. onTabCloseButton: () => tabController.closeBy(params['id']),
  36. page: FileManagerPage(
  37. key: ValueKey(params['id']),
  38. id: params['id'],
  39. password: params['password'],
  40. isSharedPassword: params['isSharedPassword'],
  41. tabController: tabController,
  42. forceRelay: params['forceRelay'],
  43. connToken: params['connToken'],
  44. )));
  45. }
  46. @override
  47. void initState() {
  48. super.initState();
  49. rustDeskWinManager.setMethodHandler((call, fromWindowId) async {
  50. debugPrint(
  51. "[FileTransfer] call ${call.method} with args ${call.arguments} from window $fromWindowId to ${windowId()}");
  52. // for simplify, just replace connectionId
  53. if (call.method == kWindowEventNewFileTransfer) {
  54. final args = jsonDecode(call.arguments);
  55. final id = args['id'];
  56. windowOnTop(windowId());
  57. tabController.add(TabInfo(
  58. key: id,
  59. label: id,
  60. selectedIcon: selectedIcon,
  61. unselectedIcon: unselectedIcon,
  62. onTabCloseButton: () => tabController.closeBy(id),
  63. page: FileManagerPage(
  64. key: ValueKey(id),
  65. id: id,
  66. password: args['password'],
  67. isSharedPassword: args['isSharedPassword'],
  68. tabController: tabController,
  69. forceRelay: args['forceRelay'],
  70. connToken: args['connToken'],
  71. )));
  72. } else if (call.method == "onDestroy") {
  73. tabController.clear();
  74. } else if (call.method == kWindowActionRebuild) {
  75. reloadCurrentWindow();
  76. }
  77. });
  78. Future.delayed(Duration.zero, () {
  79. restoreWindowPosition(WindowType.FileTransfer, windowId: windowId());
  80. });
  81. }
  82. @override
  83. Widget build(BuildContext context) {
  84. final child = Scaffold(
  85. backgroundColor: Theme.of(context).cardColor,
  86. body: DesktopTab(
  87. controller: tabController,
  88. onWindowCloseButton: handleWindowCloseButton,
  89. tail: const AddButton(),
  90. selectedBorderColor: MyTheme.accent,
  91. labelGetter: DesktopTab.tablabelGetter,
  92. ));
  93. final tabWidget = isLinux
  94. ? buildVirtualWindowFrame(context, child)
  95. : Container(
  96. decoration: BoxDecoration(
  97. border: Border.all(color: MyTheme.color(context).border!)),
  98. child: child,
  99. );
  100. return isMacOS || kUseCompatibleUiMode
  101. ? tabWidget
  102. : SubWindowDragToResizeArea(
  103. child: tabWidget,
  104. resizeEdgeSize: stateGlobal.resizeEdgeSize.value,
  105. enableResizeEdges: subWindowManagerEnableResizeEdges,
  106. windowId: stateGlobal.windowId,
  107. );
  108. }
  109. void onRemoveId(String id) {
  110. if (tabController.state.value.tabs.isEmpty) {
  111. WindowController.fromWindowId(windowId()).close();
  112. }
  113. }
  114. int windowId() {
  115. return widget.params["windowId"];
  116. }
  117. Future<bool> handleWindowCloseButton() async {
  118. final connLength = tabController.state.value.tabs.length;
  119. if (connLength <= 1) {
  120. tabController.clear();
  121. return true;
  122. } else {
  123. final bool res;
  124. if (!option2bool(kOptionEnableConfirmClosingTabs,
  125. bind.mainGetLocalOption(key: kOptionEnableConfirmClosingTabs))) {
  126. res = true;
  127. } else {
  128. res = await closeConfirmDialog();
  129. }
  130. if (res) {
  131. tabController.clear();
  132. }
  133. return res;
  134. }
  135. }
  136. }