previews: run previews as notedeck apps

This allows ./preview to be a notedeck app runner. I am currently
using it for the ProfilePic app (which will because notedeck_viz)

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-12-20 15:39:26 -08:00
parent 475314da75
commit fcac49a0a5
19 changed files with 222 additions and 238 deletions

View File

@@ -25,7 +25,38 @@ pub struct Notedeck {
theme: ThemeHandler,
tabs: Tabs,
app_rect_handler: AppSizeHandler,
egui: egui::Context,
}
fn margin_top(narrow: bool) -> f32 {
#[cfg(target_os = "android")]
{
// FIXME - query the system bar height and adjust more precisely
let _ = narrow; // suppress compiler warning on android
40.0
}
#[cfg(not(target_os = "android"))]
{
if narrow {
50.0
} else {
0.0
}
}
}
/// Our chrome, which is basically nothing
fn main_panel(style: &egui::Style, narrow: bool) -> egui::CentralPanel {
let inner_margin = egui::Margin {
top: margin_top(narrow),
left: 0.0,
right: 0.0,
bottom: 0.0,
};
egui::CentralPanel::default().frame(egui::Frame {
inner_margin,
fill: style.visuals.panel_fill,
..Default::default()
})
}
impl eframe::App for Notedeck {
@@ -36,19 +67,34 @@ impl eframe::App for Notedeck {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
// TODO: render chrome
#[cfg(feature = "profiling")]
puffin::GlobalProfiler::lock().new_frame();
// render app
if let Some(app) = &self.tabs.app {
let app = app.clone();
app.borrow_mut().update(&mut self.app_context());
}
main_panel(&ctx.style(), notedeck::ui::is_narrow(ctx)).show(ctx, |ui| {
// render app
if let Some(app) = &self.tabs.app {
let app = app.clone();
app.borrow_mut().update(&mut self.app_context(), ui);
}
});
self.app_rect_handler.try_save_app_size(ctx);
#[cfg(feature = "profiling")]
puffin_egui::profiler_window(ctx);
}
}
#[cfg(feature = "profiling")]
fn setup_profiling() {
puffin::set_scopes_on(true); // tell puffin to collect data
}
impl Notedeck {
pub fn new<P: AsRef<Path>>(ctx: &egui::Context, data_path: P, args: &[String]) -> Self {
#[cfg(feature = "profiling")]
setup_profiling();
let parsed_args = Args::parse(args);
let is_mobile = parsed_args
.is_mobile
@@ -139,7 +185,6 @@ impl Notedeck {
let img_cache = ImageCache::new(imgcache_dir);
let note_cache = NoteCache::default();
let unknown_ids = UnknownIds::default();
let egui = ctx.clone();
let tabs = Tabs::new(None);
let parsed_args = Args::parse(args);
let app_rect_handler = AppSizeHandler::new(&path);
@@ -155,7 +200,6 @@ impl Notedeck {
path: path.clone(),
args: parsed_args,
theme,
egui,
tabs,
}
}
@@ -171,7 +215,6 @@ impl Notedeck {
path: &self.path,
args: &self.args,
theme: &mut self.theme,
egui: &self.egui,
}
}

View File

@@ -3,8 +3,6 @@ use notedeck_chrome::{setup::generate_native_options, Notedeck};
use notedeck::{DataPath, DataPathType};
use notedeck_columns::Damus;
use std::path::PathBuf;
use std::str::FromStr;
use tracing_subscriber::EnvFilter;
// Entry point for wasm
@@ -64,8 +62,8 @@ fn setup_logging(path: &DataPath) {
#[cfg(not(target_arch = "wasm32"))]
#[tokio::main]
async fn main() {
let base_path = DataPath::default_base().unwrap_or(PathBuf::from_str(".").unwrap());
let path = DataPath::new(&base_path);
let base_path = DataPath::default_base_or_cwd();
let path = DataPath::new(base_path.clone());
setup_logging(&path);

View File

@@ -1,7 +1,6 @@
use notedeck::DataPath;
use notedeck_chrome::setup::{
generate_mobile_emulator_native_options, generate_native_options, setup_cc,
};
use notedeck_chrome::setup::generate_native_options;
use notedeck_chrome::Notedeck;
use notedeck_columns::ui::configure_deck::ConfigureDeckView;
use notedeck_columns::ui::edit_deck::EditDeckView;
use notedeck_columns::ui::{
@@ -10,42 +9,32 @@ use notedeck_columns::ui::{
};
use std::env;
struct PreviewRunner {
force_mobile: bool,
light_mode: bool,
}
struct PreviewRunner {}
impl PreviewRunner {
fn new(force_mobile: bool, light_mode: bool) -> Self {
PreviewRunner {
force_mobile,
light_mode,
}
fn new() -> Self {
PreviewRunner {}
}
async fn run<P>(self, preview: P)
where
P: Into<PreviewApp> + 'static,
P: notedeck::App + 'static,
{
tracing_subscriber::fmt::init();
let native_options = if self.force_mobile {
generate_mobile_emulator_native_options()
} else {
// TODO: tmp preview pathbuf?
generate_native_options(DataPath::new("previews"))
};
let base_path = DataPath::default_base_or_cwd();
let path = DataPath::new(&base_path);
let is_mobile = self.force_mobile;
let light_mode = self.light_mode;
let _res = eframe::run_native(
"Notedeck Preview",
generate_native_options(path),
Box::new(|cc| {
let args: Vec<String> = std::env::args().collect();
let mut notedeck = Notedeck::new(&cc.egui_ctx, &base_path, &args);
let _ = eframe::run_native(
"UI Preview Runner",
native_options,
Box::new(move |cc| {
let app = Into::<PreviewApp>::into(preview);
setup_cc(&cc.egui_ctx, is_mobile, light_mode);
Ok(Box::new(app))
notedeck.add_app(PreviewApp::new(preview));
Ok(Box::new(notedeck))
}),
);
}
@@ -93,7 +82,7 @@ async fn main() {
if light_mode { "enabled" } else { "disabled" }
);
let is_mobile = is_mobile.unwrap_or(notedeck::ui::is_compiled_as_mobile());
let runner = PreviewRunner::new(is_mobile, light_mode);
let runner = PreviewRunner::new();
previews!(
runner,