Skip to content

Fix: Allow developers to enable or disable drag-and-drop visual feedback. #359

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 14 additions & 15 deletions packages/desktop_drop/lib/src/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,18 @@ class DesktopDrop {
});
}

Future<bool> startAccessingSecurityScopedResource(
{required Uint8List bookmark}) async {
Future<bool> startAccessingSecurityScopedResource({required Uint8List bookmark}) async {
Map<String, dynamic> resultMap = Map();
resultMap["apple-bookmark"] = bookmark;
final bool? result = await _channel.invokeMethod(
"startAccessingSecurityScopedResource", resultMap);
final bool? result = await _channel.invokeMethod("startAccessingSecurityScopedResource", resultMap);
if (result == null) return false;
return result;
}

Future<bool> stopAccessingSecurityScopedResource(
{required Uint8List bookmark}) async {
Future<bool> stopAccessingSecurityScopedResource({required Uint8List bookmark}) async {
Map<String, dynamic> resultMap = Map();
resultMap["apple-bookmark"] = bookmark;
final bool result = await _channel.invokeMethod(
"stopAccessingSecurityScopedResource", resultMap);
final bool result = await _channel.invokeMethod("stopAccessingSecurityScopedResource", resultMap);
return result;
}

Expand Down Expand Up @@ -108,8 +104,7 @@ class DesktopDrop {
case "performOperation_linux":
// gtk notify 'exit' before 'performOperation'.
final text = (call.arguments as List<dynamic>)[0] as String;
final offset = ((call.arguments as List<dynamic>)[1] as List<dynamic>)
.cast<double>();
final offset = ((call.arguments as List<dynamic>)[1] as List<dynamic>).cast<double>();
final paths = const LineSplitter().convert(text).map((e) {
try {
return Uri.tryParse(e)?.toFilePath() ?? '';
Expand All @@ -124,11 +119,7 @@ class DesktopDrop {
));
break;
case "performOperation_web":
final results = (call.arguments as List)
.cast<Map>()
.map((e) => WebDropItem.fromJson(e.cast<String, dynamic>()))
.map((e) => e.toDropItem())
.toList();
final results = (call.arguments as List).cast<Map>().map((e) => WebDropItem.fromJson(e.cast<String, dynamic>())).map((e) => e.toDropItem()).toList();
_notifyEvent(
DropDoneEvent(location: _offset ?? Offset.zero, files: results),
);
Expand All @@ -154,4 +145,12 @@ class DesktopDrop {
assert(_listeners.contains(listener));
_listeners.remove(listener);
}

void enableDragDropVisualFeedback() {
_channel.invokeMethod('enable', {});
}

void disableDragDropVisualFeedback() {
_channel.invokeMethod('disable', {});
}
}
15 changes: 14 additions & 1 deletion packages/desktop_drop/macos/Classes/DesktopDropPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,22 @@ public class DesktopDropPlugin: NSObject, FlutterPlugin {
d.registerForDraggedTypes(NSFilePromiseReceiver.readableDraggedTypes.map { NSPasteboard.PasteboardType($0) })
d.registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL])

vc.view.addSubview(d)
// If the DropTarget be added from here, drag-and-drop visual feedback will be implemented immediately.
// This will causes the cursor to change to `+` shape and appear as if files can be dropped, even though the flutter code does not include a DropTarget widget.
// vc.view.addSubview(d)

registrar.addMethodCallDelegate(instance, channel: channel)

// Instead, it should be added only when needed and removed otherwise.
// `channel.setMethodCallHandler` should be implemented after `registrar.addMethodCallDelegate`
channel.setMethodCallHandler { call, result in
if call.method == "enable"{
vc.view.addSubview(d)
}else if (call.method == "disable"){
d.removeFromSuperview()
}
result(nil)
}
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult){
Expand Down