From 748326e13f8420ff657af7e864ad8f98242a6043 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Fri, 21 Jan 2022 13:17:16 -0800 Subject: [PATCH 1/2] Fix #330 - Multiple file browser windows are opened. --- lib/models/appstate.dart | 7 +++++++ lib/views/messageview.dart | 21 +++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/models/appstate.dart b/lib/models/appstate.dart index 8386b8dd..34d4c383 100644 --- a/lib/models/appstate.dart +++ b/lib/models/appstate.dart @@ -13,6 +13,7 @@ class AppState extends ChangeNotifier { int _hoveredIndex = -1; int? _selectedIndex; bool _unreadMessagesBelow = false; + bool _disableFilePicker = false; void SetCwtchInit() { cwtchInit = true; @@ -47,6 +48,12 @@ class AppState extends ChangeNotifier { notifyListeners(); } + bool get disableFilePicker => _disableFilePicker; + set disableFilePicker(bool newVal) { + this._disableFilePicker = newVal; + notifyListeners(); + } + // Never use this for message lookup - can be a non-indexed value // e.g. -1 int get hoveredIndex => _hoveredIndex; diff --git a/lib/views/messageview.dart b/lib/views/messageview.dart index dd81c1c2..9bdcacae 100644 --- a/lib/views/messageview.dart +++ b/lib/views/messageview.dart @@ -1,7 +1,6 @@ import 'dart:convert'; import 'dart:io'; import 'package:crypto/crypto.dart'; -import 'package:cwtch/config.dart'; import 'package:cwtch/cwtch_icons_icons.dart'; import 'package:cwtch/models/appstate.dart'; import 'package:cwtch/models/chatmessage.dart'; @@ -23,7 +22,6 @@ import 'package:flutter/services.dart'; import 'package:provider/provider.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:scrollable_positioned_list/scrollable_positioned_list.dart'; -import 'package:path/path.dart' show basename; import '../main.dart'; import '../settings.dart'; @@ -89,11 +87,13 @@ class _MessageViewState extends State { if (showFileSharing) { appBarButtons.add(IconButton( splashRadius: Material.defaultSplashRadius / 2, - icon: Icon(Icons.attach_file, size: 24), + icon: Icon(Icons.attach_file, size: 24, color: Provider.of(context).theme.mainTextColor), tooltip: AppLocalizations.of(context)!.tooltipSendFile, - onPressed: () { - _showFilePicker(context); - }, + onPressed: Provider.of(context).disableFilePicker + ? null + : () { + _showFilePicker(context); + }, )); } appBarButtons.add(IconButton( @@ -392,7 +392,16 @@ class _MessageViewState extends State { void _showFilePicker(BuildContext ctx) async { imagePreview = null; + + // only allow one file picker at a time + // note: ideally we would destroy file picker when leaving a conversation + // but we don't currently have that option. + // we need to store AppState in a variable because ctx might be destroyed + // while awaiting for pickFiles. + var appstate = Provider.of(ctx, listen: false); + appstate.disableFilePicker = true; FilePickerResult? result = await FilePicker.platform.pickFiles(); + appstate.disableFilePicker = false; if (result != null) { File file = File(result.files.first.path); // We have a maximum number of bytes we can represent in terms of From def222a8abe9558fa2f013208fb22690363f4fbb Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Fri, 21 Jan 2022 13:40:23 -0800 Subject: [PATCH 2/2] upgrade flutter file picker --- lib/views/messageview.dart | 7 ++++--- pubspec.lock | 4 ++-- pubspec.yaml | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/views/messageview.dart b/lib/views/messageview.dart index 9bdcacae..ade71beb 100644 --- a/lib/views/messageview.dart +++ b/lib/views/messageview.dart @@ -400,10 +400,11 @@ class _MessageViewState extends State { // while awaiting for pickFiles. var appstate = Provider.of(ctx, listen: false); appstate.disableFilePicker = true; - FilePickerResult? result = await FilePicker.platform.pickFiles(); + // currently lockParentWindow only works on Windows... + FilePickerResult? result = await FilePicker.platform.pickFiles(lockParentWindow: true); appstate.disableFilePicker = false; - if (result != null) { - File file = File(result.files.first.path); + if (result != null && result.files.first.path != null) { + File file = File(result.files.first.path!); // We have a maximum number of bytes we can represent in terms of // a manifest (see : https://git.openprivacy.ca/cwtch.im/cwtch/src/branch/master/protocol/files/manifest.go#L25) if (file.lengthSync() <= 10737418240) { diff --git a/pubspec.lock b/pubspec.lock index a967a6be..22a566d6 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -112,14 +112,14 @@ packages: name: file_picker url: "https://pub.dartlang.org" source: hosted - version: "4.0.1" + version: "4.3.2" file_picker_desktop: dependency: "direct main" description: name: file_picker_desktop url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.1.1" flutter: dependency: "direct main" description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index 03d6d590..47e0b916 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -41,7 +41,7 @@ dependencies: flutter_test: sdk: flutter scrollable_positioned_list: ^0.2.0-nullsafety.0 - file_picker: ^4.0.1 + file_picker: ^4.3.2 file_picker_desktop: ^1.1.0 url_launcher: ^6.0.12