11use promkit_core:: { Widget , grapheme:: StyledGraphemes } ;
22
3- pub mod node;
4- use node:: Kind ;
5- #[ path = "tree/tree.rs" ]
6- mod inner;
7- pub use inner:: Tree ;
3+ mod document;
4+ pub use document:: Document ;
85pub mod config;
96pub use config:: Config ;
7+ pub mod treez;
8+ pub use treez:: Row ;
109
1110/// Represents the state of a tree structure within the application.
1211///
@@ -17,31 +16,18 @@ pub use config::Config;
1716/// for child items in the tree.
1817#[ derive( Clone ) ]
1918pub struct State {
20- pub tree : Tree ,
19+ pub document : Document ,
2120 /// Configuration for rendering and behavior.
2221 pub config : Config ,
2322}
2423
2524impl Widget for State {
2625 fn create_graphemes ( & self , _width : u16 , height : u16 ) -> StyledGraphemes {
27- let symbol = |kind : & Kind | -> & str {
28- match kind {
29- Kind :: Folded { .. } => & self . config . folded_symbol ,
30- Kind :: Unfolded { .. } => & self . config . unfolded_symbol ,
31- }
32- } ;
33-
34- let indent = |kind : & Kind | -> usize {
35- match kind {
36- Kind :: Folded { path, .. } | Kind :: Unfolded { path, .. } => {
37- path. len ( ) * self . config . indent
38- }
39- }
40- } ;
41-
42- let id = |kind : & Kind | -> String {
43- match kind {
44- Kind :: Folded { id, .. } | Kind :: Unfolded { id, .. } => id. clone ( ) ,
26+ let symbol = |row : & Row | -> & str {
27+ if row. has_children && !row. collapsed {
28+ & self . config . unfolded_symbol
29+ } else {
30+ & self . config . folded_symbol
4531 }
4632 } ;
4733
@@ -50,29 +36,30 @@ impl Widget for State {
5036 None => height as usize ,
5137 } ;
5238
53- let kinds = self . tree . kinds ( ) ;
54- let lines = kinds
55- . iter ( )
56- . enumerate ( )
57- . filter ( |( i, _) | * i >= self . tree . position ( ) && * i < self . tree . position ( ) + height)
58- . map ( |( i, kind) | {
59- if i == self . tree . position ( ) {
60- StyledGraphemes :: from_str (
61- format ! ( "{}{}{}" , symbol( kind) , " " . repeat( indent( kind) ) , id( kind) , ) ,
62- self . config . active_item_style ,
63- )
64- } else {
65- StyledGraphemes :: from_str (
66- format ! (
67- "{}{}{}" ,
68- " " . repeat( StyledGraphemes :: from( symbol( kind) ) . widths( ) ) ,
69- " " . repeat( indent( kind) ) ,
70- id( kind) ,
71- ) ,
72- self . config . inactive_item_style ,
73- )
74- }
75- } ) ;
39+ let rows = self . document . extract_rows_from_current ( height) ;
40+ let lines = rows. iter ( ) . enumerate ( ) . map ( |( offset, row) | {
41+ if offset == 0 {
42+ StyledGraphemes :: from_str (
43+ format ! (
44+ "{}{}{}" ,
45+ symbol( row) ,
46+ " " . repeat( row. depth * self . config. indent) ,
47+ row. id,
48+ ) ,
49+ self . config . active_item_style ,
50+ )
51+ } else {
52+ StyledGraphemes :: from_str (
53+ format ! (
54+ "{}{}{}" ,
55+ " " . repeat( StyledGraphemes :: from( symbol( row) ) . widths( ) ) ,
56+ " " . repeat( row. depth * self . config. indent) ,
57+ row. id,
58+ ) ,
59+ self . config . inactive_item_style ,
60+ )
61+ }
62+ } ) ;
7663
7764 StyledGraphemes :: from_lines ( lines)
7865 }
0 commit comments