use crate::controls::UIElement;
use crate::error::Result;
use crate::events::{EventHandler, ValueChangedEventArgs};
use parking_lot::RwLock;
use std::sync::Arc;
use windows::Win32::Foundation::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SliderOrientation {
Horizontal,
Vertical,
}
#[derive(Clone)]
pub struct Slider {
element: UIElement,
inner: Arc<SliderInner>,
}
struct SliderInner {
value: RwLock<f64>,
minimum: RwLock<f64>,
maximum: RwLock<f64>,
step_frequency: RwLock<f64>,
orientation: RwLock<SliderOrientation>,
value_changed: EventHandler<ValueChangedEventArgs<f64>>,
}
impl Slider {
pub fn new() -> Result<Self> {
let inner = Arc::new(SliderInner {
value: RwLock::new(0.0),
minimum: RwLock::new(0.0),
maximum: RwLock::new(100.0),
step_frequency: RwLock::new(1.0),
orientation: RwLock::new(SliderOrientation::Horizontal),
value_changed: EventHandler::new(),
});
Ok(Slider {
element: UIElement::empty(),
inner,
})
}
pub fn value(&self) -> f64 {
*self.inner.value.read()
}
pub fn set_value(&self, value: f64) {
*self.inner.value.write() = value;
}
pub fn with_value(self, value: f64) -> Self {
self.set_value(value);
self
}
pub fn minimum(&self) -> f64 {
*self.inner.minimum.read()
}
pub fn set_minimum(&self, minimum: f64) {
*self.inner.minimum.write() = minimum;
}
pub fn with_minimum(self, minimum: f64) -> Self {
self.set_minimum(minimum);
self
}
pub fn maximum(&self) -> f64 {
*self.inner.maximum.read()
}
pub fn set_maximum(&self, maximum: f64) {
*self.inner.maximum.write() = maximum;
}
pub fn with_maximum(self, maximum: f64) -> Self {
self.set_maximum(maximum);
self
}
pub fn step_frequency(&self) -> f64 {
*self.inner.step_frequency.read()
}
pub fn set_step_frequency(&self, step: f64) {
*self.inner.step_frequency.write() = step;
}
pub fn with_step_frequency(self, step: f64) -> Self {
self.set_step_frequency(step);
self
}
pub fn orientation(&self) -> SliderOrientation {
*self.inner.orientation.read()
}
pub fn set_orientation(&self, orientation: SliderOrientation) {
*self.inner.orientation.write() = orientation;
}
pub fn with_orientation(self, orientation: SliderOrientation) -> Self {
self.set_orientation(orientation);
self
}
pub fn value_changed(&self) -> &EventHandler<ValueChangedEventArgs<f64>> {
&self.inner.value_changed
}
pub fn element(&self) -> &UIElement {
&self.element
}
pub fn hwnd(&self) -> HWND {
self.element.hwnd()
}
}
impl Default for Slider {
fn default() -> Self {
Self::new().expect("Failed to create slider")
}
}
impl From<Slider> for UIElement {
fn from(slider: Slider) -> Self {
slider.element.clone()
}
}