917 lines
32 KiB
Zig
917 lines
32 KiB
Zig
//! VirtualAdvancedTable - Widget de lista virtualizada
|
|
//!
|
|
//! Lista escalable que solo carga en memoria los registros visibles + buffer.
|
|
//! Diseñada para trabajar con bases de datos grandes (100k+ registros).
|
|
//!
|
|
//! ## Uso
|
|
//! ```zig
|
|
//! const result = virtualAdvancedTable(ctx, rect, &state, provider, .{
|
|
//! .columns = &columns,
|
|
//! .virtualization_threshold = 500,
|
|
//! });
|
|
//! if (result.selection_changed) { ... }
|
|
//! ```
|
|
|
|
const std = @import("std");
|
|
const Context = @import("../../core/context.zig").Context;
|
|
const Command = @import("../../core/command.zig");
|
|
const Layout = @import("../../core/layout.zig");
|
|
const Style = @import("../../core/style.zig");
|
|
const Input = @import("../../core/input.zig");
|
|
const text_input = @import("../text_input.zig");
|
|
|
|
// Re-exports públicos
|
|
pub const types = @import("types.zig");
|
|
pub const data_provider = @import("data_provider.zig");
|
|
pub const state_mod = @import("state.zig");
|
|
pub const cell_editor = @import("cell_editor.zig");
|
|
|
|
// Tipos principales
|
|
pub const RowData = types.RowData;
|
|
pub const ColumnDef = types.ColumnDef;
|
|
pub const SortDirection = types.SortDirection;
|
|
pub const LoadState = types.LoadState;
|
|
pub const CountInfo = types.CountInfo;
|
|
pub const VirtualAdvancedTableConfig = types.VirtualAdvancedTableConfig;
|
|
pub const FilterBarConfig = types.FilterBarConfig;
|
|
pub const FilterChipDef = types.FilterChipDef;
|
|
pub const ChipSelectMode = types.ChipSelectMode;
|
|
pub const CellId = types.CellId;
|
|
pub const CellGeometry = types.CellGeometry;
|
|
|
|
pub const DataProvider = data_provider.DataProvider;
|
|
pub const CellEditorColors = cell_editor.CellEditorColors;
|
|
pub const CellEditorResult = cell_editor.CellEditorResult;
|
|
pub const drawCellEditor = cell_editor.drawCellEditor;
|
|
pub const VirtualAdvancedTableState = state_mod.VirtualAdvancedTableState;
|
|
|
|
/// Resultado de renderizar el VirtualAdvancedTable
|
|
pub const VirtualAdvancedTableResult = struct {
|
|
/// La selección cambió este frame
|
|
selection_changed: bool = false,
|
|
|
|
/// ID del registro seleccionado
|
|
selected_id: ?i64 = null,
|
|
|
|
/// Hubo doble click en un registro
|
|
double_clicked: bool = false,
|
|
|
|
/// ID del registro donde hubo doble click
|
|
double_click_id: ?i64 = null,
|
|
|
|
/// El usuario solicitó ordenar por una columna
|
|
sort_requested: bool = false,
|
|
sort_column: ?[]const u8 = null,
|
|
sort_direction: SortDirection = .none,
|
|
|
|
/// El filtro de texto cambió
|
|
filter_changed: bool = false,
|
|
|
|
/// Texto del filtro actual
|
|
filter_text: ?[]const u8 = null,
|
|
|
|
/// Un chip/prefiltro cambió
|
|
chip_changed: bool = false,
|
|
|
|
/// Índice del chip que cambió
|
|
chip_index: ?u4 = null,
|
|
|
|
/// El chip está activo después del cambio
|
|
chip_active: bool = false,
|
|
|
|
/// El widget fue clickeado
|
|
clicked: bool = false,
|
|
};
|
|
|
|
// =============================================================================
|
|
// Widget principal
|
|
// =============================================================================
|
|
|
|
/// Renderiza un VirtualAdvancedTable
|
|
pub fn virtualAdvancedTable(
|
|
ctx: *Context,
|
|
list_state: *VirtualAdvancedTableState,
|
|
provider: DataProvider,
|
|
config: VirtualAdvancedTableConfig,
|
|
) VirtualAdvancedTableResult {
|
|
const bounds = ctx.layout.nextRect();
|
|
return virtualAdvancedTableRect(ctx, bounds, list_state, provider, config);
|
|
}
|
|
|
|
/// Renderiza un VirtualAdvancedTable en un rectángulo específico
|
|
pub fn virtualAdvancedTableRect(
|
|
ctx: *Context,
|
|
bounds: Layout.Rect,
|
|
list_state: *VirtualAdvancedTableState,
|
|
provider: DataProvider,
|
|
config: VirtualAdvancedTableConfig,
|
|
) VirtualAdvancedTableResult {
|
|
var result = VirtualAdvancedTableResult{};
|
|
|
|
if (bounds.isEmpty() or config.columns.len == 0) return result;
|
|
|
|
// Reset frame flags
|
|
list_state.resetFrameFlags();
|
|
|
|
// Get colors
|
|
const colors = config.colors orelse VirtualAdvancedTableConfig.Colors{};
|
|
|
|
// Generate unique ID for focus system
|
|
const widget_id: u64 = @intFromPtr(list_state);
|
|
|
|
// Register as focusable
|
|
ctx.registerFocusable(widget_id);
|
|
|
|
// Check mouse interaction
|
|
const mouse = ctx.input.mousePos();
|
|
const hovered = bounds.contains(mouse.x, mouse.y);
|
|
const clicked = hovered and ctx.input.mousePressed(.left);
|
|
|
|
if (clicked) {
|
|
ctx.requestFocus(widget_id);
|
|
result.clicked = true;
|
|
}
|
|
|
|
// Check if we have focus
|
|
const has_focus = ctx.hasFocus(widget_id);
|
|
list_state.has_focus = has_focus;
|
|
|
|
// Calculate total columns width
|
|
var total_columns_width: u32 = 0;
|
|
for (config.columns) |col| {
|
|
total_columns_width += col.width;
|
|
}
|
|
|
|
// Check if horizontal scroll is needed
|
|
const scrollbar_v_w: u32 = 12; // Width of vertical scrollbar
|
|
const available_width = bounds.w -| scrollbar_v_w;
|
|
const needs_h_scroll = total_columns_width > available_width;
|
|
const scrollbar_h_h: u32 = if (needs_h_scroll) 12 else 0;
|
|
|
|
// Calculate max horizontal scroll
|
|
const max_scroll_x: i32 = @max(0, @as(i32, @intCast(total_columns_width)) - @as(i32, @intCast(available_width)));
|
|
|
|
// Clamp current scroll_offset_x
|
|
if (list_state.scroll_offset_x > max_scroll_x) {
|
|
list_state.scroll_offset_x = max_scroll_x;
|
|
}
|
|
|
|
// Calculate FilterBar height
|
|
const filter_bar_h: u32 = if (config.filter_bar) |fb| fb.height else 0;
|
|
|
|
// Calculate dimensions
|
|
const header_h: u32 = config.row_height;
|
|
const footer_h: u32 = if (config.show_count) 16 else 0; // 16px para footer compacto
|
|
const content_h = bounds.h -| filter_bar_h -| header_h -| footer_h -| scrollbar_h_h;
|
|
const visible_rows: usize = @intCast(content_h / config.row_height);
|
|
|
|
// Calculate buffer size and check if refetch needed
|
|
const buffer_size = visible_rows * config.buffer_multiplier;
|
|
const needs_refetch = needsRefetch(list_state, visible_rows, buffer_size);
|
|
|
|
// Fetch window if needed
|
|
if (needs_refetch) {
|
|
if (provider.fetchWindow(list_state.scroll_offset, buffer_size)) |window| {
|
|
list_state.current_window = window;
|
|
list_state.window_start = list_state.scroll_offset;
|
|
} else |_| {
|
|
// Error fetching - keep current window
|
|
}
|
|
}
|
|
|
|
// Update counts from provider
|
|
list_state.total_count = provider.getTotalCount();
|
|
list_state.filtered_count = provider.getFilteredCount();
|
|
|
|
// Begin clipping
|
|
ctx.pushCommand(Command.clip(bounds.x, bounds.y, bounds.w, bounds.h));
|
|
|
|
// Draw FilterBar if configured
|
|
if (config.filter_bar) |fb_config| {
|
|
const filter_bounds = Layout.Rect.init(
|
|
bounds.x,
|
|
bounds.y,
|
|
bounds.w,
|
|
fb_config.height,
|
|
);
|
|
drawFilterBar(ctx, filter_bounds, fb_config, &colors, list_state, &result);
|
|
}
|
|
|
|
// Calculate header Y position (after FilterBar)
|
|
const header_y = bounds.y + @as(i32, @intCast(filter_bar_h));
|
|
|
|
// Draw header (with horizontal scroll offset)
|
|
drawHeaderAt(ctx, bounds, header_y, config, &colors, list_state, &result, list_state.scroll_offset_x);
|
|
|
|
// Draw visible rows
|
|
const content_bounds = Layout.Rect.init(
|
|
bounds.x,
|
|
header_y + @as(i32, @intCast(header_h)),
|
|
bounds.w,
|
|
content_h,
|
|
);
|
|
|
|
// Draw content background first (so empty space isn't black)
|
|
ctx.pushCommand(Command.rect(
|
|
content_bounds.x,
|
|
content_bounds.y,
|
|
content_bounds.w,
|
|
content_bounds.h,
|
|
colors.row_normal,
|
|
));
|
|
|
|
drawRows(ctx, content_bounds, config, &colors, list_state, visible_rows, &result, list_state.scroll_offset_x);
|
|
|
|
// End clipping
|
|
ctx.pushCommand(Command.clipEnd());
|
|
|
|
// Draw footer with count
|
|
if (config.show_count) {
|
|
const footer_bounds = Layout.Rect.init(
|
|
bounds.x,
|
|
bounds.y + @as(i32, @intCast(bounds.h - footer_h)),
|
|
bounds.w,
|
|
footer_h,
|
|
);
|
|
drawFooter(ctx, footer_bounds, &colors, list_state);
|
|
}
|
|
|
|
// Draw vertical scrollbar if needed
|
|
const total_rows = list_state.getDisplayCount().value;
|
|
if (total_rows > visible_rows and config.show_scrollbar) {
|
|
drawScrollbar(ctx, bounds, header_h, footer_h +| scrollbar_h_h, list_state, visible_rows, total_rows, &colors);
|
|
}
|
|
|
|
// Draw horizontal scrollbar if needed
|
|
if (needs_h_scroll and config.show_scrollbar) {
|
|
drawScrollbarH(ctx, bounds, footer_h, scrollbar_h_h, list_state.scroll_offset_x, max_scroll_x, available_width, &colors);
|
|
}
|
|
|
|
// Draw border around the entire list (always visible)
|
|
ctx.pushCommand(Command.rectOutline(bounds.x, bounds.y, bounds.w, bounds.h, colors.border));
|
|
|
|
// Draw focus ring (additional highlight when focused)
|
|
if (has_focus) {
|
|
if (Style.isFancy()) {
|
|
ctx.pushCommand(Command.focusRing(bounds.x, bounds.y, bounds.w, bounds.h, 4));
|
|
} else {
|
|
ctx.pushCommand(Command.rectOutline(
|
|
bounds.x - 1,
|
|
bounds.y - 1,
|
|
bounds.w + 2,
|
|
bounds.h + 2,
|
|
colors.border,
|
|
));
|
|
}
|
|
}
|
|
|
|
// Handle keyboard
|
|
if (has_focus) {
|
|
handleKeyboard(ctx, list_state, provider, visible_rows, total_rows, max_scroll_x, &result);
|
|
}
|
|
|
|
// Handle mouse clicks on rows
|
|
if (clicked and hovered) {
|
|
handleMouseClick(ctx, bounds, filter_bar_h, header_h, config, list_state, &result);
|
|
}
|
|
|
|
// Update result
|
|
result.selection_changed = list_state.selection_changed;
|
|
result.selected_id = list_state.selected_id;
|
|
result.double_clicked = list_state.double_clicked;
|
|
if (list_state.double_clicked) {
|
|
result.double_click_id = list_state.selected_id;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// =============================================================================
|
|
// Helper: Check if refetch needed
|
|
// =============================================================================
|
|
|
|
fn needsRefetch(list_state: *const VirtualAdvancedTableState, visible_rows: usize, buffer_size: usize) bool {
|
|
// First load
|
|
if (list_state.current_window.len == 0) return true;
|
|
|
|
// Check if scroll is outside current window
|
|
const scroll = list_state.scroll_offset;
|
|
const window_end = list_state.window_start + list_state.current_window.len;
|
|
|
|
// Refetch if scroll is near edges of window
|
|
const margin = visible_rows;
|
|
if (scroll < list_state.window_start + margin and list_state.window_start > 0) return true;
|
|
if (scroll + visible_rows + margin > window_end) return true;
|
|
|
|
_ = buffer_size;
|
|
return false;
|
|
}
|
|
|
|
// =============================================================================
|
|
// Draw: FilterBar
|
|
// =============================================================================
|
|
|
|
fn drawFilterBar(
|
|
ctx: *Context,
|
|
bounds: Layout.Rect,
|
|
config: FilterBarConfig,
|
|
colors: *const VirtualAdvancedTableConfig.Colors,
|
|
list_state: *VirtualAdvancedTableState,
|
|
result: *VirtualAdvancedTableResult,
|
|
) void {
|
|
const padding: i32 = 6;
|
|
const chip_h: u32 = 22;
|
|
const chip_padding: i32 = 10;
|
|
const chip_spacing: i32 = 6;
|
|
const chip_radius: u8 = 11; // Radio de esquinas redondeadas (mitad de altura = pill)
|
|
const clear_btn_w: u32 = 22;
|
|
|
|
// Background con gradiente sutil (color base ligeramente más oscuro abajo)
|
|
ctx.pushCommand(Command.rect(
|
|
bounds.x,
|
|
bounds.y,
|
|
bounds.w,
|
|
bounds.h,
|
|
colors.header_background,
|
|
));
|
|
|
|
// Línea sutil inferior para separación
|
|
ctx.pushCommand(Command.rect(
|
|
bounds.x,
|
|
bounds.y + @as(i32, @intCast(bounds.h)) - 1,
|
|
bounds.w,
|
|
1,
|
|
colors.border,
|
|
));
|
|
|
|
var current_x = bounds.x + padding;
|
|
const item_y = bounds.y + @divTrunc(@as(i32, @intCast(bounds.h)) - @as(i32, @intCast(chip_h)), 2);
|
|
const item_h = bounds.h -| @as(u32, @intCast(padding * 2));
|
|
const mouse = ctx.input.mousePos();
|
|
|
|
// =========================================================================
|
|
// Draw Chips (estilo pill/badge redondeado)
|
|
// =========================================================================
|
|
if (config.chips.len > 0) {
|
|
for (config.chips, 0..) |chip, idx| {
|
|
const chip_idx: u4 = @intCast(idx);
|
|
const is_active = list_state.isChipActive(chip_idx);
|
|
|
|
// Calcular ancho del chip basado en el texto
|
|
const label_len = chip.label.len;
|
|
const chip_w: u32 = @intCast(label_len * 7 + chip_padding * 2);
|
|
|
|
const chip_bounds = Layout.Rect.init(
|
|
current_x,
|
|
item_y,
|
|
chip_w,
|
|
chip_h,
|
|
);
|
|
|
|
const chip_hovered = chip_bounds.contains(mouse.x, mouse.y);
|
|
|
|
// Colores con mejor contraste para chips activos vs inactivos
|
|
const chip_bg = if (is_active)
|
|
colors.row_selected // Color primario para activo
|
|
else if (chip_hovered)
|
|
Style.Color.rgb(
|
|
colors.header_background.r -| 15,
|
|
colors.header_background.g -| 15,
|
|
colors.header_background.b -| 15,
|
|
) // Hover sutil
|
|
else
|
|
colors.header_background;
|
|
|
|
const chip_text_color = if (is_active)
|
|
colors.text_selected
|
|
else
|
|
colors.text;
|
|
|
|
const chip_border = if (is_active)
|
|
colors.row_selected // Sin borde visible cuando activo
|
|
else
|
|
colors.border;
|
|
|
|
// Fondo del chip (redondeado tipo pill)
|
|
ctx.pushCommand(Command.roundedRect(
|
|
chip_bounds.x,
|
|
chip_bounds.y,
|
|
chip_bounds.w,
|
|
chip_bounds.h,
|
|
chip_bg,
|
|
chip_radius,
|
|
));
|
|
|
|
// Borde del chip (solo si no activo, para efecto más limpio)
|
|
if (!is_active) {
|
|
ctx.pushCommand(Command.roundedRectOutline(
|
|
chip_bounds.x,
|
|
chip_bounds.y,
|
|
chip_bounds.w,
|
|
chip_bounds.h,
|
|
chip_border,
|
|
chip_radius,
|
|
));
|
|
}
|
|
|
|
// Texto del chip centrado verticalmente
|
|
ctx.pushCommand(Command.text(
|
|
chip_bounds.x + chip_padding,
|
|
chip_bounds.y + 4,
|
|
chip.label,
|
|
chip_text_color,
|
|
));
|
|
|
|
// Handle click
|
|
if (chip_hovered and ctx.input.mousePressed(.left)) {
|
|
list_state.activateChip(chip_idx, config.chip_mode);
|
|
result.chip_changed = true;
|
|
result.chip_index = chip_idx;
|
|
result.chip_active = list_state.isChipActive(chip_idx);
|
|
}
|
|
|
|
current_x += @as(i32, @intCast(chip_w)) + chip_spacing;
|
|
}
|
|
|
|
current_x += padding; // Espacio extra después de chips
|
|
}
|
|
|
|
// =========================================================================
|
|
// Draw Search Input
|
|
// =========================================================================
|
|
if (config.show_search) {
|
|
// Calcular espacio para clear button
|
|
const clear_space: u32 = if (config.show_clear_button) clear_btn_w + @as(u32, @intCast(padding)) else 0;
|
|
|
|
// Search ocupa el resto del espacio
|
|
const search_end = bounds.x + @as(i32, @intCast(bounds.w)) - padding - @as(i32, @intCast(clear_space));
|
|
const search_w: u32 = @intCast(@max(60, search_end - current_x));
|
|
|
|
const search_bounds = Layout.Rect.init(
|
|
current_x,
|
|
item_y,
|
|
search_w,
|
|
item_h,
|
|
);
|
|
|
|
// Create a temporary TextInputState from list_state fields
|
|
var text_state = text_input.TextInputState{
|
|
.buffer = &list_state.filter_buf,
|
|
.len = list_state.filter_len,
|
|
.cursor = list_state.search_cursor,
|
|
.selection_start = list_state.search_selection_start,
|
|
.focused = list_state.search_has_focus,
|
|
};
|
|
|
|
const text_result = text_input.textInputRect(ctx, search_bounds, &text_state, .{
|
|
.placeholder = config.search_placeholder,
|
|
.padding = 3,
|
|
});
|
|
|
|
// Update list_state from text_state
|
|
list_state.filter_len = text_state.len;
|
|
list_state.search_cursor = text_state.cursor;
|
|
list_state.search_selection_start = text_state.selection_start;
|
|
|
|
// Handle focus changes
|
|
if (text_result.clicked) {
|
|
list_state.search_has_focus = true;
|
|
}
|
|
|
|
// Handle text changes
|
|
if (text_result.changed) {
|
|
list_state.filter_text_changed = true;
|
|
result.filter_changed = true;
|
|
result.filter_text = list_state.filter_buf[0..list_state.filter_len];
|
|
}
|
|
|
|
current_x += @as(i32, @intCast(search_w)) + padding;
|
|
}
|
|
|
|
// =========================================================================
|
|
// Draw Clear Button (circular, estilo moderno)
|
|
// =========================================================================
|
|
if (config.show_clear_button and list_state.filter_len > 0) {
|
|
const clear_x = bounds.x + @as(i32, @intCast(bounds.w - clear_btn_w)) - padding;
|
|
const clear_bounds = Layout.Rect.init(
|
|
clear_x,
|
|
item_y,
|
|
clear_btn_w,
|
|
chip_h,
|
|
);
|
|
|
|
const clear_hovered = clear_bounds.contains(mouse.x, mouse.y);
|
|
|
|
// Color rojo sutil para indicar "eliminar"
|
|
const clear_bg = if (clear_hovered)
|
|
Style.Color.rgb(220, 80, 80) // Rojo hover
|
|
else
|
|
Style.Color.rgb(180, 60, 60); // Rojo base
|
|
|
|
const clear_text = Style.Color.rgb(255, 255, 255);
|
|
|
|
// Botón circular (radio = mitad altura)
|
|
ctx.pushCommand(Command.roundedRect(
|
|
clear_bounds.x,
|
|
clear_bounds.y,
|
|
clear_bounds.w,
|
|
clear_bounds.h,
|
|
clear_bg,
|
|
chip_radius,
|
|
));
|
|
|
|
// Draw "✕" centered (o "X" si no hay soporte unicode)
|
|
ctx.pushCommand(Command.text(
|
|
clear_bounds.x + 7,
|
|
clear_bounds.y + 4,
|
|
"X",
|
|
clear_text,
|
|
));
|
|
|
|
// Handle click
|
|
if (clear_hovered and ctx.input.mousePressed(.left)) {
|
|
list_state.clearFilterText();
|
|
list_state.search_cursor = 0;
|
|
list_state.search_selection_start = null;
|
|
result.filter_changed = true;
|
|
result.filter_text = "";
|
|
}
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Draw: Header
|
|
// =============================================================================
|
|
|
|
fn drawHeaderAt(
|
|
ctx: *Context,
|
|
bounds: Layout.Rect,
|
|
header_y: i32,
|
|
config: VirtualAdvancedTableConfig,
|
|
colors: *const VirtualAdvancedTableConfig.Colors,
|
|
list_state: *VirtualAdvancedTableState,
|
|
result: *VirtualAdvancedTableResult,
|
|
scroll_offset_x: i32,
|
|
) void {
|
|
const header_h = config.row_height;
|
|
|
|
// Header background
|
|
ctx.pushCommand(Command.rect(
|
|
bounds.x,
|
|
header_y,
|
|
bounds.w,
|
|
header_h,
|
|
colors.header_background,
|
|
));
|
|
|
|
// Draw column headers (with horizontal scroll offset)
|
|
var x: i32 = bounds.x - scroll_offset_x;
|
|
for (config.columns) |col| {
|
|
// Only draw if column is visible
|
|
const col_end = x + @as(i32, @intCast(col.width));
|
|
if (col_end > bounds.x and x < bounds.x + @as(i32, @intCast(bounds.w))) {
|
|
// Column title
|
|
ctx.pushCommand(Command.text(
|
|
x + 4,
|
|
header_y + 3, // Centrado vertical mejorado
|
|
col.title,
|
|
colors.text,
|
|
));
|
|
|
|
// Sort indicator
|
|
if (list_state.sort_column) |sort_col| {
|
|
if (std.mem.eql(u8, sort_col, col.name)) {
|
|
const indicator = list_state.sort_direction.symbol();
|
|
ctx.pushCommand(Command.text(
|
|
x + @as(i32, @intCast(col.width)) - 20,
|
|
header_y + 3, // Centrado vertical mejorado
|
|
indicator,
|
|
colors.text,
|
|
));
|
|
}
|
|
}
|
|
|
|
// Check click on header for sorting
|
|
if (col.sortable) {
|
|
const header_bounds = Layout.Rect.init(x, header_y, col.width, header_h);
|
|
const mouse = ctx.input.mousePos();
|
|
if (header_bounds.contains(mouse.x, mouse.y) and ctx.input.mousePressed(.left)) {
|
|
list_state.toggleSort(col.name);
|
|
result.sort_requested = true;
|
|
result.sort_column = col.name;
|
|
result.sort_direction = list_state.sort_direction;
|
|
}
|
|
}
|
|
|
|
// Column separator
|
|
ctx.pushCommand(Command.rect(col_end - 1, header_y, 1, header_h, colors.border));
|
|
}
|
|
|
|
x = col_end;
|
|
}
|
|
|
|
// Bottom border
|
|
ctx.pushCommand(Command.rect(
|
|
bounds.x,
|
|
header_y + @as(i32, @intCast(header_h)) - 1,
|
|
bounds.w,
|
|
1,
|
|
colors.border,
|
|
));
|
|
}
|
|
|
|
// =============================================================================
|
|
// Draw: Rows
|
|
// =============================================================================
|
|
|
|
fn drawRows(
|
|
ctx: *Context,
|
|
content_bounds: Layout.Rect,
|
|
config: VirtualAdvancedTableConfig,
|
|
colors: *const VirtualAdvancedTableConfig.Colors,
|
|
list_state: *VirtualAdvancedTableState,
|
|
visible_rows: usize,
|
|
result: *VirtualAdvancedTableResult,
|
|
scroll_offset_x: i32,
|
|
) void {
|
|
_ = result;
|
|
|
|
const row_h = config.row_height;
|
|
|
|
// Calculate offset within the window buffer
|
|
// scroll_offset es la posición global, window_start es donde empieza el buffer
|
|
const window_offset = list_state.scroll_offset -| list_state.window_start;
|
|
|
|
// Draw each visible row
|
|
var row_idx: usize = 0;
|
|
while (row_idx < visible_rows) : (row_idx += 1) {
|
|
const data_idx = window_offset + row_idx;
|
|
if (data_idx >= list_state.current_window.len) break;
|
|
|
|
const row_y = content_bounds.y + @as(i32, @intCast(row_idx * row_h));
|
|
const global_idx = list_state.scroll_offset + row_idx; // Índice global real
|
|
const row = list_state.current_window[data_idx];
|
|
|
|
// Determine row background
|
|
const is_selected = list_state.selected_id != null and row.id == list_state.selected_id.?;
|
|
const is_alternate = global_idx % 2 == 1;
|
|
|
|
const bg_color: Style.Color = if (is_selected)
|
|
if (list_state.has_focus) colors.row_selected else colors.row_selected_unfocus
|
|
else if (is_alternate)
|
|
colors.row_alternate
|
|
else
|
|
colors.row_normal;
|
|
|
|
// Row background
|
|
ctx.pushCommand(Command.rect(
|
|
content_bounds.x,
|
|
row_y,
|
|
content_bounds.w,
|
|
row_h,
|
|
bg_color,
|
|
));
|
|
|
|
// Draw cells (with horizontal scroll offset)
|
|
var x: i32 = content_bounds.x - scroll_offset_x;
|
|
for (config.columns, 0..) |col, col_idx| {
|
|
const col_end = x + @as(i32, @intCast(col.width));
|
|
// Only draw if column is visible
|
|
if (col_end > content_bounds.x and x < content_bounds.x + @as(i32, @intCast(content_bounds.w))) {
|
|
if (col_idx < row.values.len) {
|
|
const text_color = if (is_selected and list_state.has_focus)
|
|
colors.text_selected
|
|
else
|
|
colors.text;
|
|
|
|
ctx.pushCommand(Command.text(
|
|
x + 4,
|
|
row_y + 3, // Centrado vertical mejorado
|
|
row.values[col_idx],
|
|
text_color,
|
|
));
|
|
}
|
|
}
|
|
x = col_end;
|
|
}
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Draw: Footer
|
|
// =============================================================================
|
|
|
|
fn drawFooter(
|
|
ctx: *Context,
|
|
bounds: Layout.Rect,
|
|
colors: *const VirtualAdvancedTableConfig.Colors,
|
|
list_state: *VirtualAdvancedTableState,
|
|
) void {
|
|
// Background
|
|
ctx.pushCommand(Command.rect(
|
|
bounds.x,
|
|
bounds.y,
|
|
bounds.w,
|
|
bounds.h,
|
|
colors.header_background,
|
|
));
|
|
|
|
// Format count (usar buffers temporales solo para strings intermedios)
|
|
var count_buf: [64]u8 = undefined;
|
|
const count_info = list_state.getDisplayCount();
|
|
const count_str = count_info.format(&count_buf);
|
|
|
|
// Find selected position
|
|
var pos_buf: [32]u8 = undefined;
|
|
const pos_str = if (list_state.selected_id != null)
|
|
if (list_state.findSelectedInWindow()) |idx|
|
|
std.fmt.bufPrint(&pos_buf, "{d}", .{list_state.windowToGlobalIndex(idx) + 1}) catch "?"
|
|
else
|
|
"?"
|
|
else
|
|
"-";
|
|
|
|
// Combine: "pos de total" - USAR BUFFER PERSISTENTE del state
|
|
// IMPORTANTE: Los buffers de stack se invalidan al retornar. El render
|
|
// diferido necesita buffers que sobrevivan hasta después del execute().
|
|
const display_str = std.fmt.bufPrint(&list_state.footer_display_buf, "{s} de {s}", .{ pos_str, count_str }) catch "...";
|
|
list_state.footer_display_len = display_str.len;
|
|
|
|
ctx.pushCommand(Command.text(
|
|
bounds.x + 4,
|
|
bounds.y + 2,
|
|
list_state.footer_display_buf[0..list_state.footer_display_len],
|
|
colors.text,
|
|
));
|
|
}
|
|
|
|
// =============================================================================
|
|
// Draw: Scrollbar
|
|
// =============================================================================
|
|
|
|
fn drawScrollbar(
|
|
ctx: *Context,
|
|
bounds: Layout.Rect,
|
|
header_h: u32,
|
|
footer_h: u32,
|
|
list_state: *VirtualAdvancedTableState,
|
|
visible_rows: usize,
|
|
total_rows: usize,
|
|
colors: *const VirtualAdvancedTableConfig.Colors,
|
|
) void {
|
|
const scrollbar_w: u32 = 12;
|
|
const content_h = bounds.h -| header_h -| footer_h;
|
|
|
|
// Scrollbar track
|
|
const track_x = bounds.x + @as(i32, @intCast(bounds.w - scrollbar_w));
|
|
const track_y = bounds.y + @as(i32, @intCast(header_h));
|
|
ctx.pushCommand(Command.rect(track_x, track_y, scrollbar_w, content_h, colors.row_alternate));
|
|
|
|
// Thumb size and position
|
|
const visible_ratio = @as(f32, @floatFromInt(visible_rows)) / @as(f32, @floatFromInt(total_rows));
|
|
const thumb_h = @max(20, @as(u32, @intFromFloat(visible_ratio * @as(f32, @floatFromInt(content_h)))));
|
|
|
|
const scroll_ratio = @as(f32, @floatFromInt(list_state.scroll_offset)) /
|
|
@as(f32, @floatFromInt(@max(1, total_rows - visible_rows)));
|
|
const thumb_y = track_y + @as(i32, @intFromFloat(scroll_ratio * @as(f32, @floatFromInt(content_h - thumb_h))));
|
|
|
|
// Draw thumb
|
|
ctx.pushCommand(Command.rect(track_x + 2, thumb_y, scrollbar_w - 4, thumb_h, colors.border));
|
|
}
|
|
|
|
// =============================================================================
|
|
// Draw: Horizontal Scrollbar
|
|
// =============================================================================
|
|
|
|
fn drawScrollbarH(
|
|
ctx: *Context,
|
|
bounds: Layout.Rect,
|
|
footer_h: u32,
|
|
scrollbar_h: u32,
|
|
scroll_offset_x: i32,
|
|
max_scroll_x: i32,
|
|
available_width: u32,
|
|
colors: *const VirtualAdvancedTableConfig.Colors,
|
|
) void {
|
|
const scrollbar_v_w: u32 = 12; // Width of vertical scrollbar area
|
|
|
|
// Scrollbar track position (at the bottom, above footer)
|
|
const track_x = bounds.x;
|
|
const track_y = bounds.y + @as(i32, @intCast(bounds.h - footer_h - scrollbar_h));
|
|
const track_w = bounds.w -| scrollbar_v_w;
|
|
|
|
// Track background
|
|
ctx.pushCommand(Command.rect(track_x, track_y, track_w, scrollbar_h, colors.row_alternate));
|
|
|
|
// Calculate thumb size and position
|
|
if (max_scroll_x <= 0) return;
|
|
|
|
const total_width = available_width + @as(u32, @intCast(max_scroll_x));
|
|
const visible_ratio = @as(f32, @floatFromInt(available_width)) / @as(f32, @floatFromInt(total_width));
|
|
const thumb_w = @max(20, @as(u32, @intFromFloat(visible_ratio * @as(f32, @floatFromInt(track_w)))));
|
|
|
|
const scroll_ratio = @as(f32, @floatFromInt(scroll_offset_x)) / @as(f32, @floatFromInt(max_scroll_x));
|
|
const thumb_x = track_x + @as(i32, @intFromFloat(scroll_ratio * @as(f32, @floatFromInt(track_w - thumb_w))));
|
|
|
|
// Draw thumb
|
|
ctx.pushCommand(Command.rect(thumb_x, track_y + 2, thumb_w, scrollbar_h - 4, colors.border));
|
|
}
|
|
|
|
// =============================================================================
|
|
// Handle: Keyboard
|
|
// =============================================================================
|
|
|
|
fn handleKeyboard(
|
|
ctx: *Context,
|
|
list_state: *VirtualAdvancedTableState,
|
|
provider: DataProvider,
|
|
visible_rows: usize,
|
|
total_rows: usize,
|
|
max_scroll_x: i32,
|
|
result: *VirtualAdvancedTableResult,
|
|
) void {
|
|
_ = provider;
|
|
_ = result;
|
|
|
|
const h_scroll_step: i32 = 40; // Pixels per arrow key press
|
|
|
|
// Usar navKeyPressed() para soportar key repeat (tecla mantenida pulsada)
|
|
if (ctx.input.navKeyPressed()) |key| {
|
|
switch (key) {
|
|
.up => list_state.moveUp(),
|
|
.down => list_state.moveDown(visible_rows),
|
|
.left => list_state.scrollLeft(h_scroll_step),
|
|
.right => list_state.scrollRight(h_scroll_step, max_scroll_x),
|
|
.page_up => list_state.pageUp(visible_rows),
|
|
.page_down => list_state.pageDown(visible_rows, total_rows),
|
|
.home => {
|
|
list_state.goToStart();
|
|
list_state.goToStartX();
|
|
},
|
|
.end => {
|
|
list_state.goToEnd(visible_rows, total_rows);
|
|
list_state.goToEndX(max_scroll_x);
|
|
},
|
|
else => {},
|
|
}
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Handle: Mouse Click
|
|
// =============================================================================
|
|
|
|
fn handleMouseClick(
|
|
ctx: *Context,
|
|
bounds: Layout.Rect,
|
|
filter_bar_h: u32,
|
|
header_h: u32,
|
|
config: VirtualAdvancedTableConfig,
|
|
list_state: *VirtualAdvancedTableState,
|
|
result: *VirtualAdvancedTableResult,
|
|
) void {
|
|
_ = result;
|
|
|
|
const mouse = ctx.input.mousePos();
|
|
// Content starts after FilterBar + Header
|
|
const content_y = bounds.y + @as(i32, @intCast(filter_bar_h)) + @as(i32, @intCast(header_h));
|
|
|
|
// Check if click is in content area (not header or filter bar)
|
|
if (mouse.y >= content_y) {
|
|
const relative_y = mouse.y - content_y;
|
|
const screen_row = @as(usize, @intCast(relative_y)) / config.row_height;
|
|
|
|
// Convert screen row to buffer index (accounting for scroll)
|
|
const window_offset = list_state.scroll_offset -| list_state.window_start;
|
|
const data_idx = window_offset + screen_row;
|
|
|
|
if (data_idx < list_state.current_window.len) {
|
|
list_state.selectById(list_state.current_window[data_idx].id);
|
|
|
|
// Check for double click
|
|
// TODO: implement double click detection with timing
|
|
}
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Tests
|
|
// =============================================================================
|
|
|
|
test "virtual_advanced_table module imports" {
|
|
_ = types;
|
|
_ = data_provider;
|
|
_ = state_mod;
|
|
_ = RowData;
|
|
_ = ColumnDef;
|
|
_ = DataProvider;
|
|
_ = VirtualAdvancedTableState;
|
|
_ = VirtualAdvancedTableResult;
|
|
}
|
|
|
|
test {
|
|
_ = @import("types.zig");
|
|
_ = @import("data_provider.zig");
|
|
_ = @import("state.zig");
|
|
_ = @import("cell_editor.zig");
|
|
}
|