Skip to content

[file_selector_web] Listens to file input cancel event. #3683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

raju-muliyashiya
Copy link
Contributor

While using package file_selector making for multiple file request in web platform we are not able to get any data if user clicks cancel button on file selection window. This PR fixes it by watching the focus event and if onChange is not fired then return empty array, Empty array will only return if multiple selection is enabled with openFiles().

List which issues are fixed by this PR. You must list at least one issue.
Issue #121328

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [relevant style guides] and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the [CLA].
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • All existing and new tests are passing.

@google-cla
Copy link

google-cla bot commented Apr 11, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Member

@ditman ditman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @raju8000!

Thanks for this fix, however, there's now a cancel event that AFAIK is supported by almost all browsers (I just verified the event exists in Chrome Canary). See this comment.

Do you mind using that instead of the focus event trick?

I wrote a very small cancel/change event tester app here, because browsers do slightly different things when clicking ok/cancel and selecting the same file or a different one, so some logic will have to be added to figure out if the user was actually canceling the selection:

I think that this now can be solved with a combination of the change/cancel without needing to rely on the window focus.

Also, about the tests, please remove the tests that are not really testing the method that you're adding :)

@@ -144,6 +144,65 @@ void main() {
});
});

group('cancelOpenFiles', () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are not testing anything that this change does :)

@raju-muliyashiya
Copy link
Contributor Author

raju-muliyashiya commented Apr 12, 2023

@ditman ,

*https://jsfiddle.net/ditman/avdpr9Lu

I have tried above given test app on latest versions of Safari, Chrome & FIrefox, eventually the cancel event seems to be working in only FIrefox browser, Crome @ Safari are not able to trigger cancel event.

how could this issue be tackled?

@ditman
Copy link
Member

ditman commented Apr 14, 2023

Crome @ Safari are not able to trigger cancel event.

