From 34451bff185d8875f55747ee97ed746828e30f40 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 8 Jun 2023 20:11:59 +0200 Subject: Implement basic cursor availability --- examples/arc/src/main.rs | 5 ++-- examples/bezier_tool/src/main.rs | 12 ++++----- examples/clock/src/main.rs | 7 +++--- examples/color_palette/src/main.rs | 10 +++++--- examples/custom_quad/src/main.rs | 5 ++-- examples/custom_widget/src/main.rs | 5 ++-- examples/game_of_life/src/main.rs | 15 ++++++------ examples/geometry/src/main.rs | 40 +++++++++++++++--------------- examples/modal/src/main.rs | 35 +++++++++++++------------- examples/multitouch/src/main.rs | 7 +++--- examples/sierpinski_triangle/src/main.rs | 5 ++-- examples/solar_system/src/main.rs | 5 ++-- examples/toast/src/main.rs | 42 ++++++++++++-------------------- 13 files changed, 95 insertions(+), 98 deletions(-) (limited to 'examples') diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index 80ad0b5b..df565859 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -1,8 +1,9 @@ use std::{f32::consts::PI, time::Instant}; use iced::executor; +use iced::mouse; use iced::widget::canvas::{ - self, stroke, Cache, Canvas, Cursor, Geometry, Path, Stroke, + self, stroke, Cache, Canvas, Geometry, Path, Stroke, }; use iced::{ Application, Command, Element, Length, Point, Rectangle, Renderer, @@ -78,7 +79,7 @@ impl canvas::Program for Arc { renderer: &Renderer, theme: &Theme, bounds: Rectangle, - _cursor: Cursor, + _cursor: mouse::Cursor, ) -> Vec { let geometry = self.cache.draw(renderer, bounds.size(), |frame| { let palette = theme.palette(); diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index f1c83a16..b4568cbf 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -61,9 +61,7 @@ impl Sandbox for Example { mod bezier { use iced::mouse; use iced::widget::canvas::event::{self, Event}; - use iced::widget::canvas::{ - self, Canvas, Cursor, Frame, Geometry, Path, Stroke, - }; + use iced::widget::canvas::{self, Canvas, Frame, Geometry, Path, Stroke}; use iced::{Element, Length, Point, Rectangle, Renderer, Theme}; #[derive(Default)] @@ -100,7 +98,7 @@ mod bezier { state: &mut Self::State, event: Event, bounds: Rectangle, - cursor: Cursor, + cursor: mouse::Cursor, ) -> (event::Status, Option) { let cursor_position = if let Some(position) = cursor.position_in(&bounds) { @@ -155,7 +153,7 @@ mod bezier { renderer: &Renderer, _theme: &Theme, bounds: Rectangle, - cursor: Cursor, + cursor: mouse::Cursor, ) -> Vec { let content = self.state.cache.draw( renderer, @@ -183,7 +181,7 @@ mod bezier { &self, _state: &Self::State, bounds: Rectangle, - cursor: Cursor, + cursor: mouse::Cursor, ) -> mouse::Interaction { if cursor.is_over(&bounds) { mouse::Interaction::Crosshair @@ -224,7 +222,7 @@ mod bezier { &self, renderer: &Renderer, bounds: Rectangle, - cursor: Cursor, + cursor: mouse::Cursor, ) -> Geometry { let mut frame = Frame::new(renderer, bounds.size()); diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 6425e2da..fae77bc0 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,7 +1,6 @@ use iced::executor; -use iced::widget::canvas::{ - stroke, Cache, Cursor, Geometry, LineCap, Path, Stroke, -}; +use iced::mouse; +use iced::widget::canvas::{stroke, Cache, Geometry, LineCap, Path, Stroke}; use iced::widget::{canvas, container}; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Renderer, @@ -92,7 +91,7 @@ impl canvas::Program for Clock { renderer: &Renderer, _theme: &Theme, bounds: Rectangle, - _cursor: Cursor, + _cursor: mouse::Cursor, ) -> Vec { let clock = self.clock.draw(renderer, bounds.size(), |frame| { let center = frame.center(); diff --git a/examples/color_palette/src/main.rs b/examples/color_palette/src/main.rs index de01099e..736a9d53 100644 --- a/examples/color_palette/src/main.rs +++ b/examples/color_palette/src/main.rs @@ -1,8 +1,10 @@ -use iced::widget::canvas::{self, Canvas, Cursor, Frame, Geometry, Path}; +use iced::alignment::{self, Alignment}; +use iced::mouse; +use iced::widget::canvas::{self, Canvas, Frame, Geometry, Path}; use iced::widget::{column, row, text, Slider}; use iced::{ - alignment, Alignment, Color, Element, Length, Point, Rectangle, Renderer, - Sandbox, Settings, Size, Vector, + Color, Element, Length, Point, Rectangle, Renderer, Sandbox, Settings, + Size, Vector, }; use palette::{ self, convert::FromColor, rgb::Rgb, Darken, Hsl, Lighten, ShiftHue, @@ -246,7 +248,7 @@ impl canvas::Program for Theme { renderer: &Renderer, _theme: &iced::Theme, bounds: Rectangle, - _cursor: Cursor, + _cursor: mouse::Cursor, ) -> Vec { let theme = self.canvas_cache.draw(renderer, bounds.size(), |frame| { self.draw(frame); diff --git a/examples/custom_quad/src/main.rs b/examples/custom_quad/src/main.rs index b07f42ce..4b300116 100644 --- a/examples/custom_quad/src/main.rs +++ b/examples/custom_quad/src/main.rs @@ -3,7 +3,8 @@ mod quad { use iced::advanced::layout::{self, Layout}; use iced::advanced::renderer; use iced::advanced::widget::{self, Widget}; - use iced::{Color, Element, Length, Point, Rectangle, Size}; + use iced::mouse; + use iced::{Color, Element, Length, Rectangle, Size}; pub struct CustomQuad { size: f32, @@ -48,7 +49,7 @@ mod quad { _theme: &Renderer::Theme, _style: &renderer::Style, layout: Layout<'_>, - _cursor_position: Point, + _cursor: mouse::Cursor, _viewport: &Rectangle, ) { renderer.fill_quad( diff --git a/examples/custom_widget/src/main.rs b/examples/custom_widget/src/main.rs index 7854548c..713bc62d 100644 --- a/examples/custom_widget/src/main.rs +++ b/examples/custom_widget/src/main.rs @@ -12,7 +12,8 @@ mod circle { use iced::advanced::layout::{self, Layout}; use iced::advanced::renderer; use iced::advanced::widget::{self, Widget}; - use iced::{Color, Element, Length, Point, Rectangle, Size}; + use iced::mouse; + use iced::{Color, Element, Length, Rectangle, Size}; pub struct Circle { radius: f32, @@ -55,7 +56,7 @@ mod circle { _theme: &Renderer::Theme, _style: &renderer::Style, layout: Layout<'_>, - _cursor_position: Point, + _cursor: mouse::Cursor, _viewport: &Rectangle, ) { renderer.fill_quad( diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index eab8908b..aaa8efd1 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -204,15 +204,14 @@ fn view_controls<'a>( mod grid { use crate::Preset; + use iced::alignment; + use iced::mouse; use iced::touch; use iced::widget::canvas; use iced::widget::canvas::event::{self, Event}; - use iced::widget::canvas::{ - Cache, Canvas, Cursor, Frame, Geometry, Path, Text, - }; + use iced::widget::canvas::{Cache, Canvas, Frame, Geometry, Path, Text}; use iced::{ - alignment, mouse, Color, Element, Length, Point, Rectangle, Renderer, - Size, Theme, Vector, + Color, Element, Length, Point, Rectangle, Renderer, Size, Theme, Vector, }; use rustc_hash::{FxHashMap, FxHashSet}; use std::future::Future; @@ -401,7 +400,7 @@ mod grid { interaction: &mut Interaction, event: Event, bounds: Rectangle, - cursor: Cursor, + cursor: mouse::Cursor, ) -> (event::Status, Option) { if let Event::Mouse(mouse::Event::ButtonReleased(_)) = event { *interaction = Interaction::None; @@ -539,7 +538,7 @@ mod grid { renderer: &Renderer, _theme: &Theme, bounds: Rectangle, - cursor: Cursor, + cursor: mouse::Cursor, ) -> Vec { let center = Vector::new(bounds.width / 2.0, bounds.height / 2.0); @@ -670,7 +669,7 @@ mod grid { &self, interaction: &Interaction, bounds: Rectangle, - cursor: Cursor, + cursor: mouse::Cursor, ) -> mouse::Interaction { match interaction { Interaction::Drawing => mouse::Interaction::Crosshair, diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index a4183db9..1f949d10 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -7,9 +7,8 @@ mod rainbow { use iced::advanced::layout::{self, Layout}; use iced::advanced::renderer; use iced::advanced::widget::{self, Widget}; - use iced::{ - Element, Length, Point, Rectangle, Renderer, Size, Theme, Vector, - }; + use iced::mouse; + use iced::{Element, Length, Rectangle, Renderer, Size, Theme, Vector}; #[derive(Debug, Clone, Copy, Default)] pub struct Rainbow; @@ -44,13 +43,13 @@ mod rainbow { _theme: &Theme, _style: &renderer::Style, layout: Layout<'_>, - cursor_position: Point, + cursor: mouse::Cursor, _viewport: &Rectangle, ) { use iced::advanced::Renderer as _; use iced_graphics::primitive::Mesh2D; - let b = layout.bounds(); + let bounds = layout.bounds(); // R O Y G B I V let color_r = [1.0, 0.0, 0.0, 1.0]; @@ -63,24 +62,24 @@ mod rainbow { let color_v = [0.75, 0.0, 0.5, 1.0]; let posn_center = { - if b.contains(cursor_position) { - [cursor_position.x - b.x, cursor_position.y - b.y] + if let Some(cursor_position) = cursor.position_in(&bounds) { + [cursor_position.x, cursor_position.y] } else { - [b.width / 2.0, b.height / 2.0] + [bounds.width / 2.0, bounds.height / 2.0] } }; let posn_tl = [0.0, 0.0]; - let posn_t = [b.width / 2.0, 0.0]; - let posn_tr = [b.width, 0.0]; - let posn_r = [b.width, b.height / 2.0]; - let posn_br = [b.width, b.height]; - let posn_b = [(b.width / 2.0), b.height]; - let posn_bl = [0.0, b.height]; - let posn_l = [0.0, b.height / 2.0]; + let posn_t = [bounds.width / 2.0, 0.0]; + let posn_tr = [bounds.width, 0.0]; + let posn_r = [bounds.width, bounds.height / 2.0]; + let posn_br = [bounds.width, bounds.height]; + let posn_b = [(bounds.width / 2.0), bounds.height]; + let posn_bl = [0.0, bounds.height]; + let posn_l = [0.0, bounds.height / 2.0]; let mesh = Primitive::SolidMesh { - size: b.size(), + size: bounds.size(), buffers: Mesh2D { vertices: vec![ ColoredVertex2D { @@ -133,9 +132,12 @@ mod rainbow { }, }; - renderer.with_translation(Vector::new(b.x, b.y), |renderer| { - renderer.draw_primitive(mesh); - }); + renderer.with_translation( + Vector::new(bounds.x, bounds.y), + |renderer| { + renderer.draw_primitive(mesh); + }, + ); } } diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index 9e1e4c2f..82ed05d2 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -1,10 +1,11 @@ +use iced::executor; +use iced::keyboard; +use iced::subscription::{self, Subscription}; +use iced::theme; use iced::widget::{ self, button, column, container, horizontal_space, row, text, text_input, }; -use iced::{ - executor, keyboard, subscription, theme, Alignment, Application, Command, - Element, Event, Length, Settings, Subscription, -}; +use iced::{Alignment, Application, Command, Element, Event, Length, Settings}; use self::modal::Modal; @@ -254,7 +255,7 @@ mod modal { state: &mut widget::Tree, event: Event, layout: Layout<'_>, - cursor_position: Point, + cursor: mouse::Cursor, renderer: &Renderer, clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, @@ -263,7 +264,7 @@ mod modal { &mut state.children[0], event, layout, - cursor_position, + cursor, renderer, clipboard, shell, @@ -277,7 +278,7 @@ mod modal { theme: &::Theme, style: &renderer::Style, layout: Layout<'_>, - cursor_position: Point, + cursor: mouse::Cursor, viewport: &Rectangle, ) { self.base.as_widget().draw( @@ -286,7 +287,7 @@ mod modal { theme, style, layout, - cursor_position, + cursor, viewport, ); } @@ -312,14 +313,14 @@ mod modal { &self, state: &widget::Tree, layout: Layout<'_>, - cursor_position: Point, + cursor: mouse::Cursor, viewport: &Rectangle, renderer: &Renderer, ) -> mouse::Interaction { self.base.as_widget().mouse_interaction( &state.children[0], layout, - cursor_position, + cursor, viewport, renderer, ) @@ -377,7 +378,7 @@ mod modal { &mut self, event: Event, layout: Layout<'_>, - cursor_position: Point, + cursor: mouse::Cursor, renderer: &Renderer, clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, @@ -389,7 +390,7 @@ mod modal { mouse::Button::Left, )) = &event { - if !content_bounds.contains(cursor_position) { + if !cursor.is_over(&content_bounds) { shell.publish(message.clone()); return event::Status::Captured; } @@ -400,7 +401,7 @@ mod modal { self.tree, event, layout.children().next().unwrap(), - cursor_position, + cursor, renderer, clipboard, shell, @@ -413,7 +414,7 @@ mod modal { theme: &Renderer::Theme, style: &renderer::Style, layout: Layout<'_>, - cursor_position: Point, + cursor: mouse::Cursor, ) { renderer.fill_quad( renderer::Quad { @@ -434,7 +435,7 @@ mod modal { theme, style, layout.children().next().unwrap(), - cursor_position, + cursor, &layout.bounds(), ); } @@ -456,14 +457,14 @@ mod modal { fn mouse_interaction( &self, layout: Layout<'_>, - cursor_position: Point, + cursor: mouse::Cursor, viewport: &Rectangle, renderer: &Renderer, ) -> mouse::Interaction { self.content.as_widget().mouse_interaction( self.tree, layout.children().next().unwrap(), - cursor_position, + cursor, viewport, renderer, ) diff --git a/examples/multitouch/src/main.rs b/examples/multitouch/src/main.rs index 7df6c929..2830b78d 100644 --- a/examples/multitouch/src/main.rs +++ b/examples/multitouch/src/main.rs @@ -1,9 +1,10 @@ //! This example shows how to use touch events in `Canvas` to draw //! a circle around each fingertip. This only works on touch-enabled //! computers like Microsoft Surface. +use iced::mouse; use iced::widget::canvas::event; use iced::widget::canvas::stroke::{self, Stroke}; -use iced::widget::canvas::{self, Canvas, Cursor, Geometry}; +use iced::widget::canvas::{self, Canvas, Geometry}; use iced::{ executor, touch, window, Application, Color, Command, Element, Length, Point, Rectangle, Renderer, Settings, Subscription, Theme, @@ -103,7 +104,7 @@ impl canvas::Program for State { _state: &mut Self::State, event: event::Event, _bounds: Rectangle, - _cursor: Cursor, + _cursor: mouse::Cursor, ) -> (event::Status, Option) { match event { event::Event::Touch(touch_event) => match touch_event { @@ -128,7 +129,7 @@ impl canvas::Program for State { renderer: &Renderer, _theme: &Theme, bounds: Rectangle, - _cursor: Cursor, + _cursor: mouse::Cursor, ) -> Vec { let fingerweb = self.cache.draw(renderer, bounds.size(), |frame| { if self.fingers.len() < 2 { diff --git a/examples/sierpinski_triangle/src/main.rs b/examples/sierpinski_triangle/src/main.rs index 4faac6d6..ad681a91 100644 --- a/examples/sierpinski_triangle/src/main.rs +++ b/examples/sierpinski_triangle/src/main.rs @@ -1,6 +1,7 @@ use std::fmt::Debug; use iced::executor; +use iced::mouse; use iced::widget::canvas::event::{self, Event}; use iced::widget::canvas::{self, Canvas}; use iced::widget::{column, row, slider, text}; @@ -105,7 +106,7 @@ impl canvas::Program for SierpinskiGraph { _state: &mut Self::State, event: Event, bounds: Rectangle, - cursor: canvas::Cursor, + cursor: mouse::Cursor, ) -> (event::Status, Option) { let cursor_position = if let Some(position) = cursor.position_in(&bounds) { @@ -137,7 +138,7 @@ impl canvas::Program for SierpinskiGraph { renderer: &Renderer, _theme: &Theme, bounds: Rectangle, - _cursor: canvas::Cursor, + _cursor: mouse::Cursor, ) -> Vec { let geom = self.cache.draw(renderer, bounds.size(), |frame| { frame.stroke( diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index d9e660d7..58d06206 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -8,11 +8,12 @@ //! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system use iced::application; use iced::executor; +use iced::mouse; use iced::theme::{self, Theme}; use iced::widget::canvas; use iced::widget::canvas::gradient; use iced::widget::canvas::stroke::{self, Stroke}; -use iced::widget::canvas::{Cursor, Path}; +use iced::widget::canvas::Path; use iced::window; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Renderer, @@ -161,7 +162,7 @@ impl canvas::Program for State { renderer: &Renderer, _theme: &Theme, bounds: Rectangle, - _cursor: Cursor, + _cursor: mouse::Cursor, ) -> Vec { use std::f32::consts::PI; diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index 515218e7..395cbc10 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -1,10 +1,10 @@ +use iced::executor; +use iced::keyboard; +use iced::subscription::{self, Subscription}; use iced::widget::{ self, button, column, container, pick_list, row, slider, text, text_input, }; -use iced::{ - executor, keyboard, subscription, Alignment, Application, Command, Element, - Event, Length, Settings, Subscription, -}; +use iced::{Alignment, Application, Command, Element, Event, Length, Settings}; use toast::{Status, Toast}; @@ -396,7 +396,7 @@ mod toast { state: &mut Tree, event: Event, layout: Layout<'_>, - cursor_position: Point, + cursor: mouse::Cursor, renderer: &Renderer, clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, @@ -405,7 +405,7 @@ mod toast { &mut state.children[0], event, layout, - cursor_position, + cursor, renderer, clipboard, shell, @@ -419,7 +419,7 @@ mod toast { theme: &Theme, style: &renderer::Style, layout: Layout<'_>, - cursor_position: Point, + cursor: mouse::Cursor, viewport: &Rectangle, ) { self.content.as_widget().draw( @@ -428,7 +428,7 @@ mod toast { theme, style, layout, - cursor_position, + cursor, viewport, ); } @@ -437,14 +437,14 @@ mod toast { &self, state: &Tree, layout: Layout<'_>, - cursor_position: Point, + cursor: mouse::Cursor, viewport: &Rectangle, renderer: &Renderer, ) -> mouse::Interaction { self.content.as_widget().mouse_interaction( &state.children[0], layout, - cursor_position, + cursor, viewport, renderer, ) @@ -523,7 +523,7 @@ mod toast { &mut self, event: Event, layout: Layout<'_>, - cursor_position: Point, + cursor: mouse::Cursor, renderer: &Renderer, clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, @@ -572,7 +572,7 @@ mod toast { state, event.clone(), layout, - cursor_position, + cursor, renderer, clipboard, &mut local_shell, @@ -595,7 +595,7 @@ mod toast { theme: &::Theme, style: &renderer::Style, layout: Layout<'_>, - cursor_position: Point, + cursor: mouse::Cursor, ) { let viewport = layout.bounds(); @@ -606,13 +606,7 @@ mod toast { .zip(layout.children()) { child.as_widget().draw( - state, - renderer, - theme, - style, - layout, - cursor_position, - &viewport, + state, renderer, theme, style, layout, cursor, &viewport, ); } } @@ -639,7 +633,7 @@ mod toast { fn mouse_interaction( &self, layout: Layout<'_>, - cursor_position: Point, + cursor: mouse::Cursor, viewport: &Rectangle, renderer: &Renderer, ) -> mouse::Interaction { @@ -649,11 +643,7 @@ mod toast { .zip(layout.children()) .map(|((child, state), layout)| { child.as_widget().mouse_interaction( - state, - layout, - cursor_position, - viewport, - renderer, + state, layout, cursor, viewport, renderer, ) }) .max() -- cgit From 5c8cfb411ed0a9a6e55bd1193cd7e97252e63d28 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 8 Jun 2023 20:16:46 +0200 Subject: Take `Rectangle` by value in `Cursor` API --- examples/bezier_tool/src/main.rs | 6 +++--- examples/game_of_life/src/main.rs | 11 +++++------ examples/geometry/src/main.rs | 2 +- examples/modal/src/main.rs | 2 +- examples/sierpinski_triangle/src/main.rs | 12 ++++++------ 5 files changed, 16 insertions(+), 17 deletions(-) (limited to 'examples') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index b4568cbf..310be28f 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -101,7 +101,7 @@ mod bezier { cursor: mouse::Cursor, ) -> (event::Status, Option) { let cursor_position = - if let Some(position) = cursor.position_in(&bounds) { + if let Some(position) = cursor.position_in(bounds) { position } else { return (event::Status::Ignored, None); @@ -183,7 +183,7 @@ mod bezier { bounds: Rectangle, cursor: mouse::Cursor, ) -> mouse::Interaction { - if cursor.is_over(&bounds) { + if cursor.is_over(bounds) { mouse::Interaction::Crosshair } else { mouse::Interaction::default() @@ -226,7 +226,7 @@ mod bezier { ) -> Geometry { let mut frame = Frame::new(renderer, bounds.size()); - if let Some(cursor_position) = cursor.position_in(&bounds) { + if let Some(cursor_position) = cursor.position_in(bounds) { match *self { Pending::One { from } => { let line = Path::line(from, cursor_position); diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index aaa8efd1..e951d734 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -407,7 +407,7 @@ mod grid { } let cursor_position = - if let Some(position) = cursor.position_in(&bounds) { + if let Some(position) = cursor.position_in(bounds) { position } else { return (event::Status::Ignored, None); @@ -567,10 +567,9 @@ mod grid { let overlay = { let mut frame = Frame::new(renderer, bounds.size()); - let hovered_cell = - cursor.position_in(&bounds).map(|position| { - Cell::at(self.project(position, frame.size())) - }); + let hovered_cell = cursor.position_in(bounds).map(|position| { + Cell::at(self.project(position, frame.size())) + }); if let Some(cell) = hovered_cell { frame.with_save(|frame| { @@ -675,7 +674,7 @@ mod grid { Interaction::Drawing => mouse::Interaction::Crosshair, Interaction::Erasing => mouse::Interaction::Crosshair, Interaction::Panning { .. } => mouse::Interaction::Grabbing, - Interaction::None if cursor.is_over(&bounds) => { + Interaction::None if cursor.is_over(bounds) => { mouse::Interaction::Crosshair } _ => mouse::Interaction::default(), diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index 1f949d10..29f78ea1 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -62,7 +62,7 @@ mod rainbow { let color_v = [0.75, 0.0, 0.5, 1.0]; let posn_center = { - if let Some(cursor_position) = cursor.position_in(&bounds) { + if let Some(cursor_position) = cursor.position_in(bounds) { [cursor_position.x, cursor_position.y] } else { [bounds.width / 2.0, bounds.height / 2.0] diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index 82ed05d2..5c43c203 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -390,7 +390,7 @@ mod modal { mouse::Button::Left, )) = &event { - if !cursor.is_over(&content_bounds) { + if !cursor.is_over(content_bounds) { shell.publish(message.clone()); return event::Status::Captured; } diff --git a/examples/sierpinski_triangle/src/main.rs b/examples/sierpinski_triangle/src/main.rs index ad681a91..885d3c63 100644 --- a/examples/sierpinski_triangle/src/main.rs +++ b/examples/sierpinski_triangle/src/main.rs @@ -108,12 +108,12 @@ impl canvas::Program for SierpinskiGraph { bounds: Rectangle, cursor: mouse::Cursor, ) -> (event::Status, Option) { - let cursor_position = - if let Some(position) = cursor.position_in(&bounds) { - position - } else { - return (event::Status::Ignored, None); - }; + let cursor_position = if let Some(position) = cursor.position_in(bounds) + { + position + } else { + return (event::Status::Ignored, None); + }; match event { Event::Mouse(mouse_event) => { -- cgit From 57db196c3a169fe1ecd654e9b58ce66defa5dd7e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 8 Jun 2023 20:23:47 +0200 Subject: Increase width of controls in `pane_grid` example --- examples/pane_grid/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index 2ffdcc69..54c36d69 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -300,7 +300,7 @@ fn view_content<'a>( ) ] .spacing(5) - .max_width(150); + .max_width(160); if total_panes > 1 && !is_pinned { controls = controls.push( -- cgit From aba98e49654852281ed17bedd1becac6f9db8700 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 8 Jun 2023 20:35:40 +0200 Subject: Extend cursor availability to the shell level --- examples/integration/src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index c935aca7..daecfeb2 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -6,6 +6,7 @@ use scene::Scene; use iced_wgpu::graphics::Viewport; use iced_wgpu::{wgpu, Backend, Renderer, Settings}; +use iced_winit::core::mouse; use iced_winit::core::renderer; use iced_winit::core::{Color, Size}; use iced_winit::runtime::program; @@ -194,10 +195,10 @@ pub fn main() -> Result<(), Box> { // We update iced let _ = state.update( viewport.logical_size(), - conversion::cursor_position( + mouse::Cursor::Available(conversion::cursor_position( cursor_position, viewport.scale_factor(), - ), + )), &mut renderer, &Theme::Dark, &renderer::Style { text_color: Color::WHITE }, -- cgit From 733c2bd9f594a21ce20444e8edcb4c5f88118e0a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 8 Jun 2023 20:45:48 +0200 Subject: Use `mouse::Cursor` in `integration` example --- examples/integration/src/main.rs | 42 ++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'examples') diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index daecfeb2..342d4c69 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -15,7 +15,6 @@ use iced_winit::style::Theme; use iced_winit::{conversion, futures, winit, Clipboard}; use winit::{ - dpi::PhysicalPosition, event::{Event, ModifiersState, WindowEvent}, event_loop::{ControlFlow, EventLoop}, }; @@ -40,6 +39,7 @@ pub fn main() -> Result<(), Box> { .and_then(|element| element.dyn_into::().ok()) .expect("Get canvas element") }; + #[cfg(not(target_arch = "wasm32"))] env_logger::init(); @@ -59,7 +59,7 @@ pub fn main() -> Result<(), Box> { Size::new(physical_size.width, physical_size.height), 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 clipboard = Clipboard::connect(&window); @@ -166,7 +166,7 @@ pub fn main() -> Result<(), Box> { Event::WindowEvent { event, .. } => { match event { WindowEvent::CursorMoved { position, .. } => { - cursor_position = position; + cursor_position = Some(position); } WindowEvent::ModifiersChanged(new_modifiers) => { modifiers = new_modifiers; @@ -195,13 +195,20 @@ pub fn main() -> Result<(), Box> { // We update iced let _ = state.update( viewport.logical_size(), - mouse::Cursor::Available(conversion::cursor_position( - cursor_position, - viewport.scale_factor(), - )), + cursor_position + .map(|p| { + conversion::cursor_position( + p, + viewport.scale_factor(), + ) + }) + .map(mouse::Cursor::Available) + .unwrap_or(mouse::Cursor::Unavailable), &mut renderer, &Theme::Dark, - &renderer::Style { text_color: Color::WHITE }, + &renderer::Style { + text_color: Color::WHITE, + }, &mut clipboard, &mut debug, ); @@ -243,7 +250,9 @@ pub fn main() -> Result<(), Box> { 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 @@ -276,15 +285,18 @@ pub fn main() -> Result<(), Box> { frame.present(); // Update the mouse cursor - window.set_cursor_icon( - iced_winit::conversion::mouse_interaction( - state.mouse_interaction(), - ), - ); + window.set_cursor_icon( + iced_winit::conversion::mouse_interaction( + state.mouse_interaction(), + ), + ); } Err(error) => match error { wgpu::SurfaceError::OutOfMemory => { - panic!("Swapchain error: {error}. Rendering cannot continue.") + panic!( + "Swapchain error: {error}. \ + Rendering cannot continue." + ) } _ => { // Try rendering again next frame. -- cgit