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