use std::sync::Arc;
use super::{
names,
part_style::Length,
recipe::{Case, Interaction, Name, Recipe, ResolvedPart, ResolvedRecipe},
shared::Orientation,
theme::{Themed, current_theme},
};
use crate::{
app::theme::{BorderSize, PaletteColor, Theme},
components::{Column, Row, ScrollBoth, ScrollHorizontal, ScrollVertical},
layout::{
layout_kind::ScrollState,
scrollbar::{ScrollBarPlacement, ScrollBarStyle, ScrollBarVisibility},
},
node::{Element, dimension::Dimension, outline::Outline},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ScrollAreaPart {
Root,
}
names!(ScrollAreaPart {
Root => "root",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ScrollAreaState {
Vertical,
Horizontal,
Gutter,
}
names!(ScrollAreaState {
Vertical => "vertical",
Horizontal => "horizontal",
Gutter => "gutter",
});
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ScrollAreaOrientation {
#[default]
Vertical,
Horizontal,
Both,
}
impl From<Orientation> for ScrollAreaOrientation {
fn from(orientation: Orientation) -> Self {
match orientation {
Orientation::Horizontal => Self::Horizontal,
Orientation::Vertical => Self::Vertical,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ScrollAreaScrollbars {
#[default]
Auto,
Always,
Hover,
Scrolling,
Never,
}
impl From<ScrollAreaScrollbars> for ScrollBarVisibility {
fn from(scrollbars: ScrollAreaScrollbars) -> Self {
match scrollbars {
ScrollAreaScrollbars::Auto => Self::Auto,
ScrollAreaScrollbars::Always => Self::Always,
ScrollAreaScrollbars::Hover => Self::OnHover,
ScrollAreaScrollbars::Scrolling => Self::WhileScrolling,
ScrollAreaScrollbars::Never => Self::Never,
}
}
}
pub type ScrollAreaRenderer = Arc<dyn Fn(ScrollAreaView) -> Element + Send + Sync>;
pub struct ScrollArea {
content: Vec<Element>,
orientation: ScrollAreaOrientation,
scrollbars: ScrollAreaScrollbars,
gutter: bool,
fill_width: bool,
scroll_state: Option<ScrollState>,
label: Option<Arc<str>>,
states: Vec<Name>,
rules: Recipe,
forced: Option<Interaction>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl Default for ScrollArea {
fn default() -> Self {
Self::new()
}
}
impl ScrollArea {
pub fn new() -> Self {
Self {
content: Vec::new(),
orientation: ScrollAreaOrientation::Vertical,
scrollbars: ScrollAreaScrollbars::Auto,
gutter: false,
fill_width: false,
scroll_state: None,
label: None,
states: Vec::new(),
rules: Recipe::new(),
forced: None,
id: None,
key: None,
}
}
pub fn child(mut self, child: impl Into<Element>) -> Self {
self.content.push(child.into());
self
}
pub fn children<I: Into<Element>>(mut self, children: impl IntoIterator<Item = I>) -> Self {
self.content.extend(children.into_iter().map(Into::into));
self
}
pub fn orientation(mut self, orientation: impl Into<ScrollAreaOrientation>) -> Self {
self.orientation = orientation.into();
self
}
pub fn scrollbars(mut self, scrollbars: ScrollAreaScrollbars) -> Self {
self.scrollbars = scrollbars;
self
}
pub fn gutter(mut self, gutter: bool) -> Self {
self.gutter = gutter;
self
}
pub fn fill_width(mut self, fill: bool) -> Self {
self.fill_width = fill;
self
}
pub fn scroll_state(mut self, state: ScrollState) -> Self {
self.scroll_state = Some(state);
self
}
pub fn label(mut self, label: &str) -> Self {
self.label = Some(Arc::from(label));
self
}
pub fn width(mut self, width: impl Into<Length>) -> Self {
let width = width.into();
self.rules = self.rules.base(ScrollAreaPart::Root, move |s| s.width(width));
self
}
pub fn height(mut self, height: impl Into<Length>) -> Self {
let height = height.into();
self.rules = self.rules.base(ScrollAreaPart::Root, move |s| s.height(height));
self
}
pub fn max_height(mut self, height: impl Into<Length>) -> Self {
let height = height.into();
self.rules = self.rules.base(ScrollAreaPart::Root, move |s| s.max_height(height));
self
}
pub fn state(mut self, state: impl Into<Name>, on: bool) -> Self {
let state = state.into();
self.states.retain(|held| *held != state);
if on {
self.states.push(state);
}
self
}
pub fn style(mut self, part: impl Into<Name>, style: impl FnOnce(super::PartStyle) -> super::PartStyle) -> Self {
self.rules = self.rules.base(part, style);
self
}
pub fn rules(mut self, rules: &Recipe) -> Self {
self.rules = self.rules.extend(rules);
self
}
pub fn force(mut self, interaction: Option<Interaction>) -> Self {
self.forced = interaction;
self
}
pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
self.id = Some(id.into());
self
}
pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
self.key = Some(key.into());
self
}
pub fn build(self, theme: &Theme) -> Element {
let (down, across) = match self.orientation {
ScrollAreaOrientation::Vertical => (true, false),
ScrollAreaOrientation::Horizontal => (false, true),
ScrollAreaOrientation::Both => (true, true),
};
let mut case = Case::new()
.breakpoint(super::current_breakpoint())
.state(ScrollAreaState::Vertical, down)
.state(ScrollAreaState::Horizontal, across)
.state(ScrollAreaState::Gutter, self.gutter);
for state in &self.states {
case = case.state(state.clone(), true);
}
let bar = ScrollBarStyle {
visible: self.scrollbars.into(),
placement: if self.gutter {
ScrollBarPlacement::Reserved
} else {
ScrollBarPlacement::Overlay
},
..theme.scrollbar().clone()
};
let view = ScrollAreaView {
content: self.content,
orientation: self.orientation,
bar,
fill_width: self.fill_width,
scroll_state: self.scroll_state,
label: self.label,
recipe: theme
.resolve::<ScrollArea>(&case, Some(&self.rules))
.owned_by(self.id.as_ref()),
forced: self.forced,
id: self.id,
key: self.key,
};
match theme.renderer::<ScrollArea>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
}
impl From<ScrollArea> for Element {
fn from(area: ScrollArea) -> Self {
area.build(¤t_theme())
}
}
pub struct ScrollAreaView {
pub content: Vec<Element>,
pub orientation: ScrollAreaOrientation,
pub bar: ScrollBarStyle,
pub fill_width: bool,
scroll_state: Option<ScrollState>,
pub label: Option<Arc<str>>,
pub recipe: ResolvedRecipe,
forced: Option<Interaction>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl ScrollAreaView {
pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
self.recipe.part(part).forced(self.forced)
}
pub fn theme(&self) -> &Theme {
self.recipe.theme()
}
pub fn content(&mut self) -> Element {
let content = std::mem::take(&mut self.content);
match self.orientation {
ScrollAreaOrientation::Horizontal => content.into_iter().fold(Row::new(), Row::child).into(),
ScrollAreaOrientation::Vertical => content
.into_iter()
.fold(Column::new().width(Dimension::Pct(100.0)), Column::child)
.into(),
ScrollAreaOrientation::Both => content.into_iter().fold(Column::new(), Column::child).into(),
}
}
pub fn render(mut self) -> Element {
let content = self.content();
let part = self.part(ScrollAreaPart::Root);
macro_rules! dress {
($scroll:expr) => {{
let mut scroll = $scroll.part(&part).scrollbar(self.bar.clone());
if let Some(state) = self.scroll_state.clone() {
scroll = scroll.with_scroll_state(state);
}
if let Some(label) = &self.label {
scroll = scroll
.tab_index(0)
.describe("a11y_role", "region")
.describe("a11y_name", label.clone());
}
if let Some(id) = &self.id {
scroll = scroll.id(id.clone());
}
if let Some(key) = &self.key {
scroll = scroll.key(key.clone());
}
Element::from(scroll)
}};
}
match self.orientation {
ScrollAreaOrientation::Vertical => dress!(ScrollVertical::new(content)),
ScrollAreaOrientation::Horizontal => dress!(ScrollHorizontal::new(content).fill_width(self.fill_width)),
ScrollAreaOrientation::Both => dress!(ScrollBoth::new(content).fill_width(self.fill_width)),
}
}
}
impl crate::ui::Styled for ScrollArea {
type Component = Self;
fn rules(self, rules: &crate::ui::Recipe) -> Self {
Self::rules(self, rules)
}
fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
Self::state(self, state, on)
}
}
impl Themed for ScrollArea {
const NAME: &'static str = "scroll_area";
type Renderer = ScrollAreaRenderer;
fn parts() -> crate::ui::Declared {
ScrollAreaPart::declared()
}
fn states() -> crate::ui::Declared {
ScrollAreaState::declared()
}
fn recipe() -> Recipe {
Recipe::new().focus_visible(ScrollAreaPart::Root, |s| {
s.outline(Outline::new(BorderSize::Md, PaletteColor::Ring).offset(-2.0))
})
}
}