💄 Better paste & drag 'n drop handling
This commit is contained in:
		| @@ -31,16 +31,23 @@ Future<String> calculateFileSha256(File file) async { | ||||
|   return await calculateBytesSha256(bytes); | ||||
| } | ||||
|  | ||||
| Future<double> calculateDataAspectRatio(Uint8List data) async { | ||||
|   if (PlatformInfo.isWeb) { | ||||
|     return 1; | ||||
|   } | ||||
|   final decoder = await Isolate.run(() => img.findDecoderForData(data)); | ||||
|   if (decoder == null) return 1; | ||||
|   final image = await Isolate.run(() => decoder.decode(data)); | ||||
|   if (image == null) return 1; | ||||
|   return image.width / image.height; | ||||
| } | ||||
|  | ||||
| Future<double> calculateFileAspectRatio(File file) async { | ||||
|   if (PlatformInfo.isWeb) { | ||||
|     return 1; | ||||
|   } | ||||
|   final bytes = await Isolate.run(() => file.readAsBytesSync()); | ||||
|   final decoder = await Isolate.run(() => img.findDecoderForData(bytes)); | ||||
|   if (decoder == null) return 1; | ||||
|   final image = await Isolate.run(() => decoder.decode(bytes)); | ||||
|   if (image == null) return 1; | ||||
|   return image.width / image.height; | ||||
|   return await calculateDataAspectRatio(bytes); | ||||
| } | ||||
|  | ||||
| class AttachmentProvider extends GetConnect { | ||||
|   | ||||
| @@ -82,19 +82,27 @@ class AccountHeadingWidget extends StatelessWidget { | ||||
|                 crossAxisAlignment: CrossAxisAlignment.baseline, | ||||
|                 textBaseline: TextBaseline.alphabetic, | ||||
|                 children: [ | ||||
|                   Text( | ||||
|                   Flexible( | ||||
|                     child: Text( | ||||
|                       nick, | ||||
|                       maxLines: 1, | ||||
|                       overflow: TextOverflow.ellipsis, | ||||
|                       style: const TextStyle( | ||||
|                         fontSize: 17, | ||||
|                         fontWeight: FontWeight.bold, | ||||
|                       ), | ||||
|                     ).paddingOnly(right: 4), | ||||
|                   Text( | ||||
|                   ), | ||||
|                   Flexible( | ||||
|                     child: Text( | ||||
|                       '@$name', | ||||
|                       maxLines: 1, | ||||
|                       overflow: TextOverflow.ellipsis, | ||||
|                       style: const TextStyle( | ||||
|                         fontSize: 15, | ||||
|                       ), | ||||
|                     ), | ||||
|                   ), | ||||
|                 ], | ||||
|               ), | ||||
|               if (status != null) | ||||
|   | ||||
| @@ -158,30 +158,44 @@ class _AttachmentPublishPopupState extends State<AttachmentPublishPopup> { | ||||
|     final clipboard = SystemClipboard.instance; | ||||
|     if (clipboard == null) return; | ||||
|     final reader = await clipboard.read(); | ||||
|     for (final format in Formats.standardFormats.whereType<FileFormat>()) { | ||||
|       if (reader.canProvide(format)) { | ||||
|         reader.getFile(format, (file) async { | ||||
|           final data = await file.readAll(); | ||||
|           await uploadAttachment( | ||||
|             data, | ||||
|             file.fileName ?? 'unknown', | ||||
|             await calculateBytesSha256(data), | ||||
|           ); | ||||
|         }); | ||||
|       } | ||||
|     } | ||||
|     handleNativeReader(reader); | ||||
|   } | ||||
|  | ||||
|   void handlePasteFile(ClipboardReadEvent event) async { | ||||
|   void handlePasteEvent(ClipboardReadEvent event) async { | ||||
|     final reader = await event.getClipboardReader(); | ||||
|     for (final format in Formats.standardFormats.whereType<FileFormat>()) { | ||||
|     handleNativeReader(reader); | ||||
|   } | ||||
|  | ||||
|   void handleNativeReader(DataReader reader) async { | ||||
|     var read = false; | ||||
|     for (final format in Formats.standardFormats | ||||
|         .whereType<FileFormat>() | ||||
|         .where((x) => ![Formats.tiff].contains(x))) { | ||||
|       if (read) break; | ||||
|       if (reader.canProvide(format)) { | ||||
|         reader.getFile(format, (file) async { | ||||
|           if (read) return; | ||||
|  | ||||
|           final data = await file.readAll(); | ||||
|           read = true; | ||||
|  | ||||
|           // Calculate ratio if available | ||||
|           double? ratio; | ||||
|           if ([ | ||||
|             Formats.png, | ||||
|             Formats.jpeg, | ||||
|             Formats.gif, | ||||
|             Formats.ico, | ||||
|             Formats.webp | ||||
|           ].contains(format)) { | ||||
|             ratio = await calculateDataAspectRatio(data); | ||||
|           } | ||||
|  | ||||
|           await uploadAttachment( | ||||
|             data, | ||||
|             file.fileName ?? 'unknown', | ||||
|             await calculateBytesSha256(data), | ||||
|             ratio: ratio, | ||||
|           ); | ||||
|         }); | ||||
|       } | ||||
| @@ -262,13 +276,13 @@ class _AttachmentPublishPopupState extends State<AttachmentPublishPopup> { | ||||
|   void initState() { | ||||
|     super.initState(); | ||||
|     revertMetadataList(); | ||||
|     ClipboardEvents.instance?.registerPasteEventListener(handlePasteFile); | ||||
|     ClipboardEvents.instance?.registerPasteEventListener(handlePasteEvent); | ||||
|   } | ||||
|  | ||||
|   @override | ||||
|   void dispose() { | ||||
|     super.dispose(); | ||||
|     ClipboardEvents.instance?.unregisterPasteEventListener(handlePasteFile); | ||||
|     ClipboardEvents.instance?.unregisterPasteEventListener(handlePasteEvent); | ||||
|   } | ||||
|  | ||||
|   @override | ||||
| @@ -291,19 +305,7 @@ class _AttachmentPublishPopupState extends State<AttachmentPublishPopup> { | ||||
|           onPerformDrop: (event) async { | ||||
|             for (final item in event.session.items) { | ||||
|               final reader = item.dataReader!; | ||||
|               for (final format | ||||
|                   in Formats.standardFormats.whereType<FileFormat>()) { | ||||
|                 if (reader.canProvide(format)) { | ||||
|                   reader.getFile(format, (file) async { | ||||
|                     final data = await file.readAll(); | ||||
|                     await uploadAttachment( | ||||
|                       data, | ||||
|                       file.fileName ?? 'unknown', | ||||
|                       await calculateBytesSha256(data), | ||||
|                     ); | ||||
|                   }); | ||||
|                 } | ||||
|               } | ||||
|               handleNativeReader(reader); | ||||
|             } | ||||
|           }, | ||||
|           child: Column( | ||||
|   | ||||
		Reference in New Issue
	
	Block a user