Winit custom cursor

I am unable to read the winit cursor help / traitbounds.
What should be there at the ???

let custom_cursor = winit::window::CustomCursor::from_rgba(cursor_vec.as_bytes().to_vec(), cursor_vec.width() as u16, cursor_vec.height() as u16, 7, 7).unwrap();

 let win: Window = event_loop.create_window
  (
      Window::default_attributes()
          .with_cursor(???)
  ).expect("winit win creation crash");

Window::with_cursor:

pub fn with_cursor(self, cursor: impl Into<Cursor>) -> Self

So it wants Cursor or any type T where either T: Into<Cursor> or Cursor: From<T>. First, click on Cursor:

pub enum Cursor {
    Icon(CursorIcon),
    Custom(CustomCursor),
}

It has a variant that directly takes a CustomCursor, which you already have. So, presumably, you just want:

        .with_cursor(Cursor::Custom(custom_cursor))

However, scrolling down will show this implementation:

impl From<CustomCursor> for Cursor

So, given what I said earlier, and T = CustomCursor, you have Cursor: From<CustomCursor>. So you should be able to directly write:

        .with_cursor(custom_cursor)

Well actually that was what I was thinking / deducing too, however:

.with_cursor(Cursor::Custom(custom_cursor))
results in
expected `CustomCursor`, found `CustomCursorSource

And this
.with_cursor(custom_cursor)
results in

the trait bound `winit::window::Cursor: From<CustomCursorSource>` is not satisfied
the following other types implement trait `From<T>`:
  <winit::window::Cursor as From<CursorIcon>>
  <winit::window::Cursor as From<CustomCursor>>
required for `CustomCursorSource` to implement `Into<winit::window::Cursor>`

The more information you provide when asking a question, the more likely you are to get a useful answer. In particular, if you've tried something and gotten errors, say so and include the error message.

CustomCursor::from_rgba tells you the return type is CustomCursorSource (which as you saw doesn't work), so follow that. There's no obvious way to get a CustomCursor, but there is a note that says " See CustomCursor for more details.", so go back to that.

Reading through the whole page (as opposed to just jumping to the method directly), you should see [an example of how to create a custom cursor]( See CustomCursor for more details.), which contains:

let custom_cursor = event_loop.create_custom_cursor(source);

window.set_cursor(custom_cursor.clone());

So the answer is: you create the cursor from the source via the event loop, then use the result of that with with_cursor. You just need to follow the links to find what you need, at least in this case.


As an aside: I have no idea why the code is structured like this. I can't think of any good reason for it, and frankly it's absolutely baffling.

3 Likes

Daniel, thank you very much.
I got - as a relative beginner - completely lost in the Into's CursorSource's, Cursor's, from_rgba's.
The Into<Cursor> made my brain panic!().

As an aside: I have no idea why the code is structured like this. I can't think of any good reason for it, and frankly it's absolutely baffling.

agree

Into and From are used to define conversions between types. It's for conversions that cannot fail; if a conversion can fail, there is TryFrom and TryInto.

Yes, I know. But Into and From are a kind of strange animals (for me still).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.