use crate::render::RenderContext;
use crate::types::{Rect, WidgetState};
use super::settings::ScrollChevronSettings;
use super::types::ScrollChevronRenderKind;
pub use crate::ui::widgets::atomic::button::ChevronDirection;
pub struct ScrollChevronView {
pub direction: ChevronDirection,
pub hovered: bool,
pub disabled: bool,
}
#[derive(Debug, Default, Clone, Copy)]
pub struct ScrollChevronResult {
pub clicked: bool,
pub hovered: bool,
}
pub fn draw_scroll_chevron(
ctx: &mut dyn RenderContext,
rect: Rect,
state: WidgetState,
view: &ScrollChevronView,
settings: &ScrollChevronSettings,
kind: &ScrollChevronRenderKind,
) -> ScrollChevronResult {
match kind {
ScrollChevronRenderKind::Default => {
draw_scroll_chevron_inner(ctx, rect, view, settings)
}
ScrollChevronRenderKind::Custom(f) => {
f(ctx, rect, state, view, settings);
ScrollChevronResult {
clicked: false,
hovered: view.hovered && !view.disabled,
}
}
}
}
fn draw_scroll_chevron_inner(
ctx: &mut dyn RenderContext,
rect: Rect,
view: &ScrollChevronView,
settings: &ScrollChevronSettings,
) -> ScrollChevronResult {
let theme = settings.theme.as_ref();
let style = settings.style.as_ref();
let color = if view.disabled {
theme.scroll_chevron_color_disabled()
} else if view.hovered {
theme.scroll_chevron_color_hover()
} else {
theme.scroll_chevron_color()
};
if view.hovered && !view.disabled {
ctx.set_fill_color(theme.scroll_chevron_bg_hover());
ctx.fill_rounded_rect(rect.x, rect.y, rect.width, rect.height, style.hover_bg_radius());
}
let inset = style.chevron_inset();
let cx = rect.center_x();
let cy = rect.center_y();
let half = (rect.width.min(rect.height) / 2.0 - inset).max(2.0);
ctx.set_stroke_color(color);
ctx.set_stroke_width(style.chevron_thickness());
ctx.set_line_dash(&[]);
ctx.begin_path();
match view.direction {
ChevronDirection::Left => {
ctx.move_to(cx + half * 0.5, cy - half);
ctx.line_to(cx - half * 0.5, cy);
ctx.line_to(cx + half * 0.5, cy + half);
}
ChevronDirection::Right => {
ctx.move_to(cx - half * 0.5, cy - half);
ctx.line_to(cx + half * 0.5, cy);
ctx.line_to(cx - half * 0.5, cy + half);
}
ChevronDirection::Up => {
ctx.move_to(cx - half, cy + half * 0.5);
ctx.line_to(cx, cy - half * 0.5);
ctx.line_to(cx + half, cy + half * 0.5);
}
ChevronDirection::Down => {
ctx.move_to(cx - half, cy - half * 0.5);
ctx.line_to(cx, cy + half * 0.5);
ctx.line_to(cx + half, cy - half * 0.5);
}
}
ctx.stroke();
ScrollChevronResult {
clicked: false,
hovered: view.hovered && !view.disabled,
}
}