A Flutter plugin that provides access to macOS menu bar actions, allowing Flutter applications to handle standard menu items like Cut, Copy, Paste, and Select All.
- Intercept Standard Menu Actions: Handle Cut, Copy, Paste, and Select All from the macOS menu bar
- Custom Menu Items: Add your own menu items to existing menus or create new ones
- Submenu Support: Create nested menu structures with unlimited depth
- Keyboard Shortcuts: Define shortcuts using Flutter's SingleActivator class
- Clean API: Type-safe, well-documented API following Flutter best practices
- Fallback Support: Automatically falls back to system behavior when actions aren't handled
Add mac_menu_bar to your pubspec.yaml file:
dependencies:
mac_menu_bar: ^0.0.2Then run flutter pub get to install the package.
Import the package in your Dart code:
import 'package:mac_menu_bar/mac_menu_bar.dart';Set up handlers for the menu actions in your app's initialization:
@override
void initState() {
super.initState();
// Handle Cut menu item
MacMenuBar.onCut(() async {
debugPrint('Cut menu item selected');
// Implement your cut logic here
return true; // Return true to indicate the action was handled
});
// Handle Copy menu item
MacMenuBar.onCopy(() async {
debugPrint('Copy menu item selected');
// Implement your copy logic here
return true;
});
// Handle Paste menu item
MacMenuBar.onPaste(() async {
debugPrint('Paste menu item selected');
// Implement your paste logic here
return true;
});
// Handle Select All menu item
MacMenuBar.onSelectAll(() async {
debugPrint('Select All menu item selected');
// Implement your select all logic here
return true;
});
}Return false from a handler to let the system handle the action:
MacMenuBar.onPaste(() async {
if (shouldHandlePaste) {
// Handle paste operation
return true;
}
// Let the system handle the paste operation
return false;
});You can add custom menu items to existing menus or create new ones:
await MacMenuBar.addMenuItem(
menuId: 'View',
itemId: 'refresh',
title: 'Refresh',
);You can create submenus with unlimited depth:
// Create a custom submenu in the main menu bar
await MacMenuBar.addSubmenu(
parentMenuId: 'main',
submenuId: 'tools',
title: 'Tools',
);
// Add items to the submenu
await MacMenuBar.addMenuItem(
menuId: 'tools',
itemId: 'tool_1',
title: 'Developer Tools',
shortcut: const SingleActivator(
LogicalKeyboardKey.digit1,
meta: true,
alt: true,
),
);This plugin is only supported on macOS. It will have no effect on other platforms.
For a complete example, see the example directory in this repository.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
