use serde::{Deserialize, Serialize};
use crate::node::{Common, IntoNode, Node};
use crate::style::Style;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ScrollbarOrientation {
#[default]
VerticalRight,
VerticalLeft,
HorizontalBottom,
HorizontalTop,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScrollbarState {
pub content_length: u32,
pub position: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub viewport_content_length: Option<u32>,
}
impl ScrollbarState {
#[must_use]
pub fn new(content_length: u32) -> Self {
Self {
content_length,
position: 0,
viewport_content_length: None,
}
}
#[must_use]
pub fn position(mut self, position: u32) -> Self {
self.position = position;
self
}
#[must_use]
pub fn viewport_content_length(mut self, length: u32) -> Self {
self.viewport_content_length = Some(length);
self
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Scrollbar {
#[serde(flatten)]
pub common: Common,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub orientation: Option<ScrollbarOrientation>,
pub state: ScrollbarState,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub thumb_style: Option<Style>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub track_style: Option<Style>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub begin_symbol: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub end_symbol: Option<String>,
}
impl Scrollbar {
#[must_use]
pub fn new(state: ScrollbarState) -> Self {
Self {
common: Common::new(),
orientation: None,
state,
thumb_style: None,
track_style: None,
begin_symbol: None,
end_symbol: None,
}
}
#[must_use]
pub fn orientation(mut self, orientation: ScrollbarOrientation) -> Self {
self.orientation = Some(orientation);
self
}
#[must_use]
pub fn thumb_style(mut self, style: Style) -> Self {
self.thumb_style = Some(style);
self
}
#[must_use]
pub fn track_style(mut self, style: Style) -> Self {
self.track_style = Some(style);
self
}
#[must_use]
pub fn begin_symbol(mut self, symbol: impl Into<String>) -> Self {
self.begin_symbol = Some(symbol.into());
self
}
#[must_use]
pub fn end_symbol(mut self, symbol: impl Into<String>) -> Self {
self.end_symbol = Some(symbol.into());
self
}
}
impl IntoNode for Scrollbar {}
impl From<Scrollbar> for Node {
fn from(s: Scrollbar) -> Self {
Node::Scrollbar(s)
}
}