From b265d25dcb1ff3709d5f530f0184c6e4058523df Mon Sep 17 00:00:00 2001 From: csf Date: Mon, 17 Oct 2022 23:07:40 +0900 Subject: [PATCH] desktop file transfer shift + click multi selection --- .../lib/desktop/pages/file_manager_page.dart | 66 ++++++++++--------- .../lib/mobile/pages/file_manager_page.dart | 47 ------------- flutter/lib/models/file_model.dart | 46 ++++++++++++- 3 files changed, 81 insertions(+), 78 deletions(-) diff --git a/flutter/lib/desktop/pages/file_manager_page.dart b/flutter/lib/desktop/pages/file_manager_page.dart index 6c2e20e78..3ffca3f91 100644 --- a/flutter/lib/desktop/pages/file_manager_page.dart +++ b/flutter/lib/desktop/pages/file_manager_page.dart @@ -6,7 +6,6 @@ import 'package:desktop_drop/desktop_drop.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_breadcrumb/flutter_breadcrumb.dart'; -import 'package:flutter_hbb/mobile/pages/file_manager_page.dart'; import 'package:flutter_hbb/models/file_model.dart'; import 'package:get/get.dart'; import 'package:provider/provider.dart'; @@ -273,21 +272,8 @@ class _FileManagerPageState extends State return DataRow( key: ValueKey(entry.name), onSelectChanged: (s) { - final isCtrlDown = RawKeyboard.instance.keysPressed - .contains(LogicalKeyboardKey.controlLeft); - final items = getSelectedItem(isLocal); - if (isCtrlDown) { - if (s != null) { - if (s) { - items.add(isLocal, entry); - } else { - items.remove(entry); - } - } - } else { - items.clear(); - items.add(isLocal, entry); - } + _onSelectedChanged(getSelectedItem(isLocal), filteredEntries, + entry, isLocal); setState(() {}); }, selected: getSelectedItem(isLocal).contains(entry), @@ -321,20 +307,8 @@ class _FileManagerPageState extends State items.clear(); return; } - - final isCtrlDown = RawKeyboard.instance.keysPressed - .contains(LogicalKeyboardKey.controlLeft); - if (isCtrlDown) { - if (items.contains(entry)) { - items.remove(entry); - } else { - items.add(isLocal, entry); - } - } else { - items.clear(); - items.add(isLocal, entry); - } - setState(() {}); + _onSelectedChanged( + items, filteredEntries, entry, isLocal); }, ), DataCell(FittedBox( @@ -362,6 +336,38 @@ class _FileManagerPageState extends State ); } + void _onSelectedChanged(SelectedItems selectedItems, List entries, + Entry entry, bool isLocal) { + final isCtrlDown = RawKeyboard.instance.keysPressed + .contains(LogicalKeyboardKey.controlLeft); + final isShiftDown = + RawKeyboard.instance.keysPressed.contains(LogicalKeyboardKey.shiftLeft); + if (isCtrlDown) { + if (selectedItems.contains(entry)) { + selectedItems.remove(entry); + } else { + selectedItems.add(isLocal, entry); + } + } else if (isShiftDown) { + final List indexGroup = []; + for (var selected in selectedItems.items) { + indexGroup.add(entries.indexOf(selected)); + } + indexGroup.add(entries.indexOf(entry)); + indexGroup.removeWhere((e) => e == -1); + final maxIndex = indexGroup.reduce(max); + final minIndex = indexGroup.reduce(min); + selectedItems.clear(); + entries + .getRange(minIndex, maxIndex + 1) + .forEach((e) => selectedItems.add(isLocal, e)); + } else { + selectedItems.clear(); + selectedItems.add(isLocal, entry); + } + setState(() {}); + } + bool _checkDoubleClick(Entry entry) { final current = DateTime.now().millisecondsSinceEpoch; final elapsed = current - _lastClickTime; diff --git a/flutter/lib/mobile/pages/file_manager_page.dart b/flutter/lib/mobile/pages/file_manager_page.dart index ce270ffea..0ee0e0f8d 100644 --- a/flutter/lib/mobile/pages/file_manager_page.dart +++ b/flutter/lib/mobile/pages/file_manager_page.dart @@ -555,50 +555,3 @@ class BottomSheetBody extends StatelessWidget { ); } } - -class SelectedItems { - bool? _isLocal; - final List _items = []; - - List get items => _items; - - int get length => _items.length; - - bool? get isLocal => _isLocal; - - add(bool isLocal, Entry e) { - if (_isLocal == null) { - _isLocal = isLocal; - } - if (_isLocal != null && _isLocal != isLocal) { - return; - } - if (!_items.contains(e)) { - _items.add(e); - } - } - - bool contains(Entry e) { - return _items.contains(e); - } - - remove(Entry e) { - _items.remove(e); - if (_items.length == 0) { - _isLocal = null; - } - } - - bool isOtherPage(bool currentIsLocal) { - if (_isLocal == null) { - return false; - } else { - return _isLocal != currentIsLocal; - } - } - - clear() { - _items.clear(); - _isLocal = null; - } -} diff --git a/flutter/lib/models/file_model.dart b/flutter/lib/models/file_model.dart index 1ba66b864..8fb29bd41 100644 --- a/flutter/lib/models/file_model.dart +++ b/flutter/lib/models/file_model.dart @@ -3,7 +3,6 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter_hbb/common.dart'; -import 'package:flutter_hbb/mobile/pages/file_manager_page.dart'; import 'package:get/get.dart'; import 'package:path/path.dart' as Path; @@ -1123,6 +1122,51 @@ class DirectoryOption { } } +class SelectedItems { + bool? _isLocal; + final List _items = []; + + List get items => _items; + + int get length => _items.length; + + bool? get isLocal => _isLocal; + + add(bool isLocal, Entry e) { + _isLocal ??= isLocal; + if (_isLocal != null && _isLocal != isLocal) { + return; + } + if (!_items.contains(e)) { + _items.add(e); + } + } + + bool contains(Entry e) { + return _items.contains(e); + } + + remove(Entry e) { + _items.remove(e); + if (_items.length == 0) { + _isLocal = null; + } + } + + bool isOtherPage(bool currentIsLocal) { + if (_isLocal == null) { + return false; + } else { + return _isLocal != currentIsLocal; + } + } + + clear() { + _items.clear(); + _isLocal = null; + } +} + // code from file_manager pkg after edit List _sortList(List list, SortBy sortType, bool ascending) { if (sortType == SortBy.Name) {