desktop_tab_page.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_hbb/common.dart';
  3. import 'package:flutter_hbb/consts.dart';
  4. import 'package:flutter_hbb/desktop/pages/desktop_home_page.dart';
  5. import 'package:flutter_hbb/desktop/pages/desktop_setting_page.dart';
  6. import 'package:flutter_hbb/desktop/widgets/tabbar_widget.dart';
  7. import 'package:flutter_hbb/models/platform_model.dart';
  8. import 'package:flutter_hbb/models/state_model.dart';
  9. import 'package:get/get.dart';
  10. import 'package:window_manager/window_manager.dart';
  11. // import 'package:flutter/services.dart';
  12. import '../../common/shared_state.dart';
  13. class DesktopTabPage extends StatefulWidget {
  14. const DesktopTabPage({Key? key}) : super(key: key);
  15. @override
  16. State<DesktopTabPage> createState() => _DesktopTabPageState();
  17. static void onAddSetting(
  18. {SettingsTabKey initialPage = SettingsTabKey.general}) {
  19. try {
  20. DesktopTabController tabController = Get.find<DesktopTabController>();
  21. tabController.add(TabInfo(
  22. key: kTabLabelSettingPage,
  23. label: kTabLabelSettingPage,
  24. selectedIcon: Icons.build_sharp,
  25. unselectedIcon: Icons.build_outlined,
  26. page: DesktopSettingPage(
  27. key: const ValueKey(kTabLabelSettingPage),
  28. initialTabkey: initialPage,
  29. )));
  30. } catch (e) {
  31. debugPrintStack(label: '$e');
  32. }
  33. }
  34. }
  35. class _DesktopTabPageState extends State<DesktopTabPage>
  36. with WidgetsBindingObserver {
  37. final tabController = DesktopTabController(tabType: DesktopTabType.main);
  38. final RxBool _block = false.obs;
  39. // bool mouseIn = false;
  40. _DesktopTabPageState() {
  41. RemoteCountState.init();
  42. Get.put<DesktopTabController>(tabController);
  43. tabController.add(TabInfo(
  44. key: kTabLabelHomePage,
  45. label: kTabLabelHomePage,
  46. selectedIcon: Icons.home_sharp,
  47. unselectedIcon: Icons.home_outlined,
  48. closable: false,
  49. page: DesktopHomePage(
  50. key: const ValueKey(kTabLabelHomePage),
  51. )));
  52. if (bind.isIncomingOnly()) {
  53. tabController.onSelected = (key) {
  54. if (key == kTabLabelHomePage) {
  55. windowManager.setSize(getIncomingOnlyHomeSize());
  56. setResizable(false);
  57. } else {
  58. windowManager.setSize(getIncomingOnlySettingsSize());
  59. setResizable(true);
  60. }
  61. };
  62. }
  63. }
  64. @override
  65. void didChangeAppLifecycleState(AppLifecycleState state) {
  66. super.didChangeAppLifecycleState(state);
  67. if (state == AppLifecycleState.resumed) {
  68. shouldBeBlocked(_block, canBeBlocked);
  69. } else if (state == AppLifecycleState.inactive) {}
  70. }
  71. @override
  72. void initState() {
  73. super.initState();
  74. // HardwareKeyboard.instance.addHandler(_handleKeyEvent);
  75. WidgetsBinding.instance.addObserver(this);
  76. }
  77. /*
  78. bool _handleKeyEvent(KeyEvent event) {
  79. if (!mouseIn && event is KeyDownEvent) {
  80. print('key down: ${event.logicalKey}');
  81. shouldBeBlocked(_block, canBeBlocked);
  82. }
  83. return false; // allow it to propagate
  84. }
  85. */
  86. @override
  87. void dispose() {
  88. // HardwareKeyboard.instance.removeHandler(_handleKeyEvent);
  89. WidgetsBinding.instance.removeObserver(this);
  90. Get.delete<DesktopTabController>();
  91. super.dispose();
  92. }
  93. @override
  94. Widget build(BuildContext context) {
  95. final tabWidget = Container(
  96. child: Scaffold(
  97. backgroundColor: Theme.of(context).colorScheme.background,
  98. body: DesktopTab(
  99. controller: tabController,
  100. tail: Offstage(
  101. offstage: bind.isIncomingOnly() || bind.isDisableSettings(),
  102. child: ActionIcon(
  103. message: 'Settings',
  104. icon: IconFont.menu,
  105. onTap: DesktopTabPage.onAddSetting,
  106. isClose: false,
  107. ),
  108. ),
  109. blockTab: _block,
  110. )));
  111. return isMacOS || kUseCompatibleUiMode
  112. ? tabWidget
  113. : Obx(
  114. () => DragToResizeArea(
  115. resizeEdgeSize: stateGlobal.resizeEdgeSize.value,
  116. enableResizeEdges: windowManagerEnableResizeEdges,
  117. child: tabWidget,
  118. ),
  119. );
  120. }
  121. }