Refactor 'ui tests' conception to previews

Signed-off-by: kernelkind <kernelkind@gmail.com>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
kernelkind
2024-04-12 13:02:26 -04:00
committed by William Casarin
parent 03e751011b
commit cf07427204
9 changed files with 133 additions and 81 deletions

View File

@@ -0,0 +1,39 @@
use crate::egui_preview_setup::{EguiPreviewCase, EguiPreviewSetup};
use notedeck::account_login_view::{DesktopAccountLoginView, MobileAccountLoginView};
use notedeck::login_manager::LoginManager;
pub struct DesktopAccountLoginPreview {
manager: LoginManager,
}
impl EguiPreviewCase for DesktopAccountLoginPreview {
fn new(_supr: EguiPreviewSetup) -> Self {
DesktopAccountLoginPreview {
manager: LoginManager::new(),
}
}
}
impl eframe::App for DesktopAccountLoginPreview {
fn update(&mut self, ctx: &egui::Context, _: &mut eframe::Frame) {
DesktopAccountLoginView::new(ctx, &mut self.manager).panel()
}
}
pub struct MobileAccountLoginPreview {
manager: LoginManager,
}
impl EguiPreviewCase for MobileAccountLoginPreview {
fn new(_supr: EguiPreviewSetup) -> Self {
MobileAccountLoginPreview {
manager: LoginManager::new(),
}
}
}
impl eframe::App for MobileAccountLoginPreview {
fn update(&mut self, ctx: &egui::Context, _: &mut eframe::Frame) {
MobileAccountLoginView::new(ctx, &mut self.manager).panel()
}
}

View File

@@ -0,0 +1,15 @@
use notedeck::app_creation::setup_cc;
pub struct EguiPreviewSetup {}
pub trait EguiPreviewCase: eframe::App {
fn new(supr: EguiPreviewSetup) -> Self;
}
impl EguiPreviewSetup {
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
setup_cc(cc);
EguiPreviewSetup {}
}
}

46
src/ui_preview/main.rs Normal file
View File

@@ -0,0 +1,46 @@
mod account_login_preview;
mod egui_preview_setup;
use account_login_preview::{DesktopAccountLoginPreview, MobileAccountLoginPreview};
use egui_preview_setup::{EguiPreviewCase, EguiPreviewSetup};
use notedeck::app_creation::{generate_native_options, generate_mobile_emulator_native_options};
use std::env;
fn run_test_app<F, T, O>(create_supr: F, create_child: O, is_mobile: bool)
where
F: 'static + FnOnce(&eframe::CreationContext<'_>) -> EguiPreviewSetup,
T: 'static + EguiPreviewCase,
O: 'static + FnOnce(EguiPreviewSetup) -> T,
{
tracing_subscriber::fmt::init();
let native_options = if is_mobile {
generate_mobile_emulator_native_options()
} else {
generate_native_options()
};
let _ = eframe::run_native(
"UI Preview Runner",
native_options,
Box::new(|cc| Box::new(create_child(create_supr(cc)))),
);
}
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() > 1 {
match args[1].as_str() {
"DesktopAccountLoginPreview" => {
run_test_app(EguiPreviewSetup::new, DesktopAccountLoginPreview::new, false)
}
"MobileAccountLoginPreview" => {
run_test_app(EguiPreviewSetup::new, MobileAccountLoginPreview::new, true)
}
_ => println!("Component not found."),
}
} else {
println!("Please specify a component to test.");
}
}