oxedyne/fe2o3/fe2o3_pearlite/src/launch.rs
2.1 KiB, 1 run
created by r1870400018:60980, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The reader as a desktop application: a document named on the command line opens straight into the |
| 2 | //! window, and a launch with none -- a double-click, or the applications menu -- asks for one through the |
| 3 | //! platform's own open-file dialog. A launch from the desktop has no terminal to write to, so a failure |
| 4 | //! is shown in a message box as well as on standard error. |
| 5 | |
| 6 | use crate::window; |
| 7 | |
| 8 | use oxedyne_fe2o3_austenite::emit::pearl::PearlDoc; |
| 9 | |
| 10 | use oxedyne_fe2o3_core::prelude::*; |
| 11 | |
| 12 | use std::path::{ |
| 13 | Path, |
| 14 | PathBuf, |
| 15 | }; |
| 16 | |
| 17 | /// Opens `path` when given, otherwise asks for a document first. Cancelling the dialog is not an error: |
| 18 | /// the launch simply ends. Any failure is reported on screen before it is returned. |
| 19 | pub fn run(path: Option<PathBuf>) -> Outcome<()> { |
| 20 | let path = match path { |
| 21 | Some(p) => p, |
| 22 | None => match pick_document() { |
| 23 | Some(p) => p, |
| 24 | None => return Ok(()), |
| 25 | }, |
| 26 | }; |
| 27 | let outcome = open_path(&path); |
| 28 | if let Err(e) = &outcome { |
| 29 | report(&fmt!("Pearlite could not open {}.\n\n{}", path.display(), e.plain())); |
| 30 | } |
| 31 | outcome |
| 32 | } |
| 33 | |
| 34 | /// Shows the native open-file dialog, filtered to `.prl` documents. `None` when the reader cancels, or |
| 35 | /// when the platform has no dialog to show. |
| 36 | pub fn pick_document() -> Option<PathBuf> { |
| 37 | rfd::FileDialog::new() |
| 38 | .set_title("Open a Pearlite document") |
| 39 | .add_filter("Pearlite documents", &["prl"]) |
| 40 | .pick_file() |
| 41 | } |
| 42 | |
| 43 | /// Reads a document and opens it in a window titled with its file stem, since the format carries no |
| 44 | /// title of its own. |
| 45 | pub fn open_path(path: &Path) -> Outcome<()> { |
| 46 | let doc = res!(PearlDoc::read_file(path)); |
| 47 | let title = path |
| 48 | .file_stem() |
| 49 | .map(|n| n.to_string_lossy().into_owned()) |
| 50 | .filter(|s| !s.is_empty()) |
| 51 | .unwrap_or_else(|| "Pearlite".to_string()); |
| 52 | window::open(doc, title) |
| 53 | } |
| 54 | |
| 55 | /// Shows an error in a message box, and on standard error for a launch that does have a terminal. |
| 56 | pub fn report(msg: &str) { |
| 57 | eprintln!("pearlite: {}", msg); |
| 58 | let _ = rfd::MessageDialog::new() |
| 59 | .set_level(rfd::MessageLevel::Error) |
| 60 | .set_title("Pearlite") |
| 61 | .set_description(msg) |
| 62 | .set_buttons(rfd::MessageButtons::Ok) |
| 63 | .show(); |
| 64 | } |