Giter Club home page Giter Club logo

system_tray's Introduction

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

Method Description Windows macOS Linux
initSystemTray Initialize system tray ✔️ ✔️ ✔️
setSystemTrayInfo Modify the tray info
  • icon
  • toolTip
  • title
  • icon
  • toolTip
  • icon
setImage Modify the tray image ✔️ ✔️ ✔️
setTooltip Modify the tray tooltip ✔️ ✔️
setTitle / getTitle Set / Get the tray title ✔️
setContextMenu Set the tray context menu ✔️ ✔️ ✔️
popUpContextMenu Popup the tray context menu ✔️ ✔️
destroy Destroy the tray ✔️ ✔️ ✔️
registerSystemTrayEventHandler Register system tray event
  • click
  • right-click
  • double-click
  • click
  • right-click

Menu

Type Description Windows macOS Linux
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'

system_tray's People

Contributors

antler119 avatar au-top avatar birjuvachhani avatar denizt avatar ggdream avatar pana-g avatar tomoyuki28jp avatar vladdoster avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.