egui-material3 0.0.10

Material Design 3 components for egui with comprehensive theming support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
//! Material Design Spreadsheet Component
//!
//! A spreadsheet widget with DataFusion backend for data storage and manipulation.
//! Supports importing/exporting CSV, Excel, Parquet, and Arrow formats.

#[cfg(feature = "spreadsheet")]
use crate::theme::get_global_color;
#[cfg(feature = "spreadsheet")]
use std::path::PathBuf;
#[cfg(feature = "spreadsheet")]
use std::sync::Arc;
#[cfg(feature = "spreadsheet")]
use tokio::sync::Mutex;

#[cfg(feature = "spreadsheet")]
use datafusion::prelude::*;
#[cfg(feature = "spreadsheet")]
use datafusion::arrow::array::{ArrayRef, RecordBatch, StringArray};
#[cfg(feature = "spreadsheet")]
use datafusion::arrow::datatypes::{DataType, Field, Schema};
#[cfg(feature = "spreadsheet")]
use egui::{Id, Response, Sense, TextEdit, Ui, Widget};
#[cfg(feature = "spreadsheet")]
use egui_async::{Bind, StateWithData};
#[cfg(feature = "spreadsheet")]
use std::sync::Arc as StdArc;

// Re-export for convenience
#[cfg(feature = "spreadsheet")]
pub use egui_extras::{Column, TableBuilder};

/// Column definition for spreadsheet
#[cfg(feature = "spreadsheet")]
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ColumnDef {
    pub name: String,
    pub col_type: ColumnType,
    pub width: f32,
}

/// Supported column data types
#[cfg(feature = "spreadsheet")]
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum ColumnType {
    Text,
    Integer,
    Real,
    Boolean,
}

#[cfg(feature = "spreadsheet")]
impl ColumnType {
    fn to_arrow(&self) -> DataType {
        match self {
            ColumnType::Text => DataType::Utf8,
            ColumnType::Integer => DataType::Int64,
            ColumnType::Real => DataType::Float64,
            ColumnType::Boolean => DataType::Boolean,
        }
    }
}

/// A single row of data
#[cfg(feature = "spreadsheet")]
#[derive(Clone, Debug)]
pub struct RowData {
    pub id: usize,
    pub values: Vec<String>,
}

/// File format for import/export
#[cfg(feature = "spreadsheet")]
#[derive(Clone, Debug, PartialEq)]
pub enum FileFormat {
    Csv,
    Excel,
    Parquet,
    Arrow,
}

#[cfg(feature = "spreadsheet")]
impl FileFormat {
    pub fn from_path(path: &std::path::Path) -> Option<Self> {
        path.extension()?.to_str().and_then(|ext| match ext.to_lowercase().as_str() {
            "csv" => Some(FileFormat::Csv),
            "xls" | "xlsx" => Some(FileFormat::Excel),
            "parquet" => Some(FileFormat::Parquet),
            "arrow" => Some(FileFormat::Arrow),
            _ => None,
        })
    }
}

/// DataFusion-backed data model for spreadsheet
#[cfg(feature = "spreadsheet")]
pub struct SpreadsheetDataModel {
    ctx: SessionContext,
    columns: Vec<ColumnDef>,
    data: Vec<Vec<String>>, // In-memory storage for modifications
    row_count: usize,
}

#[cfg(feature = "spreadsheet")]
impl SpreadsheetDataModel {
    /// Create a new in-memory spreadsheet data model
    pub fn new(columns: Vec<ColumnDef>) -> Result<Self, String> {
        let ctx = SessionContext::new();

        let model = Self {
            ctx,
            columns: columns.clone(),
            data: Vec::new(),
            row_count: 0,
        };

        Ok(model)
    }

    fn data_to_record_batch(&self) -> Result<RecordBatch, String> {
        // Build Arrow schema from columns (use column names instead of col0, col1, etc.)
        let mut fields = vec![];
        for col in self.columns.iter() {
            fields.push(Field::new(&col.name, col.col_type.to_arrow(), true));
        }
        let schema = StdArc::new(Schema::new(fields));

        if self.data.is_empty() {
            return RecordBatch::try_new(schema, vec![])
                .map_err(|e| format!("Failed to create empty batch: {}", e));
        }

        // Create data columns (all as strings for now)
        let mut columns: Vec<ArrayRef> = vec![];
        for col_idx in 0..self.columns.len() {
            let values: Vec<String> = self.data.iter()
                .map(|row| row.get(col_idx).cloned().unwrap_or_default())
                .collect();
            columns.push(StdArc::new(StringArray::from(values)));
        }

        RecordBatch::try_new(schema, columns)
            .map_err(|e| format!("Failed to create batch: {}", e))
    }

