Skip to content

Commit

Permalink
- Added setIconPosition to MacOS. (#25)
Browse files Browse the repository at this point in the history
- Added optional position on SetIcon, default is left (MacOS)
  • Loading branch information
rafaelortizzableh authored Aug 27, 2022
1 parent d5e56fb commit 0816f5e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
14 changes: 14 additions & 0 deletions lib/src/tray_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const kEventOnTrayIconRightMouseDown = 'onTrayIconRightMouseDown';
const kEventOnTrayIconRightMouseUp = 'onTrayIconRightMouseUp';
const kEventOnTrayMenuItemClick = 'onTrayMenuItemClick';

enum TrayIconPositon { left, right }

class TrayManager {
TrayManager._() {
_channel.setMethodCallHandler(_methodCallHandler);
Expand Down Expand Up @@ -91,6 +93,7 @@ class TrayManager {
Future<void> setIcon(
String iconPath, {
bool isTemplate = false, // macOS only
TrayIconPositon iconPosition = TrayIconPositon.left, // macOS only
}) async {
ByteData imageData = await rootBundle.load(iconPath);
String base64Icon = base64Encode(imageData.buffer.asUint8List());
Expand All @@ -104,10 +107,21 @@ class TrayManager {
]),
'base64Icon': base64Icon,
'isTemplate': isTemplate,
'iconPosition': iconPosition.name,
};
await _channel.invokeMethod('setIcon', arguments);
}

/// Sets the icon position of the tray icon.
///
/// @platforms macos
Future<void> setIconPosition(TrayIconPositon trayIconPositon) async {
final arguments = <String, dynamic>{
'iconPosition': trayIconPositon.name,
};
await _channel.invokeMethod('setIconPosition', arguments);
}

/// Sets the hover text for this tray icon.
Future<void> setToolTip(String toolTip) async {
final Map<String, dynamic> arguments = {
Expand Down
13 changes: 11 additions & 2 deletions macos/Classes/TrayIcon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@ public class TrayIcon: NSView {
fatalError("init(coder:) has not been implemented")
}

public func setImage(_ image: NSImage) {
public func setImage(_ image: NSImage, _ imagePosition: String) {
if let button = statusItem?.button {
button.image = image
button.imagePosition = NSControl.ImagePosition.imageLeft
setImagePosition(imagePosition)
}


self.frame = statusItem!.button!.frame
}

public func setImagePosition(_ imagePosition: String) {
if let button = statusItem?.button {
button.imagePosition = imagePosition == "right" ? NSControl.ImagePosition.imageRight : NSControl.ImagePosition.imageLeft
}
self.frame = statusItem!.button!.frame
}
Expand Down
15 changes: 14 additions & 1 deletion macos/Classes/TrayManagerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class TrayManagerPlugin: NSObject, FlutterPlugin, NSMenuDelegate {
case "setIcon":
setIcon(call, result: result)
break
case "setIconPosition":
setIconPosition(call, result: result)
break
case "setToolTip":
setToolTip(call, result: result)
break
Expand Down Expand Up @@ -131,6 +134,7 @@ public class TrayManagerPlugin: NSObject, FlutterPlugin, NSMenuDelegate {
let args:[String: Any] = call.arguments as! [String: Any]
let base64Icon: String = args["base64Icon"] as! String;
let isTemplate: Bool = args["isTemplate"] as! Bool;
let iconPosition: String = args["iconPosition"] as! String;

let imageData = Data(base64Encoded: base64Icon, options: .ignoreUnknownCharacters)
let image = NSImage(data: imageData!)
Expand All @@ -153,11 +157,20 @@ public class TrayManagerPlugin: NSObject, FlutterPlugin, NSMenuDelegate {
}
}

trayIcon?.setImage(image!)
trayIcon?.setImage(image!, iconPosition)

result(true)
}

public func setIconPosition(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
let args:[String: Any] = call.arguments as! [String: Any]
let iconPosition: String = args["iconPosition"] as! String;

trayIcon?.setImagePosition(iconPosition)

result(true)
}

public func setToolTip(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
let args:[String: Any] = call.arguments as! [String: Any]
let toolTip: String = args["toolTip"] as! String;
Expand Down

0 comments on commit 0816f5e

Please sign in to comment.