iced-yoda/wgpu/src/renderer/widget/slider.rs

107 lines
3.2 KiB
Rust
Raw Normal View History

2020-01-07 00:28:08 +01:00
use crate::{
slider::{HandleShape, StyleSheet},
Primitive, Renderer,
};
use iced_native::{slider, Background, Color, MouseCursor, Point, Rectangle};
2019-10-12 05:07:00 +02:00
const HANDLE_HEIGHT: f32 = 22.0;
2019-10-05 19:22:51 +02:00
impl slider::Renderer for Renderer {
2020-01-07 00:28:08 +01:00
type Style = Box<dyn StyleSheet>;
fn height(&self) -> u32 {
30
2019-10-05 19:22:51 +02:00
}
fn draw(
2019-10-05 19:22:51 +02:00
&mut self,
bounds: Rectangle,
2019-10-12 05:07:00 +02:00
cursor_position: Point,
range: std::ops::RangeInclusive<f32>,
value: f32,
is_dragging: bool,
2020-01-07 00:28:08 +01:00
style_sheet: &Self::Style,
) -> Self::Output {
2019-10-12 05:07:00 +02:00
let is_mouse_over = bounds.contains(cursor_position);
2020-01-07 00:28:08 +01:00
let style = if is_dragging {
style_sheet.dragging()
} else if is_mouse_over {
style_sheet.hovered()
} else {
style_sheet.active()
};
2019-10-12 05:07:00 +02:00
let rail_y = bounds.y + (bounds.height / 2.0).round();
let (rail_top, rail_bottom) = (
Primitive::Quad {
bounds: Rectangle {
x: bounds.x,
y: rail_y,
width: bounds.width,
height: 2.0,
},
2020-01-07 00:28:08 +01:00
background: Background::Color(style.rail_colors.0),
2019-10-12 05:07:00 +02:00
border_radius: 0,
border_width: 0,
border_color: Color::TRANSPARENT,
2019-10-12 05:07:00 +02:00
},
Primitive::Quad {
bounds: Rectangle {
x: bounds.x,
y: rail_y + 2.0,
width: bounds.width,
height: 2.0,
},
2020-01-07 00:28:08 +01:00
background: Background::Color(style.rail_colors.1),
2019-10-12 05:07:00 +02:00
border_radius: 0,
border_width: 0,
border_color: Color::TRANSPARENT,
2019-10-12 05:07:00 +02:00
},
);
let (range_start, range_end) = range.into_inner();
2019-10-12 05:07:00 +02:00
2020-01-07 00:28:08 +01:00
let (handle_width, handle_height, handle_border_radius) =
match style.handle.shape {
HandleShape::Circle { radius } => {
(f32::from(radius * 2), f32::from(radius * 2), radius)
}
HandleShape::Rectangle {
width,
border_radius,
} => (f32::from(width), HANDLE_HEIGHT, border_radius),
};
let handle_offset = (bounds.width - handle_width)
* ((value - range_start) / (range_end - range_start).max(1.0));
2019-10-12 05:07:00 +02:00
let handle = Primitive::Quad {
bounds: Rectangle {
x: bounds.x + handle_offset.round(),
2020-01-07 00:28:08 +01:00
y: rail_y - handle_height / 2.0,
width: handle_width,
height: handle_height,
2019-10-12 05:07:00 +02:00
},
2020-01-07 00:28:08 +01:00
background: Background::Color(style.handle.color),
border_radius: handle_border_radius,
border_width: style.handle.border_width,
border_color: style.handle.border_color,
};
2019-10-12 05:07:00 +02:00
(
Primitive::Group {
primitives: vec![rail_top, rail_bottom, handle],
2019-10-12 05:07:00 +02:00
},
if is_dragging {
2019-10-12 05:07:00 +02:00
MouseCursor::Grabbing
} else if is_mouse_over {
MouseCursor::Grab
} else {
MouseCursor::OutOfBounds
},
)
2019-10-05 19:22:51 +02:00
}
}