galileo icon indicating copy to clipboard operation
galileo copied to clipboard

Change galileo-egui initializer to a builder pattern so native/web options can be specified.

Open frewsxcv opened this issue 7 months ago • 2 comments

Thanks for making galileo! I'm using it for an upcoming project, and I need the ability to specify native and web options for eframe. This was my attempt at changing the galileo-egui initializer to use a builder pattern, so it's more customizable. Curious what you think! Open to other ideas

Before

galileo_egui::init_with_app(Box::new(|cc| {
    Ok(Box::new(App::new(map, layer, cc, handler)))
}))
.expect("failed to initialize");

After

galileo_egui::InitBuilder::new(map)
    .with_handlers([Box::new(handler) as Box<dyn UserEventHandler>])
    .with_app_builder(|egui_map_state| Box::new(App::new(egui_map_state, layer)))
    .with_native_options(<NATIVE_OPTIONS>)
    .init()
    .expect("failed to initialize");

frewsxcv avatar May 12 '25 04:05 frewsxcv

Hi, Corey. I do agree that the way you implemented it is more convenient and customizable. But the sole purpose of the init module in galileo-egui crate is to provide a simple way to initialize map in the galileo example in a cross-platform way. I doubt very much that any real application would need such a structure to bootstrap the application. I was going to write this in the documentation for the module, but slacked on it...

To tell the truth, I cannot even imagine a useful initialization builder structure for both Wasm and native at the same time. For Wasm you would usually want to set a DOM container for your application, load fonts though ajax, configure the map to limit in-memory caches. For native you need to set up FS caches, read data and configs from disc etc. It is usually just much easier to have two separate crates for those two targets that do all the initialization for the given platform, and then call you app code from your app's core crate.

Having said that, I'm open for discussion if you have some use case in mind that would make sense for a general-case application library.

Maximkaaa avatar May 12 '25 12:05 Maximkaaa

For native you need to set up FS caches, read data and configs from disc etc. It is usually just much easier to have two separate crates for those two targets that do all the initialization for the given platform, and then call you app code from your app's core crate.

If a user of galileo (like myself) is working on a cross-platform crate, then specifying the options is straightforward. Having two separate crates would be much more complicated.

let builder = galileo_egui::InitBuilder::new(map)
    .with_handlers([Box::new(handler) as Box<dyn UserEventHandler>])
    .with_app_builder(|egui_map_state| Box::new(App::new(egui_map_state, layer)));

#[cfg(not(target_arch = "wasm32"))]
{
    builder = builder.with_native_options(<NATIVE_OPTIONS>);
}

#[cfg(target_arch = "wasm32")]
{
    builder = builder.with_web_options(<WEB_OPTIONS>);
}

builder.init().expect("failed to initialize");

My main goal with this is to specify some of the eframe::WebOptions and eframe::NativeOptions options, instead of them being hardcoded to specific values in the galileo crate.

frewsxcv avatar May 12 '25 13:05 frewsxcv

After thinking about this for a while, I tend to agree, that since this API is provided by the library, it should be convenient, even if it will only be used for simple demo/test applications. So, if you correct the problems found by the CI, we can merge this PR.

The wasm issue should be resolved if you merge the current main in.

Maximkaaa avatar May 31 '25 05:05 Maximkaaa

After thinking about this for a while, I tend to agree, that since this API is provided by the library, it should be convenient, even if it will only be used for simple demo/test applications. So, if you correct the problems found by the CI, we can merge this PR.

The wasm issue should be resolved if you merge the current main in.

Yay! I'll do this now

frewsxcv avatar Jun 03 '25 13:06 frewsxcv

When you have a moment can you approve CI running?

frewsxcv avatar Jun 03 '25 14:06 frewsxcv

@Maximkaaa I need you to approve CI again.

frewsxcv avatar Jun 04 '25 13:06 frewsxcv

@Maximkaaa Pushed the last change hopefully. Can you run the PR for CI? Also is there a reason why you have that setting on this repo?

frewsxcv avatar Jun 05 '25 12:06 frewsxcv

Never looked into the settings for this. Changed it to be more permissive, so CI should start automatically now for most users.

Maximkaaa avatar Jun 05 '25 13:06 Maximkaaa