    /// Insert multiple rows
    pub fn insert_rows(&mut self, rows: Vec<Vec<String>>) -> Result<(), String> {
        for row_values in rows {
            self.insert_row(row_values)?;
        }
        Ok(())
    }

    /// Insert a single row
    pub fn insert_row(&mut self, values: Vec<String>) -> Result<(), String> {
        self.data.push(values);
        self.row_count += 1;
        Ok(())
    }

    /// Query all rows (returns data directly from memory)
    pub fn query_rows(&self) -> Result<Vec<RowData>, String> {
        let mut result = Vec::new();
        for (id, row) in self.data.iter().enumerate() {
            result.push(RowData {
                id,
                values: row.clone(),
            });
        }
        Ok(result)
    }

    /// Update a single cell
    pub fn update_cell(&mut self, row_id: usize, col_idx: usize, value: String) -> Result<(), String> {
        // Validate value against column type
        if col_idx < self.columns.len() {
            let col_type = &self.columns[col_idx].col_type;

            // For numeric types, validate that the value can be parsed
            match col_type {
                ColumnType::Integer => {
                    if !value.is_empty() && value.parse::<i64>().is_err() {
                        return Err(format!("'{}' is not a valid integer", value));
                    }
                }
                ColumnType::Real => {
                    if !value.is_empty() && value.parse::<f64>().is_err() {
                        return Err(format!("'{}' is not a valid number", value));
                    }
                }
                ColumnType::Boolean => {
                    if !value.is_empty() && value.parse::<bool>().is_err() {
                        // Accept common boolean representations
                        let lower = value.to_lowercase();
                        if lower != "true" && lower != "false" && lower != "1" && lower != "0" {
                            return Err(format!("'{}' is not a valid boolean (use true/false or 1/0)", value));
                        }
                    }
                }
                ColumnType::Text => {} // Text accepts anything
            }
        }

        // Update in memory
        if row_id < self.data.len() && col_idx < self.columns.len() {
            self.data[row_id][col_idx] = value;
            Ok(())
        } else {
            Err("Invalid row or column index".to_string())
        }
    }

    /// Delete a row
    pub fn delete_row(&mut self, row_id: usize) -> Result<(), String> {
        if row_id < self.data.len() {
            self.data.remove(row_id);
            Ok(())
        } else {
            Err("Invalid row index".to_string())
        }
    }

    /// Export to CSV
    pub fn export_csv(&self, path: &std::path::Path) -> Result<(), String> {
        use std::fs::File;
        use std::io::Write;

        let rows = self.query_rows().map_err(|e| e.to_string())?;
        let mut file = File::create(path).map_err(|e| e.to_string())?;

        // Write header
        let header: Vec<String> = self.columns.iter().map(|c| c.name.clone()).collect();
        writeln!(file, "{}", header.join(",")).map_err(|e| e.to_string())?;

        // Write rows
        for row in rows {
            writeln!(file, "{}", row.values.join(",")).map_err(|e| e.to_string())?;
        }

        Ok(())
    }

