use std::{fmt, sync::Arc};
use gpui_kit::{Context, Hsla, component::Theme};
use super::{CellChange, CellStyle, LiveGridDelegate, LiveGridState};
use crate::{
highlight::{ChangeDirection, HighlightPolicy, RowChange, RowStamps, Stamp},
model::ColumnIx,
};
const CHANGE_OPACITY: f32 = 0.45;
#[derive(Clone, Copy, Debug)]
pub struct ChangePalette {
inserted: Hsla,
updated: Hsla,
up: Hsla,
down: Hsla,
removed: Hsla,
removed_foreground: Hsla,
}
impl ChangePalette {
pub(super) fn new(theme: &Theme) -> Self {
Self {
inserted: theme.success,
updated: theme.warning,
up: theme.success,
down: theme.danger,
removed: theme.danger,
removed_foreground: theme.muted_foreground,
}
}
pub fn inserted(&self) -> Hsla {
self.inserted
}
pub fn updated(&self) -> Hsla {
self.updated
}
pub fn up(&self) -> Hsla {
self.up
}
pub fn down(&self) -> Hsla {
self.down
}
pub fn removed(&self) -> Hsla {
self.removed
}
pub fn removed_foreground(&self) -> Hsla {
self.removed_foreground
}
}
pub trait ChangePainter: 'static {
fn paint(&self, change: &CellChange, palette: &ChangePalette, style: &mut CellStyle);
}
fn change_background(change: &CellChange, color: Hsla) -> Hsla {
color.opacity(CHANGE_OPACITY * change.intensity())
}
#[inline]
pub(super) fn stamp_change(
stamp: Stamp,
column: Option<ColumnIx>,
policy: &HighlightPolicy,
tick: u32,
) -> Option<CellChange> {
policy.intensity(stamp, tick).map(|intensity| CellChange {
intensity,
change: stamp.change(),
direction: stamp.direction(),
column_ix: column,
})
}
#[derive(Clone, Copy, Debug)]
pub(super) enum RowChanges<'a> {
Stamps(Option<RowStamps<'a>>),
Removed(CellChange),
}
impl RowChanges<'_> {
#[inline]
pub(super) fn cell(
self,
column: ColumnIx,
policy: &HighlightPolicy,
tick: u32,
) -> Option<CellChange> {
match self {
Self::Stamps(stamps) => stamps
.and_then(|stamps| stamp_change(stamps.get(column), Some(column), policy, tick)),
Self::Removed(change) => Some(CellChange {
column_ix: Some(column),
..change
}),
}
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct UniformChanges;
impl ChangePainter for UniformChanges {
fn paint(&self, change: &CellChange, palette: &ChangePalette, style: &mut CellStyle) {
match change.change() {
RowChange::Inserted => {
style.set_background(change_background(change, palette.inserted()));
}
RowChange::Updated => {
style.set_background(change_background(change, palette.updated()));
}
RowChange::Removed => {
style.set_background(change_background(change, palette.removed()));
style.set_foreground(palette.removed_foreground());
}
}
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct DirectionalChanges;
impl ChangePainter for DirectionalChanges {
fn paint(&self, change: &CellChange, palette: &ChangePalette, style: &mut CellStyle) {
match (change.change(), change.direction()) {
(RowChange::Updated, Some(direction)) => {
let color = match direction {
ChangeDirection::Up => palette.up(),
ChangeDirection::Down => palette.down(),
};
style.set_background(change_background(change, color));
style.set_trend(Some(direction));
}
_ => UniformChanges.paint(change, palette, style),
}
}
}
#[derive(Clone)]
#[non_exhaustive]
pub enum ChangePaint {
Uniform(UniformChanges),
Directional(DirectionalChanges),
Custom(Arc<dyn ChangePainter>),
}
impl Default for ChangePaint {
fn default() -> Self {
Self::Uniform(UniformChanges)
}
}
impl fmt::Debug for ChangePaint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Uniform(painter) => f.debug_tuple("Uniform").field(painter).finish(),
Self::Directional(painter) => f.debug_tuple("Directional").field(painter).finish(),
Self::Custom(_) => f.write_str("Custom(..)"),
}
}
}
impl ChangePainter for ChangePaint {
#[inline]
fn paint(&self, change: &CellChange, palette: &ChangePalette, style: &mut CellStyle) {
match self {
Self::Uniform(painter) => painter.paint(change, palette, style),
Self::Directional(painter) => painter.paint(change, palette, style),
Self::Custom(painter) => painter.paint(change, palette, style),
}
}
}
impl<D: LiveGridDelegate> LiveGridState<D> {
pub fn with_change_paint(mut self, paint: ChangePaint) -> Self {
self.change_paint = paint;
self
}
pub fn set_change_paint(&mut self, paint: ChangePaint, cx: &mut Context<Self>) {
self.change_paint = paint;
cx.notify();
}
pub fn change_paint(&self) -> &ChangePaint {
&self.change_paint
}
}