use gpui_kit::component::input::{InputEvent, InputState};
use gpui_kit::{
App, AppContext as _, Context, Entity, EventEmitter, FocusHandle, Global, KeyBinding,
ScrollHandle, SharedString, Subscription, UniformListScrollHandle, Window, actions,
};
use super::{GridFormat, GridScope, GridSnapshot, model::Model};
mod controls;
mod interaction;
mod render;
actions!(native_grid, [CopySelection]);
struct KeysBound;
impl Global for KeysBound {}
#[derive(Clone)]
pub struct GridExport {
snapshot: GridSnapshot,
format: GridFormat,
scope: GridScope,
}
impl GridExport {
pub fn snapshot(&self) -> &GridSnapshot {
&self.snapshot
}
pub fn format(&self) -> GridFormat {
self.format
}
pub fn scope(&self) -> GridScope {
self.scope
}
}
pub struct NativeGrid {
id: SharedString,
label: SharedString,
model: Model,
filters: Vec<Entity<InputState>>,
subscriptions: Vec<Subscription>,
focus: FocusHandle,
scroll: UniformListScrollHandle,
horizontal: ScrollHandle,
options: [bool; 8],
options_open: bool,
status: SharedString,
revision: u64,
toolbar: Entity<crate::overflow_toolbar::OverflowToolbar>,
}
impl EventEmitter<GridExport> for NativeGrid {}
impl NativeGrid {
pub fn new(
id: impl Into<SharedString>,
label: impl Into<SharedString>,
source: GridSnapshot,
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
bind_keys(cx);
let mut view = Self {
id: id.into(),
label: label.into(),
model: Model::new(source),
filters: Vec::new(),
subscriptions: Vec::new(),
focus: cx.focus_handle().tab_stop(true),
scroll: UniformListScrollHandle::new(),
horizontal: ScrollHandle::new(),
options: [true; controls::OPTION_COUNT],
options_open: false,
status: "".into(),
revision: 0,
toolbar: cx.new(|_| crate::overflow_toolbar::OverflowToolbar::new(Vec::new())),
};
view.build_filters(window, cx);
view
}
pub fn replace(&mut self, source: GridSnapshot, window: &mut Window, cx: &mut Context<Self>) {
self.revision = self.revision.wrapping_add(1);
let columns_changed = self.model.source.columns != source.columns;
self.model.replace(source);
if columns_changed {
self.build_filters(window, cx);
}
self.status = "".into();
cx.notify();
}
fn build_filters(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.subscriptions.clear();
self.filters = self
.model
.source
.columns
.iter()
.enumerate()
.map(|(column, _)| {
let input = cx.new(|cx| {
InputState::new(window, cx)
.placeholder("Contains…")
.default_value(self.model.filters[column].clone())
});
self.subscriptions
.push(cx.subscribe(&input, move |this, input, event, cx| {
if matches!(event, InputEvent::Change) {
this.model.filters[column] = input.read(cx).value().to_string();
this.model.rebuild();
cx.notify();
}
}));
input
})
.collect();
}
fn child_id(&self, suffix: impl std::fmt::Display) -> SharedString {
format!("{}-{suffix}", self.id).into()
}
}
fn bind_keys(cx: &mut App) {
if cx.has_global::<KeysBound>() {
return;
}
cx.set_global(KeysBound);
cx.bind_keys([
KeyBinding::new(
crate::secondary!("c"),
CopySelection,
Some("NativeGrid && !Input"),
),
KeyBinding::new("ctrl-c", CopySelection, Some("NativeGrid && !Input")),
]);
}