preview.rs (2947B)
1 use notedeck::DataPath; 2 use notedeck_chrome::setup::{ 3 generate_mobile_emulator_native_options, generate_native_options, setup_cc, 4 }; 5 use notedeck_columns::ui::configure_deck::ConfigureDeckView; 6 use notedeck_columns::ui::edit_deck::EditDeckView; 7 use notedeck_columns::ui::{ 8 account_login_view::AccountLoginView, PostView, Preview, PreviewApp, PreviewConfig, ProfilePic, 9 ProfilePreview, RelayView, 10 }; 11 use std::env; 12 13 struct PreviewRunner { 14 force_mobile: bool, 15 light_mode: bool, 16 } 17 18 impl PreviewRunner { 19 fn new(force_mobile: bool, light_mode: bool) -> Self { 20 PreviewRunner { 21 force_mobile, 22 light_mode, 23 } 24 } 25 26 async fn run<P>(self, preview: P) 27 where 28 P: Into<PreviewApp> + 'static, 29 { 30 tracing_subscriber::fmt::init(); 31 32 let native_options = if self.force_mobile { 33 generate_mobile_emulator_native_options() 34 } else { 35 // TODO: tmp preview pathbuf? 36 generate_native_options(DataPath::new("previews")) 37 }; 38 39 let is_mobile = self.force_mobile; 40 let light_mode = self.light_mode; 41 42 let _ = eframe::run_native( 43 "UI Preview Runner", 44 native_options, 45 Box::new(move |cc| { 46 let app = Into::<PreviewApp>::into(preview); 47 setup_cc(&cc.egui_ctx, is_mobile, light_mode); 48 Ok(Box::new(app)) 49 }), 50 ); 51 } 52 } 53 54 macro_rules! previews { 55 // Accept a runner and name variable, followed by one or more identifiers for the views 56 ($runner:expr, $name:expr, $is_mobile:expr, $($view:ident),* $(,)?) => { 57 match $name.as_ref() { 58 $( 59 stringify!($view) => { 60 $runner.run($view::preview(PreviewConfig { is_mobile: $is_mobile })).await; 61 } 62 )* 63 _ => println!("Component not found."), 64 } 65 }; 66 } 67 68 #[tokio::main] 69 async fn main() { 70 let mut name: Option<String> = None; 71 let mut is_mobile: Option<bool> = None; 72 let mut light_mode: bool = false; 73 74 for arg in env::args() { 75 if arg == "--mobile" { 76 is_mobile = Some(true); 77 } else if arg == "--light" { 78 light_mode = true; 79 } else { 80 name = Some(arg); 81 } 82 } 83 84 let name = if let Some(name) = name { 85 name 86 } else { 87 println!("Please specify a component to test"); 88 return; 89 }; 90 91 println!( 92 "light mode previews: {}", 93 if light_mode { "enabled" } else { "disabled" } 94 ); 95 let is_mobile = is_mobile.unwrap_or(notedeck::ui::is_compiled_as_mobile()); 96 let runner = PreviewRunner::new(is_mobile, light_mode); 97 98 previews!( 99 runner, 100 name, 101 is_mobile, 102 RelayView, 103 AccountLoginView, 104 ProfilePreview, 105 ProfilePic, 106 PostView, 107 ConfigureDeckView, 108 EditDeckView, 109 ); 110 }