@raju8000 I can see the "cancel" event in Chrome Beta (v113+) and Safari 16.4, but each browser triggers it differently (that's why "change" and looking at "files" is also needed)

  • Chrome:
    • When the file selection does NOT change, and the selector is closed (with Esc or Cancel):
      • If you didn't select any file, "cancel" triggers, "input.files" is empty.
      • If you select THE SAME FILE AS BEFORE "cancel" triggers, but "input.files" is NOT empty.
    • If you had a file selected before, reopen the selector and close it without changes (with Esc or Cancel):
      • "change" triggers, and "input.files" is emptied.
  • Firefox:
    • The one closest to what one would expect ("cancel" when the dialog is closed, "change" when a file is selected)
      • Never clears "input.files" after selecting for the first time.
        • When selecting the same file, firefox fires "change" with the same "input.files" that was selected before (this is different than in Chrome, and will need browser detection to tell apart the browsers!)
  • Safari:
    • Never clears "input.files" after selecting for the first time.
      • When selecting the same file as before, Safari fires "cancel", and input is NOT empty (same as Chrome)

I think the above covers all the possible use cases, but I'm not 100% sure. The idea is that we need to look at both the "change" and "cancel" events, and the "files" of the input to know if the user canceled or not.

@andreisas06
Copy link
Contributor

andreisas06 commented Apr 14, 2023

@raju8000 use Google Chrome Canary and Safari Technology Preview, and you will see the cancel event. I noticed it behaves different depending on the browser, just like @ditman said.

Here are some examples (for each example, the first action is at the bottom and the the most recent at the top):

Chrome Canary:
Screenshot 2023-04-14 at 12 24 18
From what I can observe, after selecting the file and opening the dialog again to press cancel, it still sees it as a change event. Only after I open the dialog again and press cancel, it registers it as a cancel event.

Safari Technology Preview:
Screenshot 2023-04-14 at 12 26 33
Even if i click cancel in the dialog after I selected an image, if shows me that I used a cancel event which is good, but even if I cancel, the browser still sees it as a selected file, although it is a canceled event with no files selected.
The first cancel event turned out as expected because I haven’t selected any images/files, but after i select a file/image and decide later on to cancel after I selected that file/image, it will keep the previously selected file name, even if u haven’t selected anything in the dialog and pressed cancel.

Firefox(stable not the Nightly version):
Screenshot 2023-04-14 at 12 35 22
Same story sa Safari Technology Preview.

Edge Canary:
Screenshot 2023-04-14 at 12 23 04
Behaves as expected, imo.

@stuartmorgan-g
Copy link
Contributor

@raju8000 Are you still planning on updating this PR based on the discussion above?

@ditman
Copy link
Member

ditman commented May 12, 2023

Thanks @raju8000! I'll review this ASAP! In the meantime, please check the "repo_checks" failure, and try to fix whatever errors it's pointing out. They're usually easy to fix!

@stuartmorgan-g stuartmorgan-g requested a review from ditman June 29, 2023 18:47
@ditman
Copy link
Member

ditman commented Jul 5, 2023

cancel seems to be enough in this case because the package never reuses the same input tag, so there's never a case of "the user has selected the same files as before" or any of the corner cases described above.

@ditman
Copy link
Member

ditman commented Jul 6, 2023

I'll try to get this fixed and landed tomorrow, and have a similar fix applied to image_picker.

@ditman ditman force-pushed the fix_file_selector_web_returns_null_on_presses_cancel branch from c0b5ec1 to bbf0c9e Compare July 12, 2023 00:58
Copy link
Member

@ditman ditman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is ready to go! (PTAL @stuartmorgan)

@ditman ditman requested a review from stuartmorgan-g July 12, 2023 01:08
@ditman ditman self-assigned this Jul 12, 2023
@ditman ditman changed the title [FileSelector][web] Fix-openFiles never returns when user presses "cancel" in system UI file picker [file_selector_web] Listens to file input cancel event. Jul 12, 2023
@ditman ditman force-pushed the fix_file_selector_web_returns_null_on_presses_cancel branch from e00b088 to fde3f95 Compare July 12, 2023 20:22
Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@stuartmorgan-g stuartmorgan-g added the autosubmit Merge PR when tree becomes green via auto submit App label Jul 12, 2023
@auto-submit auto-submit bot merged commit 2a508cb into flutter:main Jul 12, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jul 13, 2023
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Jul 13, 2023
flutter/packages@2508714...aa1eace

2023-07-13 ian@hixie.ch [rfw] Restore RFW to 100% coverage (flutter/packages#4355)
2023-07-12 65381000+raju8000@users.noreply.github.com [file_selector_web] Listens to file input cancel event. (flutter/packages#3683)
2023-07-12 sam.rawlins@gmail.com [cross_file] Correct sorting of import starting with dot-slash (flutter/packages#4449)
2023-07-12 ian@hixie.ch [metrics_center] Remove Equatable dependency (flutter/packages#4444)
2023-07-12 engine-flutter-autoroll@skia.org Roll Flutter from 3ec96a8 to 544d30d (66 revisions) (flutter/packages#4448)
2023-07-12 stuartmorgan@google.com [ci] Move snippet checks to LUCI (flutter/packages#4446)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
LouiseHsu pushed a commit to LouiseHsu/flutter that referenced this pull request Jul 13, 2023
flutter/packages@2508714...aa1eace

2023-07-13 ian@hixie.ch [rfw] Restore RFW to 100% coverage (flutter/packages#4355)
2023-07-12 65381000+raju8000@users.noreply.github.com [file_selector_web] Listens to file input cancel event. (flutter/packages#3683)
2023-07-12 sam.rawlins@gmail.com [cross_file] Correct sorting of import starting with dot-slash (flutter/packages#4449)
2023-07-12 ian@hixie.ch [metrics_center] Remove Equatable dependency (flutter/packages#4444)
2023-07-12 engine-flutter-autoroll@skia.org Roll Flutter from 3ec96a8 to 544d30d (66 revisions) (flutter/packages#4448)
2023-07-12 stuartmorgan@google.com [ci] Move snippet checks to LUCI (flutter/packages#4446)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
auto-submit bot pushed a commit that referenced this pull request Aug 4, 2023
## Changes

This PR listens to the `cancel` event from the `input type=file` used by the web implementation of the image_picker plugin, so apps don't end up endlessly awaiting for a file that will never come **in modern browsers** (Chrome 113, Safari 16.4, or newer). _Same API as #3683

Additionally, this PR:

* Removes all code and tests mentioning `PickedFile`. (Deprecated years ago, and unused since #4285) **(Breaking change)**
* Updates README to mention `XFile` which is the current return type of the package.
* Updates the dependency on `image_picker_platform_interface` to `^2.9.0`.
  * Implements all non-deprecated methods from the interface, and makes deprecated methods use the fresh ones.
  * Updates tests.

### Issues

* Fixes flutter/flutter#92176

### Testing

* Added integration testing coverage for the 'cancel' event.
* Tested manually in Chrome with the example app running on web.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: file_selector platform-web
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants