1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
use dioxus::prelude::*;
use freya_elements::elements as dioxus_elements;
use freya_elements::events::{MouseEvent, WheelEvent};
use freya_hooks::{use_get_theme, use_node_ref};
/// [`Slider`] component properties.
#[derive(Props)]
pub struct SliderProps<'a> {
/// Handler for the `onmoved` event.
pub onmoved: EventHandler<'a, f64>,
/// Width of the Slider.
pub width: f64,
/// Height of the Slider.
pub value: f64,
}
#[inline]
fn ensure_correct_slider_range(value: f64) -> f64 {
if value < 0.0 {
// TODO: Better logging
println!("Slider value is less than 0.0, setting to 0.0");
0.0
} else if value > 100.0 {
println!("Slider value is greater than 100.0, setting to 100.0");
100.0
} else {
value
}
}
/// Controlled `Slider` component.
///
/// You must pass a percentage from 0.0 to 100.0 and listen for value changes with `onmoved` and then decide if this changes are applicable,
/// and if so, apply them.
///
/// # Props
/// See [`SliderProps`].
///
/// # Styling
/// Inherits a [`SliderTheme`](freya_hooks::SliderTheme) theme.
///
/// # Example
/// ```no_run
/// # use freya::prelude::*;
/// fn app(cx: Scope) -> Element {
/// let percentage = use_state(cx, || 20.0);
///
/// render!(
/// label {
/// "Value: {percentage}"
/// }
/// Slider {
/// width: 100.0,
/// value: *percentage.get(),
/// onmoved: |p| {
/// percentage.set(p);
/// }
/// }
/// )
/// }
/// ```
#[allow(non_snake_case)]
pub fn Slider<'a>(cx: Scope<'a, SliderProps>) -> Element<'a> {
let theme = use_get_theme(cx);
let theme = &theme.slider;
let hovering = use_state(cx, || false);
let clicking = use_state(cx, || false);
let value = ensure_correct_slider_range(cx.props.value);
let (node_reference, size) = use_node_ref(cx);
let width = cx.props.width + 14.0;
let progress = (value / 100.0) * cx.props.width + 0.5;
let onmouseleave = |_: MouseEvent| {
if !(*clicking.get()) {
hovering.set(false);
}
};
let onmouseover = move |e: MouseEvent| {
hovering.set(true);
if *clicking.get() {
let coordinates = e.get_element_coordinates();
let mut x = coordinates.x - 7.5 - size.read().area.min_x() as f64;
x = x.clamp(0.0, width);
let mut percentage = x / cx.props.width * 100.0;
percentage = percentage.clamp(0.0, 100.0);
cx.props.onmoved.call(percentage);
}
};
let onmousedown = |_: MouseEvent| {
clicking.set(true);
};
let onclick = |_: MouseEvent| {
clicking.set(false);
};
let onwheel = move |e: WheelEvent| {
let wheel_y = e.get_delta_y();
let progress_x = (value / 100.0) * cx.props.width;
let mut x = progress_x + (wheel_y * 7.5);
x = x.clamp(0.0, width);
let mut percentage = x / cx.props.width * 100.0;
percentage = percentage.clamp(0.0, 100.0);
cx.props.onmoved.call(percentage);
};
render!(
rect {
reference: node_reference,
width: "{width}",
height: "20",
onmousedown: onmousedown,
onglobalclick: onclick,
onglobalmouseover: onmouseover,
onmouseleave: onmouseleave,
onwheel: onwheel,
display: "center",
direction: "both",
padding: "1",
rect {
background: "{theme.background}",
width: "100%",
height: "6",
direction: "horizontal",
corner_radius: "50",
rect {
background: "{theme.thumb_inner_background}",
width: "{progress}",
height: "100%",
corner_radius: "50",
}
rect {
width: "{progress}",
height: "100%",
offset_y: "-5",
offset_x: "-2",
rect {
background: "{theme.thumb_background}",
width: "17",
height: "17",
corner_radius: "50",
padding: "3",
rect {
height: "100%",
width: "100%",
background: "{theme.thumb_inner_background}",
corner_radius: "50"
}
}
}
}
}
)
}