Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Widget-driven animations #1647

Merged
merged 11 commits into from
Jan 13, 2023
Merged

Widget-driven animations #1647

merged 11 commits into from
Jan 13, 2023

Conversation

hecrj
Copy link
Member

@hecrj hecrj commented Jan 12, 2023

Fixes #31 and closes #560.

This PR introduces a new method request_redraw to the Shell type which enables a Widget to request a redraw during on_event.

Shell::request_redraw takes a window::RedrawRequest, which is defined as follows:

/// A request to redraw a window.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum RedrawRequest {
    /// Redraw the next frame.
    NextFrame,

    /// Redraw at the given time.
    At(Instant),
}

Additionally, a new variant RedrawRequested(Instant) has been added to window::Event. This event is triggered before the user interface is redrawn.

As a result, widgets can now choose to draw whenever necessary; making animations possible! For instance, the TextInput widget has now a blinking cursor:

2023-01-11.05-36-44.mp4

Furthermore, these changes allow us to finally implement a subscription to subscribe to the frame rate of a window. The brand new window::frames subscription produces an Instant at the same frequency as the frame rate of the screen of the user. This can be very useful to smoothly animate layout or different parts of an application (as opposed to using time::every with a fixed frame rate). The solar_system example now uses the window::frames subscription!

@hecrj hecrj force-pushed the feature/widget-request-redraw branch from d02810a to c6d0046 Compare January 12, 2023 05:26
@ids1024
Copy link
Contributor

ids1024 commented Jan 12, 2023

I guess this currently depends on GL swap_buffers or wgpu present blocking until vsync? (With an appropriate swap interval or present mode.) I guess that's the right way to do it until rust-windowing/winit#2412 is implemented.

On Wayland, Mesa's EGL/WSI implementation uses wl_surface::frame here, which may block indefinitely for occluded windows (does this block other parts of Iced in an undesirable way?). So it should be noted that the event won't necessarily be emitted regularly. I'm not sure exactly what the behavior is like on other platforms.

@13r0ck
Copy link
Member

13r0ck commented Jan 12, 2023

Furthermore, these changes allow us to finally implement a subscription to subscribe to the frame rate of a window.

That is very exciting

@hecrj
Copy link
Member Author

hecrj commented Jan 12, 2023

I guess this currently depends on GL swap_buffers or wgpu present blocking until vsync? (With an appropriate swap interval or present mode.) I guess that's the right way to do it until rust-windowing/winit#2412 is implemented.

Yes. Without vsync, the RedrawRequested event will be produced as fast as possible when subscribed to window::frames. Since redraws are graphical, it seems appropriate to let the graphics driver control the framerate (instead of trying to figure out the screen frame rate and throttle ourselves).

On Wayland, Mesa's EGL/WSI implementation uses wl_surface::frame here, which may block indefinitely for occluded windows (does this block other parts of Iced in an undesirable way?). So it should be noted that the event won't necessarily be emitted regularly. I'm not sure exactly what the behavior is like on other platforms.

I can clarify in the docs that the subscription matches the maximum refresh rate of the window at any given moment, which is in turn controlled by graphics drivers and the OS.

In any case, there is no reason to believe the event will be emitted regularly even with the current docs, since a window can be moved to a screen with different refresh rate. This is why an Instant is provided for delta calculations in the RedrawRequested event.

@hecrj hecrj merged commit 597af31 into master Jan 13, 2023
@hecrj hecrj deleted the feature/widget-request-redraw branch January 13, 2023 19:34
@thunderstorm010 thunderstorm010 mentioned this pull request Jan 25, 2023
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC] Blinking a TextInput cursor using periodic animation Animations
3 participants