Skip to content

Commit

Permalink
Try fix rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
micmonay committed Aug 26, 2024
2 parents 502801c + ef6ff81 commit fdc6658
Show file tree
Hide file tree
Showing 45 changed files with 690 additions and 389 deletions.
7 changes: 0 additions & 7 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
# clipboard api is still unstable, so web-sys requires the below flag to be passed for copy (ctrl + c) to work
# https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html
# check status at https://developer.mozilla.org/en-US/docs/Web/API/Clipboard#browser_compatibility
# we don't use `[build]` because of rust analyzer's build cache invalidation https://github.com/emilk/eframe_template/issues/93
[target.wasm32-unknown-unknown]
rustflags = ["--cfg=web_sys_unstable_apis"]

[alias]
xtask = "run --quiet --package xtask --"
6 changes: 1 addition & 5 deletions .github/workflows/deploy_web_demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ concurrency:
cancel-in-progress: false

env:
# web_sys_unstable_apis is required to enable the web_sys clipboard API which eframe web uses,
# as well as by the wasm32-backend of the wgpu crate.
# https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Clipboard.html
# https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html
RUSTFLAGS: --cfg=web_sys_unstable_apis -D warnings
RUSTFLAGS: -D warnings
RUSTDOCFLAGS: -D warnings

jobs:
Expand Down
30 changes: 23 additions & 7 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
on: [push, pull_request]
on: [ push, pull_request ]

name: Rust

env:
# web_sys_unstable_apis is required to enable the web_sys clipboard API which eframe web uses,
# as well as by the wasm32-backend of the wgpu crate.
# https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Clipboard.html
# https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html
RUSTFLAGS: --cfg=web_sys_unstable_apis -D warnings
RUSTFLAGS: -D warnings
RUSTDOCFLAGS: -D warnings

jobs:
Expand Down Expand Up @@ -108,7 +104,7 @@ jobs:
- name: wasm-bindgen
uses: jetli/[email protected]
with:
version: "0.2.92"
version: "0.2.93"

- run: ./scripts/wasm_bindgen_check.sh --skip-setup

Expand Down Expand Up @@ -167,6 +163,26 @@ jobs:

# ---------------------------------------------------------------------------

ios:
name: ios
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4

- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.76.0
targets: aarch64-apple-ios

- name: Set up cargo cache
uses: Swatinem/rust-cache@v2

# Default features are disabled because glutin doesn't compile for ios.
- run: cargo check --features wgpu --target aarch64-apple-ios --no-default-features
working-directory: crates/eframe

# ---------------------------------------------------------------------------

