commit cf074272049cb181333e424e1402498c0cff8816
parent 03e751011b7316b46bac37577e56fd9ba012042c
Author: kernelkind <kernelkind@gmail.com>
Date: Fri, 12 Apr 2024 13:02:26 -0400
Refactor 'ui tests' conception to previews
Signed-off-by: kernelkind <kernelkind@gmail.com>
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
9 files changed, 134 insertions(+), 81 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
@@ -104,5 +104,5 @@ name = "notedeck"
path = "src/bin/notedeck.rs"
[[bin]]
-name = "ui_test_harness"
-path = "src/tests_ui/main.rs"
+name = "ui_preview"
+path = "src/ui_preview/main.rs"
diff --git a/src/account_login_view.rs b/src/account_login_view.rs
@@ -7,15 +7,15 @@ use egui::{
};
use egui::{Image, TextBuffer, TextEdit};
-pub struct AccountLoginView<'a> {
+pub struct DesktopAccountLoginView<'a> {
ctx: &'a egui::Context,
manager: &'a mut LoginManager,
generate_y_intercept: Option<f32>,
}
-impl<'a> AccountLoginView<'a> {
+impl<'a> DesktopAccountLoginView<'a> {
pub fn new(ctx: &'a egui::Context, manager: &'a mut LoginManager) -> Self {
- AccountLoginView {
+ DesktopAccountLoginView {
ctx,
manager,
generate_y_intercept: None,
diff --git a/src/app_creation.rs b/src/app_creation.rs
@@ -4,16 +4,38 @@ use eframe::NativeOptions;
pub const UI_SCALE_FACTOR: f32 = 0.2;
pub fn generate_native_options() -> NativeOptions {
- let window_builder = Box::new(|builder: egui::ViewportBuilder| {
+ generate_native_options_with_builder_modifiers(|builder| {
builder
.with_fullsize_content_view(true)
.with_titlebar_shown(false)
.with_title_shown(false)
- .with_min_inner_size([660.0 * (1.0 + UI_SCALE_FACTOR) , 720.0 * (1.0 + UI_SCALE_FACTOR)])
- });
- let mut native_options = eframe::NativeOptions::default();
- native_options.window_builder = Some(window_builder);
- native_options
+ .with_min_inner_size([
+ 660.0 * (1.0 + UI_SCALE_FACTOR),
+ 720.0 * (1.0 + UI_SCALE_FACTOR),
+ ])
+ })
+}
+
+fn generate_native_options_with_builder_modifiers(apply_builder_modifiers: fn(egui::ViewportBuilder) -> egui::ViewportBuilder) -> NativeOptions {
+ let window_builder =
+ Box::new(
+ move |builder: egui::ViewportBuilder| apply_builder_modifiers(builder)
+ );
+
+ eframe::NativeOptions {
+ window_builder: Some(window_builder),
+ ..Default::default()
+ }
+}
+
+pub fn generate_mobile_emulator_native_options() -> eframe::NativeOptions {
+ generate_native_options_with_builder_modifiers(|builder| {
+ builder
+ .with_fullsize_content_view(true)
+ .with_titlebar_shown(false)
+ .with_title_shown(false)
+ .with_inner_size([405.0, 915.0])
+ })
}
pub fn setup_cc(cc: &eframe::CreationContext<'_>) {
diff --git a/src/tests_ui/account_login_view_test.rs b/src/tests_ui/account_login_view_test.rs
@@ -1,21 +0,0 @@
-use crate::egui_test_setup::{EguiTestCase, EguiTestSetup};
-use notedeck::account_login_view::AccountLoginView;
-use notedeck::login_manager::LoginManager;
-
-pub struct AccountLoginTest {
- manager: LoginManager,
-}
-
-impl EguiTestCase for AccountLoginTest {
- fn new(_supr: EguiTestSetup) -> Self {
- AccountLoginTest {
- manager: LoginManager::new(),
- }
- }
-}
-
-impl eframe::App for AccountLoginTest {
- fn update(&mut self, ctx: &egui::Context, _: &mut eframe::Frame) {
- AccountLoginView::new(ctx, &mut self.manager).panel()
- }
-}
diff --git a/src/tests_ui/egui_test_setup.rs b/src/tests_ui/egui_test_setup.rs
@@ -1,15 +0,0 @@
-use notedeck::app_creation::setup_cc;
-
-pub struct EguiTestSetup {}
-
-pub trait EguiTestCase: eframe::App {
- fn new(supr: EguiTestSetup) -> Self;
-}
-
-impl EguiTestSetup {
- pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
- setup_cc(cc);
-
- EguiTestSetup {}
- }
-}
diff --git a/src/tests_ui/main.rs b/src/tests_ui/main.rs
@@ -1,34 +0,0 @@
-mod account_login_view_test;
-mod egui_test_setup;
-use account_login_view_test::AccountLoginTest;
-use egui_test_setup::{EguiTestCase, EguiTestSetup};
-use notedeck::app_creation::generate_native_options;
-use std::env;
-
-fn run_test_app<F, T, O>(create_supr: F, create_child: O)
-where
- F: 'static + FnOnce(&eframe::CreationContext<'_>) -> EguiTestSetup,
- T: 'static + EguiTestCase,
- O: 'static + FnOnce(EguiTestSetup) -> T,
-{
- tracing_subscriber::fmt::init();
-
- let _ = eframe::run_native(
- "UI Test Harness",
- generate_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() {
- "AccountLoginView" => run_test_app(EguiTestSetup::new, AccountLoginTest::new),
- _ => println!("Component not found."),
- }
- } else {
- println!("Please specify a component to test.");
- }
-}
diff --git a/src/ui_preview/account_login_preview.rs b/src/ui_preview/account_login_preview.rs
@@ -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()
+ }
+}+
\ No newline at end of file
diff --git a/src/ui_preview/egui_preview_setup.rs b/src/ui_preview/egui_preview_setup.rs
@@ -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 {}
+ }
+}
diff --git a/src/ui_preview/main.rs b/src/ui_preview/main.rs
@@ -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.");
+ }
+}