Skip to content

File Picker dialog is not focused when it opens #1074

Open
@xylix

Description

@xylix

Description

File picker doesn't get focus on macOS when it opens

Code example to reproduce the issue:

Shortened version of https://github.com/flet-dev/examples/blob/main/python/controls/file-picker/file-picker-all-modes.py reproduces the issue:

import flet
from flet import (
    ElevatedButton,
    FilePicker,
    FilePickerResultEvent,
    Page,
    Row,
    Text,
    icons,
)


def main(page: Page):
    # Pick files dialog
    def pick_files_result(e: FilePickerResultEvent):
        selected_files.value = (
            ", ".join(map(lambda f: f.name, e.files)) if e.files else "Cancelled!"
        )
        selected_files.update()

    pick_files_dialog = FilePicker(on_result=pick_files_result)
    selected_files = Text()


    # hide all dialogs in overlay
    page.overlay.extend([pick_files_dialog])

    page.add(
        Row(
            [
                ElevatedButton(
                    "Pick files",
                    icon=icons.UPLOAD_FILE,
                    on_click=lambda _: pick_files_dialog.pick_files(
                        allow_multiple=True
                    ),
                ),
                selected_files,
            ]
        ),
    )


flet.app(target=main)

Describe the results you received:

When the file picker open's it is out of focus. This means that an extra click is required to select files in macOS. (So 3 clicks if you want to double click a file, first to focus and then 2 to select, instead of the 2 clicks expected.)

Describe the results you expected:

The file picker gets focus when it opens.

Additional information you deem important (e.g. issue happens only occasionally):

Something like this could work as a workaround if .focus() method was added to the file_picker:

def on_click(event: ControlEvent):
    pick_files_dialog.pick_files(
        allow_multiple=True
    )
    pick_files_dialog.focus()

Flet version (pip show flet):

Name: flet
Version: 0.4.2
Summary: Flet for Python - easily build interactive multi-platform apps in Python
Home-page: 
Author: Appveyor Systems Inc.
Author-email: [email protected]
License: Apache-2.0
Location: /Users/kerkko/Code/tmp/flet-filepicker-focus-test-22-02-2023/venv/lib/python3.11/site-packages
Requires: flet-core, httpx, oauthlib, packaging, watchdog, websocket-client, websockets
Required-by: 

Operating system:

macOS monterey 12.6 (21G115)

Did not test if this also happens in other operating systems.

Additional environment details:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions