antler119/system_tray

A Flutter package that makes it easy to customize and work with your Flutter desktop app's system tray.

224

stars

43

commits

C++

primary language

Jul 19, 2024

updated

flutter
linux
macos
windows

README

system_tray

Pub

A Flutter package that enables support for system tray menu for desktop flutter apps. on Windows, macOS, and Linux.

Install

In the pubspec.yaml of your flutter project, add the following dependency:

dependencies:
  ...
  system_tray: ^2.0.3

In your library add the following import:

import 'package:system_tray/system_tray.dart';

Prerequisite

Linux

sudo apt-get install appindicator3-0.1 libappindicator3-dev

or

// For Ubuntu 22.04 or greater
sudo apt-get install libayatana-appindicator3-dev

Example App

Windows

macOS

Linux

API

MethodDescriptionWindowsmacOSLinux
initSystemTrayInitialize system tray✔️✔️✔️
setSystemTrayInfoModify the tray info
  • icon
  • toolTip
  • title
  • icon
  • toolTip
  • icon
setImageModify the tray image✔️✔️✔️
setTooltipModify the tray tooltip✔️✔️
setTitle / getTitleSet / Get the tray title✔️
setContextMenuSet the tray context menu✔️✔️✔️
popUpContextMenuPopup the tray context menu✔️✔️
destroyDestroy the tray✔️✔️✔️
registerSystemTrayEventHandlerRegister system tray event
  • click
  • right-click
  • double-click
  • click
  • right-click
TypeDescriptionWindowsmacOSLinux
MenuItemLabel✔️✔️✔️
MenuItemCheckbox✔️✔️✔️
SubMenu✔️✔️✔️
MenuSeparator✔️✔️✔️

Usage

Future<void> initSystemTray() async {
  String path =
      Platform.isWindows ? 'assets/app_icon.ico' : 'assets/app_icon.png';

  final AppWindow appWindow = AppWindow();
  final SystemTray systemTray = SystemTray();

  // We first init the systray menu
  await systemTray.initSystemTray(
    title: "system tray",
    iconPath: path,
  );

  // create context menu
  final Menu menu = Menu();
  await menu.buildFrom([
    MenuItemLabel(label: 'Show', onClicked: (menuItem) => appWindow.show()),
    MenuItemLabel(label: 'Hide', onClicked: (menuItem) => appWindow.hide()),
    MenuItemLabel(label: 'Exit', onClicked: (menuItem) => appWindow.close()),
  ]);

  // set context menu
  await systemTray.setContextMenu(menu);

  // handle system tray event
  systemTray.registerSystemTrayEventHandler((eventName) {
    debugPrint("eventName: $eventName");
    if (eventName == kSystemTrayEventClick) {
       Platform.isWindows ? appWindow.show() : systemTray.popUpContextMenu();
    } else if (eventName == kSystemTrayEventRightClick) {
       Platform.isWindows ? systemTray.popUpContextMenu() : appWindow.show();
    }
  });
}

Additional Resources

Recommended library that supports window control:

Q&A

  1. Q: If you encounter the following compilation error

    Undefined symbols for architecture x86_64:
      "___gxx_personality_v0", referenced from:
          ...
    

    A: add libc++.tbd

    1. open example/macos/Runner.xcodeproj
    2. add 'libc++.tbd' to TARGET runner 'Link Binary With Libraries'
    

Contributors

antler119

33 commits

denizt

2 commits

Pana-g

2 commits

tomoyuki28jp

2 commits

antler119/system_tray

A Flutter package that makes it easy to customize and work with your Flutter desktop app's system tray.

224

stars

43

commits

C++

primary language

Jul 19, 2024

updated

flutter
linux
macos
windows

README

system_tray

Pub

A Flutter package that enables support for system tray menu for desktop flutter apps. on Windows, macOS, and Linux.

Install

In the pubspec.yaml of your flutter project, add the following dependency:

dependencies:
  ...
  system_tray: ^2.0.3

In your library add the following import:

import 'package:system_tray/system_tray.dart';

Prerequisite

Linux

sudo apt-get install appindicator3-0.1 libappindicator3-dev

or

// For Ubuntu 22.04 or greater
sudo apt-get install libayatana-appindicator3-dev

Example App

Windows

macOS

Linux

API

MethodDescriptionWindowsmacOSLinux
initSystemTrayInitialize system tray✔️✔️✔️
setSystemTrayInfoModify the tray info
  • icon
  • toolTip
  • title
  • icon
  • toolTip
  • icon
setImageModify the tray image✔️✔️✔️
setTooltipModify the tray tooltip✔️✔️
setTitle / getTitleSet / Get the tray title✔️
setContextMenuSet the tray context menu✔️✔️✔️
popUpContextMenuPopup the tray context menu✔️✔️
destroyDestroy the tray✔️✔️✔️
registerSystemTrayEventHandlerRegister system tray event
  • click
  • right-click
  • double-click
  • click
  • right-click
TypeDescriptionWindowsmacOSLinux
MenuItemLabel✔️✔️✔️
MenuItemCheckbox✔️✔️✔️
SubMenu✔️✔️✔️
MenuSeparator✔️✔️✔️

Usage

Future<void> initSystemTray() async {
  String path =
      Platform.isWindows ? 'assets/app_icon.ico' : 'assets/app_icon.png';

  final AppWindow appWindow = AppWindow();
  final SystemTray systemTray = SystemTray();

  // We first init the systray menu
  await systemTray.initSystemTray(
    title: "system tray",
    iconPath: path,
  );

  // create context menu
  final Menu menu = Menu();
  await menu.buildFrom([
    MenuItemLabel(label: 'Show', onClicked: (menuItem) => appWindow.show()),
    MenuItemLabel(label: 'Hide', onClicked: (menuItem) => appWindow.hide()),
    MenuItemLabel(label: 'Exit', onClicked: (menuItem) => appWindow.close()),
  ]);

  // set context menu
  await systemTray.setContextMenu(menu);

  // handle system tray event
  systemTray.registerSystemTrayEventHandler((eventName) {
    debugPrint("eventName: $eventName");
    if (eventName == kSystemTrayEventClick) {
       Platform.isWindows ? appWindow.show() : systemTray.popUpContextMenu();
    } else if (eventName == kSystemTrayEventRightClick) {
       Platform.isWindows ? systemTray.popUpContextMenu() : appWindow.show();
    }
  });
}

Additional Resources

Recommended library that supports window control:

Q&A

  1. Q: If you encounter the following compilation error

    Undefined symbols for architecture x86_64:
      "___gxx_personality_v0", referenced from:
          ...
    

    A: add libc++.tbd

    1. open example/macos/Runner.xcodeproj
    2. add 'libc++.tbd' to TARGET runner 'Link Binary With Libraries'
    

Contributors

antler119

33 commits

denizt

2 commits

Pana-g

2 commits

tomoyuki28jp

2 commits

Languages

C++

60.7%

Dart

16.1%

Swift

11.2%

CMake

9.5%

C

1.4%

Ruby

1.1%