use super::{Session, accent_color, display, glyph, tone_color};
use crate::theme::Theme;
use dugix_core::interactive::{Node, Tone};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use unicode_width::UnicodeWidthStr;
pub(super) fn label_column(session: &Session, width: u16) -> u16 {
let widest = session
.document()
.all_nodes()
.into_iter()
.flat_map(|node| match node {
Node::Stepper { label, .. }
| Node::Select { label, .. }
| Node::Slider { label, .. }
| Node::Rating { label, .. }
| Node::Progress { label, .. }
| Node::Meter { label, .. } => vec![display(session, label).width()],
Node::Bars { items } => items
.iter()
.map(|item| display(session, &item.label).width())
.collect(),
_ => Vec::new(),
})
.max()
.unwrap_or(0);
u16::try_from(widest + 2)
.unwrap_or(u16::MAX)
.clamp(6, (width / 3).max(6))
.min(width)
}
pub(super) fn label(buffer: &mut Buffer, area: Rect, text: &str, focused: bool, theme: &Theme) {
let style = if focused {
Style::default()
.fg(theme.accent)
.add_modifier(Modifier::BOLD)
} else {
Style::default().fg(theme.fg_dim)
};
let text = super::clipped(text, area.width.saturating_sub(1));
buffer.set_stringn(area.x, area.y, text, usize::from(area.width), style);
}
pub(super) fn bar(buffer: &mut Buffer, area: Rect, ratio: f64, fill: Color, theme: &Theme) {
if area.width == 0 {
return;
}
let scaled = (ratio.clamp(0.0, 1.0) * f64::from(area.width) * 8.0).round();
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let eighth_cells = scaled as usize;
let (whole, part) = (eighth_cells / 8, eighth_cells % 8);
let track = Style::default().fg(theme.border_dim);
for x in 0..area.width {
let cell = usize::from(x);
let (symbol, style) = if cell < whole {
(glyph::BAR_FILL, Style::default().fg(fill))
} else if cell == whole && part > 0 {
(glyph::BAR_END, Style::default().fg(fill))
} else {
(glyph::BAR_TRACK, track)
};
buffer.set_string(area.x + x, area.y, symbol, style);
}
}
pub(super) fn tone_fill(tone: Tone, session: &Session, theme: &Theme) -> (Color, Color) {
match tone {
Tone::Normal => (theme.control, theme.fg),
Tone::Muted => (theme.raised, theme.fg_dim),
tone => {
let fill = tone_color(Some(tone), accent_color(session.document(), theme), theme);
(fill, theme.on(fill))
}
}
}
pub(super) fn cap(buffer: &mut Buffer, (x, y): (u16, u16), left: bool, fill: Color, theme: &Theme) {
if theme.round_caps {
let under = buffer.cell((x, y)).map_or(theme.surface, |cell| cell.bg);
buffer.set_string(
x,
y,
if left {
glyph::CAP_LEFT
} else {
glyph::CAP_RIGHT
},
Style::default().fg(fill).bg(under),
);
} else {
buffer.set_string(x, y, " ", Style::default().bg(fill));
}
}
pub(super) fn pill(
buffer: &mut Buffer,
(x, y): (u16, u16),
limit: u16,
text: &str,
(fill, ink): (Color, Color),
(bold, theme): (bool, &Theme),
) -> u16 {
if limit < 5 {
return 0;
}
let text = super::clipped(text, limit.saturating_sub(4));
let inner = Style::default().fg(ink).bg(fill);
let inner = if bold {
inner.add_modifier(Modifier::BOLD)
} else {
inner
};
let inner_width = u16::try_from(text.width()).unwrap_or(0) + 2;
cap(buffer, (x, y), true, fill, theme);
buffer.set_string(x + 1, y, format!(" {text} "), inner);
cap(buffer, (x + 1 + inner_width, y), false, fill, theme);
inner_width + 2
}
pub(super) fn revealed(width: u16, session: &Session) -> u16 {
let shown = (f64::from(width) * session.drawn_fraction()).ceil();
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let shown = shown as u16;
shown.min(width)
}
pub(super) fn pill_width(text: &str) -> u16 {
u16::try_from(text.width())
.unwrap_or(u16::MAX)
.saturating_add(4)
}
pub(super) fn value(buffer: &mut Buffer, area: Rect, text: &str, theme: &Theme) -> u16 {
let width = u16::try_from(text.width())
.unwrap_or(u16::MAX)
.min(area.width);
buffer.set_stringn(
area.right() - width,
area.y,
text,
usize::from(width),
Style::default().fg(theme.fg).add_modifier(Modifier::BOLD),
);
width
}
pub(super) fn pill_spans(
text: &str,
(fill, ink): (Color, Color),
(bg, round): (Color, bool),
) -> Vec<ratatui::text::Span<'static>> {
use ratatui::text::Span;
let fill_or_bg = if fill == Color::Reset { bg } else { fill };
let end = |symbol: &'static str| {
if round {
Span::styled(symbol, Style::default().fg(fill_or_bg).bg(bg))
} else {
Span::styled(" ", Style::default().bg(fill_or_bg))
}
};
vec![
end(glyph::CAP_LEFT),
Span::styled(
format!(" {text} "),
Style::default()
.fg(ink)
.bg(fill)
.add_modifier(Modifier::BOLD),
),
end(glyph::CAP_RIGHT),
]
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum Kind {
Primary,
Secondary,
Disabled,
}
pub(super) fn button(
buffer: &mut Buffer,
(x, y): (u16, u16),
limit: u16,
text: &str,
(kind, focused): (Kind, bool),
panel: Color,
theme: &Theme,
) -> Rect {
if !focused && kind != Kind::Primary {
if limit < 5 {
return Rect::new(x, y, 0, 1);
}
let text = super::clipped(text, limit.saturating_sub(4));
let style = if kind == Kind::Disabled {
Style::default().fg(theme.border_dim)
} else {
Style::default()
.fg(theme.fg_dim)
.add_modifier(Modifier::UNDERLINED)
};
buffer.set_string(x + 2, y, &text, style);
return Rect::new(x, y, u16::try_from(text.width()).unwrap_or(0) + 4, 1);
}
let colors = if focused {
(theme.accent, theme.on(theme.accent))
} else {
(panel, theme.on(panel))
};
let width = pill(
buffer,
(x, y),
limit,
text,
colors,
(kind == Kind::Primary || focused, theme),
);
if focused && width > 0 {
buffer.set_string(
x + 1,
y,
glyph::SELECTED,
Style::default()
.fg(colors.1)
.bg(colors.0)
.add_modifier(Modifier::BOLD),
);
}
Rect::new(x, y, width, 1)
}
pub(super) fn segment_rects(labels: &[String], area: Rect) -> Vec<Rect> {
let mut x = area.x.saturating_add(1).min(area.right());
labels
.iter()
.map(|label| {
let width = u16::try_from(label.width())
.unwrap_or(u16::MAX)
.saturating_add(2)
.min(area.right().saturating_sub(x).saturating_sub(1));
let rect = Rect::new(x, area.y, width, 1);
x = x.saturating_add(width).saturating_add(1).min(area.right());
rect
})
.collect()
}
pub(super) fn segments_width(labels: &[String]) -> u16 {
let text: usize = labels.iter().map(|label| label.width() + 2).sum();
u16::try_from(text + labels.len().saturating_sub(1) + 2).unwrap_or(u16::MAX)
}
pub(super) fn segmented(
buffer: &mut Buffer,
area: Rect,
labels: &[String],
(active, fill): (usize, Color),
theme: &Theme,
) -> Vec<Rect> {
let rects = segment_rects(labels, area);
let surface = |index: usize| if index == active { fill } else { theme.control };
for (index, (rect, label)) in rects.iter().zip(labels).enumerate() {
if rect.width == 0 {
continue;
}
let style = if index == active {
Style::default()
.fg(theme.on(fill))
.bg(fill)
.add_modifier(Modifier::BOLD)
} else {
Style::default().fg(theme.fg).bg(theme.control)
};
buffer.set_style(*rect, style);
buffer.set_stringn(
rect.x + 1,
rect.y,
label,
usize::from(rect.width.saturating_sub(2)),
style,
);
if index + 1 < labels.len() && rect.right() < area.right() {
buffer.set_string(
rect.right(),
rect.y,
"▕",
Style::default().fg(theme.bg).bg(theme.control),
);
}
}
if let (Some(first), Some(last)) = (rects.first(), rects.last())
&& first.width > 0
&& last.right() < area.right()
{
cap(buffer, (first.x - 1, first.y), true, surface(0), theme);
cap(
buffer,
(last.right(), last.y),
false,
surface(rects.len() - 1),
theme,
);
}
rects
}
pub(super) fn tab_rects(labels: &[String], area: Rect) -> Vec<Rect> {
segment_rects(labels, area)
}
pub(super) fn chip(buffer: &mut Buffer, (x, y): (u16, u16), text: &str, style: Style) -> u16 {
let width = u16::try_from(text.width())
.unwrap_or(u16::MAX)
.saturating_add(2);
buffer.set_string(x, y, format!(" {text} "), style);
width
}
pub(super) const SWITCH_WIDTH: u16 = 4;
pub(super) const CHECK_WIDTH: u16 = 3;
pub(super) fn switch(buffer: &mut Buffer, (x, y): (u16, u16), on: bool, theme: &Theme) {
let (fill, knob) = if on {
(theme.success, theme.on(theme.success))
} else {
(theme.control, theme.fg_dim)
};
let track = Style::default().fg(knob).bg(fill);
cap(buffer, (x, y), true, fill, theme);
if on {
buffer.set_string(x + 1, y, " ", track);
buffer.set_string(x + 2, y, glyph::DOT, track);
} else {
buffer.set_string(x + 1, y, glyph::DOT, track);
buffer.set_string(x + 2, y, " ", track);
}
cap(buffer, (x + 3, y), false, fill, theme);
}
pub(super) fn check(buffer: &mut Buffer, (x, y): (u16, u16), on: bool, theme: &Theme) {
let fill = if on { theme.success } else { theme.control };
cap(buffer, (x, y), true, fill, theme);
buffer.set_string(
x + 1,
y,
if on { glyph::OK } else { " " },
Style::default()
.fg(theme.on(theme.success))
.bg(fill)
.add_modifier(Modifier::BOLD),
);
cap(buffer, (x + 2, y), false, fill, theme);
}
pub(super) fn solid_bar(buffer: &mut Buffer, area: Rect, ratio: f64, fill: Color) {
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let cells = (ratio.clamp(0.0, 1.0) * f64::from(area.width)).round() as u16;
let cells = if ratio > 0.0 { cells.max(1) } else { 0 };
buffer.set_string(
area.x,
area.y,
"▆".repeat(usize::from(cells.min(area.width))),
Style::default().fg(fill),
);
}