Refactor: move code around in `eframe` (#3575)
`run.rs` was getting way too long and complex.
This commit is contained in:
parent
a23b959fd4
commit
960b01b67a
|
|
@ -11,7 +11,7 @@ use std::any::Any;
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
#[cfg(any(feature = "glow", feature = "wgpu"))]
|
#[cfg(any(feature = "glow", feature = "wgpu"))]
|
||||||
pub use crate::native::run::UserEvent;
|
pub use crate::native::winit_integration::UserEvent;
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
use raw_window_handle::{
|
use raw_window_handle::{
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
//! Common tools used by [`super::glow_integration`] and [`super::wgpu_integration`].
|
||||||
|
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use winit::event_loop::EventLoopWindowTarget;
|
use winit::event_loop::EventLoopWindowTarget;
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -5,3 +5,11 @@ pub mod run;
|
||||||
/// File storage which can be used by native backends.
|
/// File storage which can be used by native backends.
|
||||||
#[cfg(feature = "persistence")]
|
#[cfg(feature = "persistence")]
|
||||||
pub mod file_storage;
|
pub mod file_storage;
|
||||||
|
|
||||||
|
pub(crate) mod winit_integration;
|
||||||
|
|
||||||
|
#[cfg(feature = "glow")]
|
||||||
|
mod glow_integration;
|
||||||
|
|
||||||
|
#[cfg(feature = "wgpu")]
|
||||||
|
mod wgpu_integration;
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,102 @@
|
||||||
|
use std::{rc::Rc, time::Instant};
|
||||||
|
|
||||||
|
use winit::{
|
||||||
|
event_loop::EventLoopWindowTarget,
|
||||||
|
window::{Window, WindowId},
|
||||||
|
};
|
||||||
|
|
||||||
|
use egui::ViewportId;
|
||||||
|
#[cfg(feature = "accesskit")]
|
||||||
|
use egui_winit::accesskit_winit;
|
||||||
|
|
||||||
|
use super::epi_integration::EpiIntegration;
|
||||||
|
|
||||||
|
pub const IS_DESKTOP: bool = cfg!(any(
|
||||||
|
target_os = "freebsd",
|
||||||
|
target_os = "linux",
|
||||||
|
target_os = "macos",
|
||||||
|
target_os = "openbsd",
|
||||||
|
target_os = "windows",
|
||||||
|
));
|
||||||
|
|
||||||
|
/// The custom even `eframe` uses with the [`winit`] event loop.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum UserEvent {
|
||||||
|
/// A repaint is requested.
|
||||||
|
RequestRepaint {
|
||||||
|
/// What to repaint.
|
||||||
|
viewport_id: ViewportId,
|
||||||
|
|
||||||
|
/// When to repaint.
|
||||||
|
when: Instant,
|
||||||
|
|
||||||
|
/// What the frame number was when the repaint was _requested_.
|
||||||
|
frame_nr: u64,
|
||||||
|
},
|
||||||
|
|
||||||
|
/// A request related to [`accesskit`](https://accesskit.dev/).
|
||||||
|
#[cfg(feature = "accesskit")]
|
||||||
|
AccessKitActionRequest(accesskit_winit::ActionRequestEvent),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "accesskit")]
|
||||||
|
impl From<accesskit_winit::ActionRequestEvent> for UserEvent {
|
||||||
|
fn from(inner: accesskit_winit::ActionRequestEvent) -> Self {
|
||||||
|
Self::AccessKitActionRequest(inner)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait WinitApp {
|
||||||
|
/// The current frame number, as reported by egui.
|
||||||
|
fn frame_nr(&self, viewport_id: ViewportId) -> u64;
|
||||||
|
|
||||||
|
fn is_focused(&self, window_id: WindowId) -> bool;
|
||||||
|
|
||||||
|
fn integration(&self) -> Option<&EpiIntegration>;
|
||||||
|
|
||||||
|
fn window(&self, window_id: WindowId) -> Option<Rc<Window>>;
|
||||||
|
|
||||||
|
fn window_id_from_viewport_id(&self, id: ViewportId) -> Option<WindowId>;
|
||||||
|
|
||||||
|
fn save_and_destroy(&mut self);
|
||||||
|
|
||||||
|
fn run_ui_and_paint(&mut self, window_id: WindowId) -> EventResult;
|
||||||
|
|
||||||
|
fn on_event(
|
||||||
|
&mut self,
|
||||||
|
event_loop: &EventLoopWindowTarget<UserEvent>,
|
||||||
|
event: &winit::event::Event<'_, UserEvent>,
|
||||||
|
) -> crate::Result<EventResult>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
pub enum EventResult {
|
||||||
|
Wait,
|
||||||
|
|
||||||
|
/// Causes a synchronous repaint inside the event handler. This should only
|
||||||
|
/// be used in special situations if the window must be repainted while
|
||||||
|
/// handling a specific event. This occurs on Windows when handling resizes.
|
||||||
|
///
|
||||||
|
/// `RepaintNow` creates a new frame synchronously, and should therefore
|
||||||
|
/// only be used for extremely urgent repaints.
|
||||||
|
RepaintNow(WindowId),
|
||||||
|
|
||||||
|
/// Queues a repaint for once the event loop handles its next redraw. Exists
|
||||||
|
/// so that multiple input events can be handled in one frame. Does not
|
||||||
|
/// cause any delay like `RepaintNow`.
|
||||||
|
RepaintNext(WindowId),
|
||||||
|
|
||||||
|
RepaintAt(WindowId, Instant),
|
||||||
|
|
||||||
|
Exit,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn system_theme(window: &Window, options: &crate::NativeOptions) -> Option<crate::Theme> {
|
||||||
|
if options.follow_system_theme {
|
||||||
|
window
|
||||||
|
.theme()
|
||||||
|
.map(super::epi_integration::theme_from_winit_theme)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue