2022-08-30 20:48:03 +08:00
|
|
|
import 'dart:io';
|
2022-08-30 16:50:25 +08:00
|
|
|
import 'dart:async';
|
2022-08-06 17:08:48 +08:00
|
|
|
import 'dart:math';
|
|
|
|
|
2022-08-18 11:07:53 +08:00
|
|
|
import 'package:desktop_multi_window/desktop_multi_window.dart';
|
2022-09-04 11:03:16 +08:00
|
|
|
import 'package:flutter/material.dart' hide TabBarTheme;
|
2022-08-06 17:08:48 +08:00
|
|
|
import 'package:flutter_hbb/common.dart';
|
|
|
|
import 'package:flutter_hbb/consts.dart';
|
2022-08-18 11:07:53 +08:00
|
|
|
import 'package:flutter_hbb/main.dart';
|
2022-08-30 16:50:25 +08:00
|
|
|
import 'package:flutter_hbb/models/platform_model.dart';
|
2022-08-06 17:08:48 +08:00
|
|
|
import 'package:get/get.dart';
|
2022-08-18 10:54:09 +08:00
|
|
|
import 'package:scroll_pos/scroll_pos.dart';
|
2022-08-30 16:45:47 +08:00
|
|
|
import 'package:window_manager/window_manager.dart';
|
2022-08-06 17:08:48 +08:00
|
|
|
|
2022-08-24 21:09:18 +08:00
|
|
|
import '../../utils/multi_window_manager.dart';
|
|
|
|
|
2022-08-06 17:08:48 +08:00
|
|
|
const double _kTabBarHeight = kDesktopRemoteTabBarHeight;
|
|
|
|
const double _kIconSize = 18;
|
|
|
|
const double _kDividerIndent = 10;
|
2022-08-20 19:57:16 +08:00
|
|
|
const double _kActionIconSize = 12;
|
2022-08-06 17:08:48 +08:00
|
|
|
|
2022-08-11 16:03:04 +08:00
|
|
|
class TabInfo {
|
2022-08-24 20:12:04 +08:00
|
|
|
final String key;
|
|
|
|
final String label;
|
|
|
|
final IconData? selectedIcon;
|
|
|
|
final IconData? unselectedIcon;
|
|
|
|
final bool closable;
|
|
|
|
final Widget page;
|
2022-08-11 16:03:04 +08:00
|
|
|
|
2022-08-18 10:54:09 +08:00
|
|
|
TabInfo(
|
2022-08-22 20:18:31 +08:00
|
|
|
{required this.key,
|
|
|
|
required this.label,
|
|
|
|
this.selectedIcon,
|
|
|
|
this.unselectedIcon,
|
2022-08-24 20:12:04 +08:00
|
|
|
this.closable = true,
|
|
|
|
required this.page});
|
2022-08-11 16:03:04 +08:00
|
|
|
}
|
|
|
|
|
2022-08-30 16:50:25 +08:00
|
|
|
enum DesktopTabType {
|
|
|
|
main,
|
|
|
|
cm,
|
|
|
|
remoteScreen,
|
|
|
|
fileTransfer,
|
|
|
|
portForward,
|
|
|
|
rdp,
|
|
|
|
}
|
|
|
|
|
2022-08-24 20:17:51 +08:00
|
|
|
class DesktopTabState {
|
2022-08-24 20:12:04 +08:00
|
|
|
final List<TabInfo> tabs = [];
|
2022-08-18 10:54:09 +08:00
|
|
|
final ScrollPosController scrollController =
|
|
|
|
ScrollPosController(itemCount: 0);
|
2022-08-24 20:12:04 +08:00
|
|
|
final PageController pageController = PageController();
|
|
|
|
int selected = 0;
|
2022-08-06 17:08:48 +08:00
|
|
|
|
2022-08-24 20:17:51 +08:00
|
|
|
DesktopTabState() {
|
2022-08-18 10:54:09 +08:00
|
|
|
scrollController.itemCount = tabs.length;
|
|
|
|
}
|
2022-08-24 20:12:04 +08:00
|
|
|
}
|
2022-08-06 17:08:48 +08:00
|
|
|
|
2022-08-24 20:17:51 +08:00
|
|
|
class DesktopTabController {
|
|
|
|
final state = DesktopTabState().obs;
|
2022-09-01 21:18:53 +08:00
|
|
|
final DesktopTabType tabType;
|
2022-08-11 18:08:35 +08:00
|
|
|
|
2022-08-24 20:56:42 +08:00
|
|
|
/// index, key
|
|
|
|
Function(int, String)? onRemove;
|
2022-08-24 21:52:21 +08:00
|
|
|
Function(int)? onSelected;
|
|
|
|
|
2022-09-01 21:18:53 +08:00
|
|
|
DesktopTabController({required this.tabType});
|
|
|
|
|
|
|
|
void add(TabInfo tab, {bool authorized = false}) {
|
2022-08-23 15:25:18 +08:00
|
|
|
if (!isDesktop) return;
|
2022-08-24 20:12:04 +08:00
|
|
|
final index = state.value.tabs.indexWhere((e) => e.key == tab.key);
|
|
|
|
int toIndex;
|
2022-08-11 18:08:35 +08:00
|
|
|
if (index >= 0) {
|
2022-08-24 20:12:04 +08:00
|
|
|
toIndex = index;
|
2022-08-11 18:08:35 +08:00
|
|
|
} else {
|
2022-08-24 20:12:04 +08:00
|
|
|
state.update((val) {
|
|
|
|
val!.tabs.add(tab);
|
|
|
|
});
|
2022-08-30 14:43:57 +08:00
|
|
|
state.value.scrollController.itemCount = state.value.tabs.length;
|
2022-08-24 20:12:04 +08:00
|
|
|
toIndex = state.value.tabs.length - 1;
|
|
|
|
assert(toIndex >= 0);
|
2022-08-11 18:08:35 +08:00
|
|
|
}
|
2022-09-01 21:18:53 +08:00
|
|
|
if (tabType == DesktopTabType.cm) {
|
|
|
|
Future.delayed(Duration.zero, () async {
|
|
|
|
window_on_top(null);
|
|
|
|
});
|
|
|
|
if (authorized) {
|
|
|
|
Future.delayed(const Duration(seconds: 3), () {
|
|
|
|
windowManager.minimize();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-08-22 20:18:31 +08:00
|
|
|
try {
|
2022-08-24 20:12:04 +08:00
|
|
|
jumpTo(toIndex);
|
2022-08-22 20:18:31 +08:00
|
|
|
} catch (e) {
|
|
|
|
// call before binding controller will throw
|
2022-08-24 20:12:04 +08:00
|
|
|
debugPrint("Failed to jumpTo: $e");
|
2022-08-22 20:18:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 20:12:04 +08:00
|
|
|
void remove(int index) {
|
2022-08-23 15:25:18 +08:00
|
|
|
if (!isDesktop) return;
|
2022-08-24 20:12:04 +08:00
|
|
|
final len = state.value.tabs.length;
|
2022-08-24 20:56:42 +08:00
|
|
|
if (index < 0 || index > len - 1) return;
|
|
|
|
final key = state.value.tabs[index].key;
|
2022-08-24 20:12:04 +08:00
|
|
|
final currentSelected = state.value.selected;
|
|
|
|
int toIndex = 0;
|
|
|
|
if (index == len - 1) {
|
|
|
|
toIndex = max(0, currentSelected - 1);
|
|
|
|
} else if (index < len - 1 && index < currentSelected) {
|
|
|
|
toIndex = max(0, currentSelected - 1);
|
2022-08-22 20:18:31 +08:00
|
|
|
}
|
2022-08-24 20:12:04 +08:00
|
|
|
state.value.tabs.removeAt(index);
|
|
|
|
state.value.scrollController.itemCount = state.value.tabs.length;
|
|
|
|
jumpTo(toIndex);
|
2022-08-24 20:56:42 +08:00
|
|
|
onRemove?.call(index, key);
|
2022-08-22 20:18:31 +08:00
|
|
|
}
|
|
|
|
|
2022-08-24 20:12:04 +08:00
|
|
|
void jumpTo(int index) {
|
2022-09-01 21:18:53 +08:00
|
|
|
if (!isDesktop || index < 0) return;
|
2022-08-24 20:12:04 +08:00
|
|
|
state.update((val) {
|
|
|
|
val!.selected = index;
|
2022-08-30 14:43:57 +08:00
|
|
|
Future.delayed(Duration.zero, (() {
|
|
|
|
if (val.pageController.hasClients) {
|
|
|
|
val.pageController.jumpToPage(index);
|
|
|
|
}
|
|
|
|
if (val.scrollController.hasClients &&
|
|
|
|
val.scrollController.canScroll &&
|
2022-09-01 21:18:53 +08:00
|
|
|
val.scrollController.itemCount > index) {
|
2022-08-30 14:43:57 +08:00
|
|
|
val.scrollController.scrollToItem(index, center: true, animate: true);
|
|
|
|
}
|
|
|
|
}));
|
2022-08-24 20:12:04 +08:00
|
|
|
});
|
2022-09-01 21:18:53 +08:00
|
|
|
if (state.value.tabs.length > index) {
|
|
|
|
onSelected?.call(index);
|
|
|
|
}
|
2022-08-11 18:08:35 +08:00
|
|
|
}
|
2022-08-24 20:56:42 +08:00
|
|
|
|
|
|
|
void closeBy(String? key) {
|
|
|
|
if (!isDesktop) return;
|
|
|
|
assert(onRemove != null);
|
|
|
|
if (key == null) {
|
|
|
|
if (state.value.selected < state.value.tabs.length) {
|
|
|
|
remove(state.value.selected);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
state.value.tabs.indexWhere((tab) => tab.key == key);
|
|
|
|
remove(state.value.selected);
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 20:48:03 +08:00
|
|
|
|
|
|
|
void clear() {
|
|
|
|
state.value.tabs.clear();
|
|
|
|
state.refresh();
|
|
|
|
}
|
2022-08-24 20:12:04 +08:00
|
|
|
}
|
2022-08-22 20:18:31 +08:00
|
|
|
|
2022-08-29 18:48:12 +08:00
|
|
|
class TabThemeConf {
|
|
|
|
double iconSize;
|
2022-09-04 11:03:16 +08:00
|
|
|
TabThemeConf({required this.iconSize});
|
2022-08-29 18:48:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef TabBuilder = Widget Function(
|
|
|
|
String key, Widget icon, Widget label, TabThemeConf themeConf);
|
|
|
|
typedef LabelGetter = Rx<String> Function(String key);
|
|
|
|
|
2022-08-24 20:12:04 +08:00
|
|
|
class DesktopTab extends StatelessWidget {
|
|
|
|
final Function(String)? onTabClose;
|
2022-08-24 21:20:50 +08:00
|
|
|
final bool showTabBar;
|
2022-08-24 20:12:04 +08:00
|
|
|
final bool showLogo;
|
|
|
|
final bool showTitle;
|
|
|
|
final bool showMinimize;
|
|
|
|
final bool showMaximize;
|
|
|
|
final bool showClose;
|
|
|
|
final Widget Function(Widget pageView)? pageViewBuilder;
|
|
|
|
final Widget? tail;
|
2022-08-30 20:48:03 +08:00
|
|
|
final VoidCallback? onClose;
|
2022-08-29 18:48:12 +08:00
|
|
|
final TabBuilder? tabBuilder;
|
|
|
|
final LabelGetter? labelGetter;
|
2022-08-24 20:12:04 +08:00
|
|
|
|
2022-08-24 20:17:51 +08:00
|
|
|
final DesktopTabController controller;
|
2022-08-30 20:48:03 +08:00
|
|
|
Rx<DesktopTabState> get state => controller.state;
|
2022-09-01 21:18:53 +08:00
|
|
|
late final DesktopTabType tabType;
|
|
|
|
late final bool isMainWindow;
|
2022-08-24 20:12:04 +08:00
|
|
|
|
2022-09-01 21:18:53 +08:00
|
|
|
DesktopTab({
|
|
|
|
Key? key,
|
2022-08-29 18:48:12 +08:00
|
|
|
required this.controller,
|
|
|
|
this.onTabClose,
|
|
|
|
this.showTabBar = true,
|
|
|
|
this.showLogo = true,
|
|
|
|
this.showTitle = true,
|
|
|
|
this.showMinimize = true,
|
|
|
|
this.showMaximize = true,
|
|
|
|
this.showClose = true,
|
|
|
|
this.pageViewBuilder,
|
|
|
|
this.tail,
|
|
|
|
this.onClose,
|
|
|
|
this.tabBuilder,
|
|
|
|
this.labelGetter,
|
2022-09-01 21:18:53 +08:00
|
|
|
}) : super(key: key) {
|
|
|
|
tabType = controller.tabType;
|
|
|
|
isMainWindow =
|
|
|
|
tabType == DesktopTabType.main || tabType == DesktopTabType.cm;
|
|
|
|
}
|
2022-08-24 20:12:04 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Column(children: [
|
2022-08-24 21:20:50 +08:00
|
|
|
Offstage(
|
|
|
|
offstage: !showTabBar,
|
2022-09-04 11:03:16 +08:00
|
|
|
child: SizedBox(
|
2022-08-24 21:20:50 +08:00
|
|
|
height: _kTabBarHeight,
|
|
|
|
child: Column(
|
|
|
|
children: [
|
2022-09-04 11:03:16 +08:00
|
|
|
SizedBox(
|
2022-08-24 21:20:50 +08:00
|
|
|
height: _kTabBarHeight - 1,
|
|
|
|
child: _buildBar(),
|
|
|
|
),
|
2022-09-04 11:03:16 +08:00
|
|
|
const Divider(
|
2022-08-24 21:20:50 +08:00
|
|
|
height: 1,
|
|
|
|
thickness: 1,
|
|
|
|
),
|
|
|
|
],
|
2022-08-24 20:12:04 +08:00
|
|
|
),
|
2022-08-24 21:20:50 +08:00
|
|
|
)),
|
2022-08-24 20:12:04 +08:00
|
|
|
Expanded(
|
|
|
|
child: pageViewBuilder != null
|
|
|
|
? pageViewBuilder!(_buildPageView())
|
|
|
|
: _buildPageView())
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-08-30 16:50:25 +08:00
|
|
|
Widget _buildBlock({required Widget child}) {
|
|
|
|
if (tabType != DesktopTabType.main) {
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
var block = false.obs;
|
|
|
|
return Obx(() => MouseRegion(
|
|
|
|
onEnter: (_) async {
|
|
|
|
if (!option2bool(
|
|
|
|
'allow-remote-config-modification',
|
|
|
|
await bind.mainGetOption(
|
|
|
|
key: 'allow-remote-config-modification'))) {
|
|
|
|
var time0 = DateTime.now().millisecondsSinceEpoch;
|
|
|
|
await bind.mainCheckMouseTime();
|
|
|
|
Timer(const Duration(milliseconds: 120), () async {
|
|
|
|
var d = time0 - await bind.mainGetMouseTime();
|
|
|
|
if (d < 120) {
|
|
|
|
block.value = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onExit: (_) => block.value = false,
|
|
|
|
child: Stack(
|
|
|
|
children: [
|
|
|
|
child,
|
|
|
|
Offstage(
|
|
|
|
offstage: !block.value,
|
|
|
|
child: Container(
|
|
|
|
color: Colors.black.withOpacity(0.5),
|
|
|
|
)),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-08-24 20:12:04 +08:00
|
|
|
Widget _buildPageView() {
|
2022-08-30 16:50:25 +08:00
|
|
|
return _buildBlock(
|
|
|
|
child: Obx(() => PageView(
|
|
|
|
controller: state.value.pageController,
|
|
|
|
children: state.value.tabs
|
|
|
|
.map((tab) => tab.page)
|
|
|
|
.toList(growable: false))));
|
2022-08-24 20:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildBar() {
|
|
|
|
return Row(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Row(
|
|
|
|
children: [
|
2022-08-30 20:48:03 +08:00
|
|
|
Offstage(
|
2022-08-29 18:48:12 +08:00
|
|
|
offstage: !Platform.isMacOS,
|
|
|
|
child: const SizedBox(
|
|
|
|
width: 78,
|
|
|
|
)),
|
2022-08-24 20:12:04 +08:00
|
|
|
Row(children: [
|
|
|
|
Offstage(
|
|
|
|
offstage: !showLogo,
|
|
|
|
child: Image.asset(
|
|
|
|
'assets/logo.ico',
|
|
|
|
width: 20,
|
|
|
|
height: 20,
|
|
|
|
)),
|
|
|
|
Offstage(
|
|
|
|
offstage: !showTitle,
|
2022-09-04 11:03:16 +08:00
|
|
|
child: const Text(
|
2022-08-24 20:12:04 +08:00
|
|
|
"RustDesk",
|
|
|
|
style: TextStyle(fontSize: 13),
|
|
|
|
).marginOnly(left: 2))
|
|
|
|
]).marginOnly(
|
|
|
|
left: 5,
|
|
|
|
right: 10,
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: GestureDetector(
|
|
|
|
onPanStart: (_) {
|
|
|
|
if (isMainWindow) {
|
|
|
|
windowManager.startDragging();
|
|
|
|
} else {
|
|
|
|
WindowController.fromWindowId(windowId!)
|
|
|
|
.startDragging();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
child: _ListView(
|
|
|
|
controller: controller,
|
|
|
|
onTabClose: onTabClose,
|
2022-08-29 18:48:12 +08:00
|
|
|
tabBuilder: tabBuilder,
|
|
|
|
labelGetter: labelGetter,
|
2022-08-24 20:12:04 +08:00
|
|
|
)),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Offstage(offstage: tail == null, child: tail),
|
|
|
|
WindowActionPanel(
|
|
|
|
mainTab: isMainWindow,
|
2022-09-01 12:07:05 +08:00
|
|
|
tabType: tabType,
|
|
|
|
state: state,
|
2022-08-24 20:12:04 +08:00
|
|
|
showMinimize: showMinimize,
|
|
|
|
showMaximize: showMaximize,
|
|
|
|
showClose: showClose,
|
2022-08-30 20:48:03 +08:00
|
|
|
onClose: onClose,
|
2022-08-24 20:12:04 +08:00
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
2022-08-22 20:18:31 +08:00
|
|
|
}
|
2022-08-06 17:08:48 +08:00
|
|
|
}
|
|
|
|
|
2022-08-18 11:07:53 +08:00
|
|
|
class WindowActionPanel extends StatelessWidget {
|
|
|
|
final bool mainTab;
|
2022-09-01 12:07:05 +08:00
|
|
|
final DesktopTabType tabType;
|
|
|
|
final Rx<DesktopTabState> state;
|
2022-08-18 11:07:53 +08:00
|
|
|
|
2022-08-22 20:18:31 +08:00
|
|
|
final bool showMinimize;
|
|
|
|
final bool showMaximize;
|
|
|
|
final bool showClose;
|
2022-08-30 20:48:03 +08:00
|
|
|
final VoidCallback? onClose;
|
2022-08-22 20:18:31 +08:00
|
|
|
|
2022-08-18 11:07:53 +08:00
|
|
|
const WindowActionPanel(
|
2022-08-22 20:18:31 +08:00
|
|
|
{Key? key,
|
|
|
|
required this.mainTab,
|
2022-09-01 12:07:05 +08:00
|
|
|
required this.tabType,
|
|
|
|
required this.state,
|
2022-08-22 20:18:31 +08:00
|
|
|
this.showMinimize = true,
|
|
|
|
this.showMaximize = true,
|
2022-08-30 20:48:03 +08:00
|
|
|
this.showClose = true,
|
|
|
|
this.onClose})
|
2022-08-18 11:07:53 +08:00
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Row(
|
|
|
|
children: [
|
2022-08-22 20:18:31 +08:00
|
|
|
Offstage(
|
|
|
|
offstage: !showMinimize,
|
2022-08-24 20:12:04 +08:00
|
|
|
child: ActionIcon(
|
2022-08-22 20:18:31 +08:00
|
|
|
message: 'Minimize',
|
|
|
|
icon: IconFont.min,
|
2022-08-20 19:57:16 +08:00
|
|
|
onTap: () {
|
|
|
|
if (mainTab) {
|
2022-08-22 20:18:31 +08:00
|
|
|
windowManager.minimize();
|
2022-08-20 19:57:16 +08:00
|
|
|
} else {
|
2022-08-22 20:18:31 +08:00
|
|
|
WindowController.fromWindowId(windowId!).minimize();
|
2022-08-20 19:57:16 +08:00
|
|
|
}
|
|
|
|
},
|
2022-09-04 11:03:16 +08:00
|
|
|
isClose: false,
|
2022-08-22 20:18:31 +08:00
|
|
|
)),
|
2022-08-22 17:58:48 +08:00
|
|
|
// TODO: drag makes window restore
|
2022-08-22 20:18:31 +08:00
|
|
|
Offstage(
|
|
|
|
offstage: !showMaximize,
|
|
|
|
child: FutureBuilder(builder: (context, snapshot) {
|
2022-09-04 11:03:16 +08:00
|
|
|
RxBool isMaximized = false.obs;
|
2022-08-22 20:18:31 +08:00
|
|
|
if (mainTab) {
|
|
|
|
windowManager.isMaximized().then((maximized) {
|
2022-09-04 11:03:16 +08:00
|
|
|
isMaximized.value = maximized;
|
2022-08-22 20:18:31 +08:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
final wc = WindowController.fromWindowId(windowId!);
|
|
|
|
wc.isMaximized().then((maximized) {
|
2022-09-04 11:03:16 +08:00
|
|
|
isMaximized.value = maximized;
|
2022-08-22 20:18:31 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return Obx(
|
2022-08-24 20:12:04 +08:00
|
|
|
() => ActionIcon(
|
2022-09-04 11:03:16 +08:00
|
|
|
message: isMaximized.value ? "Restore" : "Maximize",
|
|
|
|
icon: isMaximized.value ? IconFont.restore : IconFont.max,
|
2022-08-22 20:18:31 +08:00
|
|
|
onTap: () {
|
|
|
|
if (mainTab) {
|
2022-09-04 11:03:16 +08:00
|
|
|
if (isMaximized.value) {
|
2022-08-22 20:18:31 +08:00
|
|
|
windowManager.unmaximize();
|
|
|
|
} else {
|
2022-08-22 17:58:48 +08:00
|
|
|
windowManager.maximize();
|
2022-08-22 20:18:31 +08:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-22 17:58:48 +08:00
|
|
|
// TODO: subwindow is maximized but first query result is not maximized.
|
2022-08-22 20:18:31 +08:00
|
|
|
final wc = WindowController.fromWindowId(windowId!);
|
2022-09-04 11:03:16 +08:00
|
|
|
if (isMaximized.value) {
|
2022-08-22 20:18:31 +08:00
|
|
|
wc.unmaximize();
|
|
|
|
} else {
|
2022-08-22 17:58:48 +08:00
|
|
|
wc.maximize();
|
2022-08-22 20:18:31 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-04 11:03:16 +08:00
|
|
|
isMaximized.value = !isMaximized.value;
|
2022-08-22 20:18:31 +08:00
|
|
|
},
|
2022-09-04 11:03:16 +08:00
|
|
|
isClose: false,
|
2022-08-22 20:18:31 +08:00
|
|
|
),
|
|
|
|
);
|
|
|
|
})),
|
|
|
|
Offstage(
|
|
|
|
offstage: !showClose,
|
2022-08-24 20:12:04 +08:00
|
|
|
child: ActionIcon(
|
2022-08-22 20:18:31 +08:00
|
|
|
message: 'Close',
|
|
|
|
icon: IconFont.close,
|
2022-09-01 12:07:05 +08:00
|
|
|
onTap: () async {
|
|
|
|
action() {
|
|
|
|
if (mainTab) {
|
|
|
|
windowManager.close();
|
|
|
|
} else {
|
|
|
|
// only hide for multi window, not close
|
|
|
|
Future.delayed(Duration.zero, () {
|
|
|
|
WindowController.fromWindowId(windowId!).hide();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
onClose?.call();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tabType != DesktopTabType.main &&
|
|
|
|
state.value.tabs.length > 1) {
|
|
|
|
closeConfirmDialog(action);
|
2022-08-22 20:18:31 +08:00
|
|
|
} else {
|
2022-09-01 12:07:05 +08:00
|
|
|
action();
|
2022-08-22 20:18:31 +08:00
|
|
|
}
|
|
|
|
},
|
2022-09-04 11:03:16 +08:00
|
|
|
isClose: true,
|
2022-08-22 20:18:31 +08:00
|
|
|
)),
|
2022-08-18 11:07:53 +08:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
2022-09-01 12:07:05 +08:00
|
|
|
|
|
|
|
closeConfirmDialog(Function() callback) async {
|
2022-09-03 18:19:50 +08:00
|
|
|
final res = await gFFI.dialogManager.show<bool>((setState, close) {
|
|
|
|
submit() => close(true);
|
|
|
|
return CustomAlertDialog(
|
|
|
|
title: Row(children: [
|
|
|
|
const Icon(Icons.warning_amber_sharp,
|
|
|
|
color: Colors.redAccent, size: 28),
|
|
|
|
const SizedBox(width: 10),
|
|
|
|
Text(translate("Warning")),
|
|
|
|
]),
|
|
|
|
content: Text(translate("Disconnect all devices?")),
|
|
|
|
actions: [
|
|
|
|
TextButton(onPressed: close, child: Text(translate("Cancel"))),
|
|
|
|
ElevatedButton(onPressed: submit, child: Text(translate("OK"))),
|
|
|
|
],
|
|
|
|
onSubmit: submit,
|
|
|
|
onCancel: close,
|
|
|
|
);
|
|
|
|
});
|
2022-09-01 12:07:05 +08:00
|
|
|
if (res == true) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
2022-08-18 11:07:53 +08:00
|
|
|
}
|
|
|
|
|
2022-08-20 19:57:16 +08:00
|
|
|
// ignore: must_be_immutable
|
2022-08-18 10:54:09 +08:00
|
|
|
class _ListView extends StatelessWidget {
|
2022-08-24 20:17:51 +08:00
|
|
|
final DesktopTabController controller;
|
2022-08-22 20:18:31 +08:00
|
|
|
final Function(String key)? onTabClose;
|
2022-08-29 18:48:12 +08:00
|
|
|
|
|
|
|
final TabBuilder? tabBuilder;
|
|
|
|
final LabelGetter? labelGetter;
|
|
|
|
|
2022-08-30 20:48:03 +08:00
|
|
|
Rx<DesktopTabState> get state => controller.state;
|
2022-08-18 10:54:09 +08:00
|
|
|
|
2022-09-04 11:03:16 +08:00
|
|
|
const _ListView(
|
2022-08-29 18:48:12 +08:00
|
|
|
{required this.controller,
|
|
|
|
required this.onTabClose,
|
|
|
|
this.tabBuilder,
|
|
|
|
this.labelGetter});
|
2022-08-18 10:54:09 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-08-24 20:12:04 +08:00
|
|
|
return Obx(() => ListView(
|
|
|
|
controller: state.value.scrollController,
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
shrinkWrap: true,
|
2022-09-04 11:03:16 +08:00
|
|
|
physics: const BouncingScrollPhysics(),
|
2022-08-24 20:12:04 +08:00
|
|
|
children: state.value.tabs.asMap().entries.map((e) {
|
|
|
|
final index = e.key;
|
|
|
|
final tab = e.value;
|
|
|
|
return _Tab(
|
|
|
|
index: index,
|
2022-08-29 18:48:12 +08:00
|
|
|
label: labelGetter == null
|
|
|
|
? Rx<String>(tab.label)
|
|
|
|
: labelGetter!(tab.label),
|
2022-08-24 20:12:04 +08:00
|
|
|
selectedIcon: tab.selectedIcon,
|
|
|
|
unselectedIcon: tab.unselectedIcon,
|
|
|
|
closable: tab.closable,
|
|
|
|
selected: state.value.selected,
|
|
|
|
onClose: () => controller.remove(index),
|
|
|
|
onSelected: () => controller.jumpTo(index),
|
2022-08-29 18:48:12 +08:00
|
|
|
tabBuilder: tabBuilder == null
|
|
|
|
? null
|
|
|
|
: (Widget icon, Widget labelWidget, TabThemeConf themeConf) {
|
|
|
|
return tabBuilder!(
|
|
|
|
tab.label,
|
|
|
|
icon,
|
|
|
|
labelWidget,
|
|
|
|
themeConf,
|
|
|
|
);
|
|
|
|
},
|
2022-08-24 20:12:04 +08:00
|
|
|
);
|
|
|
|
}).toList()));
|
2022-08-18 10:54:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 19:23:32 +08:00
|
|
|
class _Tab extends StatefulWidget {
|
2022-09-04 11:03:16 +08:00
|
|
|
final int index;
|
|
|
|
final Rx<String> label;
|
|
|
|
final IconData? selectedIcon;
|
|
|
|
final IconData? unselectedIcon;
|
|
|
|
final bool closable;
|
|
|
|
final int selected;
|
|
|
|
final Function() onClose;
|
|
|
|
final Function() onSelected;
|
2022-08-29 18:48:12 +08:00
|
|
|
final Widget Function(Widget icon, Widget label, TabThemeConf themeConf)?
|
|
|
|
tabBuilder;
|
2022-08-06 17:08:48 +08:00
|
|
|
|
2022-09-04 11:03:16 +08:00
|
|
|
const _Tab({
|
|
|
|
Key? key,
|
|
|
|
required this.index,
|
|
|
|
required this.label,
|
|
|
|
this.selectedIcon,
|
|
|
|
this.unselectedIcon,
|
|
|
|
this.tabBuilder,
|
|
|
|
required this.closable,
|
|
|
|
required this.selected,
|
|
|
|
required this.onClose,
|
|
|
|
required this.onSelected,
|
|
|
|
}) : super(key: key);
|
2022-08-06 17:08:48 +08:00
|
|
|
|
2022-08-31 19:23:32 +08:00
|
|
|
@override
|
|
|
|
State<_Tab> createState() => _TabState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _TabState extends State<_Tab> with RestorationMixin {
|
|
|
|
final RestorableBool restoreHover = RestorableBool(false);
|
|
|
|
|
2022-08-29 18:48:12 +08:00
|
|
|
Widget _buildTabContent() {
|
2022-08-31 19:23:32 +08:00
|
|
|
bool showIcon =
|
|
|
|
widget.selectedIcon != null && widget.unselectedIcon != null;
|
|
|
|
bool isSelected = widget.index == widget.selected;
|
2022-08-29 18:48:12 +08:00
|
|
|
|
|
|
|
final icon = Offstage(
|
|
|
|
offstage: !showIcon,
|
|
|
|
child: Icon(
|
2022-08-31 19:23:32 +08:00
|
|
|
isSelected ? widget.selectedIcon : widget.unselectedIcon,
|
2022-08-29 18:48:12 +08:00
|
|
|
size: _kIconSize,
|
|
|
|
color: isSelected
|
2022-09-04 11:03:16 +08:00
|
|
|
? MyTheme.tabbar(context).selectedTabIconColor
|
|
|
|
: MyTheme.tabbar(context).unSelectedTabIconColor,
|
2022-08-29 18:48:12 +08:00
|
|
|
).paddingOnly(right: 5));
|
|
|
|
final labelWidget = Obx(() {
|
|
|
|
return Text(
|
2022-08-31 19:23:32 +08:00
|
|
|
translate(widget.label.value),
|
2022-08-29 18:48:12 +08:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
color: isSelected
|
2022-09-04 11:03:16 +08:00
|
|
|
? MyTheme.tabbar(context).selectedTextColor
|
|
|
|
: MyTheme.tabbar(context).unSelectedTextColor),
|
2022-08-29 18:48:12 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-08-31 19:23:32 +08:00
|
|
|
if (widget.tabBuilder == null) {
|
2022-08-29 18:48:12 +08:00
|
|
|
return Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
icon,
|
|
|
|
labelWidget,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
} else {
|
2022-09-04 11:03:16 +08:00
|
|
|
return widget.tabBuilder!(
|
|
|
|
icon, labelWidget, TabThemeConf(iconSize: _kIconSize));
|
2022-08-29 18:48:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 17:08:48 +08:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-08-31 19:23:32 +08:00
|
|
|
bool isSelected = widget.index == widget.selected;
|
|
|
|
bool showDivider =
|
|
|
|
widget.index != widget.selected - 1 && widget.index != widget.selected;
|
|
|
|
RxBool hover = restoreHover.value.obs;
|
2022-08-20 19:57:16 +08:00
|
|
|
return Ink(
|
|
|
|
child: InkWell(
|
2022-08-31 19:23:32 +08:00
|
|
|
onHover: (value) {
|
|
|
|
hover.value = value;
|
|
|
|
restoreHover.value = value;
|
|
|
|
},
|
|
|
|
onTap: () => widget.onSelected(),
|
2022-08-20 19:57:16 +08:00
|
|
|
child: Row(
|
|
|
|
children: [
|
2022-08-31 19:23:32 +08:00
|
|
|
SizedBox(
|
2022-08-20 19:57:16 +08:00
|
|
|
height: _kTabBarHeight,
|
|
|
|
child: Row(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
2022-08-29 18:48:12 +08:00
|
|
|
_buildTabContent(),
|
2022-08-31 19:23:32 +08:00
|
|
|
Obx((() => _CloseButton(
|
|
|
|
visiable: hover.value && widget.closable,
|
|
|
|
tabSelected: isSelected,
|
|
|
|
onClose: () => widget.onClose(),
|
|
|
|
)))
|
2022-08-20 19:57:16 +08:00
|
|
|
])).paddingSymmetric(horizontal: 10),
|
|
|
|
Offstage(
|
2022-08-29 18:48:12 +08:00
|
|
|
offstage: !showDivider,
|
2022-08-20 19:57:16 +08:00
|
|
|
child: VerticalDivider(
|
|
|
|
width: 1,
|
|
|
|
indent: _kDividerIndent,
|
|
|
|
endIndent: _kDividerIndent,
|
2022-09-04 11:03:16 +08:00
|
|
|
color: MyTheme.tabbar(context).dividerColor,
|
2022-08-20 19:57:16 +08:00
|
|
|
thickness: 1,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
2022-08-10 16:40:04 +08:00
|
|
|
),
|
2022-08-20 19:57:16 +08:00
|
|
|
),
|
2022-08-06 17:08:48 +08:00
|
|
|
);
|
|
|
|
}
|
2022-08-31 19:23:32 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
String? get restorationId => "_Tab${widget.label.value}";
|
|
|
|
|
|
|
|
@override
|
|
|
|
void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
|
|
|
|
registerForRestoration(restoreHover, 'restoreHover');
|
|
|
|
}
|
2022-08-06 17:08:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class _CloseButton extends StatelessWidget {
|
2022-08-11 16:03:04 +08:00
|
|
|
final bool visiable;
|
2022-08-09 22:35:29 +08:00
|
|
|
final bool tabSelected;
|
2022-08-06 17:08:48 +08:00
|
|
|
final Function onClose;
|
|
|
|
|
2022-09-04 11:03:16 +08:00
|
|
|
const _CloseButton({
|
2022-08-06 17:08:48 +08:00
|
|
|
Key? key,
|
2022-08-11 16:03:04 +08:00
|
|
|
required this.visiable,
|
2022-08-09 22:35:29 +08:00
|
|
|
required this.tabSelected,
|
2022-08-06 17:08:48 +08:00
|
|
|
required this.onClose,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-08-11 16:03:04 +08:00
|
|
|
return SizedBox(
|
|
|
|
width: _kIconSize,
|
|
|
|
child: Offstage(
|
|
|
|
offstage: !visiable,
|
|
|
|
child: InkWell(
|
2022-08-31 19:23:32 +08:00
|
|
|
customBorder: const RoundedRectangleBorder(),
|
2022-08-11 16:03:04 +08:00
|
|
|
onTap: () => onClose(),
|
|
|
|
child: Icon(
|
|
|
|
Icons.close,
|
|
|
|
size: _kIconSize,
|
|
|
|
color: tabSelected
|
2022-09-04 11:03:16 +08:00
|
|
|
? MyTheme.tabbar(context).selectedIconColor
|
|
|
|
: MyTheme.tabbar(context).unSelectedIconColor,
|
2022-08-11 16:03:04 +08:00
|
|
|
),
|
|
|
|
),
|
2022-08-11 21:29:43 +08:00
|
|
|
)).paddingOnly(left: 5);
|
2022-08-06 17:08:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 20:12:04 +08:00
|
|
|
class ActionIcon extends StatelessWidget {
|
2022-08-20 19:57:16 +08:00
|
|
|
final String message;
|
|
|
|
final IconData icon;
|
|
|
|
final Function() onTap;
|
2022-09-04 11:03:16 +08:00
|
|
|
final bool isClose;
|
2022-08-24 20:12:04 +08:00
|
|
|
const ActionIcon({
|
2022-08-20 19:57:16 +08:00
|
|
|
Key? key,
|
|
|
|
required this.message,
|
|
|
|
required this.icon,
|
|
|
|
required this.onTap,
|
2022-09-04 11:03:16 +08:00
|
|
|
required this.isClose,
|
2022-08-20 19:57:16 +08:00
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
RxBool hover = false.obs;
|
|
|
|
return Obx(() => Tooltip(
|
|
|
|
message: translate(message),
|
2022-09-04 11:03:16 +08:00
|
|
|
waitDuration: const Duration(seconds: 1),
|
2022-08-20 19:57:16 +08:00
|
|
|
child: InkWell(
|
2022-09-04 11:03:16 +08:00
|
|
|
hoverColor: isClose
|
|
|
|
? const Color.fromARGB(255, 196, 43, 28)
|
|
|
|
: MyTheme.tabbar(context).hoverColor,
|
2022-08-20 19:57:16 +08:00
|
|
|
onHover: (value) => hover.value = value,
|
2022-09-04 11:03:16 +08:00
|
|
|
onTap: onTap,
|
|
|
|
child: SizedBox(
|
2022-08-20 19:57:16 +08:00
|
|
|
height: _kTabBarHeight - 1,
|
|
|
|
width: _kTabBarHeight - 1,
|
|
|
|
child: Icon(
|
|
|
|
icon,
|
2022-09-04 11:03:16 +08:00
|
|
|
color: hover.value && isClose
|
2022-08-20 19:57:16 +08:00
|
|
|
? Colors.white
|
2022-09-04 11:03:16 +08:00
|
|
|
: MyTheme.tabbar(context).unSelectedIconColor,
|
2022-08-20 19:57:16 +08:00
|
|
|
size: _kActionIconSize,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 21:09:18 +08:00
|
|
|
class AddButton extends StatelessWidget {
|
2022-09-04 11:03:16 +08:00
|
|
|
const AddButton({
|
2022-08-24 21:09:18 +08:00
|
|
|
Key? key,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return ActionIcon(
|
|
|
|
message: 'New Connection',
|
|
|
|
icon: IconFont.add,
|
|
|
|
onTap: () =>
|
|
|
|
rustDeskWinManager.call(WindowType.Main, "main_window_on_top", ""),
|
2022-09-04 11:03:16 +08:00
|
|
|
isClose: false);
|
2022-08-24 21:09:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-04 11:03:16 +08:00
|
|
|
class TabbarTheme extends ThemeExtension<TabbarTheme> {
|
|
|
|
final Color? selectedTabIconColor;
|
|
|
|
final Color? unSelectedTabIconColor;
|
|
|
|
final Color? selectedTextColor;
|
|
|
|
final Color? unSelectedTextColor;
|
|
|
|
final Color? selectedIconColor;
|
|
|
|
final Color? unSelectedIconColor;
|
|
|
|
final Color? dividerColor;
|
|
|
|
final Color? hoverColor;
|
|
|
|
|
|
|
|
const TabbarTheme(
|
|
|
|
{required this.selectedTabIconColor,
|
|
|
|
required this.unSelectedTabIconColor,
|
|
|
|
required this.selectedTextColor,
|
|
|
|
required this.unSelectedTextColor,
|
|
|
|
required this.selectedIconColor,
|
|
|
|
required this.unSelectedIconColor,
|
|
|
|
required this.dividerColor,
|
|
|
|
required this.hoverColor});
|
|
|
|
|
|
|
|
static const light = TabbarTheme(
|
|
|
|
selectedTabIconColor: MyTheme.accent,
|
|
|
|
unSelectedTabIconColor: Color.fromARGB(255, 162, 203, 241),
|
|
|
|
selectedTextColor: Color.fromARGB(255, 26, 26, 26),
|
|
|
|
unSelectedTextColor: Color.fromARGB(255, 96, 96, 96),
|
|
|
|
selectedIconColor: Color.fromARGB(255, 26, 26, 26),
|
|
|
|
unSelectedIconColor: Color.fromARGB(255, 96, 96, 96),
|
|
|
|
dividerColor: Color.fromARGB(255, 238, 238, 238),
|
|
|
|
hoverColor: Color.fromARGB(51, 158, 158, 158));
|
|
|
|
|
|
|
|
static const dark = TabbarTheme(
|
|
|
|
selectedTabIconColor: MyTheme.accent,
|
|
|
|
unSelectedTabIconColor: Color.fromARGB(255, 30, 65, 98),
|
|
|
|
selectedTextColor: Color.fromARGB(255, 255, 255, 255),
|
|
|
|
unSelectedTextColor: Color.fromARGB(255, 207, 207, 207),
|
|
|
|
selectedIconColor: Color.fromARGB(255, 215, 215, 215),
|
|
|
|
unSelectedIconColor: Color.fromARGB(255, 255, 255, 255),
|
|
|
|
dividerColor: Color.fromARGB(255, 64, 64, 64),
|
|
|
|
hoverColor: Colors.black26);
|
|
|
|
|
|
|
|
@override
|
|
|
|
ThemeExtension<TabbarTheme> copyWith({
|
|
|
|
Color? selectedTabIconColor,
|
|
|
|
Color? unSelectedTabIconColor,
|
|
|
|
Color? selectedTextColor,
|
|
|
|
Color? unSelectedTextColor,
|
|
|
|
Color? selectedIconColor,
|
|
|
|
Color? unSelectedIconColor,
|
|
|
|
Color? dividerColor,
|
|
|
|
Color? hoverColor,
|
|
|
|
}) {
|
|
|
|
return TabbarTheme(
|
|
|
|
selectedTabIconColor: selectedTabIconColor ?? this.selectedTabIconColor,
|
|
|
|
unSelectedTabIconColor:
|
|
|
|
unSelectedTabIconColor ?? this.unSelectedTabIconColor,
|
|
|
|
selectedTextColor: selectedTextColor ?? this.selectedTextColor,
|
|
|
|
unSelectedTextColor: unSelectedTextColor ?? this.unSelectedTextColor,
|
|
|
|
selectedIconColor: selectedIconColor ?? this.selectedIconColor,
|
|
|
|
unSelectedIconColor: unSelectedIconColor ?? this.unSelectedIconColor,
|
|
|
|
dividerColor: dividerColor ?? this.dividerColor,
|
|
|
|
hoverColor: hoverColor ?? this.hoverColor,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
ThemeExtension<TabbarTheme> lerp(
|
|
|
|
ThemeExtension<TabbarTheme>? other, double t) {
|
|
|
|
if (other is! TabbarTheme) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
return TabbarTheme(
|
|
|
|
selectedTabIconColor:
|
|
|
|
Color.lerp(selectedTabIconColor, other.selectedTabIconColor, t),
|
|
|
|
unSelectedTabIconColor:
|
|
|
|
Color.lerp(unSelectedTabIconColor, other.unSelectedTabIconColor, t),
|
|
|
|
selectedTextColor:
|
|
|
|
Color.lerp(selectedTextColor, other.selectedTextColor, t),
|
|
|
|
unSelectedTextColor:
|
|
|
|
Color.lerp(unSelectedTextColor, other.unSelectedTextColor, t),
|
|
|
|
selectedIconColor:
|
|
|
|
Color.lerp(selectedIconColor, other.selectedIconColor, t),
|
|
|
|
unSelectedIconColor:
|
|
|
|
Color.lerp(unSelectedIconColor, other.unSelectedIconColor, t),
|
|
|
|
dividerColor: Color.lerp(dividerColor, other.dividerColor, t),
|
|
|
|
hoverColor: Color.lerp(hoverColor, other.hoverColor, t),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static color(BuildContext context) {
|
|
|
|
return Theme.of(context).extension<ColorThemeExtension>()!;
|
|
|
|
}
|
2022-08-09 22:35:29 +08:00
|
|
|
}
|