Handle mouse cursor in iced_wgpu
This commit is contained in:
parent
8846a239cf
commit
a031a6f213
10 changed files with 104 additions and 67 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
/// The state of the mouse cursor.
|
/// The state of the mouse cursor.
|
||||||
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
#[derive(Debug, Eq, PartialEq, Clone, Copy, PartialOrd, Ord)]
|
||||||
pub enum MouseCursor {
|
pub enum MouseCursor {
|
||||||
/// The cursor is out of the bounds of the user interface.
|
/// The cursor is out of the bounds of the user interface.
|
||||||
OutOfBounds,
|
OutOfBounds,
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,7 @@ impl Renderer {
|
||||||
|
|
||||||
fn draw(
|
fn draw(
|
||||||
&mut self,
|
&mut self,
|
||||||
primitive: &Primitive,
|
(primitive, mouse_cursor): &(Primitive, MouseCursor),
|
||||||
target: &mut Target,
|
target: &mut Target,
|
||||||
) -> MouseCursor {
|
) -> MouseCursor {
|
||||||
log::debug!("Drawing");
|
log::debug!("Drawing");
|
||||||
|
|
@ -152,7 +152,7 @@ impl Renderer {
|
||||||
|
|
||||||
self.queue.submit(&[encoder.finish()]);
|
self.queue.submit(&[encoder.finish()]);
|
||||||
|
|
||||||
MouseCursor::OutOfBounds
|
*mouse_cursor
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_primitive(&mut self, primitive: &Primitive) {
|
fn draw_primitive(&mut self, primitive: &Primitive) {
|
||||||
|
|
@ -243,8 +243,7 @@ impl Renderer {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl iced_native::Renderer for Renderer {
|
impl iced_native::Renderer for Renderer {
|
||||||
// TODO: Add `MouseCursor` here (?)
|
type Output = (Primitive, MouseCursor);
|
||||||
type Output = Primitive;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Windowed for Renderer {
|
impl Windowed for Renderer {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::{Primitive, Renderer};
|
use crate::{Primitive, Renderer};
|
||||||
use iced_native::{
|
use iced_native::{
|
||||||
button, Align, Background, Button, Color, Layout, Length, Node, Point,
|
button, Align, Background, Button, Color, Layout, Length, MouseCursor,
|
||||||
Style,
|
Node, Point, Style,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl button::Renderer for Renderer {
|
impl button::Renderer for Renderer {
|
||||||
|
|
@ -24,26 +24,35 @@ impl button::Renderer for Renderer {
|
||||||
) -> Self::Output {
|
) -> Self::Output {
|
||||||
let bounds = layout.bounds();
|
let bounds = layout.bounds();
|
||||||
|
|
||||||
Primitive::Group {
|
let (content, _) = button.content.draw(
|
||||||
primitives: vec![
|
self,
|
||||||
Primitive::Quad {
|
layout.children().next().unwrap(),
|
||||||
bounds,
|
cursor_position,
|
||||||
background: button.background.unwrap_or(Background::Color(
|
);
|
||||||
Color {
|
|
||||||
r: 0.8,
|
(
|
||||||
b: 0.8,
|
Primitive::Group {
|
||||||
g: 0.8,
|
primitives: vec![
|
||||||
a: 1.0,
|
Primitive::Quad {
|
||||||
},
|
bounds,
|
||||||
)),
|
background: button.background.unwrap_or(
|
||||||
border_radius: button.border_radius,
|
Background::Color(Color {
|
||||||
},
|
r: 0.8,
|
||||||
button.content.draw(
|
b: 0.8,
|
||||||
self,
|
g: 0.8,
|
||||||
layout.children().next().unwrap(),
|
a: 1.0,
|
||||||
cursor_position,
|
}),
|
||||||
),
|
),
|
||||||
],
|
border_radius: button.border_radius,
|
||||||
}
|
},
|
||||||
|
content,
|
||||||
|
],
|
||||||
|
},
|
||||||
|
if bounds.contains(cursor_position) {
|
||||||
|
MouseCursor::Pointer
|
||||||
|
} else {
|
||||||
|
MouseCursor::OutOfBounds
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
use crate::{Primitive, Renderer};
|
use crate::{Primitive, Renderer};
|
||||||
use iced_native::{checkbox, Checkbox, Layout, Node, Point, Style};
|
use iced_native::{
|
||||||
|
checkbox, Checkbox, Layout, MouseCursor, Node, Point, Style,
|
||||||
|
};
|
||||||
|
|
||||||
impl checkbox::Renderer for Renderer {
|
impl checkbox::Renderer for Renderer {
|
||||||
fn node<Message>(&self, _checkbox: &Checkbox<Message>) -> Node {
|
fn node<Message>(&self, _checkbox: &Checkbox<Message>) -> Node {
|
||||||
|
|
@ -13,6 +15,6 @@ impl checkbox::Renderer for Renderer {
|
||||||
_cursor_position: Point,
|
_cursor_position: Point,
|
||||||
) -> Self::Output {
|
) -> Self::Output {
|
||||||
// TODO
|
// TODO
|
||||||
Primitive::None
|
(Primitive::None, MouseCursor::OutOfBounds)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{Primitive, Renderer};
|
use crate::{Primitive, Renderer};
|
||||||
use iced_native::{column, Column, Layout, Point};
|
use iced_native::{column, Column, Layout, MouseCursor, Point};
|
||||||
|
|
||||||
impl column::Renderer for Renderer {
|
impl column::Renderer for Renderer {
|
||||||
fn draw<Message>(
|
fn draw<Message>(
|
||||||
|
|
@ -8,15 +8,27 @@ impl column::Renderer for Renderer {
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
cursor_position: Point,
|
cursor_position: Point,
|
||||||
) -> Self::Output {
|
) -> Self::Output {
|
||||||
Primitive::Group {
|
let mut mouse_cursor = MouseCursor::OutOfBounds;
|
||||||
primitives: column
|
|
||||||
.children
|
(
|
||||||
.iter()
|
Primitive::Group {
|
||||||
.zip(layout.children())
|
primitives: column
|
||||||
.map(|(child, layout)| {
|
.children
|
||||||
child.draw(self, layout, cursor_position)
|
.iter()
|
||||||
})
|
.zip(layout.children())
|
||||||
.collect(),
|
.map(|(child, layout)| {
|
||||||
}
|
let (primitive, new_mouse_cursor) =
|
||||||
|
child.draw(self, layout, cursor_position);
|
||||||
|
|
||||||
|
if new_mouse_cursor > mouse_cursor {
|
||||||
|
mouse_cursor = new_mouse_cursor;
|
||||||
|
}
|
||||||
|
|
||||||
|
primitive
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
},
|
||||||
|
mouse_cursor,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{Primitive, Renderer};
|
use crate::{Primitive, Renderer};
|
||||||
use iced_native::{image, Image, Layout, Node, Style};
|
use iced_native::{image, Image, Layout, MouseCursor, Node, Style};
|
||||||
|
|
||||||
impl image::Renderer<&str> for Renderer {
|
impl image::Renderer<&str> for Renderer {
|
||||||
fn node(&self, _image: &Image<&str>) -> Node {
|
fn node(&self, _image: &Image<&str>) -> Node {
|
||||||
|
|
@ -11,6 +11,6 @@ impl image::Renderer<&str> for Renderer {
|
||||||
_image: &Image<&str>,
|
_image: &Image<&str>,
|
||||||
_layout: Layout<'_>,
|
_layout: Layout<'_>,
|
||||||
) -> Self::Output {
|
) -> Self::Output {
|
||||||
Primitive::None
|
(Primitive::None, MouseCursor::OutOfBounds)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{Primitive, Renderer};
|
use crate::{Primitive, Renderer};
|
||||||
use iced_native::{radio, Layout, Node, Point, Radio, Style};
|
use iced_native::{radio, Layout, MouseCursor, Node, Point, Radio, Style};
|
||||||
|
|
||||||
impl radio::Renderer for Renderer {
|
impl radio::Renderer for Renderer {
|
||||||
fn node<Message>(&self, _checkbox: &Radio<Message>) -> Node {
|
fn node<Message>(&self, _checkbox: &Radio<Message>) -> Node {
|
||||||
|
|
@ -12,6 +12,6 @@ impl radio::Renderer for Renderer {
|
||||||
_layout: Layout<'_>,
|
_layout: Layout<'_>,
|
||||||
_cursor_position: Point,
|
_cursor_position: Point,
|
||||||
) -> Self::Output {
|
) -> Self::Output {
|
||||||
Primitive::None
|
(Primitive::None, MouseCursor::OutOfBounds)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{Primitive, Renderer};
|
use crate::{Primitive, Renderer};
|
||||||
use iced_native::{row, Layout, Point, Row};
|
use iced_native::{row, Layout, MouseCursor, Point, Row};
|
||||||
|
|
||||||
impl row::Renderer for Renderer {
|
impl row::Renderer for Renderer {
|
||||||
fn draw<Message>(
|
fn draw<Message>(
|
||||||
|
|
@ -8,15 +8,27 @@ impl row::Renderer for Renderer {
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
cursor_position: Point,
|
cursor_position: Point,
|
||||||
) -> Self::Output {
|
) -> Self::Output {
|
||||||
Primitive::Group {
|
let mut mouse_cursor = MouseCursor::OutOfBounds;
|
||||||
primitives: row
|
|
||||||
.children
|
(
|
||||||
.iter()
|
Primitive::Group {
|
||||||
.zip(layout.children())
|
primitives: row
|
||||||
.map(|(child, layout)| {
|
.children
|
||||||
child.draw(self, layout, cursor_position)
|
.iter()
|
||||||
})
|
.zip(layout.children())
|
||||||
.collect(),
|
.map(|(child, layout)| {
|
||||||
}
|
let (primitive, new_mouse_cursor) =
|
||||||
|
child.draw(self, layout, cursor_position);
|
||||||
|
|
||||||
|
if new_mouse_cursor > mouse_cursor {
|
||||||
|
mouse_cursor = new_mouse_cursor;
|
||||||
|
}
|
||||||
|
|
||||||
|
primitive
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
},
|
||||||
|
mouse_cursor,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{Primitive, Renderer};
|
use crate::{Primitive, Renderer};
|
||||||
use iced_native::{slider, Layout, Node, Point, Slider, Style};
|
use iced_native::{slider, Layout, MouseCursor, Node, Point, Slider, Style};
|
||||||
|
|
||||||
impl slider::Renderer for Renderer {
|
impl slider::Renderer for Renderer {
|
||||||
fn node<Message>(&self, _slider: &Slider<Message>) -> Node {
|
fn node<Message>(&self, _slider: &Slider<Message>) -> Node {
|
||||||
|
|
@ -12,6 +12,6 @@ impl slider::Renderer for Renderer {
|
||||||
_layout: Layout<'_>,
|
_layout: Layout<'_>,
|
||||||
_cursor_position: Point,
|
_cursor_position: Point,
|
||||||
) -> Self::Output {
|
) -> Self::Output {
|
||||||
Primitive::None
|
(Primitive::None, MouseCursor::OutOfBounds)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{Primitive, Renderer};
|
use crate::{Primitive, Renderer};
|
||||||
use iced_native::{text, Color, Layout, Node, Style, Text};
|
use iced_native::{text, Color, Layout, MouseCursor, Node, Style, Text};
|
||||||
|
|
||||||
use wgpu_glyph::{GlyphCruncher, Section};
|
use wgpu_glyph::{GlyphCruncher, Section};
|
||||||
|
|
||||||
|
|
@ -68,13 +68,16 @@ impl text::Renderer for Renderer {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw(&mut self, text: &Text, layout: Layout<'_>) -> Self::Output {
|
fn draw(&mut self, text: &Text, layout: Layout<'_>) -> Self::Output {
|
||||||
Primitive::Text {
|
(
|
||||||
content: text.content.clone(),
|
Primitive::Text {
|
||||||
size: f32::from(text.size.unwrap_or(20)),
|
content: text.content.clone(),
|
||||||
bounds: layout.bounds(),
|
size: f32::from(text.size.unwrap_or(20)),
|
||||||
color: text.color.unwrap_or(Color::BLACK),
|
bounds: layout.bounds(),
|
||||||
horizontal_alignment: text.horizontal_alignment,
|
color: text.color.unwrap_or(Color::BLACK),
|
||||||
vertical_alignment: text.vertical_alignment,
|
horizontal_alignment: text.horizontal_alignment,
|
||||||
}
|
vertical_alignment: text.vertical_alignment,
|
||||||
|
},
|
||||||
|
MouseCursor::OutOfBounds,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue