From e2c4567ed0bc84b6a9487722c4d3058bd1b692af Mon Sep 17 00:00:00 2001 From: Li Wei Date: Mon, 3 Jul 2023 19:02:08 +0800 Subject: [PATCH] fix: macos directory open (#127) --- ui/flutter/lib/util/file_explorer.dart | 48 ++++++++++++++++---------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/ui/flutter/lib/util/file_explorer.dart b/ui/flutter/lib/util/file_explorer.dart index 61f14f24..e0ff2b35 100644 --- a/ui/flutter/lib/util/file_explorer.dart +++ b/ui/flutter/lib/util/file_explorer.dart @@ -2,27 +2,37 @@ import 'dart:io'; class FileExplorer { static Future openAndSelectFile(String filePath) async { - if (!await FileSystemEntity.isDirectory(filePath) && - !await FileSystemEntity.isFile(filePath)) { - return; + if (await FileSystemEntity.isFile(filePath)) { + _openFile(filePath); + } else if (await FileSystemEntity.isDirectory(filePath)) { + _openDirectory(filePath); } - if (await FileSystemEntity.isDirectory(filePath)) { - if (Platform.isWindows) { - Process.run('explorer.exe', [filePath]); - } else if (Platform.isMacOS || Platform.isLinux) { - Process.run('xdg-open', [filePath]); + } + + static Future _openDirectory(String directoryPath) async { + if (Platform.isWindows) { + Process.run('explorer.exe', [directoryPath]); + } else if (Platform.isMacOS) { + Process.run('open', [directoryPath]); + } else if (Platform.isLinux) { + if (await _isUbuntuOrDebian()) { + Process.run('xdg-open', [directoryPath]); + } else if (await _isCentOS()) { + Process.run('nautilus', [directoryPath]); } - } else if (await FileSystemEntity.isFile(filePath)) { - if (Platform.isWindows) { - Process.run('explorer.exe', ['/select,', filePath]); - } else if (Platform.isMacOS) { - Process.run('open', ['-R', filePath]); - } else if (Platform.isLinux) { - if (await _isUbuntuOrDebian()) { - Process.run('xdg-open', [filePath]); - } else if (await _isCentOS()) { - Process.run('nautilus', ['--select', filePath]); - } + } + } + + static Future _openFile(String filePath) async { + if (Platform.isWindows) { + Process.run('explorer.exe', ['/select,', filePath]); + } else if (Platform.isMacOS) { + Process.run('open', ['-R', filePath]); + } else if (Platform.isLinux) { + if (await _isUbuntuOrDebian()) { + Process.run('xdg-open', [filePath]); + } else if (await _isCentOS()) { + Process.run('nautilus', ['--select', filePath]); } } }