Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eframe app creation refactor #1363

Merged
merged 11 commits into from
Mar 16, 2022
Merged

eframe app creation refactor #1363

merged 11 commits into from
Mar 16, 2022

Conversation

emilk
Copy link
Owner

@emilk emilk commented Mar 14, 2022

This refactors how eframe creates an app, removing the need for App::setup.

The purpose of this is to be able to access the following state before/during creation of ones app:

  • egui::Context: alllow users to set fonts, style and maybe create textures.
  • epi::Storage: for reading app state from storage.
  • epi::Frame: can be cloned and stored so we can run request_repaint.
  • glow::Context: for creating rendering resources.

Previously eframe was designed so that these were passed into App::setup, but that meant one would have to create an app before running the setup of it, making App::setup a weird "second constructor". This PR solves that problem.

App::name is now also removed, and is instead passed in by argument to eframe::run_native.

Additionally, eframe now re-exports everything in epi, so eframe users can ignore epi completely.


This now changes the eframe use to look like so:

EDIT: this has been edited to reflect the changes added in #1373

use eframe::egui;

#[cfg(not(target_arch = "wasm32"))]
fn main() {
    let native_options = eframe::NativeOptions::default();
    eframe::run_native("MyApp", native_options, Box::new(|cc| Box::new(MyEguiApp::new(cc))));
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn start(canvas_id: &str) -> Result<(), eframe::wasm_bindgen::JsValue> {
    eframe::start_web(canvas_id, Box::new(|cc| Box::new(MyApp::new(cc))))
}

#[derive(Default)]
struct MyEguiApp {}

impl MyEguiApp {
    fn new(cc: &eframe::CreationContext<'_>) -> Self {
        // Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_visuals.
        // Restore app state using cc.storage (requires the "persistence" feature).
        // Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use
        // for e.g. egui::PaintCallback.
        Self::default()
    }
}

impl eframe::App for MyEguiApp {
   fn update(&mut self, ctx: &egui::Context, frame: &eframe::Frame) {
       egui::CentralPanel::default().show(ctx, |ui| {
           ui.heading("Hello World!");
       });
   }
}

@emilk emilk force-pushed the eframe-app-creation-refactor branch 2 times, most recently from 5f4ee67 to bebca37 Compare March 14, 2022 16:30
@emilk emilk marked this pull request as ready for review March 15, 2022 08:31
@emilk emilk force-pushed the eframe-app-creation-refactor branch from bebca37 to 1f0e84a Compare March 15, 2022 16:22
@emilk emilk merged commit c8f6cae into master Mar 16, 2022
@emilk emilk deleted the eframe-app-creation-refactor branch March 16, 2022 14:39
@DusterTheFirst
Copy link
Contributor

This is a very welcome refactor! Thanks for the work you have put into this.

@emilk emilk mentioned this pull request Mar 18, 2022
@AngelOnFira
Copy link
Contributor

Sorry for the comment noise, just wanted to mention that it took me a while to find this while trying to understand why setup + name existed on ewebsocket's example app. This PR seems to give a good overview of how an app should be created; would it be worth specifying in some of eframe/epi's docs about how code used to look, so that people know how to migrate from setup if they need? If so, I can try upstreaming some docs.

I'm working on updating ewebsock's example app, so hopefully confusion shouldn't spawn from there in the future :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants