Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SetIconPosition (MacOS) #25

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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