Skip to main content

GridValue

Trait GridValue 

Source
pub trait GridValue:
    Any
    + Debug
    + Send
    + Sync {
    // Required methods
    fn cmp_value(&self, other: &dyn GridValue) -> Ordering;
    fn write_text(&self, out: &mut String);

    // Provided methods
    fn export_value(&self) -> Option<CellValue<'_>> { ... }
    fn to_owned_value(&self) -> OwnedValue { ... }
}
Expand description

A cell value of the application’s own type (CellValue::Custom): a severity that sorts by rank, a tenor, a price in ticks, a time in the local zone. The type decides its order, its text and what an export writes; the row lends it, so a cell does not allocate.

Called on the UI thread in prepaint for visible cells, and in the background for every row a rebuild sorts or filters: cmp_value and write_text must not allocate (beyond out growing) or lock.

use std::{any::TypeId, cmp::Ordering};

use gpui_kit_grid::{CellValue, GridValue};

/// Sorts by rank, worst first; shows its name.
#[derive(Clone, Copy, Debug)]
enum Severity {
    Critical,
    Warning,
    Info,
}

impl GridValue for Severity {
    fn cmp_value(&self, other: &dyn GridValue) -> Ordering {
        match other.downcast_ref::<Severity>() {
            Some(other) => (*self as u8).cmp(&(*other as u8)),
            // A value of another type: by type, so the order stays total.
            None => TypeId::of::<Self>().cmp(&other.as_any().type_id()),
        }
    }

    fn write_text(&self, out: &mut String) {
        out.push_str(match self {
            Self::Critical => "Critical",
            Self::Warning => "Warning",
            Self::Info => "Info",
        });
    }
}

let (critical, info) = (Severity::Critical, Severity::Info);
let (critical, info) = (CellValue::Custom(&critical), CellValue::Custom(&info));
assert!(critical.cmp_total(&info).is_lt());
// Custom values sort after text.
assert!(CellValue::Text("Critical").cmp_total(&critical).is_lt());

Required Methods§

Source

fn cmp_value(&self, other: &dyn GridValue) -> Ordering

The order of this value against other, which may be of another type: downcast_ref tells. Values of two types must order by type, the same way every time (for example by the TypeId of as_any), so the order stays total: a sort relies on it. Equal also makes two cells ==, which is how GridRow::changed_columns sees a change.

Source

fn write_text(&self, out: &mut String)

Appends the text shown in the cell, which the quick filter searches and a displayed copy writes. Appends with push_str or write!: out may already hold text, which must stay.

Provided Methods§

Source

fn export_value(&self) -> Option<CellValue<'_>>

What a raw copy (RawCells) and a file export write for the cell; None (the default) writes the text. A CellValue::Custom here is written as its text.

Source

fn to_owned_value(&self) -> OwnedValue

The owned form of the value: a group key, a Min or Max aggregate, an edited value. The default is OwnedValue::Text of GridValue::write_text: a group caption shows the text, and an order by the aggregate (AggregateOrder) follows the text.

Return OwnedValue::custom of a copy to keep the value itself: its order, its text, and its data for a caption hook, which downcasts GroupCaptionContext::value. Groups follow the values’ own order either way: the rows are sorted by the borrowed values before the groups are found.

Implementations§

Source§

impl dyn GridValue

Source

pub fn as_any(&self) -> &dyn Any

The value as Any, to downcast it.

Source

pub fn downcast_ref<T: GridValue>(&self) -> Option<&T>

The value as T, if it is one.

Implementors§