Skip to content

Commit

Permalink
Fix trash bug and allow dock file drops
Browse files Browse the repository at this point in the history
  • Loading branch information
blopker committed Nov 3, 2024
1 parent 816aebf commit 1bccf31
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 34 deletions.
4 changes: 2 additions & 2 deletions lib/compressor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void compressor(ImageFile imageFile, void Function(ImageFile) callback) {
if (sizeAfterOptimization.toDouble() / imageFile.size >
compressionThreshold) {
// delete the file if it's not smaller
trash(outFile);
await trash(outFile);
callback(imageFile.copyWith(
status: ImageFileStatus.unoptimized,
));
Expand All @@ -61,7 +61,7 @@ void compressor(ImageFile imageFile, void Function(ImageFile) callback) {
// Success!
if (!config.enablePostfix) {
// If postfix is disabled, replace the original file with the optimized one
trash(File(imageFile.path));
await trash(File(imageFile.path));
outFile.rename(replaceLast(outFile.path, config.postfix, ''));
}
callback(imageFile.copyWith(
Expand Down
17 changes: 17 additions & 0 deletions lib/dockdrop.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'dart:io';

import 'package:alic/filesystem.dart';
import 'package:alic/log.dart';
import 'package:flutter/services.dart';

registerDockDropHandler(Function(List<File>) onDrop) async {
const platform = MethodChannel('io.kbl.alic');
platform.setMethodCallHandler((call) async {
log.d(call);
if (call.method == 'openImage') {
var path = call.arguments as String;
var paths = await resolvePaths([path]);
onDrop(paths);
}
});
}
27 changes: 2 additions & 25 deletions lib/dropper.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:async';
import 'dart:io';

import 'package:alic/filesystem.dart';
import 'package:alic/log.dart';
import 'package:alic/imagefiles.dart';
import 'package:flutter/material.dart';
import 'package:super_clipboard/super_clipboard.dart';
import 'package:super_drag_and_drop/super_drag_and_drop.dart';
Expand Down Expand Up @@ -97,7 +97,7 @@ class _ImageDropRegionState extends State<ImageDropRegion> {
}
}
}
final paths = await _resolvePaths(mixedPaths);
final paths = await resolvePaths(mixedPaths);
widget.onDrop(paths);
},
child: Stack(children: [
Expand Down Expand Up @@ -140,26 +140,3 @@ Future<String?> _getValueFromItem(DropItem item) async {
});
return completer.future;
}

Future<List<File>> _resolvePaths(List<String> paths) async {
List<File> resolvedPaths = [];
for (var path in paths) {
if (path.endsWith('/')) {
resolvedPaths.addAll(await _getImagesFromDirectory(Directory(path)));
} else {
resolvedPaths.add(File(path));
}
}
return resolvedPaths;
}

// Return a list of images from a directory, recursively
Future<List<File>> _getImagesFromDirectory(Directory dir) async {
List<File> imageFiles = [];
await for (var entity in dir.list(recursive: true, followLinks: false)) {
if (entity is File && ImageFormats.isImage(entity.path)) {
imageFiles.add(entity);
}
}
return imageFiles;
}
27 changes: 27 additions & 0 deletions lib/filesystem.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'dart:io';

import 'package:alic/imagefiles.dart';

Future<List<File>> resolvePaths(List<String> paths) async {
List<File> resolvedPaths = [];
for (var path in paths) {
final dir = Directory(path);
final file = File(path);
if (await dir.exists()) {
resolvedPaths.addAll(await getImagesFromDirectory(dir));
} else if (await file.exists()) {
resolvedPaths.add(file);
}
}
return resolvedPaths;
}

// Return a list of images from a directory, recursively
Future<List<File>> getImagesFromDirectory(Directory dir) async {
return await dir
.list(recursive: true, followLinks: false)
.where((entity) => entity is File)
.map((entity) => entity as File)
.where((file) => ImageFormats.isImage(file.path))
.toList();
}
6 changes: 5 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:io';

import 'package:alic/dockdrop.dart';
import 'package:alic/dropper.dart';
import 'package:alic/imagefiles.dart';
import 'package:alic/settings.dart';
Expand All @@ -21,7 +22,6 @@ void main() async {
await RustLib.init();
await windowManager.ensureInitialized();
Config.init();

WindowOptions windowOptions = const WindowOptions(
minimumSize: Size(600, 400),
skipTaskbar: false,
Expand All @@ -30,6 +30,10 @@ void main() async {
await windowManager.show();
await windowManager.focus();
});
registerDockDropHandler((files) {
log.d('Dropped: $files');
ImageFiles.add(files);
});
runApp(const Alic());
}

Expand Down
2 changes: 1 addition & 1 deletion lib/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class _SettingsWidgetState extends State<SettingsWidget> {
return AlertDialog(
title: const Text('Reset Settings'),
content: const Text(
'Are you sure you want to reset all settings to default?'),
'Are you sure you want to reset all settings to the defaults?'),
actions: <Widget>[
TextButton(
child: const Text('Cancel'),
Expand Down
4 changes: 2 additions & 2 deletions lib/trash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class NativeFileManager {
}
}

trash(File file) {
NativeFileManager.trashItem(file.path);
trash(File file) async {
await NativeFileManager.trashItem(file.path);
}
21 changes: 18 additions & 3 deletions macos/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@ import FlutterMacOS

@main
class AppDelegate: FlutterAppDelegate {
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
override func application(_ application: NSApplication, open urls: [URL]) {
guard
let flutterViewController = mainFlutterWindow?.contentViewController
as? FlutterViewController
else {
return
}

let channel = FlutterMethodChannel(
name: "io.kbl.alic", binaryMessenger: flutterViewController.engine.binaryMessenger)

for url in urls {
channel.invokeMethod("openImage", arguments: url.path)
}
}
}
26 changes: 26 additions & 0 deletions macos/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,31 @@
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>

<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>My File Type</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>public.image</string>
<string>public.folder</string>
</array>
<key>CFBundleTypeExtensions</key>
<array>
<string>jpg</string>
<string>jpeg</string>
<string>png</string>
<string>gif</string>
<string>webp</string>
<string>tiff</string>
</array>
</dict>
</array>
</dict>
</plist>

0 comments on commit 1bccf31

Please sign in to comment.