    /// Import from CSV
    pub fn import_csv(&mut self, path: &std::path::Path) -> Result<(), String> {
        use std::fs::File;
        use std::io::BufRead;

        let file = File::open(path).map_err(|e| format!("Cannot open file: {}", e))?;
        let reader = std::io::BufReader::new(file);
        let all_lines: Vec<String> = reader.lines()
            .collect::<Result<Vec<_>, _>>()
            .map_err(|e| format!("Failed to read file: {}", e))?;
        
        if all_lines.is_empty() {
            return Err("CSV file is empty".to_string());
        }
        
        if all_lines.len() < 2 {
            return Err("CSV file has only one line".to_string());
        }

        let first_line = &all_lines[0];
        let second_line = &all_lines[1];
        let last_line = all_lines.last().unwrap();
        
        // Detect delimiter by comparing counts in first, second, and last lines
        let delimiters = [',', ';', '\t'];
        let mut best_delimiter = ',';
        let mut best_score = 0;
        
        for &delim in &delimiters {
            let count1 = first_line.matches(delim).count();
            let count2 = second_line.matches(delim).count();
            let count_last = last_line.matches(delim).count();
            
            // Score based on consistency across lines and total count
            if count1 > 0 && count1 == count2 && count2 == count_last {
                // Perfect consistency
                let score = count1 * 100;
                if score > best_score {
                    best_score = score;
                    best_delimiter = delim;
                }
            } else if count1 > 0 && count2 > 0 {
                // Partial consistency - prefer if counts are similar
                let min_count = count1.min(count2).min(count_last);
                let max_count = count1.max(count2).max(count_last);
                if max_count > 0 && min_count > 0 {
                    let score = (min_count * 50) / max_count;
                    if score > best_score {
                        best_score = score;
                        best_delimiter = delim;
                    }
                }
            }
        }
        
        let delimiter = best_delimiter;
        let delimiter_name = match delimiter {
            ',' => "comma",
            ';' => "semicolon",
            '\t' => "tab",
            _ => "unknown",
        };
        
        let first_values: Vec<&str> = first_line.split(delimiter).collect();
        let second_values: Vec<&str> = second_line.split(delimiter).collect();
        
        let col_count = first_values.len();

        // Determine if first line is a header (improved heuristic)
        // Check multiple conditions:
        // 1. First line values look like column names (short, non-numeric, no special chars)
        // 2. Type difference between first and second line
        // 3. First line has unique values (columns should have unique names)
        let looks_like_header = first_values.iter().all(|v| {
            let trimmed = v.trim();
            // Column names are typically short and don't start with numbers
            trimmed.len() < 50 &&
            !trimmed.is_empty() &&
            trimmed.parse::<f64>().is_err() && // Not purely numeric
            !trimmed.contains(|c: char| c.is_numeric() && trimmed.len() > 20) // Not long with numbers
        });

        let has_type_difference = first_values.iter().zip(second_values.iter()).any(|(v1, v2)| {
            let v1_is_num = v1.trim().parse::<f64>().is_ok();
            let v2_is_num = v2.trim().parse::<f64>().is_ok();
            v1_is_num != v2_is_num
        });

        let has_unique_values = {
            let mut seen = std::collections::HashSet::new();
            first_values.iter().all(|v| seen.insert(v.trim()))
        };

        // First line is header if it looks like header OR has type difference
        let first_line_is_header = looks_like_header || has_type_difference || (has_unique_values && looks_like_header);
        
        // Create new column definitions
        let new_columns: Vec<ColumnDef> = if first_line_is_header {
            first_values.iter().enumerate().map(|(_i, name)| {
                ColumnDef {
                    name: name.trim().to_string(),
                    col_type: ColumnType::Text,
                    width: 100.0,
                }
            }).collect()
        } else {
            (0..col_count).map(|i| {
                ColumnDef {
                    name: format!("column{}", i + 1),
                    col_type: ColumnType::Text,
                    width: 100.0,
                }
            }).collect()
        };
        
        eprintln!("Detected {} columns with {} delimiter", col_count, delimiter_name);
        eprintln!("First line is header: {}", first_line_is_header);
        
        // Recreate table with new columns
        self.columns = new_columns;
        self.data.clear();
        self.row_count = 0;
        
        // Prepare data rows
        let start_idx = if first_line_is_header { 1 } else { 0 };
        let data_lines: Vec<&String> = all_lines.iter()
            .skip(start_idx)
            .filter(|line| !line.trim().is_empty())
            .collect();
        
        // Insert data rows
        for (idx, line) in data_lines.iter().enumerate() {
            let values: Vec<String> = line.split(delimiter).map(|s| s.trim().to_string()).collect();
            
            // Validate column count
            if values.len() != col_count {
                return Err(format!(
                    "CSV row {} has {} columns, but expected {} columns",
                    idx + if first_line_is_header { 2 } else { 1 },
                    values.len(),
                    col_count
                ));
            }
            
            self.insert_row(values)
                .map_err(|e| format!("Failed to insert row {}: {}", idx + 1, e))?;
        }

        eprintln!("Successfully imported {} rows from CSV", data_lines.len());
        Ok(())
    }

    /// Export to Parquet using Arrow
    pub async fn export_parquet(&self, path: &std::path::Path) -> Result<(), String> {
        use datafusion::parquet::arrow::ArrowWriter;
        use std::fs::File;

        let batch = self.data_to_record_batch()?;
        let file = File::create(path)
            .map_err(|e| format!("Failed to create file: {}", e))?;

        let mut writer = ArrowWriter::try_new(file, batch.schema(), None)
            .map_err(|e| format!("Failed to create parquet writer: {}", e))?;

        writer.write(&batch)
            .map_err(|e| format!("Failed to write batch: {}", e))?;

        writer.close()
            .map_err(|e| format!("Failed to close writer: {}", e))?;

        Ok(())
    }

