blooemu 0.0.2

the best library for OS API's manipulation
Documentation
// Import winapi for Windows only
#[cfg(target_os = "windows")]
extern crate winapi;
#[cfg(target_os = "windows")]
use winapi::um::winuser::{
    MessageBoxW, IDNO, IDOK, IDYES, MB_ICONERROR, MB_ICONWARNING, MB_OK, MB_YESNO,
};

#[cfg(target_os = "windows")]
use std::ffi::OsStr;
#[cfg(target_os = "windows")]
use std::os::windows::ffi::OsStrExt;

// Function to display a warning window
pub fn alert_message(
    message: &str,
    title: &str,
    buttons: Option<&str>,
    yes_callback: Option<fn()>,
    no_callback: Option<fn()>,
) {
    // Logic for Windows
    #[cfg(target_os = "windows")]
    {
        let wide_message: Vec<u16> = OsStr::new(message)
            .encode_wide()
            .chain(std::iter::once(0))
            .collect();

        let wide_title: Vec<u16> = OsStr::new(title)
            .encode_wide()
            .chain(std::iter::once(0))
            .collect();

        let flags = match buttons {
            Some("yesno") => MB_YESNO | MB_ICONWARNING,
            _ => MB_OK,
        };

        let result = unsafe {
            MessageBoxW(
                std::ptr::null_mut(),
                wide_message.as_ptr(),
                wide_title.as_ptr(),
                flags,
            )
        };

        match result {
            IDYES => {
                if let Some(yes_fn) = yes_callback {
                    yes_fn();
                }
            }
            IDNO => {
                if let Some(no_fn) = no_callback {
                    no_fn();
                }
            }
            _ => {}
        }
    }

    // Logic for Linux
    #[cfg(target_os = "linux")]
    {
        use notify_rust::Notification;

        Notification::new()
            .summary(title)
            .body(message)
            .show()
            .expect("Failed to send notification");

        // Calling yes_callback when available (Linux does not support yes/no dialogs by default)
        if let Some(yes_fn) = yes_callback {
            yes_fn();
        }
    }

    // Logic for other operating systems
    #[cfg(not(any(target_os = "windows", target_os = "linux")))]
    {
        println!("{}: {}", title, message);

        // Вызов yes_callback при наличии
        if let Some(yes_fn) = yes_callback {
            yes_fn();
        }
    }
}

// Function to display the error window
pub fn error_message(message: &str, title: &str, callback: Option<fn()>) {
    // Logic for Windows
    #[cfg(target_os = "windows")]
    {
        let wide_message: Vec<u16> = OsStr::new(message)
            .encode_wide()
            .chain(std::iter::once(0))
            .collect();

        let wide_title: Vec<u16> = OsStr::new(title)
            .encode_wide()
            .chain(std::iter::once(0))
            .collect();

        // Set flags for the error window with a system icon and an error sound
        let flags = MB_OK | MB_ICONERROR;

        // Отображаем окно и получаем результат
        let result = unsafe {
            MessageBoxW(
                std::ptr::null_mut(),
                wide_message.as_ptr(),
                wide_title.as_ptr(),
                flags,
            )
        };

        // Displaying the window and getting the result
        if result == IDOK {
            if let Some(callback_fn) = callback {
                callback_fn();
            }
        }
    }

    // Logic for Linux
    #[cfg(target_os = "linux")]
    {
        use notify_rust::Notification;

        Notification::new()
            .summary(title)
            .body(message)
            .urgency(notify_rust::Urgency::Critical)
            .show()
            .expect("Failed to send error notification");

        // Выполнение callback, если задан
        if let Some(callback_fn) = callback {
            callback_fn();
        }
    }

    // Logic for other operating systems
    #[cfg(not(any(target_os = "windows", target_os = "linux")))]
    {
        eprintln!("{}: {}", title, message);

        // Выполнение callback, если задан
        if let Some(callback_fn) = callback {
            callback_fn();
        }
    }
}