Handle mouse cursor in iced_wgpu

This commit is contained in:
Héctor Ramón Jiménez 2019-10-11 23:30:53 +02:00
parent 8846a239cf
commit a031a6f213
10 changed files with 104 additions and 67 deletions

View file

@ -1,5 +1,5 @@
/// The state of the mouse cursor.
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[derive(Debug, Eq, PartialEq, Clone, Copy, PartialOrd, Ord)]
pub enum MouseCursor {
/// The cursor is out of the bounds of the user interface.
OutOfBounds,

View file

@ -100,7 +100,7 @@ impl Renderer {
fn draw(
&mut self,
primitive: &Primitive,
(primitive, mouse_cursor): &(Primitive, MouseCursor),
target: &mut Target,
) -> MouseCursor {
log::debug!("Drawing");
@ -152,7 +152,7 @@ impl Renderer {
self.queue.submit(&[encoder.finish()]);
MouseCursor::OutOfBounds
*mouse_cursor
}
fn draw_primitive(&mut self, primitive: &Primitive) {
@ -243,8 +243,7 @@ impl Renderer {
}
impl iced_native::Renderer for Renderer {
// TODO: Add `MouseCursor` here (?)
type Output = Primitive;
type Output = (Primitive, MouseCursor);
}
impl Windowed for Renderer {

View file

@ -1,7 +1,7 @@
use crate::{Primitive, Renderer};
use iced_native::{
button, Align, Background, Button, Color, Layout, Length, Node, Point,
Style,
button, Align, Background, Button, Color, Layout, Length, MouseCursor,
Node, Point, Style,
};
impl button::Renderer for Renderer {
@ -24,26 +24,35 @@ impl button::Renderer for Renderer {
) -> Self::Output {
let bounds = layout.bounds();
let (content, _) = button.content.draw(
self,
layout.children().next().unwrap(),
cursor_position,
);
(
Primitive::Group {
primitives: vec![
Primitive::Quad {
bounds,
background: button.background.unwrap_or(Background::Color(
Color {
background: button.background.unwrap_or(
Background::Color(Color {
r: 0.8,
b: 0.8,
g: 0.8,
a: 1.0,
},
)),
}),
),
border_radius: button.border_radius,
},
button.content.draw(
self,
layout.children().next().unwrap(),
cursor_position,
),
content,
],
}
},
if bounds.contains(cursor_position) {
MouseCursor::Pointer
} else {
MouseCursor::OutOfBounds
},
)
}
}

View file

@ -1,5 +1,7 @@
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 {
fn node<Message>(&self, _checkbox: &Checkbox<Message>) -> Node {
@ -13,6 +15,6 @@ impl checkbox::Renderer for Renderer {
_cursor_position: Point,
) -> Self::Output {
// TODO
Primitive::None
(Primitive::None, MouseCursor::OutOfBounds)
}
}

View file

@ -1,5 +1,5 @@
use crate::{Primitive, Renderer};
use iced_native::{column, Column, Layout, Point};
use iced_native::{column, Column, Layout, MouseCursor, Point};
impl column::Renderer for Renderer {
fn draw<Message>(
@ -8,15 +8,27 @@ impl column::Renderer for Renderer {
layout: Layout<'_>,
cursor_position: Point,
) -> Self::Output {
let mut mouse_cursor = MouseCursor::OutOfBounds;
(
Primitive::Group {
primitives: column
.children
.iter()
.zip(layout.children())
.map(|(child, layout)| {
child.draw(self, layout, cursor_position)
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,
)
}
}

View file

@ -1,5 +1,5 @@
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 {
fn node(&self, _image: &Image<&str>) -> Node {
@ -11,6 +11,6 @@ impl image::Renderer<&str> for Renderer {
_image: &Image<&str>,
_layout: Layout<'_>,
) -> Self::Output {
Primitive::None
(Primitive::None, MouseCursor::OutOfBounds)
}
}

View file

@ -1,5 +1,5 @@
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 {
fn node<Message>(&self, _checkbox: &Radio<Message>) -> Node {
@ -12,6 +12,6 @@ impl radio::Renderer for Renderer {
_layout: Layout<'_>,
_cursor_position: Point,
) -> Self::Output {
Primitive::None
(Primitive::None, MouseCursor::OutOfBounds)
}
}

View file

@ -1,5 +1,5 @@
use crate::{Primitive, Renderer};
use iced_native::{row, Layout, Point, Row};
use iced_native::{row, Layout, MouseCursor, Point, Row};
impl row::Renderer for Renderer {
fn draw<Message>(
@ -8,15 +8,27 @@ impl row::Renderer for Renderer {
layout: Layout<'_>,
cursor_position: Point,
) -> Self::Output {
let mut mouse_cursor = MouseCursor::OutOfBounds;
(
Primitive::Group {
primitives: row
.children
.iter()
.zip(layout.children())
.map(|(child, layout)| {
child.draw(self, layout, cursor_position)
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,
)
}
}

View file

@ -1,5 +1,5 @@
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 {
fn node<Message>(&self, _slider: &Slider<Message>) -> Node {
@ -12,6 +12,6 @@ impl slider::Renderer for Renderer {
_layout: Layout<'_>,
_cursor_position: Point,
) -> Self::Output {
Primitive::None
(Primitive::None, MouseCursor::OutOfBounds)
}
}

View file

@ -1,5 +1,5 @@
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};
@ -68,6 +68,7 @@ impl text::Renderer for Renderer {
}
fn draw(&mut self, text: &Text, layout: Layout<'_>) -> Self::Output {
(
Primitive::Text {
content: text.content.clone(),
size: f32::from(text.size.unwrap_or(20)),
@ -75,6 +76,8 @@ impl text::Renderer for Renderer {
color: text.color.unwrap_or(Color::BLACK),
horizontal_alignment: text.horizontal_alignment,
vertical_alignment: text.vertical_alignment,
}
},
MouseCursor::OutOfBounds,
)
}
}