Skip to content

Commit

Permalink
Refactor: move things into eframe (#1542)
Browse files Browse the repository at this point in the history
* Move all epi-related code from egui_glow into eframe

* Move epi stuff from egui-winit into eframe

* Remove mention of epi in egui

* Remove mention of epi in egui_glium

* Remove trait epi::NativeTexture

* Remove confusing mentions of epi

* Refactor egui_web: break up into smaller files

* Clean up feature flags further, and update changelogs

* Clean up check.sh

* Small cleanup of egui_web/Cargo.toml

* Fix dependencies for pure_glow example

* Fix clippy false positive
  • Loading branch information
emilk authored Apr 29, 2022
1 parent 5ea51c3 commit ed002ac
Show file tree
Hide file tree
Showing 27 changed files with 796 additions and 802 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion eframe/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ NOTE: [`egui_web`](../egui_web/CHANGELOG.md), [`egui-winit`](../egui-winit/CHANG
* Changed `App::update` to take `&mut Frame` instead of `&Frame`.
* `Frame` is no longer `Clone` or `Sync`.
* Add `glow` (OpenGL) context to `Frame` ([#1425](https://github.com/emilk/egui/pull/1425)).
* `dark-light` (dark mode detection) is now an opt-in feature ([#1437](https://github.com/emilk/egui/pull/1437)).
* Fixed potential scale bug when DPI scaling changes (e.g. when dragging a window between different displays) ([#1441](https://github.com/emilk/egui/pull/1441)).
* MSRV (Minimum Supported Rust Version) is now `1.60.0` ([#1467](https://github.com/emilk/egui/pull/1467)).
* `dark-light` (dark mode detection) is now an opt-in feature ([#1437](https://github.com/emilk/egui/pull/1437)).
* Added new feature `puffin` to add [`puffin profiler`](https://github.com/EmbarkStudios/puffin) scopes ([#1483](https://github.com/emilk/egui/pull/1483)).
* Moved app persistence to a background thread, allowing for smoother frame rates (on native).

Expand Down
17 changes: 9 additions & 8 deletions eframe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,27 @@ all-features = true
default = ["default_fonts"]

# detect dark mode system preference
dark-light = ["egui-winit/dark-light"]
dark-light = ["dep:dark-light"]

# If set, egui will use `include_bytes!` to bundle some fonts.
# If you plan on specifying your own fonts you may disable this feature.
default_fonts = ["egui/default_fonts"]

# Enable saving app state to disk.
persistence = [
"egui-winit/persistence",
"egui_glow/persistence",
"egui-winit/serde",
"egui/persistence",
"epi/persistence",
]

# Enable profiling with the puffin crate: https://github.com/EmbarkStudios/puffin
# Only enabled on native, because of the low resolution (1ms) of time keeping in browsers.
# eframe will call `puffin::GlobalProfiler::lock().new_frame()` for you
puffin = ["egui_glow/puffin"]
puffin = ["dep:puffin", "egui_glow/puffin"]

# enable screen reader support (requires `ctx.options().screen_reader = true;`)
screen_reader = [
"egui-winit/screen_reader",
"egui_glow/screen_reader",
"egui_web/screen_reader",
]

Expand All @@ -58,11 +56,14 @@ epi = { version = "0.17.0", path = "../epi" }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
egui_glow = { version = "0.17.0", path = "../egui_glow", default-features = false, features = [
"clipboard",
"epi",
"links",
"winit",
] }
egui-winit = { version = "0.17.0", path = "../egui-winit", default-features = false }
dark-light = { version = "0.2.1", optional = true }
egui-winit = { version = "0.17.0", path = "../egui-winit", default-features = false, features = ["clipboard", "links"] }
glow = "0.11"
glutin = { version = "0.28.0" }
puffin = { version = "0.13", optional = true }
winit = "0.26.1"

# web:
[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
29 changes: 28 additions & 1 deletion eframe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ pub fn start_web(canvas_id: &str, app_creator: AppCreator) -> Result<(), wasm_bi
// ----------------------------------------------------------------------------
// When compiling natively

#[cfg(not(target_arch = "wasm32"))]
mod native;

/// This is how you start a native (desktop) app.
///
/// The first argument is name of your app, used for the title bar of the native window
Expand Down Expand Up @@ -135,5 +138,29 @@ pub fn start_web(canvas_id: &str, app_creator: AppCreator) -> Result<(), wasm_bi
#[cfg(not(target_arch = "wasm32"))]
#[allow(clippy::needless_pass_by_value)]
pub fn run_native(app_name: &str, native_options: NativeOptions, app_creator: AppCreator) -> ! {
egui_glow::run(app_name, &native_options, app_creator)
native::run(app_name, &native_options, app_creator)
}

// ---------------------------------------------------------------------------

/// Profiling macro for feature "puffin"
#[cfg(not(target_arch = "wasm32"))]
macro_rules! profile_function {
($($arg: tt)*) => {
#[cfg(feature = "puffin")]
puffin::profile_function!($($arg)*);
};
}
#[cfg(not(target_arch = "wasm32"))]
pub(crate) use profile_function;

/// Profiling macro for feature "puffin"
#[cfg(not(target_arch = "wasm32"))]
macro_rules! profile_scope {
($($arg: tt)*) => {
#[cfg(feature = "puffin")]
puffin::profile_scope!($($arg)*);
};
}
#[cfg(not(target_arch = "wasm32"))]
pub(crate) use profile_scope;
28 changes: 15 additions & 13 deletions egui-winit/src/epi.rs → eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use egui_winit::{native_pixels_per_point, WindowSettings};

pub fn points_to_size(points: egui::Vec2) -> winit::dpi::LogicalSize<f64> {
winit::dpi::LogicalSize {
width: points.x as f64,
Expand All @@ -7,7 +9,7 @@ pub fn points_to_size(points: egui::Vec2) -> winit::dpi::LogicalSize<f64> {

pub fn window_builder(
native_options: &epi::NativeOptions,
window_settings: &Option<crate::WindowSettings>,
window_settings: &Option<WindowSettings>,
) -> winit::window::WindowBuilder {
let epi::NativeOptions {
always_on_top,
Expand Down Expand Up @@ -109,7 +111,7 @@ pub fn handle_app_output(
width: (current_pixels_per_point * window_size.x).round(),
height: (current_pixels_per_point * window_size.y).round(),
}
.to_logical::<f32>(crate::native_pixels_per_point(window) as f64),
.to_logical::<f32>(native_pixels_per_point(window) as f64),
);
}

Expand Down Expand Up @@ -145,10 +147,10 @@ pub fn create_storage(_app_name: &str) -> Option<Box<dyn epi::Storage>> {
/// Everything needed to make a winit-based integration for [`epi`].
pub struct EpiIntegration {
pub frame: epi::Frame,
last_auto_save: instant::Instant,
last_auto_save: std::time::Instant,
pub egui_ctx: egui::Context,
pending_full_output: egui::FullOutput,
egui_winit: crate::State,
egui_winit: egui_winit::State,
/// When set, it is time to quit
quit: bool,
can_drag_window: bool,
Expand All @@ -174,7 +176,7 @@ impl EpiIntegration {
web_info: None,
prefer_dark_mode,
cpu_usage: None,
native_pixels_per_point: Some(crate::native_pixels_per_point(window)),
native_pixels_per_point: Some(native_pixels_per_point(window)),
},
output: Default::default(),
storage,
Expand All @@ -189,9 +191,9 @@ impl EpiIntegration {

Self {
frame,
last_auto_save: instant::Instant::now(),
last_auto_save: std::time::Instant::now(),
egui_ctx,
egui_winit: crate::State::new(max_texture_side, window),
egui_winit: egui_winit::State::new(max_texture_side, window),
pending_full_output: Default::default(),
quit: false,
can_drag_window: false,
Expand Down Expand Up @@ -235,7 +237,7 @@ impl EpiIntegration {
app: &mut dyn epi::App,
window: &winit::window::Window,
) -> egui::FullOutput {
let frame_start = instant::Instant::now();
let frame_start = std::time::Instant::now();

let raw_input = self.egui_winit.take_egui_input(window);
let full_output = self.egui_ctx.run(raw_input, |egui_ctx| {
Expand All @@ -252,10 +254,10 @@ impl EpiIntegration {
if app_output.quit {
self.quit = app.on_exit_event();
}
crate::epi::handle_app_output(window, self.egui_ctx.pixels_per_point(), app_output);
handle_app_output(window, self.egui_ctx.pixels_per_point(), app_output);
}

let frame_time = (instant::Instant::now() - frame_start).as_secs_f64() as f32;
let frame_time = (std::time::Instant::now() - frame_start).as_secs_f64() as f32;
self.frame.info.cpu_usage = Some(frame_time);

full_output
Expand All @@ -274,7 +276,7 @@ impl EpiIntegration {
// Persistance stuff:

pub fn maybe_autosave(&mut self, app: &mut dyn epi::App, window: &winit::window::Window) {
let now = instant::Instant::now();
let now = std::time::Instant::now();
if now - self.last_auto_save > app.auto_save_interval() {
self.save(app, window);
self.last_auto_save = now;
Expand All @@ -291,7 +293,7 @@ impl EpiIntegration {
epi::set_value(
storage,
STORAGE_WINDOW_KEY,
&crate::WindowSettings::from_display(_window),
&WindowSettings::from_display(_window),
);
}
if _app.persist_egui_memory() {
Expand All @@ -314,7 +316,7 @@ const STORAGE_EGUI_MEMORY_KEY: &str = "egui";
#[cfg(feature = "persistence")]
const STORAGE_WINDOW_KEY: &str = "window";

pub fn load_window_settings(_storage: Option<&dyn epi::Storage>) -> Option<crate::WindowSettings> {
pub fn load_window_settings(_storage: Option<&dyn epi::Storage>) -> Option<WindowSettings> {
#[cfg(feature = "persistence")]
{
epi::get_value(_storage?, STORAGE_WINDOW_KEY)
Expand Down
4 changes: 4 additions & 0 deletions eframe/src/native/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod epi_integration;
mod run;

pub use run::run;
13 changes: 7 additions & 6 deletions egui_glow/src/epi_backend.rs → eframe/src/native/run.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::epi_integration;
use egui_winit::winit;

struct RequestRepaintEvent;
Expand Down Expand Up @@ -37,18 +38,18 @@ pub use epi::NativeOptions;
/// Run an egui app
#[allow(unsafe_code)]
pub fn run(app_name: &str, native_options: &epi::NativeOptions, app_creator: epi::AppCreator) -> ! {
let storage = egui_winit::epi::create_storage(app_name);
let window_settings = egui_winit::epi::load_window_settings(storage.as_deref());
let storage = epi_integration::create_storage(app_name);
let window_settings = epi_integration::load_window_settings(storage.as_deref());
let window_builder =
egui_winit::epi::window_builder(native_options, &window_settings).with_title(app_name);
epi_integration::window_builder(native_options, &window_settings).with_title(app_name);
let event_loop = winit::event_loop::EventLoop::with_user_event();
let (gl_window, gl) = create_display(native_options, window_builder, &event_loop);
let gl = std::rc::Rc::new(gl);

let mut painter = crate::Painter::new(gl.clone(), None, "")
let mut painter = egui_glow::Painter::new(gl.clone(), None, "")
.unwrap_or_else(|error| panic!("some OpenGL error occurred {}\n", error));

let mut integration = egui_winit::epi::EpiIntegration::new(
let mut integration = epi_integration::EpiIntegration::new(
"egui_glow",
gl.clone(),
painter.max_texture_side(),
Expand Down Expand Up @@ -94,7 +95,7 @@ pub fn run(app_name: &str, native_options: &epi::NativeOptions, app_creator: epi
crate::profile_scope!("frame");
let screen_size_in_pixels: [u32; 2] = gl_window.window().inner_size().into();

crate::painter::clear(&gl, screen_size_in_pixels, app.clear_color());
egui_glow::painter::clear(&gl, screen_size_in_pixels, app.clear_color());

let egui::FullOutput {
platform_output,
Expand Down
5 changes: 3 additions & 2 deletions egui-winit/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ All notable changes to the `egui-winit` integration will be noted in this file.

## Unreleased
* Reexport `egui` crate
* Renamed the feature `convert_bytemuck` to `bytemuck` ([#1467](https://github.com/emilk/egui/pull/1467)).
* Renamed the feature `serialize` to `serde` ([#1467](https://github.com/emilk/egui/pull/1467)).
* MSRV (Minimum Supported Rust Version) is now `1.60.0` ([#1467](https://github.com/emilk/egui/pull/1467)).
* Added new feature `puffin` to add [`puffin profiler`](https://github.com/EmbarkStudios/puffin) scopes ([#1483](https://github.com/emilk/egui/pull/1483)).
* Renamed the feature `convert_bytemuck` to `bytemuck` ([#1467](https://github.com/emilk/egui/pull/1467)).
* Renamed the feature `serialize` to `serde` ([#1467](https://github.com/emilk/egui/pull/1467)).
* Removed the features `dark-light` and `persistence` ([#1542](https://github.com/emilk/egui/pull/1542)).


## 0.17.0 - 2022-02-22
Expand Down
19 changes: 4 additions & 15 deletions egui-winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ all-features = true


[features]
default = ["clipboard", "dark-light", "links"]
default = ["clipboard", "links"]

# implement bytemuck on most types.
bytemuck = ["egui/bytemuck"]
Expand All @@ -27,39 +27,28 @@ bytemuck = ["egui/bytemuck"]
# if disabled a clipboard will be simulated so you can still copy/paste within the egui app.
clipboard = ["arboard"]

# detect dark mode system preference
dark-light = ["dep:dark-light"]

# 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"]

# Enable profiling with the puffin crate: https://github.com/EmbarkStudios/puffin
# enable profiling with the puffin crate: https://github.com/EmbarkStudios/puffin
puffin = ["dep:puffin"]

# experimental support for a screen reader
screen_reader = ["tts"]

persistence = ["egui/serde", "serde", "epi?/persistence"]
# to serialize `WindowSettings`
serde = ["egui/serde", "dep:serde"]


[dependencies]
egui = { version = "0.17.0", path = "../egui", default-features = false, features = [
"tracing",
] }
instant = { version = "0.1", features = ["wasm-bindgen"] }
instant = { version = "0.1", features = ["wasm-bindgen"] } # We use instant so we can (maybe) compile for web
tracing = "0.1"
winit = "0.26.1"

# Optional:
epi = { version = "0.17.0", path = "../epi", optional = true }

arboard = { version = "2.1", optional = true, default-features = false }
dark-light = { version = "0.2.1", optional = true }
glow = { version = "0.11", optional = true }
puffin = { version = "0.13", optional = true }
serde = { version = "1.0", optional = true, features = ["derive"] }
webbrowser = { version = "0.6", optional = true }
Expand Down
3 changes: 0 additions & 3 deletions egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ pub mod clipboard;
pub mod screen_reader;
mod window_settings;

#[cfg(feature = "epi")]
pub mod epi;

pub use window_settings::WindowSettings;

pub fn native_pixels_per_point(window: &winit::window::Window) -> f32 {
Expand Down
2 changes: 1 addition & 1 deletion egui/src/data/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct RawInput {
/// Dragged files dropped into egui.
///
/// Note: when using `eframe` on Windows you need to enable
/// drag-and-drop support using `epi::NativeOptions`.
/// drag-and-drop support using `eframe::NativeOptions`.
pub dropped_files: Vec<DroppedFile>,
}

Expand Down
Loading

0 comments on commit ed002ac

Please sign in to comment.