use std::path::{Path, PathBuf};
use crate::e2e::{CaptureImage, CaptureTarget, Driver};
use crate::MockBackend;
use native_gui_core::error::{GuiError, GuiResult};
use native_gui_core::events::UiEvent;
#[cfg(feature = "widgets-pickers")]
use native_gui_core::pickers::{ColorDialogData, FontDescriptor, FontDialogData};
use native_gui_core::ui::{LoopFlow, Ui};
#[cfg(feature = "widgets-containers")]
use native_gui_core::widgets::AlertLevel;
use native_gui_core::widgets::{ContainerDirection, WidgetKind};
#[cfg(feature = "widgets-pickers")]
use native_gui_core::Rgba;
#[derive(Debug, Clone)]
pub struct GalleryScenarioSpec {
pub id: &'static str,
pub title: &'static str,
pub category: &'static str,
pub description: &'static str,
pub widget_doc: &'static str,
}
#[derive(Debug, Clone)]
pub struct GalleryCaptureResult {
pub spec: GalleryScenarioSpec,
pub width: u32,
pub height: u32,
pub output_path: Option<PathBuf>,
pub size_bytes: usize,
}
pub fn standard_gallery_scenarios() -> Vec<GalleryScenarioSpec> {
vec![
#[cfg(feature = "widgets-containers")]
GalleryScenarioSpec {
id: "gui_widget_alert_dialog",
title: "AlertDialog",
category: "widget",
description: "Modal warning alert dialog prompt with warning icon, explanatory message, and confirmation action.",
widget_doc: "reference/widget-reference.md#alertdialog",
},
GalleryScenarioSpec {
id: "gui_dialog_confirm",
title: "ConfirmationDialog",
category: "widget",
description: "Modal confirmation prompt for unsaved changes with Save, Don't Save, and Cancel options.",
widget_doc: "reference/widget-reference.md#alertdialog",
},
#[cfg(feature = "widgets-pickers")]
GalleryScenarioSpec {
id: "gui_widget_file_chooser_dialog",
title: "FileChooserDialog",
category: "widget",
description: "Native file open and save dialog with directory navigation and file-type filters.",
widget_doc: "reference/widget-reference.md#filechooserdialog",
},
#[cfg(feature = "widgets-pickers")]
GalleryScenarioSpec {
id: "gui_widget_color_dialog",
title: "ColorPickerDialog",
category: "widget",
description: "Color picker dialog featuring custom palette swatches, RGB channel controls, and live color preview.",
widget_doc: "reference/widget-reference.md#colordialog",
},
#[cfg(feature = "widgets-pickers")]
GalleryScenarioSpec {
id: "gui_widget_font_dialog",
title: "FontPickerDialog",
category: "widget",
description: "Font chooser dialog selecting family, point size, bold/italic styles, and live preview rendering.",
widget_doc: "reference/widget-reference.md#fontdialog",
},
GalleryScenarioSpec {
id: "gui_form_validation",
title: "Form Validation Errors",
category: "state",
description: "Active form validation error state displaying invalid field highlights, warning icons, and inline validation banners.",
widget_doc: "examples-index.md#app-style-applications",
},
#[cfg(all(feature = "widgets-containers", feature = "widgets-choice"))]
GalleryScenarioSpec {
id: "gui_multi_state_tabs",
title: "Multi-State Tabs",
category: "state",
description: "Secondary active tab state displaying advanced diagnostics, memory limits, and nested slider configuration.",
widget_doc: "reference/widget-reference.md#tabs",
},
]
}
fn noop_handler(_ui: &mut Ui<MockBackend>, _ev: UiEvent) -> GuiResult<LoopFlow> {
Ok(LoopFlow::Continue)
}
#[cfg(feature = "widgets-containers")]
pub fn capture_scenario_alert_dialog() -> GuiResult<CaptureImage> {
let mut ui = Ui::new(MockBackend::default());
let window = ui.create(WidgetKind::Window, Some("Application Main Window"))?;
let container = ui.create(WidgetKind::Container, None)?;
ui.set_container_direction(container, ContainerDirection::Vertical)?;
ui.attach(window, container)?;
let label = ui.create(WidgetKind::Label, Some("Document Workspace: Untitled-1.rs"))?;
let alert = ui.create(
WidgetKind::AlertDialog,
Some("Warning: You have unsaved changes that will be discarded. Do you wish to continue?"),
)?;
ui.set_alert_level(alert, AlertLevel::Warning)?;
ui.attach(container, label)?;
ui.attach(container, alert)?;
ui.set_layout_root(window)?;
ui.show(window)?;
ui.show(alert)?;
let mut driver = Driver::new(ui, noop_handler);
driver.pump()?;
driver.screenshot(CaptureTarget::Window(alert))
}
#[cfg(not(feature = "widgets-containers"))]
pub fn capture_scenario_alert_dialog() -> GuiResult<CaptureImage> {
Err(GuiError::UnsupportedWidget("AlertDialog"))
}
pub fn capture_scenario_confirm_dialog() -> GuiResult<CaptureImage> {
let mut ui = Ui::new(MockBackend::default());
let window = ui.create(WidgetKind::Window, Some("Editor Workspace"))?;
let container = ui.create(WidgetKind::Container, None)?;
ui.set_container_direction(container, ContainerDirection::Vertical)?;
ui.attach(window, container)?;
let dialog = ui.create(WidgetKind::Window, Some("Confirm Close"))?;
let dialog_box = ui.create(WidgetKind::Container, None)?;
ui.set_container_direction(dialog_box, ContainerDirection::Vertical)?;
ui.attach(dialog, dialog_box)?;
let msg = ui.create(
WidgetKind::Label,
Some("Save changes to 'annual_report_2026.csv' before exiting?"),
)?;
let btn_bar = ui.create(WidgetKind::Container, None)?;
ui.set_container_direction(btn_bar, ContainerDirection::Horizontal)?;
let btn_save = ui.create(WidgetKind::Button, Some("Save"))?;
let btn_dont = ui.create(WidgetKind::Button, Some("Don't Save"))?;
let btn_cancel = ui.create(WidgetKind::Button, Some("Cancel"))?;
ui.attach(btn_bar, btn_save)?;
ui.attach(btn_bar, btn_dont)?;
ui.attach(btn_bar, btn_cancel)?;
ui.attach(dialog_box, msg)?;
ui.attach(dialog_box, btn_bar)?;
ui.set_layout_root(window)?;
ui.set_layout_root(dialog)?;
ui.show(window)?;
ui.show(dialog)?;
let mut driver = Driver::new(ui, noop_handler);
driver.pump()?;
driver.screenshot(CaptureTarget::Window(dialog))
}
#[cfg(feature = "widgets-pickers")]
pub fn capture_scenario_file_chooser() -> GuiResult<CaptureImage> {
let mut ui = Ui::new(MockBackend::default());
let window = ui.create(WidgetKind::Window, Some("Open Project File"))?;
let chooser = ui.create(
WidgetKind::FileChooserDialog,
Some("Select Source Code (*.rs, *.toml)"),
)?;
ui.set_text(
chooser,
"C:\\Users\\alex\\Projects\\sample-app\\src\\main.rs",
)?;
ui.set_layout_root(window)?;
ui.attach(window, chooser)?;
ui.show(window)?;
ui.show(chooser)?;
let mut driver = Driver::new(ui, noop_handler);
driver.pump()?;
driver.screenshot(CaptureTarget::Window(chooser))
}
#[cfg(not(feature = "widgets-pickers"))]
pub fn capture_scenario_file_chooser() -> GuiResult<CaptureImage> {
Err(GuiError::UnsupportedWidget("FileChooserDialog"))
}
#[cfg(feature = "widgets-pickers")]
pub fn capture_scenario_color_picker() -> GuiResult<CaptureImage> {
let mut ui = Ui::new(MockBackend::default());
let window = ui.create(WidgetKind::Window, Some("Palette Selection"))?;
let color_dialog = ui.create(WidgetKind::ColorDialog, Some("Choose Theme Accent Color"))?;
let mut color_data = ColorDialogData::new(Rgba::new(42, 127, 255, 255));
let _ = color_data.push_custom(Rgba::new(255, 69, 58, 255));
let _ = color_data.push_custom(Rgba::new(48, 209, 88, 255));
let _ = color_data.push_custom(Rgba::new(10, 132, 255, 255));
ui.set_color_dialog_data(color_dialog, color_data)?;
ui.set_layout_root(window)?;
ui.attach(window, color_dialog)?;
ui.show(window)?;
ui.show(color_dialog)?;
let mut driver = Driver::new(ui, noop_handler);
driver.pump()?;
driver.screenshot(CaptureTarget::Window(color_dialog))
}
#[cfg(not(feature = "widgets-pickers"))]
pub fn capture_scenario_color_picker() -> GuiResult<CaptureImage> {
Err(GuiError::UnsupportedWidget("ColorDialog"))
}
#[cfg(feature = "widgets-pickers")]
pub fn capture_scenario_font_picker() -> GuiResult<CaptureImage> {
let mut ui = Ui::new(MockBackend::default());
let window = ui.create(WidgetKind::Window, Some("Typography Settings"))?;
let font_dialog = ui.create(WidgetKind::FontDialog, Some("Select Editor Font"))?;
let font_data = FontDialogData::new(FontDescriptor::new("Segoe UI Variable Display", 13.5));
ui.set_font_dialog_data(font_dialog, font_data)?;
ui.set_layout_root(window)?;
ui.attach(window, font_dialog)?;
ui.show(window)?;
ui.show(font_dialog)?;
let mut driver = Driver::new(ui, noop_handler);
driver.pump()?;
driver.screenshot(CaptureTarget::Window(font_dialog))
}
#[cfg(not(feature = "widgets-pickers"))]
pub fn capture_scenario_font_picker() -> GuiResult<CaptureImage> {
Err(GuiError::UnsupportedWidget("FontDialog"))
}
#[cfg(feature = "widgets-text")]
pub fn capture_scenario_form_validation() -> GuiResult<CaptureImage> {
let mut ui = Ui::new(MockBackend::default());
let window = ui.create(
WidgetKind::Window,
Some("Account Registration - Validation Required"),
)?;
let container = ui.create(WidgetKind::Container, None)?;
ui.set_container_direction(container, ContainerDirection::Vertical)?;
ui.attach(window, container)?;
let banner = ui.create(
WidgetKind::Label,
Some("[!] Error: Please correct 3 highlighted issues before submitting."),
)?;
ui.attach(container, banner)?;
let name_lbl = ui.create(WidgetKind::Label, Some("User Name: *"))?;
let name_input = ui.create(WidgetKind::TextInput, Some("usr"))?;
let name_err = ui.create(
WidgetKind::Label,
Some("x Name must be at least 4 characters long"),
)?;
ui.attach(container, name_lbl)?;
ui.attach(container, name_input)?;
ui.attach(container, name_err)?;
let email_lbl = ui.create(WidgetKind::Label, Some("Email Address: *"))?;
let email_input = ui.create(WidgetKind::TextInput, Some("developer@"))?;
let email_err = ui.create(
WidgetKind::Label,
Some("x Please enter a valid email address (e.g. dev@example.com)"),
)?;
ui.attach(container, email_lbl)?;
ui.attach(container, email_input)?;
ui.attach(container, email_err)?;
let submit_btn = ui.create(WidgetKind::Button, Some("Register Account"))?;
ui.attach(container, submit_btn)?;
ui.set_layout_root(window)?;
ui.show(window)?;
let mut driver = Driver::new(ui, noop_handler);
driver.pump()?;
driver.screenshot(CaptureTarget::Window(window))
}
#[cfg(not(feature = "widgets-text"))]
pub fn capture_scenario_form_validation() -> GuiResult<CaptureImage> {
Err(GuiError::UnsupportedWidget("TextInput"))
}
#[cfg(not(all(feature = "widgets-containers", feature = "widgets-choice")))]
pub fn capture_scenario_multi_state_tabs() -> GuiResult<CaptureImage> {
Err(GuiError::UnsupportedWidget("Tabs"))
}
#[cfg(all(feature = "widgets-containers", feature = "widgets-choice"))]
pub fn capture_scenario_multi_state_tabs() -> GuiResult<CaptureImage> {
let mut ui = Ui::new(MockBackend::default());
let window = ui.create(
WidgetKind::Window,
Some("System Configuration - Advanced Settings"),
)?;
let container = ui.create(WidgetKind::Container, None)?;
ui.set_container_direction(container, ContainerDirection::Vertical)?;
ui.attach(window, container)?;
let tabs = ui.create(WidgetKind::Tabs, None)?;
ui.attach(container, tabs)?;
ui.set_min_size(tabs, 520, 260)?;
let general = ui.create(WidgetKind::Container, None)?;
ui.set_container_direction(general, ContainerDirection::Vertical)?;
ui.attach(tabs, general)?;
let general_hint = ui.create(
WidgetKind::Label,
Some("Profile-wide defaults apply to every network location."),
)?;
ui.attach(general, general_hint)?;
let network = ui.create(WidgetKind::Container, None)?;
ui.set_container_direction(network, ContainerDirection::Vertical)?;
ui.attach(tabs, network)?;
let network_hint = ui.create(
WidgetKind::Label,
Some("Auto-detect proxy settings (WPAD) for the active interface."),
)?;
ui.attach(network, network_hint)?;
let pane = ui.create(WidgetKind::Container, None)?;
ui.set_container_direction(pane, ContainerDirection::Vertical)?;
ui.attach(tabs, pane)?;
let desc = ui.create(
WidgetKind::Label,
Some("Hardware Acceleration & Diagnostics Subsystem:"),
)?;
let chk_gpu = ui.create(
WidgetKind::Checkbox,
Some("Enable High-DPI Vulkan Compositor"),
)?;
ui.set_checked(chk_gpu, true)?;
let chk_logs = ui.create(
WidgetKind::Checkbox,
Some("Verbose Ring Buffer Event Tracing"),
)?;
ui.set_checked(chk_logs, true)?;
#[cfg(feature = "widgets-range")]
let slider_lbl = ui.create(WidgetKind::Label, Some("Memory Pool Size: 512 MB"))?;
#[cfg(feature = "widgets-range")]
let slider = ui.create(WidgetKind::Slider, None)?;
#[cfg(feature = "widgets-range")]
ui.set_range(slider, 64, 2048)?;
#[cfg(feature = "widgets-range")]
ui.set_value(slider, 512)?;
ui.attach(pane, desc)?;
ui.attach(pane, chk_gpu)?;
ui.attach(pane, chk_logs)?;
#[cfg(feature = "widgets-range")]
ui.attach(pane, slider_lbl)?;
#[cfg(feature = "widgets-range")]
ui.attach(pane, slider)?;
let tab_labels = vec![
"General".to_string(),
"Network & Proxy".to_string(),
"Advanced Diagnostics (Active)".to_string(),
];
ui.set_tab_items(tabs, &tab_labels)?;
ui.set_selected_index(tabs, 2)?;
ui.set_layout_root(window)?;
ui.show(window)?;
let mut driver = Driver::new(ui, noop_handler);
driver.pump()?;
driver.screenshot(CaptureTarget::Window(window))
}
pub fn capture_scenario_by_id(id: &str) -> GuiResult<CaptureImage> {
match id {
"gui_widget_alert_dialog" => capture_scenario_alert_dialog(),
"gui_dialog_confirm" => capture_scenario_confirm_dialog(),
"gui_widget_file_chooser_dialog" => capture_scenario_file_chooser(),
"gui_widget_color_dialog" => capture_scenario_color_picker(),
"gui_widget_font_dialog" => capture_scenario_font_picker(),
"gui_form_validation" => capture_scenario_form_validation(),
"gui_multi_state_tabs" => capture_scenario_multi_state_tabs(),
other => Err(GuiError::Backend(format!(
"e2e.gallery: unknown scenario id '{other}'"
))),
}
}
pub fn run_all_gallery_captures(
output_dir: impl AsRef<Path>,
) -> GuiResult<Vec<GalleryCaptureResult>> {
let out_dir = output_dir.as_ref();
std::fs::create_dir_all(out_dir)
.map_err(|e| GuiError::Backend(format!("e2e.gallery: failed to create output dir: {e}")))?;
let scenarios = standard_gallery_scenarios();
let mut results = Vec::new();
for spec in scenarios {
let img = capture_scenario_by_id(spec.id)?;
let file_name = format!("{}.png", spec.id);
let dest = out_dir.join(&file_name);
img.save_png(&dest)?;
let size_bytes = std::fs::metadata(&dest)
.map(|m| m.len() as usize)
.unwrap_or(0);
results.push(GalleryCaptureResult {
spec,
width: img.width,
height: img.height,
output_path: Some(dest),
size_bytes,
});
}
Ok(results)
}