platform_menu.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:flutter/widgets.dart';
  5. class AppPlatformMenu extends StatefulWidget {
  6. const AppPlatformMenu({super.key, required this.child});
  7. final Widget child;
  8. @override
  9. State<AppPlatformMenu> createState() => _AppPlatformMenuState();
  10. }
  11. class _AppPlatformMenuState extends State<AppPlatformMenu> {
  12. @override
  13. Widget build(BuildContext context) {
  14. if (defaultTargetPlatform != TargetPlatform.macOS) {
  15. return widget.child;
  16. }
  17. return PlatformMenuBar(
  18. menus: <MenuItem>[
  19. PlatformMenu(
  20. label: 'TerminalStudio',
  21. menus: [
  22. if (PlatformProvidedMenuItem.hasMenu(
  23. PlatformProvidedMenuItemType.about,
  24. ))
  25. const PlatformProvidedMenuItem(
  26. type: PlatformProvidedMenuItemType.about,
  27. ),
  28. PlatformMenuItemGroup(
  29. members: [
  30. if (PlatformProvidedMenuItem.hasMenu(
  31. PlatformProvidedMenuItemType.servicesSubmenu,
  32. ))
  33. const PlatformProvidedMenuItem(
  34. type: PlatformProvidedMenuItemType.servicesSubmenu,
  35. ),
  36. ],
  37. ),
  38. PlatformMenuItemGroup(
  39. members: [
  40. if (PlatformProvidedMenuItem.hasMenu(
  41. PlatformProvidedMenuItemType.hide,
  42. ))
  43. const PlatformProvidedMenuItem(
  44. type: PlatformProvidedMenuItemType.hide,
  45. ),
  46. if (PlatformProvidedMenuItem.hasMenu(
  47. PlatformProvidedMenuItemType.hideOtherApplications,
  48. ))
  49. const PlatformProvidedMenuItem(
  50. type: PlatformProvidedMenuItemType.hideOtherApplications,
  51. ),
  52. ],
  53. ),
  54. if (PlatformProvidedMenuItem.hasMenu(
  55. PlatformProvidedMenuItemType.quit,
  56. ))
  57. const PlatformProvidedMenuItem(
  58. type: PlatformProvidedMenuItemType.quit,
  59. ),
  60. ],
  61. ),
  62. PlatformMenu(
  63. label: 'Edit',
  64. menus: [
  65. PlatformMenuItemGroup(
  66. members: [
  67. PlatformMenuItem(
  68. label: 'Copy',
  69. shortcut: const SingleActivator(
  70. LogicalKeyboardKey.keyC,
  71. meta: true,
  72. ),
  73. onSelected: () {
  74. final primaryContext = primaryFocus?.context;
  75. if (primaryContext == null) {
  76. return;
  77. }
  78. Actions.invoke(
  79. primaryContext,
  80. CopySelectionTextIntent.copy,
  81. );
  82. },
  83. ),
  84. PlatformMenuItem(
  85. label: 'Paste',
  86. shortcut: const SingleActivator(
  87. LogicalKeyboardKey.keyV,
  88. meta: true,
  89. ),
  90. onSelected: () {
  91. final primaryContext = primaryFocus?.context;
  92. if (primaryContext == null) {
  93. return;
  94. }
  95. Actions.invoke(
  96. primaryContext,
  97. const PasteTextIntent(SelectionChangedCause.keyboard),
  98. );
  99. },
  100. ),
  101. PlatformMenuItem(
  102. label: 'Select All',
  103. shortcut: const SingleActivator(
  104. LogicalKeyboardKey.keyA,
  105. meta: true,
  106. ),
  107. onSelected: () {
  108. final primaryContext = primaryFocus?.context;
  109. if (primaryContext == null) {
  110. return;
  111. }
  112. print(primaryContext);
  113. try {
  114. final action = Actions.maybeFind<Intent>(
  115. primaryContext,
  116. intent: const SelectAllTextIntent(
  117. SelectionChangedCause.keyboard,
  118. ),
  119. );
  120. print('action: $action');
  121. } catch (e) {
  122. print(e);
  123. }
  124. Actions.invoke<Intent>(
  125. primaryContext,
  126. const SelectAllTextIntent(SelectionChangedCause.keyboard),
  127. );
  128. },
  129. ),
  130. ],
  131. ),
  132. if (PlatformProvidedMenuItem.hasMenu(
  133. PlatformProvidedMenuItemType.quit,
  134. ))
  135. const PlatformProvidedMenuItem(
  136. type: PlatformProvidedMenuItemType.quit,
  137. ),
  138. ],
  139. ),
  140. ],
  141. body: widget.child,
  142. );
  143. }
  144. }