    /// Import from Parquet using DataFusion SQL
    pub async fn import_parquet(&mut self, path: &std::path::Path) -> Result<(), String> {
        // Clear existing data
        self.data.clear();
        self.row_count = 0;

        // Register parquet file with DataFusion
        let table_name = "imported_data";
        self.ctx.register_parquet(
            table_name,
            path.to_str().unwrap(),
            ParquetReadOptions::default(),
        )
        .await
        .map_err(|e| format!("Failed to register parquet: {}", e))?;

        // Query all data using SQL
        let df = self.ctx
            .sql(&format!("SELECT * FROM {}", table_name))
            .await
            .map_err(|e| format!("Failed to query parquet: {}", e))?;

        // Get schema and update columns
        let schema = df.schema();
        let mut new_columns = vec![];
        for field in schema.fields() {
            let col_type = match field.data_type() {
                DataType::Int64 | DataType::Int32 | DataType::Int16 | DataType::Int8 => ColumnType::Integer,
                DataType::Float64 | DataType::Float32 => ColumnType::Real,
                DataType::Boolean => ColumnType::Boolean,
                _ => ColumnType::Text,
            };
            new_columns.push(ColumnDef {
                name: field.name().clone(),
                col_type,
                width: 100.0,
            });
        }
        self.columns = new_columns;

        // Collect batches
        let batches = df.collect()
            .await
            .map_err(|e| format!("Failed to collect batches: {}", e))?;

        if batches.is_empty() {
            return Ok(());
        }

        // Process each batch
        for batch in batches {
            let num_rows = batch.num_rows();

            for row_idx in 0..num_rows {
                let mut row_values = Vec::new();

                // Get all data columns
                for col_idx in 0..batch.num_columns() {
                    let column = batch.column(col_idx);
                    let value = datafusion::arrow::util::display::array_value_to_string(column, row_idx)
                        .map_err(|e| format!("Failed to convert value: {}", e))?;
                    row_values.push(value);
                }

                self.insert_row(row_values)?;
            }
        }

        // Deregister table to clean up
        let _ = self.ctx.deregister_table(table_name);

        Ok(())
    }
}

/// Actions that can be performed on spreadsheet
#[cfg(feature = "spreadsheet")]
#[derive(Debug, Clone)]
pub enum SpreadsheetAction {
    CellEdited { row_id: usize, col_idx: usize, value: String },
    RowAdded,
    RowDeleted(usize),
    DataLoaded(PathBuf),
    DataSaved(PathBuf),
}

/// Material Design Spreadsheet widget
#[cfg(feature = "spreadsheet")]
pub struct MaterialSpreadsheet {
    id: Id,
    pub data_model: Arc<Mutex<SpreadsheetDataModel>>,
    cached_rows: Vec<RowData>,
    editing_cell: Option<(usize, usize)>,
    edit_buffer: String,
    allow_editing: bool,
    allow_selection: bool,
    striped: bool,
    row_height: f32,
    load_bind: Bind<Vec<RowData>, String>,
    save_bind: Bind<(), String>,
    load_processed: bool, // Track if we've processed the load result
}

#[cfg(feature = "spreadsheet")]
impl MaterialSpreadsheet {
    /// Create a new spreadsheet with the given columns
    pub fn new(id: &str, columns: Vec<ColumnDef>) -> Result<Self, String> {
        let data_model = SpreadsheetDataModel::new(columns).map_err(|e| e.to_string())?;

        Ok(Self {
            id: Id::new(id),
            data_model: Arc::new(Mutex::new(data_model)),
            cached_rows: Vec::new(),
            editing_cell: None,
            edit_buffer: String::new(),
            allow_editing: true,
            allow_selection: true,
            striped: true,
            row_height: 36.0,
            load_bind: Bind::new(false),
            save_bind: Bind::new(false),
            load_processed: false,
        })
    }

    /// Initialize spreadsheet with data (sync method for use in constructors)
    /// This is a convenience method that doesn't require an async context
    pub fn init_with_data(&mut self, rows: Vec<Vec<String>>) {
        // Use try_lock in a loop to avoid needing a runtime
        loop {
            if let Ok(mut model) = self.data_model.try_lock() {
                for row in rows {
                    let _ = model.insert_row(row);
                }
                self.cached_rows = model.query_rows().unwrap_or_default();
                break;
            }
            // Brief sleep to avoid busy waiting
            std::thread::sleep(std::time::Duration::from_micros(10));
        }
    }

