-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced_table.cr
More file actions
375 lines (319 loc) · 11 KB
/
advanced_table.cr
File metadata and controls
375 lines (319 loc) · 11 KB
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
require "../../src/uing"
UIng.init
# Column constants
enum Column
Avatar = 0
Name = 1
Age = 2
Department = 3
Salary = 4
Progress = 5
Active = 6
end
COLUMN_NAMES = ["Avatar", "Name", "Age", "Department", "Salary", "Progress", "Active"]
SORTABLE_COLUMNS = [Column::Name, Column::Age, Column::Department, Column::Salary, Column::Active]
# Sample data structure for a more complex table
class Employee
property name : String
property age : Int32
property department : String
property salary : Int32
property active : Bool
property avatar : UIng::Image?
property progress : Int32 # Progress value (0-100, or -1 for indeterminate)
def initialize(@name : String, @age : Int32, @department : String, @salary : Int32, @active : Bool = true, @avatar : UIng::Image? = nil, @progress : Int32 = 0)
end
end
# Create sample avatar images (simple colored rectangles)
def create_avatar_image(r : Float64, g : Float64, b : Float64) : UIng::Image
width = 32
height = 32
image = UIng::Image.new(width, height)
# Create a simple colored rectangle as avatar
pixels = Bytes.new(width * height * 4) # RGBA
(0...height).each do |y|
(0...width).each do |x|
offset = (y * width + x) * 4
pixels[offset] = (r * 255).to_u8 # Red
pixels[offset + 1] = (g * 255).to_u8 # Green
pixels[offset + 2] = (b * 255).to_u8 # Blue
pixels[offset + 3] = 255_u8 # Alpha
end
end
image.append(pixels.to_unsafe, width, height, width * 4)
image
end
# Create a default gray avatar for employees without avatars
DEFAULT_AVATAR = create_avatar_image(0.5, 0.5, 0.5)
# Global employee data (mutable for sorting)
EMPLOYEES = [
Employee.new("Alice Johnson", 28, "Engineering", 75000, true, create_avatar_image(0.8, 0.2, 0.2), 85),
Employee.new("Bob Smith", 35, "Marketing", 65000, true, create_avatar_image(0.2, 0.8, 0.2), 72),
Employee.new("Carol Davis", 42, "Engineering", 85000, true, create_avatar_image(0.2, 0.2, 0.8), 95),
Employee.new("David Wilson", 31, "Sales", 55000, true, create_avatar_image(0.8, 0.8, 0.2), 60),
Employee.new("Eve Brown", 29, "HR", 60000, true, create_avatar_image(0.8, 0.2, 0.8), 45),
Employee.new("Frank Miller", 38, "Engineering", 90000, true, create_avatar_image(0.2, 0.8, 0.8), 88),
Employee.new("Grace Lee", 26, "Marketing", 58000, true, create_avatar_image(0.6, 0.4, 0.2), 30),
Employee.new("Henry Taylor", 45, "Sales", 70000, true, create_avatar_image(0.4, 0.6, 0.8), 78),
Employee.new("Ivy Chen", 33, "Engineering", 80000, true, create_avatar_image(0.8, 0.6, 0.4), 92),
Employee.new("Jack Anderson", 27, "HR", 52000, true, create_avatar_image(0.4, 0.8, 0.6), -1), # Indeterminate progress
] of Employee
# Global sort state
SORT_COLUMN = -1
SORT_ASCENDING = true
main_window = UIng::Window.new("Advanced Table Example", 800, 600)
main_window.margined = true
vbox = UIng::Box.new(:vertical)
vbox.padded = true
main_window.child = vbox
# Control panel
control_panel = UIng::Box.new(:horizontal)
control_panel.padded = true
# Add employee button
add_button = UIng::Button.new("Add Employee")
control_panel.append(add_button, false)
# Delete selected button
delete_button = UIng::Button.new("Delete Selected")
control_panel.append(delete_button, false)
# Toggle active status button
toggle_button = UIng::Button.new("Toggle Selected Active")
control_panel.append(toggle_button, false)
# Selection mode controls
selection_label = UIng::Label.new("Selection Mode:")
control_panel.append(selection_label, false)
single_radio = UIng::RadioButtons.new
single_radio.append("Single")
single_radio.append("Multiple")
single_radio.selected = 0
control_panel.append(single_radio, false)
vbox.append(control_panel, false)
# Status label
status_label = UIng::Label.new("Ready")
vbox.append(status_label, false)
# Create table model handler
model_handler = UIng::Table::Model::Handler.new do
num_columns do
COLUMN_NAMES.size
end
column_type do |column|
case Column.new(column)
when .avatar?
UIng::Table::Value::Type::Image
when .name?, .age?, .department?, .salary?
UIng::Table::Value::Type::String
when .progress?
UIng::Table::Value::Type::Int
when .active?
UIng::Table::Value::Type::Int
else
UIng::Table::Value::Type::String
end
end
num_rows do
EMPLOYEES.size
end
cell_value do |row, column|
next UIng::Table::Value.new("") if row >= EMPLOYEES.size
employee = EMPLOYEES[row]
case Column.new(column)
when .avatar?
avatar = employee.avatar || DEFAULT_AVATAR
UIng::Table::Value.new(avatar)
when .name?
UIng::Table::Value.new(employee.name)
when .age?
UIng::Table::Value.new(employee.age.to_s)
when .department?
UIng::Table::Value.new(employee.department)
when .salary?
UIng::Table::Value.new(employee.salary.to_s)
when .progress?
UIng::Table::Value.new(employee.progress)
when .active?
UIng::Table::Value.new(employee.active ? 1 : 0)
else
UIng::Table::Value.new("")
end
end
set_cell_value do |row, column, table_value|
next if row >= EMPLOYEES.size
case Column.new(column)
when .avatar?, .progress?
# Read-only columns
when .name?
if name = table_value.string
EMPLOYEES[row].name = name
end
when .age?
if age_str = table_value.string
EMPLOYEES[row].age = age_str.to_i? || EMPLOYEES[row].age
end
when .department?
if dept = table_value.string
EMPLOYEES[row].department = dept
end
when .salary?
if salary_str = table_value.string
EMPLOYEES[row].salary = salary_str.to_i? || EMPLOYEES[row].salary
end
when .active?
EMPLOYEES[row].active = table_value.int != 0
end
end
end
# Create table model and table
table_model = UIng::Table::Model.new(model_handler)
table = UIng::Table.new(table_model) do
# Add columns with different types
append_image_column("Avatar", 0) # Image column (read-only)
append_text_column("Name", 1, 1) # Editable text
append_text_column("Age", 2, 2) # Editable number (as text)
append_text_column("Department", 3, 3) # Editable text
append_text_column("Salary", 4, 4) # Editable number (as text)
append_progress_bar_column("Progress", 5) # Progress bar (read-only)
append_checkbox_column("Active", 6, 6) # Editable checkbox
end
# Configure table
table.header_visible = true
table.selection_mode = UIng::Table::Selection::Mode::ZeroOrMany
vbox.append(table, true)
# Table event handlers
table.on_selection_changed do |selection|
count = selection.num_rows
if count == 0
status_label.text = "No selection"
elsif count == 1
row = selection.rows[0]
employee = EMPLOYEES[row]
status_label.text = "Selected: #{employee.name} (#{employee.department})"
else
status_label.text = "Selected #{count} employees"
end
end
table.on_row_clicked do |row|
employee = EMPLOYEES[row]
puts "Clicked: #{employee.name}"
end
table.on_row_double_clicked do |row|
employee = EMPLOYEES[row]
puts "Double-clicked: #{employee.name} - Salary: $#{employee.salary}"
end
table.on_header_clicked do |column|
puts "Header clicked: #{COLUMN_NAMES[column]? || "Unknown"}"
# Skip sorting for non-sortable columns
column_enum = Column.new(column)
next unless SORTABLE_COLUMNS.includes?(column_enum)
# Clear previous sort indicators
(0...7).each do |col|
table.header_set_sort_indicator(col, :none) if col != column
end
# Determine new sort direction
current = table.header_sort_indicator(column)
ascending = case current
when .none?
true
when .ascending?
false
else
true
end
# Sort the data
case column_enum
when .name?
EMPLOYEES.sort! { |a, b| ascending ? a.name <=> b.name : b.name <=> a.name }
when .age?
EMPLOYEES.sort! { |a, b| ascending ? a.age <=> b.age : b.age <=> a.age }
when .department?
EMPLOYEES.sort! { |a, b| ascending ? a.department <=> b.department : b.department <=> a.department }
when .salary?
EMPLOYEES.sort! { |a, b| ascending ? a.salary <=> b.salary : b.salary <=> a.salary }
when .active?
EMPLOYEES.sort! { |a, b| ascending ? (a.active ? 1 : 0) <=> (b.active ? 1 : 0) : (b.active ? 1 : 0) <=> (a.active ? 1 : 0) }
end
# Update sort indicator
new_indicator = ascending ? UIng::Table::SortIndicator::Ascending : UIng::Table::SortIndicator::Descending
table.header_set_sort_indicator(column, new_indicator)
# Refresh the entire table
(0...EMPLOYEES.size).each do |row|
table_model.row_changed(row)
end
status_label.text = "Sorted by #{COLUMN_NAMES[column]} (#{ascending ? "ascending" : "descending"})"
end
# Button event handlers
add_button.on_clicked do
new_employee = Employee.new("New Employee", 25, "Unknown", 50000)
EMPLOYEES << new_employee
table_model.row_inserted(EMPLOYEES.size - 1)
status_label.text = "Added new employee"
end
delete_button.on_clicked do
table.selection do |selection|
if selection.num_rows > 0
# Delete in reverse order to maintain indices
rows_to_delete = [] of Int32
(0...selection.num_rows).each do |i|
rows_to_delete << selection.rows[i]
end
rows_to_delete.sort!.reverse!
rows_to_delete.each do |row|
# Free avatar image before deleting the employee, but not if it's DEFAULT_AVATAR or nil
if avatar = EMPLOYEES[row].avatar
unless avatar.same?(DEFAULT_AVATAR)
avatar.free
end
end
EMPLOYEES.delete_at(row)
table_model.row_deleted(row)
end
status_label.text = "Deleted #{rows_to_delete.size} employee(s)"
else
status_label.text = "No employees selected for deletion"
end
end
end
toggle_button.on_clicked do
table.selection do |selection|
if selection.num_rows > 0
(0...selection.num_rows).each do |i|
row = selection.rows[i]
if row < EMPLOYEES.size
EMPLOYEES[row].active = !EMPLOYEES[row].active
table_model.row_changed(row)
end
end
status_label.text = "Toggled active status for #{selection.num_rows} employee(s)"
else
status_label.text = "No employees selected"
end
end
end
# Selection mode change handler
single_radio.on_selected do
mode = single_radio.selected == 0 ? UIng::Table::Selection::Mode::ZeroOrOne : UIng::Table::Selection::Mode::ZeroOrMany
table.selection_mode = mode
mode_name = single_radio.selected == 0 ? "Single" : "Multiple"
status_label.text = "Selection mode changed to: #{mode_name}"
end
# Window event handlers
main_window.on_closing do
puts "Cleaning up resources..."
# Free all avatar images
EMPLOYEES.each do |employee|
if avatar = employee.avatar
avatar.free
end
end
# Free the default avatar
DEFAULT_AVATAR.free
# FIXME: Free the table model and table
# https://github.com/kojix2/uing/issues/6
vbox.delete(2)
table.destroy
table_model.free
UIng.quit
true
end
# Show window and start main loop
main_window.show
UIng.main
UIng.uninit