Skip to content

Commit

Permalink
Add glow::Context to epi::App::setup
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Mar 13, 2022
1 parent a382d73 commit 39e2c7d
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions eframe/examples/custom_font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl epi::App for MyApp {
ctx: &egui::Context,
_frame: &epi::Frame,
_storage: Option<&dyn epi::Storage>,
_gl: &std::rc::Rc<epi::glow::Context>,
) {
// Start with the default fonts (we will be adding to them rather than replacing them).
let mut fonts = egui::FontDefinitions::default();
Expand Down
10 changes: 7 additions & 3 deletions egui-winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ default = ["clipboard", "dark-light", "links"]
# if disabled a clipboard will be simulated so you can still copy/paste within the egui app.
clipboard = ["copypasta"]

# implement bytemuck on most types.
convert_bytemuck = ["egui/convert_bytemuck"]

# Only for `egui_glow` - the official eframe/epi backend.
epi_backend = ["epi", "glow"]

# enable opening links in a browser when an egui hyperlink is clicked.
links = ["webbrowser"]

Expand All @@ -36,9 +42,6 @@ persistence = [
] # can't add epi/persistence here because of https://github.com/rust-lang/cargo/issues/8832
serialize = ["egui/serialize", "serde"]

# implement bytemuck on most types.
convert_bytemuck = ["egui/convert_bytemuck"]


[dependencies]
egui = { version = "0.17.0", path = "../egui", default-features = false, features = [
Expand All @@ -53,6 +56,7 @@ epi = { version = "0.17.0", path = "../epi", optional = true }

copypasta = { version = "0.7", optional = true }
dark-light = { version = "0.2.1", optional = true } # detect dark mode system preference
glow = { version = "0.11", optional = true }
serde = { version = "1.0", optional = true, features = ["derive"] }
webbrowser = { version = "0.6", optional = true }

Expand Down
7 changes: 4 additions & 3 deletions egui-winit/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ impl EpiIntegration {
integration_name: &'static str,
max_texture_side: usize,
window: &winit::window::Window,
gl: &std::rc::Rc<glow::Context>,
repaint_signal: std::sync::Arc<dyn epi::backend::RepaintSignal>,
persistence: crate::epi::Persistence,
app: Box<dyn epi::App>,
Expand Down Expand Up @@ -271,17 +272,17 @@ impl EpiIntegration {
can_drag_window: false,
};

slf.setup(window);
slf.setup(window, gl);
if slf.app.warm_up_enabled() {
slf.warm_up(window);
}

slf
}

fn setup(&mut self, window: &winit::window::Window) {
fn setup(&mut self, window: &winit::window::Window, gl: &std::rc::Rc<glow::Context>) {
self.app
.setup(&self.egui_ctx, &self.frame, self.persistence.storage());
.setup(&self.egui_ctx, &self.frame, self.persistence.storage(), gl);
let app_output = self.frame.take_app_output();

if app_output.quit {
Expand Down
1 change: 1 addition & 0 deletions egui_demo_lib/src/apps/demo/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl epi::App for DemoApp {
_ctx: &egui::Context,
_frame: &epi::Frame,
_storage: Option<&dyn epi::Storage>,
_gl: &std::rc::Rc<epi::glow::Context>,
) {
#[cfg(feature = "persistence")]
if let Some(storage) = _storage {
Expand Down
1 change: 1 addition & 0 deletions egui_demo_lib/src/wrap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl epi::App for WrapApp {
_ctx: &egui::Context,
_frame: &epi::Frame,
_storage: Option<&dyn epi::Storage>,
_gl: &std::rc::Rc<epi::glow::Context>,
) {
#[cfg(feature = "persistence")]
if let Some(storage) = _storage {
Expand Down
2 changes: 1 addition & 1 deletion egui_glow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ tracing = "0.1"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
egui-winit = { version = "0.17.0", path = "../egui-winit", optional = true, default-features = false, features = [
"dark-light",
"epi",
"epi_backend",
] }
glutin = { version = "0.28.0", optional = true }

Expand Down
1 change: 1 addition & 0 deletions egui_glow/src/epi_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
"egui_glow",
painter.max_texture_side(),
gl_window.window(),
&gl,
repaint_signal,
persistence,
app,
Expand Down
9 changes: 4 additions & 5 deletions egui_web/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,10 @@ impl AppRunner {

runner.input.raw.max_texture_side = Some(runner.painter.max_texture_side());

{
runner
.app
.setup(&runner.egui_ctx, &runner.frame, Some(&runner.storage));
}
let gl = runner.painter.painter.gl();
runner
.app
.setup(&runner.egui_ctx, &runner.frame, Some(&runner.storage), gl);

Ok(runner)
}
Expand Down
1 change: 1 addition & 0 deletions epi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ persistence = ["ron", "serde", "egui/persistence"]
egui = { version = "0.17.0", path = "../egui", default-features = false, features = [
"single_threaded",
] }
glow = "0.11"
tracing = "0.1"

directories-next = { version = "2", optional = true }
Expand Down
17 changes: 14 additions & 3 deletions epi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
pub mod file_storage;

pub use egui; // Re-export for user convenience
pub use glow; // Re-export for user convenience

use std::sync::{Arc, Mutex};

Expand All @@ -112,13 +113,23 @@ pub trait App {
/// or call [`Frame::request_repaint`] at any time (e.g. from another thread).
fn update(&mut self, ctx: &egui::Context, frame: &Frame);

/// Called once before the first frame.
/// Called exactly once at startup, before any call to [`Self::update`].
///
/// Allows you to do setup code, e.g to call [`egui::Context::set_fonts`],
/// [`egui::Context::set_visuals`] etc.
///
/// Also allows you to restore state, if there is a storage (required the "persistence" feature).
fn setup(&mut self, _ctx: &egui::Context, _frame: &Frame, _storage: Option<&dyn Storage>) {}
/// Also allows you to restore state, if there is a storage (requires the "persistence" feature).
///
/// The [`glow::Context`] allows you to initialize OpenGL resources (e.g. shaders) that
/// you might want to use later from a [`egui::PaintCallback`].
fn setup(
&mut self,
_ctx: &egui::Context,
_frame: &Frame,
_storage: Option<&dyn Storage>,
_gl: &std::rc::Rc<glow::Context>,
) {
}

/// Called on shutdown, and perhaps at regular intervals. Allows you to save state.
///
Expand Down

0 comments on commit 39e2c7d

Please sign in to comment.