use gpui::{App, IntoElement, ParentElement, Pixels, RenderOnce, Styled, Window, div, px};
use crate::theme::Theme;
const THICKNESS: f32 = 1.;
const INSET: f32 = 16.;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum DividerInset {
#[default]
None,
Start,
Both,
Middle,
}
impl DividerInset {
pub fn margins(self) -> (f32, f32) {
match self {
DividerInset::None => (0., 0.),
DividerInset::Start => (INSET, 0.),
DividerInset::Both | DividerInset::Middle => (INSET, INSET),
}
}
}
#[derive(IntoElement)]
pub struct Divider {
vertical: bool,
inset: DividerInset,
}
impl Divider {
pub fn horizontal() -> Self {
Self {
vertical: false,
inset: DividerInset::None,
}
}
pub fn vertical() -> Self {
Self {
vertical: true,
inset: DividerInset::None,
}
}
pub fn inset(mut self, inset: DividerInset) -> Self {
self.inset = inset;
self
}
}
impl RenderOnce for Divider {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
let color = Theme::of(cx).outline_variant;
let (start, end) = self.inset.margins();
let (start, end): (Pixels, Pixels) = (px(start), px(end));
if self.vertical {
div()
.flex()
.flex_col()
.flex_none()
.h_full()
.w(px(THICKNESS))
.pt(start)
.pb(end)
.child(div().flex_1().w_full().bg(color))
} else {
div()
.flex()
.flex_none()
.w_full()
.h(px(THICKNESS))
.pl(start)
.pr(end)
.child(div().flex_1().h_full().bg(color))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn insets_follow_the_spec() {
assert_eq!(DividerInset::None.margins(), (0., 0.));
assert_eq!(DividerInset::Start.margins(), (16., 0.));
assert_eq!(DividerInset::Both.margins(), (16., 16.));
assert_eq!(DividerInset::Middle.margins(), DividerInset::Both.margins());
}
}