Skip to content
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

Fix bug, when user canceled filechooser, on_selection does not dispatched #666

Merged
merged 3 commits into from
Feb 20, 2022

Conversation

Neizvestnyj
Copy link
Contributor

When clicking the close button, on OS X on_selection was not called

from textwrap import dedent

from plyer import filechooser

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.uix.button import Button


class FileChoose(Button):
    '''
    Button that triggers 'filechooser.open_file()' and processes
    the data response from filechooser Activity.
    '''

    selection = ListProperty([])

    def choose(self):
        '''
        Call plyer filechooser API to run a filechooser Activity.
        '''
        filechooser.open_file(on_selection=self.handle_selection)

    def handle_selection(self, selection):
        '''
        Callback function for handling the selection response from Activity.
        '''
        self.selection = selection

    def on_selection(self, *a, **k):
        '''
        Update TextInput.text after FileChoose.selection is changed
        via FileChoose.handle_selection.
        '''
        print(self.selection)
        App.get_running_app().root.ids.result.text = str(self.selection)


class ChooserApp(App):
    '''
    Application class with root built in KV.
    '''

    def build(self):
        return Builder.load_string(dedent('''
            <FileChoose>:
            BoxLayout:
                BoxLayout:
                    orientation: 'vertical'
                    TextInput:
                        id: result
                        text: ''
                        hint_text: 'selected path'
                    FileChoose:
                        size_hint_y: 0.1
                        on_release: self.choose()
                        text: 'Select a file'
        ''')
                                   )


if __name__ == '__main__':
    ChooserApp().run()
Demo.mp4

@tshirtman
Copy link
Member

But, you didn't select anything, why should it be called?

@Neizvestnyj
Copy link
Contributor Author

But, you didn't select anything, why should it be called?

Because on others platform (win/linux) its called and if the user clicked the close button there is no way to find out it

@HyTurtle
Copy link
Contributor

Does setting allownone and/or force_dispatch to True on you ListProperty not do the same? And without the issue created of defaulting to a True/Truthy value

@Neizvestnyj
Copy link
Contributor Author

Does setting allownone and/or force_dispatch to True on you ListProperty not do the same? And without the issue created of defaulting to a True/Truthy value

No, selection = ListProperty(['']) / selection = ListProperty([True]) does not work

@tshirtman
Copy link
Member

Doesn't seem to be the case on Linux? (i just tested and if i close the selection window, there is no change)
https://github.com/kivy/plyer/blob/master/plyer/platforms/linux/filechooser.py#L68
but on Windows indeed seems to be the case
https://github.com/kivy/plyer/blob/master/plyer/platforms/win/filechooser.py#L149
although it returns [] not [''].
on android, it seems it would dispatch [] as well.
https://github.com/kivy/plyer/blob/master/plyer/platforms/android/filechooser.py#L194
not sure on ios
https://github.com/kivy/plyer/blob/master/plyer/platforms/ios/filechooser.py#L77

@Neizvestnyj
Copy link
Contributor Author

Neizvestnyj commented Feb 19, 2022

Doesn't seem to be the case on Linux? (i just tested and if i close the selection window, there is no change) https://github.com/kivy/plyer/blob/master/plyer/platforms/linux/filechooser.py#L68 but on Windows indeed seems to be the case https://github.com/kivy/plyer/blob/master/plyer/platforms/win/filechooser.py#L149 although it returns [] not ['']. on android, it seems it would dispatch [] as well. https://github.com/kivy/plyer/blob/master/plyer/platforms/android/filechooser.py#L194 not sure on ios https://github.com/kivy/plyer/blob/master/plyer/platforms/ios/filechooser.py#L77

Yes, on different platforms different return...

@HyTurtle
Copy link
Contributor

Does setting allownone and/or force_dispatch to True on you ListProperty not do the same? And without the issue created of defaulting to a True/Truthy value

No, selection = ListProperty(['']) / selection = ListProperty([True]) does not work

that would be selection = ListProperty([], allownone=True, force_dispatch=True)

@Neizvestnyj
Copy link
Contributor Author

Neizvestnyj commented Feb 19, 2022

@tshirtman I can change in commit selection = [''] to selection = [], but in example code I need to set selection = List Property([''])

@Neizvestnyj
Copy link
Contributor Author

selection = ListProperty([], allownone=True, force_dispatch=True)

does not work

@HyTurtle
Copy link
Contributor

Could adjust your commit so it passes None to _handle_selection - that shouldn't break how it currently operates.

@Neizvestnyj
Copy link
Contributor Author

Neizvestnyj commented Feb 19, 2022

selection = None

if panel.runModal():
    if self.mode == "save" or not self.multiple:
        selection = [panel.filename().UTF8String()]
    else:
        filename = panel.filenames()
        selection = [
            filename.objectAtIndex_(x).UTF8String()
            for x in range(filename.count())]

self._handle_selection(selection)

return selection
# in app
selection = ListProperty([], allownone=True)

so?

@HyTurtle
Copy link
Contributor

Seems harmless to me, does that selection trigger / not trigger or error without the allownone. and _handle_selection is a dummy method so in app you can redefine that if need be.

@Neizvestnyj
Copy link
Contributor Author

Seems harmless to me, does that selection trigger / not trigger or error without the allownone. and _handle_selection is a dummy method so in app you can redefine that if need be.

Thanks, done

@HyTurtle
Copy link
Contributor

Just tried to read the relevant docs here, mind testing print(panel.runModal()) with a selection made and an exit; won't matter too much just curious if the exit/cancel is just 0.

@Neizvestnyj
Copy link
Contributor Author

Just tried to read the relevant docs here, mind testing print(panel.runModal()) with a selection made and an exit; won't matter too much just curious if the exit/cancel is just 0.

hmm, yes, I see

Copy link
Member

@tshirtman tshirtman left a comment

Choose a reason for hiding this comment

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

LGTM

please fix the style issue though, (line > 80 breaks pep8)

@Neizvestnyj
Copy link
Contributor Author

LGTM

please fix the style issue though, (line > 80 breaks pep8)

Ah, yes, I see, fixed it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants