Skip to content

Commit

Permalink
feat: add useful method for tray
Browse files Browse the repository at this point in the history
  • Loading branch information
antler119 committed Feb 18, 2022
1 parent 4fbb7a8 commit a0aa59c
Show file tree
Hide file tree
Showing 18 changed files with 161 additions and 174 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.1.0

* [Feature] add some helper functions (setImage / setTooltip / setTitle / getTitle)
* [Fixed] enable change icon image on macOS

## 0.0.9

* [Feature] add popUpContextMenu
Expand Down
134 changes: 22 additions & 112 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ In the pubspec.yaml of your flutter project, add the following dependency:
```yaml
dependencies:
...
system_tray: ^0.0.9
system_tray: ^0.1.0
```
In your library add the following import:
Expand Down Expand Up @@ -77,6 +77,27 @@ sudo apt-get install appindicator3-0.1 libappindicator3-dev
</ul>
</td>
</tr>
<tr>
<td>setImage</td>
<td>Modify the tray image</td>
<td>✔️</td>
<td>✔️</td>
<td>✔️</td>
</tr>
<tr>
<td>setTooltip</td>
<td>Modify the tray tooltip</td>
<td>✔️</td>
<td>✔️</td>
<td>➖</td>
</tr>
<tr>
<td>setTitle / getTitle</td>
<td>Set / Get the tray title</td>
<td>➖</td>
<td>✔️</td>
<td>➖</td>
</tr>
<tr>
<td>setContextMenu</td>
<td>Set the tray context menu</td>
Expand Down Expand Up @@ -116,7 +137,6 @@ sudo apt-get install appindicator3-0.1 libappindicator3-dev
</table>

## Usage
Smallest example:

```dart
Future<void> initSystemTray() async {
Expand Down Expand Up @@ -151,116 +171,6 @@ Future<void> initSystemTray() async {
}
```

Flashing icon example:

```dart
Future<void> initSystemTray() async {
String path =
Platform.isWindows ? 'assets/app_icon.ico' : 'assets/app_icon.png';
final menu = [
MenuItem(label: 'Show', onClicked: _appWindow.show),
MenuItem(label: 'Hide', onClicked: _appWindow.hide),
MenuItem(
label: 'Start flash tray icon',
onClicked: () {
debugPrint("Start flash tray icon");
_timer ??= Timer.periodic(
const Duration(milliseconds: 500),
(timer) {
_toogleTrayIcon = !_toogleTrayIcon;
_systemTray.setSystemTrayInfo(
iconPath: _toogleTrayIcon ? "" : path,
);
},
);
},
),
MenuItem(
label: 'Stop flash tray icon',
onClicked: () {
debugPrint("Stop flash tray icon");
_timer?.cancel();
_timer = null;
_systemTray.setSystemTrayInfo(
iconPath: path,
);
},
),
MenuSeparator(),
SubMenu(
label: "Test API",
children: [
SubMenu(
label: "setSystemTrayInfo",
children: [
MenuItem(
label: 'set title',
onClicked: () {
final String text = WordPair.random().asPascalCase;
debugPrint("click 'set title' : $text");
_systemTray.setSystemTrayInfo(
title: text,
);
},
),
MenuItem(
label: 'set icon path',
onClicked: () {
debugPrint("click 'set icon path' : $path");
_systemTray.setSystemTrayInfo(
iconPath: path,
);
},
),
MenuItem(
label: 'set tooltip',
onClicked: () {
final String text = WordPair.random().asPascalCase;
debugPrint("click 'set tooltip' : $text");
_systemTray.setSystemTrayInfo(
toolTip: text,
);
},
),
],
),
MenuItem(label: 'disabled Item', enabled: false),
],
),
MenuSeparator(),
MenuItem(
label: 'Exit',
onClicked: _appWindow.close,
),
];
// We first init the systray menu and then add the menu entries
await _systemTray.initSystemTray(
title: "system tray",
iconPath: path,
toolTip: "How to use system tray with Flutter",
);
await _systemTray.setContextMenu(menu);
// handle system tray event
_systemTray.registerSystemTrayEventHandler((eventName) {
debugPrint("eventName: $eventName");
if (eventName == "leftMouseDown") {
} else if (eventName == "leftMouseUp") {
_systemTray.popUpContextMenu();
} else if (eventName == "rightMouseDown") {
} else if (eventName == "rightMouseUp") {
_appWindow.show();
}
});
}
```

# Addition

Recommended library that supports window control:
Expand Down
Binary file added example/assets/darts_icon.bmp
Binary file not shown.
Binary file added example/assets/darts_icon.ico
Binary file not shown.
Binary file added example/assets/darts_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/assets/gift_icon.ico
Binary file not shown.
Binary file added example/assets/gift_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 26 additions & 21 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:io';
import 'dart:math';

import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:english_words/english_words.dart';
Expand Down Expand Up @@ -51,6 +52,8 @@ class _MyAppState extends State<MyApp> {
String path =
Platform.isWindows ? 'assets/app_icon.ico' : 'assets/app_icon.png';

List<String> iconList = ['darts_icon', 'gift_icon'];

final menu = [
MenuItem(label: 'Show', onClicked: _appWindow.show),
MenuItem(label: 'Hide', onClicked: _appWindow.hide),
Expand All @@ -63,9 +66,7 @@ class _MyAppState extends State<MyApp> {
const Duration(milliseconds: 500),
(timer) {
_toogleTrayIcon = !_toogleTrayIcon;
_systemTray.setSystemTrayInfo(
iconPath: _toogleTrayIcon ? "" : path,
);
_systemTray.setImage(_toogleTrayIcon ? "" : path);
},
);
},
Expand All @@ -78,9 +79,7 @@ class _MyAppState extends State<MyApp> {
_timer?.cancel();
_timer = null;

_systemTray.setSystemTrayInfo(
iconPath: path,
);
_systemTray.setImage(path);
},
),
MenuSeparator(),
Expand All @@ -91,32 +90,38 @@ class _MyAppState extends State<MyApp> {
label: "setSystemTrayInfo",
children: [
MenuItem(
label: 'set title',
label: 'setTitle',
onClicked: () {
final String text = WordPair.random().asPascalCase;
debugPrint("click 'set title' : $text");
_systemTray.setSystemTrayInfo(
title: text,
);
debugPrint("click 'setTitle' : $text");
_systemTray.setTitle(text);
},
),
MenuItem(
label: 'set icon path',
label: 'setImage',
onClicked: () {
debugPrint("click 'set icon path' : $path");
_systemTray.setSystemTrayInfo(
iconPath: path,
);
String iconName = iconList[Random().nextInt(iconList.length)];
String path = Platform.isWindows
? 'assets/$iconName.ico'
: 'assets/$iconName.png';

debugPrint("click 'setImage' : $path");
_systemTray.setImage(path);
},
),
MenuItem(
label: 'set toolTip',
label: 'setToolTip',
onClicked: () {
final String text = WordPair.random().asPascalCase;
debugPrint("click 'set toolTip' : $text");
_systemTray.setSystemTrayInfo(
toolTip: text,
);
debugPrint("click 'setToolTip' : $text");
_systemTray.setToolTip(text);
},
),
MenuItem(
label: 'getTitle [macOS]',
onClicked: () async {
String title = await _systemTray.getTitle();
debugPrint("click 'getTitle' : $title");
},
),
],
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.9"
version: "0.1.0"
term_glyph:
dependency: transitive
description:
Expand Down
4 changes: 4 additions & 0 deletions lib/src/app_window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const String _kShowAppWindow = "ShowAppWindow";
const String _kHideAppWindow = "HideAppWindow";
const String _kCloseAppWindow = "CloseAppWindow";

/// Representation of native window
class AppWindow {
AppWindow() {
_platformChannel.setMethodCallHandler(_callbackHandler);
Expand All @@ -17,14 +18,17 @@ class AppWindow {

static const MethodChannel _platformChannel = MethodChannel(_kChannelName);

/// Show native window
Future<void> show() async {
await _platformChannel.invokeMethod(_kShowAppWindow);
}

/// Hide native window
Future<void> hide() async {
await _platformChannel.invokeMethod(_kHideAppWindow);
}

/// Close native window
Future<void> close() async {
await _platformChannel.invokeMethod(_kCloseAppWindow);
}
Expand Down
Loading

0 comments on commit a0aa59c

Please sign in to comment.