zero-dialog 0.1.0

Ultra-lightweight, dependency-free system dialog library.
Documentation
//! An ultra-lightweight, dependency-free system dialog library.
//! Adds no GUI dependencies, requires no linking,
//! and introduces zero extra code complexity.
//!
//! Designed to show system-native dialogs as a last-resort
//! user-facing error/exception handler without adding project bloat.
//!
//! ## Usage
//!
//! Show a modal dialog with title `Hello`, content `World`, warning icon, and an `OK` button (closes when clicked):
//!
//! ```no_run
//! zero_dialog::show("Hello", "World", &Default::default());
//! ```
//!
//! Show error icon with `OK` / `Cancel` buttons and capture user choice:
//!
//! ```no_run
//! use zero_dialog::*;
//!
//! let title = "Fatal Error";
//! let msg = "Required assets not found, click Ok to exit";
//! let config = DialogConfig {
//!     icon: IconKind::Error,
//!     btn: ButtonKind::OkCancel,
//! };
//! let result = zero_dialog::show(title, msg, &config);
//! match result {
//!     Ok(Response::Ok) => std::process::exit(1),
//!     Ok(Response::Cancel) => println!("Cancel"),
//!     _ => println!("None"),
//! }
//! ```
//!
//! Suitable for use after `panic!()` to display critical errors.
//!
//! ```no_run
//! use std::panic;
//! use zero_dialog::show;
//!
//! fn main() {
//!     panic::set_hook(Box::new(|_| {
//!         let title = "Error";
//!         let msg = "Something went wrong";
//!         let _ = show(title, msg, &Default::default());
//!     }));
//!
//!     panic!("Normal panic");
//! }
//! ```

mod dialog;

/// User response when clicking buttons
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Response {
    /// Dialog closed, but button not clicked
    None,
    /// OK button clicked
    Ok,
    /// Cancel button clicked
    Cancel,
}

/// Possible errors when attempting to show the dialog
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Error {
    /// String conversion failure
    InvalidString,
    /// Dynamic link library (DLL) not found
    FailedToLoadLibrary,
    /// Required symbol not found in DLL
    FailedToFindSymbol,
    /// Failed to execute dialog
    FailedToRunDialog,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum IconKind {
    Info,
    Warning,
    Error,
    Question,
}

/// Dialog button layout
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ButtonKind {
    /// Show OK button only
    Ok,
    /// Show both OK and Cancel buttons
    OkCancel,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct DialogConfig {
    pub icon: IconKind,
    pub btn: ButtonKind,
}

impl Default for DialogConfig {
    fn default() -> Self {
        Self {
            icon: IconKind::Warning,
            btn: ButtonKind::Ok,
        }
    }
}

/// Displays a modal dialog that blocks the calling thread and waits for user input.
///
/// Allows configuration of the dialog icon and button layout (default: Warning icon with OK button).
/// Use `Default::default()` if no custom configuration is needed.
pub fn show(title: &str, message: &str, config: &DialogConfig) -> Result<Response, Error> {
    #[cfg(target_os = "windows")]
    {
        dialog::os::win32_show_dialog(title, message, config.icon, config.btn)
    }

    #[cfg(target_os = "linux")]
    {
        dialog::os::gtk3_show_dialog(title, message, config.icon, config.btn)
    }
}