Repository navigation
Description
Part of #3367, opening to discuss separately.
Winit is designed around a single crate with a specific set of backends, which is great for users that can use that, but there is a need for other backends that are not implemented in Winit today, like GTK. Additionally, the backend-specific extensions that we do have usually constrain some other part of the design.
So it would probably be useful to try to split Winit into multiple smaller crates (though still in the same repository), to ease doing this work. The top-level winit crate would still remain.
Along with this, we'd want a way to write out-of-tree backends, and allow the user to integrate them into their application. This could probably be solved by introducing internal / "backend" traits, and keep a dyn in structures inside the winit crate.
A rough implementation plan could be:
- Create a
winit-corecrate, which contains the common types that all backends use (likedpiand keyboard types) - Make traits in
winit-core, that implements the desired functionality, and move each backend to also implement those traits. - Move the main
winitcrate to an API that is notcfg-based, butdyn Trait-based. - Introduce a
winit-commoncrate to have shared functionality between the crates, which will be intended for winit internal usage and help writing new backends. - Move the backends out into separate crates, which all depend on
winit-core, but still have the mainwinitcrate "merge" these backends withcfgs, not with a trait.
@kchibisov: I've tried to fairly faithfully reproduce what I felt was the important points from #3367, but please edit this issue description with your own.
EDIT: Implementation history:
- Refactor to
dyntraits: - Refactorings:
- Remove
dummy()fromWindowIdandDeviceIdand useOption<DeviceId>#3864 - Merge all
WindowIdinto a single type in the core crate #3902 - api: add
DeviceId::{into,from}_raw#3941 - Add
as_innerhelper method on trait objects #4160 - chore: Import from winit-core instead of the top-level crate #4225
- Move
EventLoopExt*towinit-core#4228 - Use workspace dependencies everywhere #4240
- chore: Move event loop recreation check into backends themselves #4257
- Remove
- Core split: winit-core: new crate #4208
- Figure out naming: Backend naming #4246
- Moving backends:
- Common: Move shared code to new crate
winit-common#4241 - Android: Move Android backend to
winit-android#4250 - AppKit: Move AppKit backend to
winit-appkit#4248 - Orbital: Split Orbital backend out into
winit-orbital#4243 - UIKit: Split UIKit backend out into
winit-uikit#4242 - Web: Move Web backend to
winit-web#4249 - Windows: Move Windows backend to
winit-win32#4247 - Wayland: Move Wayland backend to
winit-wayland#4252 - X11: Move X11 backend to
winit-x11#4253
- Common: Move shared code to new crate
- Reserve crate names (DONE).
- Move top-level crate: Move winit itself to crate #4258
- Docs + changelog cleanup: TODO
- Clean up CI: TODO
- doc_auto_cfg: TODO
- Rename / refactor extension traits: TODO
- Figure out how we do releases / versioning: Use a shared version number for all winit-* crates #4274
- Make sure features like
serdeis properly enabled throughout: TODO
Activity
amrbashir commentedon Feb 20, 2024
Is there any interest in splitting
dpitypes into its own crates?There is a few crates that we maintain at Tauri that use
Physical/Logical-Size/Position, and they are duplicated in each crate, currentlymudaandtray-iconcrates but now thatwryalso needs, it is time to create a new crate.We would also need to incorporate #2395 as we already included that change in our fork which was implemented based on #2148.
madsmtm commentedon Feb 20, 2024
Hmm, I guess that actually makes a lot of sense - and it might even be useful for
glutinandsoftbufferto usePhysicalUnit<NonZeroU32>for theirwidths andheights?I've reserved the crate name
dpifor now.amrbashir commentedon Feb 21, 2024
I'd be happy to help with the migration if there is a repo I can contribute to, or should it live within this repo?
madsmtm commentedon Feb 21, 2024
I'll bring it up in our meeting on friday, then I'll get back to you with a plan.
madsmtm commentedon Feb 23, 2024
We talked about it, and don't really think we want to pull in the extra dependency for
softbufferandglutin. That said, if there is interest in a separate crate, then we're fine with maintaining it, though we'd probably want it to stay in this repo, as that's much easier to maintain.I think we agreed on doing the split like I described in #3455 (comment), have put up #3518 to move the module, then you're free to submit PRs to improve that afterwards.
ten3roberts commentedon Mar 5, 2024
Hey, thank you for this initiative.
Do you think a split like this would enable us to use winit types (like keycodes) independently from winit and in external APIs. I am finding winits input and event types, especially the KeyCode to be massively beneficial to use in e.g game engine or ui library public APIs rather than re-defining the enum and using
into/from.Would this be a usecase you would be willing to support, I.e; using winit even types in the API (I am ok with breaking changes and refactors, just as long as I can use these in an API) :)
kchibisov commentedon Mar 5, 2024
Given that the
winit-corewon't depend on anything else I don't see an issue with using it, if you want so.n1ght-hunter commentedon Jul 10, 2024
just wondering if part of this would include a way to access the event loop types and os types in a another crate.
example is i am working on tray icon support for iced in a fork of winit. this reuses alot types and event loop from winit so i have built it intergrated into the winit fork. but ideally it would be possible to do this in a separate crate that depends on some of the winit crates that expose more methods.
link to the fork showing what is used.
iced-rs/winit@v0.29.x...n1ght-hunter:winit:v0.29.x
nicoburns commentedon Jul 24, 2024
One set of backends this would enable that would be particularly useful are dummy/proxy backends that could be used for:
daxpedda commentedon Jul 24, 2024
We have discussed this before and we do have a plan to allow other crates to offer plugin-like interfaces that users can hook up.
Definitely something we will be investigating in more detail when its time to make the change. Right now its not an issue because
WindowEventstill exists and can be passed into various handlers like it works in the current ecosystem.n1ght-hunter commentedon Jul 26, 2024
is there any public discussions as to why this is?
IMO it makes sense for winit to make public its types under a feature flag to allow third party crates to reuse some of the winit logic. especially as winit is the main window library in rust
daxpedda commentedon Jul 26, 2024
I'm not following exactly where you are going with this. With current Winit, I'm unsure how exposing the types (its internals) would make any sense. Mostly their internals are backend specific anyway, so I'm unsure how anybody would make use of it.
For future Winit: we have talked about reusability a lot. There it makes much more sense, because we want to allow external backends and split the crate as discussed in this issue. So internal access is actually a requirement. However we should also make sure that the top-level crate doesn't actually allow internal access, otherwise it would all just be very terrible to use and probably
unsafe/unstable.But if you have a specific suggestion with a specific use-case in mind for current Winit, let us know!
4 remaining items
daxpedda commentedon Jul 27, 2024
See
tray-iconas well.@n1ght-hunter looking at your tray implementation I don't believe this is a "I don't have access to internal types" issue, you need access to the internal event loop to integrate your tray in.
This seems to be an entirely different issue to me, probably more like #2120.
Let me know if I missed anything!
n1ght-hunter commentedon Jul 28, 2024
@daxpedda didn't want to overload this issue so moved to discussion
#3835 (comment)
jgcodes2020 commentedon Oct 11, 2024
I'd like to help push this along. Is there anything I could potentially start work on?
daxpedda commentedon Oct 11, 2024
I think our next step is #3927 and #3941.
However I believe its a bit hard for non-maintainers to help with this kind of process.
The most valuable contribution is to leave your 2¢ in discussions!
madsmtm commentedon May 12, 2025
We haven't really discussed this yet, but I'd like to make the case that the top-level
winitcrate should end up re-exportingwinit_core,winit_appkit, etc. This mirrors what Bevy does, and makes the API easier for application authors to consume (they don't have to keep the versions of a bunch of different crates in sync inCargo.toml), while library authors can still pick and choose the parts that they need. I also think it's valuable for us to keep re-exporting the parts ofkeyboard-typesanddpithat we use.Given the above, I'd argue that we need every type to be uniquely named, and having something like
struct winit::EventLoopandtrait winit_core::EventLoopis not going to work.Spawned by discussion in #4221 (comment).
amrbashir commentedon May 12, 2025
This should be fine IMO as long as every thing is put behind appropriate feature flag.
I don't see the need TBH since one will be imported using
winit::EventLoopand the other usingwinit::core::EventLoop. The later probably would only be imported by library authors not end user of winit.madsmtm commentedon May 12, 2025
If we make it a trait, the end user has to import it as well, otherwise they won't get access to its functionality.
amrbashir commentedon May 12, 2025
ah I see and I assume there is more traits for window and monitor, in that case maybe the traits could have a prefix sorta of
IEventLoop,IWindowwhich is pretty common in C# but didn't see in Rust much.nicoburns commentedon May 12, 2025
There is precedent in Stylo of using a T prefix for rust traits.
kchibisov commentedon May 26, 2025
@madsmtm do we need anything else here?
kchibisov commentedon Jun 7, 2025
Completed.