dymokomi/luce-dialogs
Source of release 0.6.1
0.6.1 / src / dialogs / module.lucb
#==============================================================================================
#
# dialogs - Native file dialogs
#
# DESCRIPTION:
# The desktop's own open and save file panels, run modally from the UI thread. A chosen
# path is returned owning its bytes; a cancelled dialog returns none.
#
#==============================================================================================
import c
import interop
import memory
import platform
from luce_std import process
from luce_std import files
from luce_std import collections
import strings
import thread
from luce_window import window
## The dialog could not be shown or its result could not be read.
pub let failed: ErrorCode = ErrorCode.package(123)
## Ask for an existing file to open, with `message` above the file list.
## Returns the chosen path, or none when the dialog was cancelled.
pub func open_file(message: str) -> interop.Owned[str]?!:
try check_access()
if platform.macos:
return try mac_choose(message, "", false, false)
if platform.windows:
return try win_choose(message, "", false, false)
if platform.linux:
return try linux_choose(message, "", false, false)
error(window.unsupported, "native file dialogs are unavailable on this target")
## Ask for one or more existing files to open, with `message` above the file
## list. Returns the chosen paths one to a line, or none when cancelled.
pub func open_files(message: str) -> interop.Owned[str]?!:
try check_access()
if platform.macos:
return try mac_choose(message, "", false, true)
if platform.windows:
return try win_choose(message, "", false, true)
if platform.linux:
return try linux_choose(message, "", false, true)
error(window.unsupported, "native file dialogs are unavailable on this target")
## Ask for an existing folder, with `message` above the panel. Returns the chosen
## folder's path, or none when the dialog was cancelled.
pub func choose_folder(message: str) -> interop.Owned[str]?!:
try check_access()
if platform.macos:
return try mac_choose(message, "", false, false, folders = true)
if platform.windows:
return try win_choose_folder(message)
if platform.linux:
return try linux_choose(message, "", false, false, folders = true)
error(window.unsupported, "native file dialogs are unavailable on this target")
## Ask where to save a file, suggesting `name`, with `message` above the field.
## Returns the chosen path, or none when the dialog was cancelled.
pub func save_file(message: str, name: str) -> interop.Owned[str]?!:
try check_access()
if platform.macos:
return try mac_choose(message, name, true, false)
if platform.windows:
return try win_choose(message, name, true, false)
if platform.linux:
return try linux_choose(message, name, true, false)
error(window.unsupported, "native file dialogs are unavailable on this target")
## Get the open dialog ready ahead of time, so the first `open_file`,
## `open_files` or `choose_folder` shows at once. On macOS this makes AppKit's
## open panel, which takes about a quarter of a second the first time: call it
## when the app is idle, after its window first shows. Elsewhere it does nothing.
pub func prepare() -> !:
try check_access()
if platform.macos:
try mac_prepare()
func check_access() -> !:
if not thread.is_main():
error(window.wrong_thread, "file dialogs belong to the UI thread")