hasenbanck / egui_winit_platform Goto Github PK
View Code? Open in Web Editor NEWPlatform code to use egui with winit.
License: Apache License 2.0
Platform code to use egui with winit.
License: Apache License 2.0
Unlike the egui_glium platform, this platform doesn't seem to support zoom input. This code from the glium backend seems directly portable to this platform.
Same as hasenbanck/egui_wgpu_backend#15 ๐
I updated winit to 1.29.2, and, because there doesn't seem to be support for it already, decided to quickly update the platform code.
Fortunately, it's all pretty smooth! Although there are a bunch of breaking changes, everything seems to work just fine except one thing: winit::event::WindowEvent::ScaleFactorChanged
The current implementation uses new_inner_size
, but this field has been replaced by inner_size_writer
in the new version of winit. The new inner_size_writer
doesn't allow you to get the suggested inner_size (i.e. new_inner_size
) for some reason... So we have to have access to the Window
(for Window::inner_size()
) Maybe I haven't looked hard enough, but I don't see any way to access the window from anywhere inside Platform
(the optional argument in end_frame
doesn't work)
My question is: is passing a window reference to handle_event
the only way to do this for now?
Note: I will make a pull request when this gets resolved (or should I do it now?)
Trying to use egui_winit_platform on Android (and I guess the same might be true on iOS) we can only create a winit Window once the application receives a Resumed
lifecycle event, even though, ideally, the Platform could be constructed once when initializing the app.
This is awkward because it leads to passing in dummy scale_factor
and physical_width/height
Descriptor values while we don't yet know anything about the Window we'll be rendering with.
If Platform
is initialized with these dummy values then it's also necessary to explicitly update RawInput
pixels_per_point
and screen_rect
to make up for this, which works against the way in which handle_event
is designed to handle updating this state automatically in general.
The biggest difficulty though is that the private Platform
scale_factor
is practically inaccessible and the only (awkward) way this state can be corrected later is by passing a fake ScaleFactorChanged
changed event to `handle_events.
All in all I currently do the following to update the Platform
state that can't always be determined until the app is Resumed and we get a Window...
let mut size = window.inner_size();
let scale_factor = window.scale_factor() as f32;
let raw_input = platform.raw_input_mut();
raw_input.pixels_per_point = Some(scale_factor);
raw_input.screen_rect = Some(egui::Rect::from_min_size(
egui::Pos2::default(),
egui::vec2(
size.width as f32,
size.height as f32,
) / scale_factor,
));
platform.handle_event::<Event>(&winit::event::Event::WindowEvent {
window_id: window.id(),
event: winit::event::WindowEvent::ScaleFactorChanged {
scale_factor: scale_factor as f64,
new_inner_size: &mut size }
});
which is uncomfortably dependent on implementation details of egui_winit_platform
.
I wonder if it could be best to remove the physical_width/height
and scale_factor
arguments from Platform::new()
considering that this state can't be assumed to be available early on for all platforms.
Maybe there could a set_window
API that could be used for associating the Platform with a winit Window which would be an opportunity to initialize both the physical width/height and scale factor which can then be correctly maintained by handle_event
.
Perhaps though it's also possible to remove the separate scale_factor
state internally and just refer to RawInput::pixels_per_point
which seems to be the same scale factor?
egui_winit_platform/src/lib.rs
Line 288 in 1c78c0b
In this line it is not checked whether the key was pressed or released, so egui::Event::Text
gets added twice. Once on press, once on release.
I noticed this while using egui::TextEdit
.
I think changing this line to
if pressed && !self.modifier_state.intersects(ModifiersState::CONTROL | ModifiersState::SUPER)
fixes the issue
Hello!
In 9fd9278, egui_winit_platform was upgraded to use winit 0.25. However, this has not been published to crates.io yet. Would be it be possible to cut a 0.8.0 release that uses winit 0.25?
winit 0.28.x introduced some breaking changes that broke egui_winit_platform:
error: Please select a feature to build for unix: `x11`, `wayland`
--> /home/<redacted>/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.27.5/src/platform_impl/linux/mod.rs:10:1
|
10 | compile_error!("Please select a feature to build for unix: `x11`, `wayland`");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0392]: parameter `T` is never used
--> /home/<redacted>/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.27.5/src/event_loop.rs:35:22
|
35 | pub struct EventLoop<T: 'static> {
| ^ unused parameter
|
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
error[E0392]: parameter `T` is never used
--> /home/<redacted>/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.27.5/src/event_loop.rs:46:34
|
46 | pub struct EventLoopWindowTarget<T: 'static> {
| ^ unused parameter
|
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
error[E0392]: parameter `T` is never used
--> /home/<redacted>/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.27.5/src/event_loop.rs:350:27
|
350 | pub struct EventLoopProxy<T: 'static> {
| ^ unused parameter
|
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
error[E0392]: parameter `T` is never used
--> /home/<redacted>/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.27.5/src/platform_impl/linux/mod.rs:635:20
|
635 | pub enum EventLoop<T: 'static> {
| ^ unused parameter
|
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
error[E0392]: parameter `T` is never used
--> /home/<redacted>/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.27.5/src/platform_impl/linux/mod.rs:642:25
|
642 | pub enum EventLoopProxy<T: 'static> {
| ^ unused parameter
|
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
error[E0392]: parameter `T` is never used
--> /home/<redacted>/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.27.5/src/platform_impl/linux/mod.rs:769:32
|
769 | pub enum EventLoopWindowTarget<T> {
| ^ unused parameter
|
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
= help: if you intended `T` to be a const parameter, use `const T: usize` instead
For more information about this error, try `rustc --explain E0392`.
error: could not compile `winit` due to 7 previous errors
warning: build failed, waiting for other jobs to finish...
... Not sure where to begin to fix this! Maybe updating the dep on winit would suffice?
Egui already tells us if it wants keyboard or mouse input, but it would be nice if the integration could sort out for us which winit events that includes. See this prior art for imgui. This is useful to be able to do other stuff with events that egui doesn't care about. If this is something that you would like to see, I could try making a pull request.
BTW. Other dependencies has new version.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.