Use mouse::Cursor in integration example

This commit is contained in:
Héctor Ramón Jiménez 2023-06-08 20:45:48 +02:00
parent aba98e4965
commit 733c2bd9f5
No known key found for this signature in database
GPG key ID: 140CC052C94F138E

View file

@ -15,7 +15,6 @@ use iced_winit::style::Theme;
use iced_winit::{conversion, futures, winit, Clipboard}; use iced_winit::{conversion, futures, winit, Clipboard};
use winit::{ use winit::{
dpi::PhysicalPosition,
event::{Event, ModifiersState, WindowEvent}, event::{Event, ModifiersState, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::{ControlFlow, EventLoop},
}; };
@ -40,6 +39,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
.and_then(|element| element.dyn_into::<HtmlCanvasElement>().ok()) .and_then(|element| element.dyn_into::<HtmlCanvasElement>().ok())
.expect("Get canvas element") .expect("Get canvas element")
}; };
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
env_logger::init(); env_logger::init();
@ -59,7 +59,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
Size::new(physical_size.width, physical_size.height), Size::new(physical_size.width, physical_size.height),
window.scale_factor(), window.scale_factor(),
); );
let mut cursor_position = PhysicalPosition::new(-1.0, -1.0); let mut cursor_position = None;
let mut modifiers = ModifiersState::default(); let mut modifiers = ModifiersState::default();
let mut clipboard = Clipboard::connect(&window); let mut clipboard = Clipboard::connect(&window);
@ -166,7 +166,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
Event::WindowEvent { event, .. } => { Event::WindowEvent { event, .. } => {
match event { match event {
WindowEvent::CursorMoved { position, .. } => { WindowEvent::CursorMoved { position, .. } => {
cursor_position = position; cursor_position = Some(position);
} }
WindowEvent::ModifiersChanged(new_modifiers) => { WindowEvent::ModifiersChanged(new_modifiers) => {
modifiers = new_modifiers; modifiers = new_modifiers;
@ -195,13 +195,20 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
// We update iced // We update iced
let _ = state.update( let _ = state.update(
viewport.logical_size(), viewport.logical_size(),
mouse::Cursor::Available(conversion::cursor_position( cursor_position
cursor_position, .map(|p| {
viewport.scale_factor(), conversion::cursor_position(
)), p,
viewport.scale_factor(),
)
})
.map(mouse::Cursor::Available)
.unwrap_or(mouse::Cursor::Unavailable),
&mut renderer, &mut renderer,
&Theme::Dark, &Theme::Dark,
&renderer::Style { text_color: Color::WHITE }, &renderer::Style {
text_color: Color::WHITE,
},
&mut clipboard, &mut clipboard,
&mut debug, &mut debug,
); );
@ -243,7 +250,9 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let program = state.program(); let program = state.program();
let view = frame.texture.create_view(&wgpu::TextureViewDescriptor::default()); let view = frame.texture.create_view(
&wgpu::TextureViewDescriptor::default(),
);
{ {
// We clear the frame // We clear the frame
@ -276,15 +285,18 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
frame.present(); frame.present();
// Update the mouse cursor // Update the mouse cursor
window.set_cursor_icon( window.set_cursor_icon(
iced_winit::conversion::mouse_interaction( iced_winit::conversion::mouse_interaction(
state.mouse_interaction(), state.mouse_interaction(),
), ),
); );
} }
Err(error) => match error { Err(error) => match error {
wgpu::SurfaceError::OutOfMemory => { wgpu::SurfaceError::OutOfMemory => {
panic!("Swapchain error: {error}. Rendering cannot continue.") panic!(
"Swapchain error: {error}. \
Rendering cannot continue."
)
} }
_ => { _ => {
// Try rendering again next frame. // Try rendering again next frame.