windows:
name: Check Windows
runs-on: windows-latest
Expand Down
29 changes: 15 additions & 14 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2235,9 +2235,9 @@ dependencies = [

[[package]]
name = "js-sys"
version = "0.3.69"
version = "0.3.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d"
checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a"
dependencies = [
"wasm-bindgen",
]
Expand Down Expand Up @@ -4208,19 +4208,20 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

[[package]]
name = "wasm-bindgen"
version = "0.2.92"
version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5"
dependencies = [
"cfg-if",
"once_cell",
"wasm-bindgen-macro",
]

[[package]]
name = "wasm-bindgen-backend"
version = "0.2.92"
version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da"
checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b"
dependencies = [
"bumpalo",
"log",
Expand All @@ -4245,19 +4246,19 @@ dependencies = [

[[package]]
name = "wasm-bindgen-macro"
version = "0.2.92"
version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726"
checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]

[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.92"
version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836"
dependencies = [
"proc-macro2",
"quote",
Expand All @@ -4268,9 +4269,9 @@ dependencies = [

[[package]]
name = "wasm-bindgen-shared"
version = "0.2.92"
version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484"

[[package]]
name = "wayland-backend"
Expand Down Expand Up @@ -4383,9 +4384,9 @@ dependencies = [

[[package]]
name = "web-sys"
version = "0.3.69"
version = "0.3.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef"
checksum = "26fdeaafd9bd129f65e7c031593c24d62186301e0c72c8978fa1678be7d532c0"
dependencies = [
"js-sys",
"wasm-bindgen",
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ thiserror = "1.0.37"
web-time = "1.1.0" # Timekeeping for native and web
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
web-sys = "0.3.58"
web-sys = "0.3.70"
wgpu = { version = "22.1.0", default-features = false, features = [
# Make the renderer `Sync` even on wasm32, because it makes the code simpler:
"fragile-send-sync-non-atomic-wasm",
Expand Down
1 change: 0 additions & 1 deletion crates/eframe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ include = [

[package.metadata.docs.rs]
all-features = true
rustc-args = ["--cfg=web_sys_unstable_apis"]
targets = ["x86_64-unknown-linux-gnu", "wasm32-unknown-unknown"]

[lints]
Expand Down
2 changes: 0 additions & 2 deletions crates/eframe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ You need to either use `edition = "2021"`, or set `resolver = "2"` in the `[work

You can opt-in to the using [`egui_wgpu`](https://github.com/emilk/egui/tree/master/crates/egui_wgpu) for rendering by enabling the `wgpu` feature and setting `NativeOptions::renderer` to `Renderer::Wgpu`.

To get copy-paste working on web, you need to compile with `export RUSTFLAGS=--cfg=web_sys_unstable_apis`.

## Alternatives
`eframe` is not the only way to write an app using `egui`! You can also try [`egui-miniquad`](https://github.com/not-fl3/egui-miniquad), [`bevy_egui`](https://github.com/mvlabat/bevy_egui), [`egui_sdl2_gl`](https://github.com/ArjunNair/egui_sdl2_gl), and others.

Expand Down
3 changes: 0 additions & 3 deletions crates/eframe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
//! call [`crate::run_native`] from your `main.rs`, and/or use `eframe::WebRunner` from your `lib.rs`.
//!
//! ## Compiling for web
//! To get copy-paste working on web, you need to compile with
//! `export RUSTFLAGS=--cfg=web_sys_unstable_apis`.
//!
//! You need to install the `wasm32` target with `rustup target add wasm32-unknown-unknown`.
//!
//! Build the `.wasm` using `cargo build --target wasm32-unknown-unknown`
Expand Down
33 changes: 18 additions & 15 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use winit::event_loop::ActiveEventLoop;

use raw_window_handle::{HasDisplayHandle as _, HasWindowHandle as _};

use egui::{DeferredViewportUiCallback, NumExt as _, ViewportBuilder, ViewportId};
use egui::{DeferredViewportUiCallback, ViewportBuilder, ViewportId};
use egui_winit::{EventResponse, WindowSettings};

use crate::epi;

#[cfg_attr(target_os = "ios", allow(dead_code, unused_variables, unused_mut))]
pub fn viewport_builder(
egui_zoom_factor: f32,
event_loop: &ActiveEventLoop,
Expand Down Expand Up @@ -53,8 +54,10 @@ pub fn viewport_builder(

if clamp_size_to_monitor_size {
if let Some(initial_window_size) = viewport_builder.inner_size {
let initial_window_size = initial_window_size
.at_most(largest_monitor_point_size(egui_zoom_factor, event_loop));
let initial_window_size = egui::NumExt::at_most(
initial_window_size,
largest_monitor_point_size(egui_zoom_factor, event_loop),
);
viewport_builder = viewport_builder.with_inner_size(initial_window_size);
}
}
Expand All @@ -65,7 +68,10 @@ pub fn viewport_builder(
#[cfg(not(target_os = "ios"))]
if native_options.centered {
crate::profile_scope!("center");
if let Some(monitor) = event_loop.available_monitors().next() {
if let Some(monitor) = event_loop
.primary_monitor()
.or_else(|| event_loop.available_monitors().next())
{
let monitor_size = monitor
.size()
.to_logical::<f32>(egui_zoom_factor as f64 * monitor.scale_factor());
Expand Down Expand Up @@ -95,6 +101,7 @@ pub fn apply_window_settings(
}
}

#[cfg(not(target_os = "ios"))]
fn largest_monitor_point_size(egui_zoom_factor: f32, event_loop: &ActiveEventLoop) -> egui::Vec2 {
crate::profile_function!();

Expand Down Expand Up @@ -235,17 +242,13 @@ impl EpiIntegration {

use winit::event::{ElementState, MouseButton, WindowEvent};

match event {
WindowEvent::Destroyed => {
log::debug!("Received WindowEvent::Destroyed");
self.close = true;
}
WindowEvent::MouseInput {
button: MouseButton::Left,
state: ElementState::Pressed,
..
} => self.can_drag_window = true,
_ => {}
if let WindowEvent::MouseInput {
button: MouseButton::Left,
state: ElementState::Pressed,
..
} = event
{
self.can_drag_window = true;
}

egui_winit.on_window_event(window, event)
Expand Down
13 changes: 13 additions & 0 deletions crates/eframe/src/native/glow_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,18 @@ impl GlowWinitRunning {
}
}

winit::event::WindowEvent::Destroyed => {
log::debug!(
"Received WindowEvent::Destroyed for viewport {:?}",
viewport_id
);
if viewport_id == Some(ViewportId::ROOT) {
return EventResult::Exit;
} else {
return EventResult::Wait;
}
}

_ => {}
}

Expand Down Expand Up @@ -1360,6 +1372,7 @@ fn initialize_or_update_viewport(
);
viewport.window = None;
viewport.egui_winit = None;
viewport.gl_surface = None;
}

viewport.deferred_commands.append(&mut delta_commands);
Expand Down
15 changes: 3 additions & 12 deletions crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cell::RefCell, time::Instant};
use std::time::Instant;

use winit::{
application::ApplicationHandler,
Expand Down Expand Up @@ -32,11 +32,12 @@ fn create_event_loop(native_options: &mut epi::NativeOptions) -> Result<EventLoo
///
/// We reuse the event-loop so we can support closing and opening an eframe window
/// multiple times. This is just a limitation of winit.
#[cfg(not(target_os = "ios"))]
fn with_event_loop<R>(
mut native_options: epi::NativeOptions,
f: impl FnOnce(&mut EventLoop<UserEvent>, epi::NativeOptions) -> R,
) -> Result<R> {
thread_local!(static EVENT_LOOP: RefCell<Option<EventLoop<UserEvent>>> = RefCell::new(None));
thread_local!(static EVENT_LOOP: std::cell::RefCell<Option<EventLoop<UserEvent>>> = std::cell::RefCell::new(None));

EVENT_LOOP.with(|event_loop| {
// Since we want to reference NativeOptions when creating the EventLoop we can't
Expand Down Expand Up @@ -174,16 +175,6 @@ impl<T: WinitApp> WinitAppWrapper<T> {
});

if let Some(next_repaint_time) = next_repaint_time {
// WaitUntil seems to not work on iOS
#[cfg(target_os = "ios")]
winit_app
.window_id_from_viewport_id(egui::ViewportId::ROOT)
.map(|window_id| {
winit_app
.window(window_id)
.map(|window| window.request_redraw())
});

event_loop.set_control_flow(ControlFlow::WaitUntil(next_repaint_time));
};
}
Expand Down
26 changes: 20 additions & 6 deletions crates/eframe/src/native/wgpu_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ impl WgpuWinitApp {
ViewportClass::Root,
self.native_options.viewport.clone(),
None,
painter,
)
.initialize_window(event_loop, egui_ctx, viewport_from_window, painter);
}
Expand Down Expand Up @@ -927,8 +928,14 @@ fn render_immediate_viewport(
..
} = &mut *shared.borrow_mut();

let viewport =
initialize_or_update_viewport(viewports, ids, ViewportClass::Immediate, builder, None);
let viewport = initialize_or_update_viewport(
viewports,
ids,
ViewportClass::Immediate,
builder,
None,
painter,
);
if viewport.window.is_none() {
event_loop_context::with_current_event_loop(|event_loop| {
viewport.initialize_window(event_loop, egui_ctx, viewport_from_window, painter);
Expand Down Expand Up @@ -1051,7 +1058,7 @@ fn handle_viewport_output(
let ids = ViewportIdPair::from_self_and_parent(viewport_id, parent);

let viewport =
initialize_or_update_viewport(viewports, ids, class, builder, viewport_ui_cb);
initialize_or_update_viewport(viewports, ids, class, builder, viewport_ui_cb, painter);

if let Some(window) = viewport.window.as_ref() {
let old_inner_size = window.inner_size();
Expand Down Expand Up @@ -1084,13 +1091,14 @@ fn handle_viewport_output(
remove_viewports_not_in(viewports, painter, viewport_from_window, viewport_output);
}

fn initialize_or_update_viewport(
viewports: &mut Viewports,
fn initialize_or_update_viewport<'a>(
viewports: &'a mut Viewports,
ids: ViewportIdPair,
class: ViewportClass,
mut builder: ViewportBuilder,
viewport_ui_cb: Option<Arc<dyn Fn(&egui::Context) + Send + Sync>>,
) -> &mut Viewport {
painter: &mut egui_wgpu::winit::Painter,
) -> &'a mut Viewport {
crate::profile_function!();

if builder.icon.is_none() {
Expand Down Expand Up @@ -1135,6 +1143,12 @@ fn initialize_or_update_viewport(
);
viewport.window = None;
viewport.egui_winit = None;
if let Err(err) = pollster::block_on(painter.set_window(viewport.ids.this, None)) {
log::error!(
"when rendering viewport_id={:?}, set_window Error {err}",
viewport.ids.this
);
}
}

viewport.deferred_commands.append(&mut delta_commands);
Expand Down
Loading

0 comments on commit fdc6658

Please sign in to comment.