    /// Set whether cells can be edited
    pub fn allow_editing(mut self, allow: bool) -> Self {
        self.allow_editing = allow;
        self
    }

    /// Set whether rows can be selected
    pub fn allow_selection(mut self, allow: bool) -> Self {
        self.allow_selection = allow;
        self
    }

    /// Set striped rows
    pub fn striped(mut self, striped: bool) -> Self {
        self.striped = striped;
        self
    }

    /// Set whether cells can be edited (mutable setter)
    pub fn set_allow_editing(&mut self, allow: bool) {
        self.allow_editing = allow;
    }

    /// Set whether rows can be selected (mutable setter)
    pub fn set_allow_selection(&mut self, allow: bool) {
        self.allow_selection = allow;
    }

    /// Set striped rows (mutable setter)
    pub fn set_striped(&mut self, striped: bool) {
        self.striped = striped;
    }

    /// Add a new empty row
    pub async fn add_row(&mut self) -> Result<(), String> {
        let mut model = self.data_model.lock().await;
        let col_count = model.columns.len();
        let empty_values = vec![String::new(); col_count];
        model.insert_row(empty_values).map_err(|e| e.to_string())?;
        // Refresh cached rows to show the new row
        self.cached_rows = model.query_rows().map_err(|e| e.to_string())?;
        Ok(())
    }

    /// Delete a row by ID
    pub async fn delete_row(&mut self, row_id: usize) -> Result<(), String> {
        let mut model = self.data_model.lock().await;
        model.delete_row(row_id).map_err(|e| e.to_string())?;
        Ok(())
    }

    /// Refresh cached data from database
    pub async fn refresh_data(&mut self) -> Result<(), String> {
        let model = self.data_model.lock().await;
        self.cached_rows = model.query_rows().map_err(|e| e.to_string())?;
        Ok(())
    }

    /// Load data from file (async)
    pub fn load_from_file(&mut self, path: PathBuf) {
        self.load_processed = false; // Reset flag for new load
        let model = Arc::clone(&self.data_model);
        self.load_bind.refresh(async move {
            let format = FileFormat::from_path(&path).ok_or_else(|| "Unknown file format".to_string())?;

            match format {
                FileFormat::Csv => {
                    let mut locked_model = model.lock().await;
                    locked_model.import_csv(&path)?;
                    locked_model.query_rows()
                }
                FileFormat::Parquet => {
                    // Import parquet asynchronously
                    let mut locked_model = model.lock().await;
                    locked_model.import_parquet(&path).await?;
                    locked_model.query_rows()
                }
                FileFormat::Excel => Err("Excel import not yet implemented".to_string()),
                FileFormat::Arrow => Err("Arrow import not yet implemented".to_string()),
            }
        });
    }

    /// Save data to file (async)
    pub fn save_to_file(&mut self, path: PathBuf) {
        let model = Arc::clone(&self.data_model);
        self.save_bind.refresh(async move {
            let format = FileFormat::from_path(&path).ok_or_else(|| "Unknown file format".to_string())?;

            match format {
                FileFormat::Csv => {
                    let locked_model = model.lock().await;
                    locked_model.export_csv(&path)?;
                    Ok(())
                }
                FileFormat::Parquet => {
                    let locked_model = model.lock().await;
                    locked_model.export_parquet(&path).await?;
                    Ok(())
                }
                FileFormat::Excel => Err("Excel export not yet implemented".to_string()),
                FileFormat::Arrow => Err("Arrow export not yet implemented".to_string()),
            }
        });
    }

    /// Get the column definitions (blocking version for sync context)
    pub fn columns(&self) -> Vec<ColumnDef> {
        // Use try_lock to avoid needing a runtime
        loop {
            if let Ok(model) = self.data_model.try_lock() {
                return model.columns.clone();
            }
            std::thread::sleep(std::time::Duration::from_micros(10));
        }
    }

    /// Get the current rows
    pub fn rows(&self) -> Vec<RowData> {
        self.cached_rows.clone()
    }

