Skip to content

Split Winit up into multiple crates #3433

Closed

Description

@madsmtm

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-core crate, which contains the common types that all backends use (like dpi and keyboard types)
  • Make traits in winit-core, that implements the desired functionality, and move each backend to also implement those traits.
  • Move the main winit crate to an API that is not cfg-based, but dyn Trait-based.
  • Introduce a winit-common crate 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 main winit crate "merge" these backends with cfgs, 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:

Activity

  1. added
    S - api
    C - needs discussion
    on Jan 27, 2024
  2. added this to the Version 0.31.0 milestone on Jan 27, 2024
  3. amrbashir commented on Feb 20, 2024

    @amrbashir
    Contributor

    Is there any interest in splitting dpi types 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, currently muda and tray-icon crates but now that wry also 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.

  4. madsmtm commented on Feb 20, 2024

    @madsmtm
    MemberAuthor

    Is there any interest in splitting dpi types into its own crates?

    Hmm, I guess that actually makes a lot of sense - and it might even be useful for glutin and softbuffer to use PhysicalUnit<NonZeroU32> for their widths and heights?

    I've reserved the crate name dpi for now.

  5. amrbashir commented on Feb 21, 2024

    @amrbashir
    Contributor

    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?

  6. madsmtm commented on Feb 21, 2024

    @madsmtm
    MemberAuthor

    I'll bring it up in our meeting on friday, then I'll get back to you with a plan.

  7. madsmtm commented on Feb 23, 2024

    @madsmtm
    MemberAuthor

    We talked about it, and don't really think we want to pull in the extra dependency for softbuffer and glutin. 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.

  8. ten3roberts commented on Mar 5, 2024

    @ten3roberts

    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) :)

  9. kchibisov commented on Mar 5, 2024

    @kchibisov
    Member

    Given that the winit-core won't depend on anything else I don't see an issue with using it, if you want so.

  10. n1ght-hunter commented on Jul 10, 2024

    @n1ght-hunter

    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

  11. nicoburns commented on Jul 24, 2024

    @nicoburns
    Contributor

    One set of backends this would enable that would be particularly useful are dummy/proxy backends that could be used for:

    • Automated testing of winit-based applications (where events are synthesized)
    • Embedding a winit app (or many winit apps!) within a larger app (where events might originally be generated by winit but go through an intermediate level of processing before being passed back into a different winit event loop)
  12. daxpedda commented on Jul 24, 2024

    @daxpedda
    Member

    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

    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 WindowEvent still exists and can be passed into various handlers like it works in the current ecosystem.

  13. n1ght-hunter commented on Jul 26, 2024

    @n1ght-hunter

    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

    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 WindowEvent still exists and can be passed into various handlers like it works in the current ecosystem.

    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

  14. daxpedda commented on Jul 26, 2024

    @daxpedda
    Member

    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

    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!

  15. 4 remaining items

  16. daxpedda commented on Jul 27, 2024

    @daxpedda
    Member

    See tray-icon as 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!

  17. n1ght-hunter commented on Jul 28, 2024

    @n1ght-hunter

    @daxpedda didn't want to overload this issue so moved to discussion
    #3835 (comment)

  18. jgcodes2020 commented on Oct 11, 2024

    @jgcodes2020
    Contributor

    I'd like to help push this along. Is there anything I could potentially start work on?

  19. daxpedda commented on Oct 11, 2024

    @daxpedda
    Member

    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!

  20. pinned this issue on Mar 27, 2025
  21. madsmtm commented on May 12, 2025

    @madsmtm
    MemberAuthor

    We haven't really discussed this yet, but I'd like to make the case that the top-level winit crate should end up re-exporting winit_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 in Cargo.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 of keyboard-types and dpi that we use.

    Given the above, I'd argue that we need every type to be uniquely named, and having something like struct winit::EventLoop and trait winit_core::EventLoop is not going to work.

    Spawned by discussion in #4221 (comment).

  22. amrbashir commented on May 12, 2025

    @amrbashir
    Contributor

    We haven't really discussed this yet, but I'd like to make the case that the top-level winit crate should end up re-exporting winit_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 in Cargo.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 of keyboard-types and dpi that we use.

    This should be fine IMO as long as every thing is put behind appropriate feature flag.

    Given the above, I'd argue that we need every type to be uniquely named, and having something like struct winit::EventLoop and trait winit_core::EventLoop is not going to work.

    I don't see the need TBH since one will be imported using winit::EventLoop and the other using winit::core::EventLoop. The later probably would only be imported by library authors not end user of winit.

  23. madsmtm commented on May 12, 2025

    @madsmtm
    MemberAuthor

    If we make it a trait, the end user has to import it as well, otherwise they won't get access to its functionality.

  24. amrbashir commented on May 12, 2025

    @amrbashir
    Contributor

    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, IWindow which is pretty common in C# but didn't see in Rust much.

  25. nicoburns commented on May 12, 2025

    @nicoburns
    Contributor

    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, IWindow which is pretty common in C# but didn't see in Rust much.

    There is precedent in Stylo of using a T prefix for rust traits.

  26. kchibisov commented on May 26, 2025

    @kchibisov
    Member

    @madsmtm do we need anything else here?

  27. kchibisov commented on Jun 7, 2025

    @kchibisov
    Member

    Completed.

  28. unpinned this issue on Jun 11, 2025
  29. self-assigned this
    on Sep 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C - needs discussionS - api

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions