use gpui_kit::component::menu::PopupMenu;
use gpui_kit::{AnyElement, App, Context, FontWeight, Hsla, SharedString, Window};
use super::{
drag::{DragPayload, RowDragContext},
edit::{EditError, EditMessageContext, EditState, EditValidateContext, EditableContext},
groups::{CaptionTag, GroupCaptionContext},
menu::GridMenuContext,
progress::ProgressContext,
tooltip::{CellTooltip, CellTooltipContext, HeaderTooltipContext},
widgets::CellElementContext,
};
use crate::{
engine::GroupInfo,
highlight::{ChangeDirection, RowChange},
model::{CellValue, ColumnIx, GridRow},
store::{GridProgress, PinnedEdge},
};
#[derive(Clone, Copy, Debug, Default)]
pub struct CellStyle {
pub(super) background: Option<Hsla>,
pub(super) foreground: Option<Hsla>,
pub(super) font_weight: Option<FontWeight>,
pub(super) trend: Option<ChangeDirection>,
pub(super) error_mark: bool,
}
impl CellStyle {
pub fn set_background(&mut self, color: Hsla) {
self.background = Some(color);
}
pub fn set_foreground(&mut self, color: Hsla) {
self.foreground = Some(color);
}
pub fn background(&self) -> Option<Hsla> {
self.background
}
pub fn foreground(&self) -> Option<Hsla> {
self.foreground
}
pub fn set_font_weight(&mut self, weight: FontWeight) {
self.font_weight = Some(weight);
}
pub fn font_weight(&self) -> Option<FontWeight> {
self.font_weight
}
pub fn set_trend(&mut self, trend: Option<ChangeDirection>) {
self.trend = trend;
}
pub fn trend(&self) -> Option<ChangeDirection> {
self.trend
}
pub fn set_error_mark(&mut self, shown: bool) {
self.error_mark = shown;
}
pub fn has_error_mark(&self) -> bool {
self.error_mark
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CellChange {
pub(super) intensity: f32,
pub(super) change: RowChange,
pub(super) direction: Option<ChangeDirection>,
pub(super) column_ix: Option<ColumnIx>,
}
impl CellChange {
pub fn intensity(&self) -> f32 {
self.intensity
}
pub fn change(&self) -> RowChange {
self.change
}
pub fn direction(&self) -> Option<ChangeDirection> {
self.direction
}
pub fn column_ix(&self) -> Option<ColumnIx> {
self.column_ix
}
pub fn fade(&self, color: Hsla) -> Hsla {
color.opacity(self.intensity)
}
}
pub struct CellStyleContext<'a, R: GridRow> {
pub(super) row: &'a R,
pub(super) column: R::Column,
pub(super) value: CellValue<'a>,
pub(super) change: Option<CellChange>,
pub(super) display_ix: usize,
pub(super) pinned: Option<PinnedEdge>,
pub(super) selected: bool,
pub(super) focused: bool,
pub(super) edit_state: Option<&'a EditState>,
}
impl<'a, R: GridRow> CellStyleContext<'a, R> {
pub fn row(&self) -> &'a R {
self.row
}
pub fn column(&self) -> R::Column {
self.column
}
pub fn value(&self) -> CellValue<'a> {
self.value
}
pub fn change(&self) -> Option<CellChange> {
self.change
}
pub fn display_ix(&self) -> usize {
self.display_ix
}
pub fn pinned_edge(&self) -> Option<PinnedEdge> {
self.pinned
}
pub fn is_selected(&self) -> bool {
self.selected
}
pub fn is_focused(&self) -> bool {
self.focused
}
pub fn edit_state(&self) -> Option<&'a EditState> {
self.edit_state
}
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub enum AggregateLine<'a> {
Caption(GroupInfo<'a>),
Footer(GroupInfo<'a>),
Totals(PinnedEdge),
}
pub struct AggregateStyleContext<'a, R: GridRow> {
pub(super) line: AggregateLine<'a>,
pub(super) column: R::Column,
pub(super) value: Option<CellValue<'a>>,
pub(super) change: Option<CellChange>,
pub(super) focused: bool,
}
impl<'a, R: GridRow> AggregateStyleContext<'a, R> {
pub fn line(&self) -> AggregateLine<'a> {
self.line
}
pub fn column(&self) -> R::Column {
self.column
}
pub fn value(&self) -> Option<CellValue<'a>> {
self.value
}
pub fn change(&self) -> Option<CellChange> {
self.change
}
pub fn is_focused(&self) -> bool {
self.focused
}
}
pub trait LiveGridDelegate: Sized + 'static {
type Row: GridRow;
fn cell_style(
&self,
_cell: &CellStyleContext<'_, Self::Row>,
_style: &mut CellStyle,
_cx: &App,
) {
}
fn group_caption(
&self,
caption: &GroupCaptionContext<'_, Self::Row>,
out: &mut String,
_cx: &App,
) {
caption.default_caption(out);
}
fn group_caption_tag(
&self,
_caption: &GroupCaptionContext<'_, Self::Row>,
_out: &mut String,
_cx: &App,
) -> Option<CaptionTag> {
None
}
fn aggregate_style(
&self,
_cell: &AggregateStyleContext<'_, Self::Row>,
_style: &mut CellStyle,
_cx: &App,
) {
}
fn primary_command(
&self,
_menu: &GridMenuContext<'_, Self::Row>,
_cx: &App,
) -> Option<SharedString> {
None
}
fn context_menu(
&self,
menu_context: &GridMenuContext<'_, Self::Row>,
menu: PopupMenu,
window: &mut Window,
cx: &mut Context<PopupMenu>,
) -> PopupMenu {
menu_context.standard_items(menu, window, cx)
}
fn tooltip(&self, cell: &CellTooltipContext<'_, Self::Row>, _cx: &App) -> Option<CellTooltip> {
cell.default_tooltip()
}
fn header_tooltip(
&self,
header: &HeaderTooltipContext<'_, <Self::Row as GridRow>::Column>,
_cx: &App,
) -> Option<CellTooltip> {
header.default_tooltip()
}
fn render_progress(
&self,
progress: &GridProgress,
overlay: &ProgressContext,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
overlay.default_overlay(progress, window, cx)
}
fn render_cell(
&self,
_cell: &CellElementContext<'_, Self::Row>,
_window: &mut Window,
_cx: &mut App,
) -> Option<AnyElement> {
None
}
fn is_editable(&self, _cell: &EditableContext<'_, Self::Row>, _cx: &App) -> bool {
true
}
fn validate_edit(
&self,
_edit: &EditValidateContext<'_, Self::Row>,
_cx: &App,
) -> Result<(), EditError> {
Ok(())
}
fn render_edit_message(
&self,
message: &EditMessageContext<'_>,
window: &mut Window,
cx: &mut App,
) -> Option<AnyElement> {
Some(message.default_popup(window, cx))
}
fn drag_payload(
&self,
_drag: &RowDragContext<'_, Self::Row>,
_cx: &App,
) -> Option<DragPayload> {
Some(DragPayload::new())
}
}
#[derive(Clone, Copy)]
pub(super) struct Palette {
pub(super) foreground: Hsla,
pub(super) row_border: Hsla,
pub(super) stripe: Hsla,
pub(super) selected: Hsla,
pub(super) ring: Hsla,
pub(super) divider: Hsla,
pub(super) pinned: Hsla,
pub(super) pinned_foreground: Hsla,
pub(super) muted: Hsla,
pub(super) danger: Hsla,
pub(super) link: Hsla,
}