    /// Show the spreadsheet UI (alternative to Widget trait)
    pub fn show(&mut self, ui: &mut Ui) -> Response {
        // Register egui-async plugin (requires egui 0.33+)
        ui.ctx().plugin_or_default::<egui_async::EguiAsyncPlugin>();

        // Handle async load state
        match self.load_bind.state() {
            StateWithData::Pending => {
                ui.ctx().request_repaint();
                return ui.label("Loading...").interact(Sense::hover());
            }
            StateWithData::Finished(rows) => {
                // Only update cached_rows once when load completes
                if !self.load_processed {
                    eprintln!("DEBUG: Load finished, updating cached_rows with {} rows", rows.len());
                    self.cached_rows = rows.clone();
                    self.load_processed = true;
                }
            }
            StateWithData::Failed(err) => {
                return ui.label(format!("Load error: {}", err)).interact(Sense::hover());
            }
            StateWithData::Idle => {}
        }

        // Handle async save state
        match self.save_bind.state() {
            StateWithData::Pending => {
                ui.ctx().request_repaint();
                ui.label("Saving...");
            }
            StateWithData::Finished(_) => {
                ui.label("✓ Save completed successfully");
            }
            StateWithData::Failed(err) => {
                ui.colored_label(egui::Color32::RED, format!("Save error: {}", err));
            }
            StateWithData::Idle => {}
        }

        // Get column definitions (using try_lock for sync UI context)
        let columns = loop {
            if let Ok(model) = self.data_model.try_lock() {
                break model.columns.clone();
            }
            std::thread::sleep(std::time::Duration::from_micros(10));
        };

        // Get theme colors
        let on_surface = get_global_color("on-surface");
        let surface_variant = get_global_color("surface-variant");

        // Build table
        let available_height = ui.available_height();

        let mut table = TableBuilder::new(ui)
            .striped(self.striped)
            .resizable(true)
            .cell_layout(egui::Layout::left_to_right(egui::Align::Center))
            .min_scrolled_height(0.0)
            .max_scroll_height(available_height);

        // Add columns
        for col in columns.iter() {
            table = table.column(Column::initial(col.width).at_least(50.0).resizable(true));
        }

        // Clone cached rows for rendering to avoid borrow issues
        let display_rows = self.cached_rows.clone();
        eprintln!("DEBUG: Rendering table with {} cached rows", display_rows.len());
        if !display_rows.is_empty() && display_rows.len() > 4 {
            eprintln!("DEBUG: Row 4 data: {:?}", display_rows[4].values);
        }

        // Use UI memory to store pending cell updates
        let pending_update_id = self.id.with("pending_cell_update");
        
        table
            .header(30.0, |mut header| {
                for col in columns.iter() {
                    header.col(|ui| {
                        // Paint header background color like datatable
                        let rect = ui.max_rect();
                        ui.painter().rect_filled(rect, egui::CornerRadius::ZERO, surface_variant);
                        
                        ui.style_mut().visuals.override_text_color = Some(on_surface);
                        ui.strong(&col.name);
                    });
                }
            })
            .body(|mut body| {
                for row_data in &display_rows {
                    body.row(self.row_height, |mut row| {
                        for (col_idx, value) in row_data.values.iter().enumerate() {
                            row.col(|ui| {
                                let is_editing = self.editing_cell == Some((row_data.id, col_idx));

                                if is_editing {
                                    // Edit mode with TextEdit
                                    eprintln!("DEBUG: Rendering TextEdit - row_id: {}, col_idx: {}, buffer: '{}'", row_data.id, col_idx, self.edit_buffer);
                                    let edit_response = ui.add(
                                        TextEdit::singleline(&mut self.edit_buffer)
                                            .desired_width(f32::INFINITY)
                                    );
                                    eprintln!("DEBUG: TextEdit state - has_focus: {}, lost_focus: {}, gained_focus: {}", 
                                        edit_response.has_focus(), edit_response.lost_focus(), edit_response.gained_focus());

                                    // Handle Enter to save, Escape to cancel, or save on blur
                                    if edit_response.lost_focus() {
                                        let escape_pressed = ui.input(|i| i.key_pressed(egui::Key::Escape));
                                        eprintln!("DEBUG: TextEdit lost focus - escape_pressed: {}", escape_pressed);
                                        
                                        if !escape_pressed {
                                            eprintln!("DEBUG: Storing cell update - row_id: {}, col_idx: {}, value: '{}'", row_data.id, col_idx, self.edit_buffer);
                                            // Store the update in UI memory for processing after rendering
                                            ui.memory_mut(|mem| {
                                                mem.data.insert_temp(pending_update_id, (row_data.id, col_idx, self.edit_buffer.clone()));
                                            });
                                        } else {
                                            eprintln!("DEBUG: Edit cancelled with Escape");
                                        }
                                        // Always exit edit mode when losing focus
                                        self.editing_cell = None;
                                    }

                                    if edit_response.gained_focus() {
                                        edit_response.request_focus();
                                        eprintln!("DEBUG: Requested focus for TextEdit");
                                    }
                                } else {
                                    // View mode with label
                                    let label_response = ui.label(value);

                                    // Single-click to edit (changed from double-click)
                                    if self.allow_editing && label_response.clicked() {
                                        eprintln!("DEBUG: Starting edit mode - row_id: {}, col_idx: {}, current_value: {}", row_data.id, col_idx, value);
                                        self.editing_cell = Some((row_data.id, col_idx));
                                        self.edit_buffer = value.clone();
                                    }
                                }
                            });
                        }
                    });
                }
            });

        // Apply any pending cell update after rendering
        if let Some((row_id, col_idx, new_value)) = ui.memory(|mem| {
            mem.data.get_temp::<(usize, usize, String)>(pending_update_id)
        }) {
            eprintln!("DEBUG: Retrieved cell update - row_id: {}, col_idx: {}, value: {}", row_id, col_idx, new_value);
            
            // Clear the pending update
            ui.memory_mut(|mem| {
                mem.data.remove::<(usize, usize, String)>(pending_update_id);
            });


            // Use try_lock to avoid needing runtime in UI context
            let mut model = loop {
                if let Ok(guard) = self.data_model.try_lock() {
                    break guard;
                }
                std::thread::sleep(std::time::Duration::from_micros(10));
            };
            match model.update_cell(row_id, col_idx, new_value.clone()) {
                Ok(_) => {
                    eprintln!("DEBUG: Cell updated in database successfully");
                    // Refresh cached rows to show the update
                    match model.query_rows() {
                        Ok(rows) => {
                            eprintln!("DEBUG: Refreshed {} rows from database", rows.len());
                            if rows.len() > 4 {
                                eprintln!("DEBUG: After query, row 4 data: {:?}", rows[4].values);
                            }
                            self.cached_rows = rows;
                            // Request repaint so the updated data appears immediately
                            ui.ctx().request_repaint();
                            eprintln!("DEBUG: Requested repaint");
                        }
                        Err(e) => {
                            eprintln!("DEBUG: Failed to query rows: {}", e);
                        }
                    }
                }
                Err(e) => {
                    eprintln!("DEBUG: Failed to update cell: {}", e);
                    
                    // Show error to user - store in temp memory for display
                    ui.memory_mut(|mem| {
                        mem.data.insert_temp(
                            self.id.with("cell_error"),
                            e.clone()
                        );
                    });
                    ui.ctx().request_repaint();
                }
            }
        } else {
            eprintln!("DEBUG: No pending cell update found");
        }

        // Display error message if there's a cell error
        if let Some(error_msg) = ui.memory(|mem| {
            mem.data.get_temp::<String>(self.id.with("cell_error"))
        }) {
            // Clear the error
            ui.memory_mut(|mem| {
                mem.data.remove::<String>(self.id.with("cell_error"));
            });
            
            // Show error message at the top
            ui.ctx().debug_painter().text(
                ui.max_rect().center_top() + egui::vec2(0.0, 10.0),
                egui::Align2::CENTER_TOP,
                &error_msg,
                egui::FontId::proportional(14.0),
                get_global_color("error"),
            );
            
            // Keep showing the error for a bit
            ui.ctx().request_repaint_after(std::time::Duration::from_secs(3));
        }

        ui.interact(ui.max_rect(), self.id, Sense::hover())
    }
}

