Skip to content

An open-source cross-platform alternative to AirDrop

feat(macos): add drag-and-drop support for menubar icon (#1716)

Shlomob5ed1e642d0d
diff --git a/app/lib/util/native/tray_helper.dart b/app/lib/util/native/tray_helper.dart
index b18bd779..6f12e24e 100644
--- a/app/lib/util/native/tray_helper.dart
+++ b/app/lib/util/native/tray_helper.dart
@@ -23,7 +23,8 @@ Future<void> initTray() async {
if (checkPlatform([TargetPlatform.windows])) {
await tm.trayManager.setIcon(Assets.img.logo);
} else if (checkPlatform([TargetPlatform.macOS])) {
- await tm.trayManager.setIcon(Assets.img.logo32Black.path, isTemplate: true);
+ // The menu bar icon will created in AppDelegate.swift
+ return;
} else if (checkPlatform([TargetPlatform.linux])) {
await tm.trayManager.setIcon(Assets.img.logo32White.path);
} else {
diff --git a/app/macos/Flutter/GeneratedPluginRegistrant.swift b/app/macos/Flutter/GeneratedPluginRegistrant.swift
index 1ca43798..f7fd37ef 100644
--- a/app/macos/Flutter/GeneratedPluginRegistrant.swift
+++ b/app/macos/Flutter/GeneratedPluginRegistrant.swift
@@ -27,7 +27,7 @@ import wakelock_plus
import window_manager
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
- ConnectivityPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlugin"))
+ ConnectivityPlusPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlusPlugin"))
DesktopDropPlugin.register(with: registry.registrar(forPlugin: "DesktopDropPlugin"))
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
DynamicColorPlugin.register(with: registry.registrar(forPlugin: "DynamicColorPlugin"))
diff --git a/app/macos/Podfile.lock b/app/macos/Podfile.lock
index 13230c47..0349ba8a 100644
--- a/app/macos/Podfile.lock
+++ b/app/macos/Podfile.lock
@@ -1,7 +1,7 @@
PODS:
- connectivity_plus (0.0.1):
+ - Flutter
- FlutterMacOS
- - ReachabilitySwift
- desktop_drop (0.0.1):
- FlutterMacOS
- device_info_plus (0.0.1):
@@ -29,7 +29,6 @@ PODS:
- photo_manager (2.0.0):
- Flutter
- FlutterMacOS
- - ReachabilitySwift (5.2.3)
- screen_retriever (0.0.1):
- FlutterMacOS
- shared_preferences_foundation (0.0.1):
@@ -50,7 +49,7 @@ PODS:
- FlutterMacOS
DEPENDENCIES:
- - connectivity_plus (from `Flutter/ephemeral/.symlinks/plugins/connectivity_plus/macos`)
+ - connectivity_plus (from `Flutter/ephemeral/.symlinks/plugins/connectivity_plus/darwin`)
- desktop_drop (from `Flutter/ephemeral/.symlinks/plugins/desktop_drop/macos`)
- device_info_plus (from `Flutter/ephemeral/.symlinks/plugins/device_info_plus/macos`)
- dynamic_color (from `Flutter/ephemeral/.symlinks/plugins/dynamic_color/macos`)
@@ -72,13 +71,9 @@ DEPENDENCIES:
- wakelock_plus (from `Flutter/ephemeral/.symlinks/plugins/wakelock_plus/macos`)
- window_manager (from `Flutter/ephemeral/.symlinks/plugins/window_manager/macos`)
-SPEC REPOS:
- trunk:
- - ReachabilitySwift
-
EXTERNAL SOURCES:
connectivity_plus:
- :path: Flutter/ephemeral/.symlinks/plugins/connectivity_plus/macos
+ :path: Flutter/ephemeral/.symlinks/plugins/connectivity_plus/darwin
desktop_drop:
:path: Flutter/ephemeral/.symlinks/plugins/desktop_drop/macos
device_info_plus:
@@ -121,26 +116,25 @@ EXTERNAL SOURCES:
:path: Flutter/ephemeral/.symlinks/plugins/window_manager/macos
SPEC CHECKSUMS:
- connectivity_plus: 18d3c32514c886e046de60e9c13895109866c747
+ connectivity_plus: ddd7f30999e1faaef5967c23d5b6d503d10434db
desktop_drop: 69eeff437544aa619c8db7f4481b3a65f7696898
- device_info_plus: 5401765fde0b8d062a2f8eb65510fb17e77cf07f
+ device_info_plus: ce1b7762849d3ec103d0e0517299f2db7ad60720
dynamic_color: 2eaa27267de1ca20d879fbd6e01259773fb1670f
file_selector_macos: 54fdab7caa3ac3fc43c9fac4d7d8d231277f8cf2
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
gal: 61e868295d28fe67ffa297fae6dacebf56fd53e1
- in_app_purchase_storekit: 9e9931234f0adcf71ae323f8c83785b96030edf1
+ in_app_purchase_storekit: 8c3b0b3eb1b0f04efbff401c3de6266d4258d433
network_info_plus: aeb9c4ed699cae128bc94b9d0f04f2389a414cbb
- package_info_plus: 02d7a575e80f194102bef286361c6c326e4c29ce
+ package_info_plus: fa739dd842b393193c5ca93c26798dff6e3d0e0c
pasteboard: 9b69dba6fedbb04866be632205d532fe2f6b1d99
- path_provider_foundation: 3784922295ac71e43754bd15e0653ccfd36a147c
- photo_manager: 4f6810b7dfc4feb03b461ac1a70dacf91fba7604
- ReachabilitySwift: 7f151ff156cea1481a8411701195ac6a984f4979
+ path_provider_foundation: 2b6b4c569c0fb62ec74538f866245ac84301af46
+ photo_manager: ff695c7a1dd5bc379974953a2b5c0a293f7c4c8a
screen_retriever: 59634572a57080243dd1bf715e55b6c54f241a38
- shared_preferences_foundation: b4c3b4cddf1c21f02770737f147a3f5da9d39695
+ shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78
tray_manager: 9064e219c56d75c476e46b9a21182087930baf90
uri_content: bfedc2e58f0ee4de88a63e5bcb826398389b22cb
url_launcher_macos: 5f437abeda8c85500ceb03f5c1938a8c5a705399
- video_player_avfoundation: e9e6f9cae7d7a6d9b43519b0aab382bca60fcfd1
+ video_player_avfoundation: 7c6c11d8470e1675df7397027218274b6d2360b3
wakelock_plus: 4783562c9a43d209c458cb9b30692134af456269
window_manager: 3a1844359a6295ab1e47659b1a777e36773cd6e8
diff --git a/app/macos/Runner/AppDelegate.swift b/app/macos/Runner/AppDelegate.swift
index d6078dc4..636f4e26 100644
--- a/app/macos/Runner/AppDelegate.swift
+++ b/app/macos/Runner/AppDelegate.swift
@@ -3,44 +3,122 @@ import FlutterMacOS
@NSApplicationMain
class AppDelegate: FlutterAppDelegate {
- override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
- // LocalSend handles the close event manually
- return false
- }
-
- // START: method channel logic
- private var channel: FlutterMethodChannel?
- private var cachedFiles: [String]? = []
-
- override func applicationDidFinishLaunching(_ notification: Notification) {
- let controller = mainFlutterWindow?.contentViewController as! FlutterViewController
- channel = FlutterMethodChannel(name: "main-delegate-channel", binaryMessenger: controller.engine.binaryMessenger)
- channel?.setMethodCallHandler(handle)
- }
-
- public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
- switch call.method {
- case "getFiles":
- result(cachedFiles ?? [])
- cachedFiles = nil // files has been fetched, no need to cache anymore
- default:
- result(FlutterMethodNotImplemented)
- }
+ override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
+ // LocalSend handles the close event manually
+ return false
}
+
+ private var statusItem: NSStatusItem?
+ private var channel: FlutterMethodChannel?
+ private var cachedFiles: [String]? = []
+
+ override func applicationDidFinishLaunching(_ notification: Notification) {
+ let controller = mainFlutterWindow?.contentViewController as! FlutterViewController
+ channel = FlutterMethodChannel(name: "main-delegate-channel", binaryMessenger: controller.engine.binaryMessenger)
+ channel?.setMethodCallHandler(handle)
+
+ setupStatusBarItem()
+ }
+
+ private func setupStatusBarItem() {
+ statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
+ if let button = statusItem?.button {
+ let image = NSImage(named: "StatusBarItemIcon")
+ image!.size = NSSize(width: 18, height: 18)
+ image!.isTemplate = true
+ button.image = image
+
+ let menu = NSMenu()
+
+ let openItem = NSMenuItem(title: "Open", action: #selector(openApp), keyEquivalent: "o")
+ menu.addItem(openItem)
+
+ let quitItem = NSMenuItem(title: "Quit LocalSend", action: #selector(quitApp), keyEquivalent: "q")
+ menu.addItem(quitItem)
+
+ statusItem?.menu = menu
+
+ let dragView = FileDropView(frame: button.bounds)
+ dragView.appDelegate = self
+ button.addSubview(dragView)
+
+ dragView.translatesAutoresizingMaskIntoConstraints = false
+ NSLayoutConstraint.activate([
+ dragView.topAnchor.constraint(equalTo: button.topAnchor),
+ dragView.leadingAnchor.constraint(equalTo: button.leadingAnchor),
+ dragView.trailingAnchor.constraint(equalTo: button.trailingAnchor),
+ dragView.bottomAnchor.constraint(equalTo: button.bottomAnchor)
+ ])
+ }
+ }
+
+ @objc func openApp() {
+ NSApp.activate(ignoringOtherApps: true)
+ mainFlutterWindow?.makeKeyAndOrderFront(nil)
+ }
+
+ @objc private func quitApp() {
+ NSApp.terminate(nil)
+ }
+
+ func sendFilesToFlutter(_ filenames: [String]) {
+ cachedFiles?.append(contentsOf: filenames)
+ channel?.invokeMethod("onFiles", arguments: filenames)
+ }
+
+ // START: handle opened files
+ private func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
+ switch call.method {
+ case "getFiles":
+ result(cachedFiles ?? [])
+ cachedFiles = nil // files has been fetched, no need to cache anymore
+ default:
+ result(FlutterMethodNotImplemented)
+ }
+ }
+
+ override func application(_ sender: NSApplication, openFile filename: String) -> Bool {
+ sendFilesToFlutter([filename])
+ return true
+ }
+
+ override func application(_ sender: NSApplication, openFiles filenames: [String]) {
+ sendFilesToFlutter(filenames)
+ }
+ // END: handle opened files
+}
- // END: method channel logic
-
- // START: handle opened files
-
- override func application(_ sender: NSApplication, openFile filename: String) -> Bool {
- cachedFiles?.append(filename)
- channel?.invokeMethod("onFiles", arguments: [filename])
- return true
- }
-
- override func application(_ sender: NSApplication, openFiles filenames: [String]) {
- cachedFiles?.append(contentsOf: filenames)
- channel?.invokeMethod("onFiles", arguments: filenames)
- }
- // END: handle opened files
+class FileDropView: NSView {
+ weak var appDelegate: AppDelegate?
+
+ override init(frame frameRect: NSRect) {
+ super.init(frame: frameRect)
+ setup()
+ }
+
+ required init?(coder: NSCoder) {
+ super.init(coder: coder)
+ setup()
+ }
+
+ private func setup() {
+ registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL])
+ }
+
+ override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
+ return .copy
+ }
+
+ override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
+ let pasteboard = sender.draggingPasteboard
+
+ if let fileUrls = pasteboard.readObjects(forClasses: [NSURL.self], options: [.urlReadingFileURLsOnly: true]) as? [URL] {
+ let filenames = fileUrls.map { $0.path }
+ appDelegate?.sendFilesToFlutter(filenames)
+ appDelegate?.openApp()
+ return true
+ }
+
+ return false
+ }
}
diff --git a/app/macos/Runner/Assets.xcassets/Contents.json b/app/macos/Runner/Assets.xcassets/Contents.json
new file mode 100644
index 00000000..73c00596
--- /dev/null
+++ b/app/macos/Runner/Assets.xcassets/Contents.json
@@ -0,0 +1,6 @@
+{
+ "info" : {
+ "author" : "xcode",
+ "version" : 1
+ }
+}
diff --git a/app/macos/Runner/Assets.xcassets/StatusBarItemIcon.imageset/Contents.json b/app/macos/Runner/Assets.xcassets/StatusBarItemIcon.imageset/Contents.json
new file mode 100644
index 00000000..5a3a4f74
--- /dev/null
+++ b/app/macos/Runner/Assets.xcassets/StatusBarItemIcon.imageset/Contents.json
@@ -0,0 +1,21 @@
+{
+ "images" : [
+ {
+ "filename" : "logo-32-black.png",
+ "idiom" : "universal",
+ "scale" : "1x"
+ },
+ {
+ "idiom" : "universal",
+ "scale" : "2x"
+ },
+ {
+ "idiom" : "universal",
+ "scale" : "3x"
+ }
+ ],
+ "info" : {
+ "author" : "xcode",
+ "version" : 1
+ }
+}
diff --git a/app/macos/Runner/Assets.xcassets/StatusBarItemIcon.imageset/logo-32-black.png b/app/macos/Runner/Assets.xcassets/StatusBarItemIcon.imageset/logo-32-black.png
new file mode 100644
index 00000000..871e5c93
Binary files /dev/null and b/app/macos/Runner/Assets.xcassets/StatusBarItemIcon.imageset/logo-32-black.png differ