use super::*;
impl SourceControlPanel {
pub(super) fn render_soft_split_button(
&self,
cx: &ViewCx,
buf: &mut Buffer,
main: Rect,
caret: Rect,
) {
use crate::widgets::surface::{CHEVRON_UNFOLDED, paint_soft_button};
let (fill, fg) = primary_button_colors(cx.theme, cx.theme.gradient());
let lift = |r: Rect| {
if crate::widgets::hover::contains(r, cx.pointer) {
lift_button(fill)
} else {
fill
}
};
let (main_fill, caret_fill) = (lift(main), lift(caret));
let main_face = paint_soft_button(buf, main, main_fill, true, false);
let caret_face = paint_soft_button(buf, caret, caret_fill, false, true);
let gap = Rect {
x: main.right(),
width: caret.x.saturating_sub(main.right()),
..main
};
let mut caret_label = caret_face;
if gap.width > 0 {
let gap_face = paint_soft_button(buf, gap, caret_fill, false, false);
if let Color::Rgb(r, g, b) = fill
&& gap_face.width > 0
{
let seam = crate::gradient::rgb_color(crate::gradient::lerp_rgb(
(r, g, b),
(0, 0, 0),
0.45,
));
buf[(gap_face.x, gap_face.y)]
.set_symbol("\u{258f}")
.set_fg(seam);
}
if caret_face.width > 0 && gap_face.width > 0 {
caret_label = Rect {
x: gap_face.x,
width: gap_face.width + caret_face.width,
..caret_face
};
}
}
paint_button_label(buf, main_face, "\u{eab2} Commit", main_fill, fg);
paint_button_label(
buf,
caret_label,
&CHEVRON_UNFOLDED.to_string(),
caret_fill,
fg,
);
}
}
pub fn render_rounded_button(buf: &mut Buffer, area: Rect, label: &str, bg: Color, fg: Color) {
if area.width < 2 || area.height < 1 {
return;
}
let solid = Style::default().bg(bg);
for ry in 0..area.height {
for rx in 0..area.width {
buf[(area.x + rx, area.y + ry)]
.set_symbol(" ")
.set_style(solid);
}
}
let corner_style = Style::default().fg(bg);
if area.height >= 2 {
buf[(area.x, area.y)]
.set_symbol("╭")
.set_style(corner_style);
buf[(area.x + area.width - 1, area.y)]
.set_symbol("╮")
.set_style(corner_style);
buf[(area.x, area.y + area.height - 1)]
.set_symbol("╰")
.set_style(corner_style);
buf[(area.x + area.width - 1, area.y + area.height - 1)]
.set_symbol("╯")
.set_style(corner_style);
}
let max_w = area.width as usize;
let visible_label: String = label.chars().take(max_w).collect();
if visible_label.is_empty() {
return;
}
let label_w = visible_label.chars().count() as u16;
let label_x = area.x + (area.width - label_w) / 2;
let label_y = area.y + area.height / 2;
buf.set_string(
label_x,
label_y,
&visible_label,
Style::default().fg(fg).bg(bg).add_modifier(Modifier::BOLD),
);
}
pub fn primary_button_colors(theme: crate::theme::Theme, focus_gradient: bool) -> (Color, Color) {
let teal = crate::gradient::rgb_color(crate::gradient::PRIMARY_BTN_BG);
match crate::theme::tones(theme) {
Some(t) => (teal, t.strong()),
None => (
if focus_gradient {
teal
} else {
Color::Rgb(BUTTON_BG_RGB.0, BUTTON_BG_RGB.1, BUTTON_BG_RGB.2)
},
Color::Rgb(BUTTON_FG_RGB.0, BUTTON_FG_RGB.1, BUTTON_FG_RGB.2),
),
}
}
pub(super) fn lift_button(fill: Color) -> Color {
match fill {
Color::Rgb(r, g, b) => crate::gradient::rgb_color(crate::gradient::lerp_rgb(
(r, g, b),
(0xff, 0xff, 0xff),
0.12,
)),
other => other,
}
}
pub(super) fn paint_button_label(buf: &mut Buffer, area: Rect, label: &str, bg: Color, fg: Color) {
let visible_label: String = label.chars().take(area.width as usize).collect();
if visible_label.is_empty() {
return;
}
let label_w = visible_label.chars().count() as u16;
buf.set_string(
area.x + (area.width - label_w) / 2,
area.y + area.height / 2,
&visible_label,
Style::default().fg(fg).bg(bg).add_modifier(Modifier::BOLD),
);
}
pub fn render_button(
buf: &mut Buffer,
area: Rect,
label: &str,
bg: Color,
fg: Color,
theme: crate::theme::Theme,
hovered: bool,
) {
if crate::theme::tones(theme).is_none() {
render_rounded_button(buf, area, label, bg, fg);
return;
}
if area.width < 2 || area.height < 1 {
return;
}
let fill = if hovered { lift_button(bg) } else { bg };
let face = crate::widgets::surface::paint_soft_button(buf, area, fill, true, true);
paint_button_label(buf, face, label, fill, fg);
}