#[cfg(feature = "spreadsheet")]
impl Widget for MaterialSpreadsheet {
    fn ui(mut self, ui: &mut Ui) -> Response {
        self.show(ui)
    }
}

/// Helper function to create a column definition
#[cfg(feature = "spreadsheet")]
pub fn column(name: impl Into<String>, col_type: ColumnType, width: f32) -> ColumnDef {
    ColumnDef {
        name: name.into(),
        col_type,
        width,
    }
}

/// Helper function to create a text column
#[cfg(feature = "spreadsheet")]
pub fn text_column(name: impl Into<String>, width: f32) -> ColumnDef {
    column(name, ColumnType::Text, width)
}

/// Helper function to create a number column
#[cfg(feature = "spreadsheet")]
pub fn number_column(name: impl Into<String>, width: f32) -> ColumnDef {
    column(name, ColumnType::Real, width)
}

/// Helper function to create an integer column
#[cfg(feature = "spreadsheet")]
pub fn integer_column(name: impl Into<String>, width: f32) -> ColumnDef {
    column(name, ColumnType::Integer, width)
}

#[cfg(test)]
#[cfg(feature = "spreadsheet")]
mod tests {
    use super::*;

    #[test]
    fn test_spreadsheet_init() {
        let columns = vec![
            text_column("Name", 100.0),
            text_column("Value", 100.0),
        ];

        let mut spreadsheet = MaterialSpreadsheet::new("test", columns)
            .expect("Failed to create spreadsheet");

        // Initialize with data
        spreadsheet.init_with_data(vec![
            vec!["Item1".to_string(), "Value1".to_string()],
            vec!["Item2".to_string(), "Value2".to_string()],
        ]);

        // Verify rows
        let rows = spreadsheet.rows();
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0].values[0], "Item1");
        assert_eq!(rows[1].values[1], "Value2");
    }

    #[tokio::test]
    async fn test_data_model_operations() {
        let columns = vec![
            ColumnDef { name: "Name".to_string(), col_type: ColumnType::Text, width: 100.0 },
            ColumnDef { name: "Count".to_string(), col_type: ColumnType::Integer, width: 80.0 },
        ];

        let mut model = SpreadsheetDataModel::new(columns).expect("Failed to create model");

        // Insert data
        model.insert_row(vec!["Test".to_string(), "42".to_string()]).expect("Failed to insert");

        // Query data
        let rows = model.query_rows().expect("Failed to query");
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].values[0], "Test");
        assert_eq!(rows[0].values[1], "42");

        // Update cell
        model.update_cell(0, 0, "Updated".to_string()).expect("Failed to update");
        let rows = model.query_rows().expect("Failed to query");
        assert_eq!(rows[0].values[0], "Updated");
    }

    #[test]
    fn test_csv_import_export() {
        use std::path::Path;

        let columns = vec![
            text_column("Name", 100.0),
            text_column("Value", 100.0),
        ];

        let mut model = SpreadsheetDataModel::new(columns).expect("Failed to create model");

        // Add some data
        model.insert_row(vec!["Item1".to_string(), "Value1".to_string()]).expect("Failed to insert");
        model.insert_row(vec!["Item2".to_string(), "Value2".to_string()]).expect("Failed to insert");

        // Export to CSV
        let export_path = Path::new("/tmp/test_export.csv");
        model.export_csv(export_path).expect("Failed to export CSV");

        // Create new model and import
        let columns2 = vec![text_column("Col1", 100.0), text_column("Col2", 100.0)];
        let mut model2 = SpreadsheetDataModel::new(columns2).expect("Failed to create model");
        model2.import_csv(export_path).expect("Failed to import CSV");

        // Verify data
        let rows = model2.query_rows().expect("Failed to query");
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0].values[0], "Item1");
        assert_eq!(rows[1].values[1], "Value2");
    }

    #[tokio::test]
    async fn test_parquet_import_export() {
        use std::path::Path;

        let columns = vec![
            text_column("Product", 100.0),
            text_column("Price", 100.0),
        ];

        let mut model = SpreadsheetDataModel::new(columns).expect("Failed to create model");

        // Add some data
        model.insert_row(vec!["Laptop".to_string(), "999.99".to_string()]).expect("Failed to insert");
        model.insert_row(vec!["Mouse".to_string(), "29.99".to_string()]).expect("Failed to insert");

        // Export to Parquet
        let export_path = Path::new("/tmp/test_export.parquet");
        model.export_parquet(export_path).await.expect("Failed to export Parquet");

        // Create new model and import using DataFusion SQL
        let columns2 = vec![text_column("Col1", 100.0)];
        let mut model2 = SpreadsheetDataModel::new(columns2).expect("Failed to create model");
        model2.import_parquet(export_path).await.expect("Failed to import Parquet");

        // Verify data
        let rows = model2.query_rows().expect("Failed to query");
        assert_eq!(rows.len(), 2, "Expected 2 rows");
        assert_eq!(rows[0].values[0], "Laptop");
        assert_eq!(rows[1].values[1], "29.99");

        // Verify columns were updated from parquet schema
        assert_eq!(model2.columns.len(), 2, "Should have 2 columns from parquet file");
        assert_eq!(model2.columns[0].name, "Product");
        assert_eq!(model2.columns[1].name, "Price");
    }
}