Module Webview_desktop.DialogSource

Native modal dialogs.

The system's own dialogs, not HTML ones: an alert asking the user to confirm, and the file browser asking them to pick a file or a directory.

How to call them

Every function here is modal and synchronous: it blocks, running the platform's own event loop, until the user dismisses the dialog, and it returns what they chose. The window behind it stops responding for the duration, which is the point of a modal dialog.

They must be called on the UI thread — the thread running Webview.run. In practice that means from a binding callback, which is the natural place for them:

  Webview.bind w "pick_file" (fun id _req ->
      let result =
        match Webview_desktop.Dialog.open_file w ~title:"Choose a file" () with
        | Some path -> Printf.sprintf "%S" path
        | None -> "null"
      in
      Webview.return w id ~error:false ~result)

From any other thread, hop onto the UI thread with Webview.dispatch first. Note that a dispatched callback returns unit, so the answer has to be carried out of it by other means — a ref, a mutex, an Lwt_mvar.t.

On macOS the process must be an active application for a modal dialog to appear, which it only becomes once Webview.run has started — the same constraint as Icon.set_app_icon.

Platform notes

Sourceval confirm : Webview.t -> ?title:string -> string -> bool

confirm w message shows a question dialog with the platform's standard OK and Cancel buttons, and returns true if the user confirmed.

Cancelling returns false, and so does dismissing the dialog any other way — Escape, or the window's close button. Treat false as "did not confirm" rather than as "pressed Cancel".

?title is the headline: the window title on Linux and Windows, the bold first line of the alert on macOS, where message then becomes the smaller informative text below it. Omit it for a plain one-line question.

  if Dialog.confirm w ~title:"Quit" "Discard unsaved changes?" then
    Webview.terminate w
Sourceval open_file : Webview.t -> ?title:string -> unit -> string option

open_file w () opens the system file browser and returns the absolute path of the single existing file the user picked, or None if they cancelled.

?title replaces the dialog's default heading. There is no file-type filter: every file is selectable.

Sourceval open_directory : Webview.t -> ?title:string -> unit -> string option

open_directory w () is open_file for directories: it returns the absolute path of the single existing directory the user picked, or None if they cancelled.

Sourceval save_file : Webview.t -> ?title:string -> ?name:string -> unit -> string option

save_file w () asks where to save, and returns the absolute path the user settled on, or None if they cancelled.

Unlike open_file, the path need not exist: this is the dialog that lets the user type a new name, which is what saving a document needs. Nothing is written — creating the file is the caller's business.

?name pre-fills the name field, as in ~name:"report.pdf"; the user is free to change it. ?title is the dialog's heading, as everywhere else in this module, and is a different thing: one is what the dialog is called, the other what the file is called.

Picking a path that already exists makes each platform ask for confirmation before this returns, so a non-None result means the user accepted any overwrite.