Skip to content

Add .cursor(icon) to ElementBuilder #15

@symil

Description

@symil

Summary

Currently the way to change the cursor's icon is through miniquad's re-exported set_mouse_cursor function. However calling it multiple times per frame can cause the cursor to flicker because calling it actually changes the cursor right away. It would be convenient to have a mechanism that wraps this and call the function once per frame max.

Motivation

It's very common to change the cursor's icon, when a button is hovered for example. Right now the workaround is to carry a variable that holds the cursor's final value and call set_mouse_cursor at the end of the frame. This is not very natural, and becomes more and more tedious as the rendering tree grows.

Proposed API

Re-export miniquad's CursorIcon in the prelude and add .cursor() to ElementBuilder:

let mut cursor = None;

if is_hovered {
    cursor = Some(CursorIcon::Pointer);
}

ui.element().cursor(cursor).empty();

It takes an Option so we can chain the functions nicely regardless of whether or not we want to display a cursor, otherwise we would have to do:

let mut elt = ui.element();
if is_hovered {
    elt = elt.cursor(CursorIcon::Pointer;
}
elt.empty();
// Very ugly!

Behavior

  • The cursor is stored in the root Ply (or the root element, not sure how this works exactly yet).
  • The cursor associated with each element is stored. When ui.show() is called, it takes the one with the highest z-index and displays it through set_window_cursor (defaulting to CursorIcon::Default if none was specified).
  • Even though .cursor() is a member of ElementBuilder, it updates the cursor regardless of whether or not the element is hovered (contrary to CSS's cursor attribute).

Metadata

Metadata

Assignees

No one assigned

    Labels

    featNew feature or requestproposedA feature proposal that hasn't been approved

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions