yew_datatable_core/column/header_context.rs
1//! Context provided when rendering a column header.
2//!
3//! Contains all information needed to render a column header,
4//! including sort state, filter capability, and resize state.
5
6use crate::column::column_id::ColumnId;
7use crate::column::sort_direction::DataTableSortDirection;
8
9/// Context provided when rendering a column header.
10///
11/// Contains sort state, filter capability, resize state,
12/// pinning status, and column width information.
13#[derive(Debug, Clone)]
14pub struct DataTableHeaderContext {
15 /// The column ID.
16 pub column_id: ColumnId,
17
18 /// The column index.
19 pub column_index: usize,
20
21 /// Whether the column is currently sorted.
22 pub is_sorted: bool,
23
24 /// The sort direction if sorted.
25 pub sort_direction: Option<DataTableSortDirection>,
26
27 /// The sort index for multi-column sorting.
28 pub sort_index: Option<usize>,
29
30 /// Whether the column can be sorted.
31 pub can_sort: bool,
32
33 /// Whether the column can be filtered.
34 pub can_filter: bool,
35
36 /// Whether the column can be resized.
37 pub can_resize: bool,
38
39 /// Whether the column is pinned.
40 pub is_pinned: bool,
41
42 /// The current column width.
43 pub width: Option<f64>,
44}
45
46impl DataTableHeaderContext {
47 /// Creates a new header context.
48 ///
49 /// # Parameters
50 ///
51 /// - `column_id`: The column identifier.
52 /// - `column_index`: The column index in the current view.
53 ///
54 /// # Returns
55 ///
56 /// - `DataTableHeaderContext`: A new header context with default capabilities enabled.
57 pub fn new(column_id: ColumnId, column_index: usize) -> Self {
58 Self {
59 column_id,
60 column_index,
61 is_sorted: false,
62 sort_direction: None,
63 sort_index: None,
64 can_sort: true,
65 can_filter: true,
66 can_resize: true,
67 is_pinned: false,
68 width: None,
69 }
70 }
71
72 /// Sets the sort state.
73 ///
74 /// # Parameters
75 ///
76 /// - `direction`: The sort direction.
77 /// - `index`: The sort priority index for multi-column sorting.
78 ///
79 /// # Returns
80 ///
81 /// - `Self`: The modified header context.
82 pub fn with_sort(mut self, direction: DataTableSortDirection, index: usize) -> Self {
83 // Mark the column as sorted and set direction and index.
84 self.is_sorted = true;
85 self.sort_direction = Some(direction);
86 self.sort_index = Some(index);
87 self
88 }
89
90 /// Sets whether the column can be sorted.
91 ///
92 /// # Parameters
93 ///
94 /// - `can_sort`: Whether sorting is enabled for this column.
95 ///
96 /// # Returns
97 ///
98 /// - `Self`: The modified header context.
99 pub fn with_can_sort(mut self, can_sort: bool) -> Self {
100 // Update the sortable capability.
101 self.can_sort = can_sort;
102 self
103 }
104
105 /// Sets whether the column can be filtered.
106 ///
107 /// # Parameters
108 ///
109 /// - `can_filter`: Whether filtering is enabled for this column.
110 ///
111 /// # Returns
112 ///
113 /// - `Self`: The modified header context.
114 pub fn with_can_filter(mut self, can_filter: bool) -> Self {
115 // Update the filterable capability.
116 self.can_filter = can_filter;
117 self
118 }
119
120 /// Sets whether the column can be resized.
121 ///
122 /// # Parameters
123 ///
124 /// - `can_resize`: Whether resizing is enabled for this column.
125 ///
126 /// # Returns
127 ///
128 /// - `Self`: The modified header context.
129 pub fn with_can_resize(mut self, can_resize: bool) -> Self {
130 // Update the resizable capability.
131 self.can_resize = can_resize;
132 self
133 }
134
135 /// Sets the pinned state.
136 ///
137 /// # Parameters
138 ///
139 /// - `pinned`: Whether the column is pinned.
140 ///
141 /// # Returns
142 ///
143 /// - `Self`: The modified header context.
144 pub fn with_pinned(mut self, pinned: bool) -> Self {
145 // Update the pinned state.
146 self.is_pinned = pinned;
147 self
148 }
149
150 /// Sets the column width.
151 ///
152 /// # Parameters
153 ///
154 /// - `width`: The column width in pixels.
155 ///
156 /// # Returns
157 ///
158 /// - `Self`: The modified header context.
159 pub fn with_width(mut self, width: f64) -> Self {
160 // Set the column width.
161 self.width = Some(width);
162 self
163 }
164}