From 4f4ac672287f34253b4e88a0c465bb24e98e3d19 Mon Sep 17 00:00:00 2001 From: csf Date: Wed, 24 Aug 2022 21:09:18 +0800 Subject: [PATCH] refactor DesktopTab impl for file_manager_tab_page.dart --- .../desktop/pages/connection_tab_page.dart | 21 ----- .../desktop/pages/file_manager_tab_page.dart | 81 ++++++++----------- .../lib/desktop/widgets/tabbar_widget.dart | 27 +++++-- 3 files changed, 55 insertions(+), 74 deletions(-) diff --git a/flutter/lib/desktop/pages/connection_tab_page.dart b/flutter/lib/desktop/pages/connection_tab_page.dart index 8f9d4f349..cf221c4d0 100644 --- a/flutter/lib/desktop/pages/connection_tab_page.dart +++ b/flutter/lib/desktop/pages/connection_tab_page.dart @@ -35,7 +35,6 @@ class _ConnectionTabPageState extends State { label: params['id'], selectedIcon: selectedIcon, unselectedIcon: unselectedIcon, - closable: false, page: RemotePage( id: params['id'], tabBarHeight: @@ -118,23 +117,3 @@ class _ConnectionTabPageState extends State { return widget.params["windowId"]; } } - -class AddButton extends StatelessWidget { - late final TarBarTheme theme; - - AddButton({ - Key? key, - required this.theme, - }) : super(key: key); - - @override - Widget build(BuildContext context) { - return ActionIcon( - message: 'New Connection', - icon: IconFont.add, - theme: theme, - onTap: () => - rustDeskWinManager.call(WindowType.Main, "main_window_on_top", ""), - is_close: false); - } -} diff --git a/flutter/lib/desktop/pages/file_manager_tab_page.dart b/flutter/lib/desktop/pages/file_manager_tab_page.dart index 78f0842ad..7ae8e36b3 100644 --- a/flutter/lib/desktop/pages/file_manager_tab_page.dart +++ b/flutter/lib/desktop/pages/file_manager_tab_page.dart @@ -20,25 +20,26 @@ class FileManagerTabPage extends StatefulWidget { } class _FileManagerTabPageState extends State { - // refactor List when using multi-tab - // this singleton is only for test - RxList tabs = List.empty(growable: true).obs; - final IconData selectedIcon = Icons.file_copy_sharp; - final IconData unselectedIcon = Icons.file_copy_outlined; + final tabController = Get.put(DesktopTabController()); + + static final IconData selectedIcon = Icons.file_copy_sharp; + static final IconData unselectedIcon = Icons.file_copy_outlined; _FileManagerTabPageState(Map params) { - if (params['id'] != null) { - tabs.add(TabInfo( - key: params['id'], - label: params['id'], - selectedIcon: selectedIcon, - unselectedIcon: unselectedIcon)); - } + tabController.state.value.tabs.add(TabInfo( + key: params['id'], + label: params['id'], + selectedIcon: selectedIcon, + unselectedIcon: unselectedIcon, + page: FileManagerPage(id: params['id']))); } @override void initState() { super.initState(); + + tabController.onRemove = (_, id) => onRemoveId(id); + rustDeskWinManager.setMethodHandler((call, fromWindowId) async { print( "call ${call.method} with args ${call.arguments} from window ${fromWindowId}"); @@ -47,18 +48,16 @@ class _FileManagerTabPageState extends State { final args = jsonDecode(call.arguments); final id = args['id']; window_on_top(windowId()); - DesktopTabBar.onAdd( - tabs, - TabInfo( - key: id, - label: id, - selectedIcon: selectedIcon, - unselectedIcon: unselectedIcon)); + tabController.add(TabInfo( + key: id, + label: id, + selectedIcon: selectedIcon, + unselectedIcon: unselectedIcon, + page: FileManagerPage(id: id))); } else if (call.method == "onDestroy") { - print( - "executing onDestroy hook, closing ${tabs.map((tab) => tab.label).toList()}"); - tabs.forEach((tab) { - final tag = 'ft_${tab.label}'; + tabController.state.value.tabs.forEach((tab) { + print("executing onDestroy hook, closing ${tab.label}}"); + final tag = tab.label; ffi(tag).close().then((_) { Get.delete(tag: tag); }); @@ -70,43 +69,29 @@ class _FileManagerTabPageState extends State { @override Widget build(BuildContext context) { + final theme = isDarkTheme() ? TarBarTheme.dark() : TarBarTheme.light(); return SubWindowDragToResizeArea( windowId: windowId(), child: Container( decoration: BoxDecoration( border: Border.all(color: MyTheme.color(context).border!)), child: Scaffold( - backgroundColor: MyTheme.color(context).bg, - body: Column( - children: [ - DesktopTabBar( - tabs: tabs, - onTabClose: onRemoveId, - dark: isDarkTheme(), - mainTab: false, - ), - Expanded( - child: Obx( - () => PageView( - controller: DesktopTabBar.controller.value, - children: tabs - .map((tab) => FileManagerPage( - key: ValueKey(tab.label), - id: tab - .label)) //RemotePage(key: ValueKey(e), id: e)) - .toList()), - ), - ) - ], - ), - ), + backgroundColor: MyTheme.color(context).bg, + body: DesktopTab( + controller: tabController, + theme: theme, + isMainWindow: false, + tail: AddButton( + theme: theme, + ).paddingOnly(left: 10), + )), ), ); } void onRemoveId(String id) { ffi("ft_$id").close(); - if (tabs.length == 0) { + if (tabController.state.value.tabs.length == 0) { WindowController.fromWindowId(windowId()).close(); } } diff --git a/flutter/lib/desktop/widgets/tabbar_widget.dart b/flutter/lib/desktop/widgets/tabbar_widget.dart index 48116b374..8aa8377c6 100644 --- a/flutter/lib/desktop/widgets/tabbar_widget.dart +++ b/flutter/lib/desktop/widgets/tabbar_widget.dart @@ -9,6 +9,8 @@ import 'package:get/get.dart'; import 'package:window_manager/window_manager.dart'; import 'package:scroll_pos/scroll_pos.dart'; +import '../../utils/multi_window_manager.dart'; + const double _kTabBarHeight = kDesktopRemoteTabBarHeight; const double _kIconSize = 18; const double _kDividerIndent = 10; @@ -40,11 +42,6 @@ class DesktopTabState { DesktopTabState() { scrollController.itemCount = tabs.length; - // TODO test - // WidgetsBinding.instance.addPostFrameCallback((_) { - // scrollController.scrollToItem(selected, - // center: true, animate: true); - // }); } } @@ -533,6 +530,26 @@ class ActionIcon extends StatelessWidget { } } +class AddButton extends StatelessWidget { + late final TarBarTheme theme; + + AddButton({ + Key? key, + required this.theme, + }) : super(key: key); + + @override + Widget build(BuildContext context) { + return ActionIcon( + message: 'New Connection', + icon: IconFont.add, + theme: theme, + onTap: () => + rustDeskWinManager.call(WindowType.Main, "main_window_on_top", ""), + is_close: false); + } +} + class TarBarTheme { final Color unSelectedtabIconColor; final Color selectedtabIconColor;