1use std::cell::RefCell;
29use std::collections::HashMap;
30use std::f32;
31use std::rc::Rc;
32
33use crate::{
34 round_layout_value, Children, ControlContext, ControlEvent, ControlObject, DrawingContext,
35 EventContext, Point, Rect, Size, Style, StyledControl, ViewContext,
36};
37use drawing::primitive::Primitive;
38use typed_builder::TypedBuilder;
39
40#[derive(Debug, Copy, Clone, PartialEq)]
45pub enum Length {
46 Auto,
48
49 Exact(f32),
51
52 Fill(f32),
54}
55
56struct DefinitionBase {
61 pub user_size: Length,
62 pub size_type: Length,
63 pub user_max_size: f32,
65
66 pub min_size: f32,
68
69 pub measure_size: f32,
71
72 pub size_cache: f32,
74
75 pub final_offset: f32,
77}
78
79impl DefinitionBase {
80 pub fn new(
81 user_size: Length,
82 user_min_size: f32,
83 user_max_size: f32,
84 treat_fill_as_auto: bool,
85 ) -> DefinitionBase {
86 let mut user_min_size = user_min_size;
87 let user_size_value;
88 let size_type = match user_size {
89 Length::Exact(v) => {
90 user_size_value = v;
91 user_min_size = user_min_size.max(user_size_value.min(user_max_size));
92 Length::Exact(v)
93 }
94 Length::Auto => {
95 user_size_value = f32::INFINITY;
96 Length::Auto
97 }
98 Length::Fill(v) => {
99 user_size_value = f32::INFINITY;
100 if treat_fill_as_auto {
101 Length::Auto
102 } else {
103 Length::Fill(v)
104 }
105 }
106 };
107
108 DefinitionBase {
109 user_size: user_size,
110 size_type: size_type,
111 user_max_size: user_max_size,
113 min_size: user_min_size,
114 measure_size: user_min_size.max(user_size_value.min(user_max_size)),
115 size_cache: 0.0f32,
116 final_offset: 0.0f32,
117 }
118 }
119
120 pub fn update_min_size(&mut self, min_size: f32) {
121 self.min_size = self.min_size.max(min_size)
122 }
123
124 pub fn get_preferred_size(&self) -> f32 {
125 if let Length::Auto = self.user_size {
127 self.min_size
128 } else {
129 self.min_size.max(self.measure_size)
130 }
131 }
132
133 pub fn get_min_size_for_arrange(&self) -> f32 {
134 self.min_size
136 }
137
138 pub fn is_shared(&self) -> bool {
139 false
141 }
142}
143
144struct CellCache {
149 pub child_index: usize,
150 pub column_index: usize,
151 pub row_index: usize,
152 pub column_span: usize,
153 pub row_span: usize,
154 pub is_fill_u: bool,
155 pub is_auto_u: bool,
156 pub is_fill_v: bool,
157 pub is_auto_v: bool,
158}
159
160pub struct Row;
165impl typemap::Key for Row {
166 type Value = i32;
167}
168
169pub struct RowSpan;
170impl typemap::Key for RowSpan {
171 type Value = i32;
172}
173
174pub struct Column;
175impl typemap::Key for Column {
176 type Value = i32;
177}
178
179pub struct ColumnSpan;
180impl typemap::Key for ColumnSpan {
181 type Value = i32;
182}
183
184#[derive(TypedBuilder)]
189pub struct Grid {
190 #[builder(default = 0)]
191 pub rows: i32,
192
193 #[builder(default = 0)]
194 pub columns: i32,
195
196 #[builder(default = Length::Fill(1.0f32))]
197 pub default_width: Length,
198
199 #[builder(default = Length::Fill(1.0f32))]
200 pub default_height: Length,
201
202 #[builder(default = Vec::new())]
203 pub widths: Vec<(i32, Length)>,
204
205 #[builder(default = Vec::new())]
206 pub heights: Vec<(i32, Length)>,
207
208 #[builder(default = 0.0f32)]
209 pub default_min_width: f32,
210
211 #[builder(default = 0.0f32)]
212 pub default_min_height: f32,
213
214 #[builder(default = f32::INFINITY)]
215 pub default_max_width: f32,
216
217 #[builder(default = f32::INFINITY)]
218 pub default_max_height: f32,
219
220 #[builder(default = Vec::new())]
221 pub min_widths: Vec<(i32, f32)>,
222
223 #[builder(default = Vec::new())]
224 pub min_heights: Vec<(i32, f32)>,
225
226 #[builder(default = Vec::new())]
227 pub max_widths: Vec<(i32, f32)>,
228
229 #[builder(default = Vec::new())]
230 pub max_heights: Vec<(i32, f32)>,
231}
232
233impl Grid {
234 pub fn to_view(
235 self,
236 style: Option<Box<dyn Style<Self>>>,
237 context: ViewContext,
238 ) -> Rc<RefCell<dyn ControlObject>> {
239 StyledControl::new(
240 self,
241 style.unwrap_or_else(|| {
242 Box::new(DefaultGridStyle::new(
243 DefaultGridStyleParams::builder().build(),
244 ))
245 }),
246 context,
247 )
248 }
249}
250
251#[derive(TypedBuilder)]
256pub struct DefaultGridStyleParams {}
257
258pub struct DefaultGridStyle {
259 definitions_u: Vec<DefinitionBase>,
260 definitions_v: Vec<DefinitionBase>,
261 cell_group_1: Vec<CellCache>,
262 cell_group_2: Vec<CellCache>,
263 cell_group_3: Vec<CellCache>,
264 cell_group_4: Vec<CellCache>,
265 has_fill_cells_u: bool,
266 has_fill_cells_v: bool,
267 has_group_3_cells_in_auto_rows: bool,
268}
269
270impl DefaultGridStyle {
271 pub fn new(_params: DefaultGridStyleParams) -> Self {
272 DefaultGridStyle {
273 definitions_u: Vec::new(),
274 definitions_v: Vec::new(),
275 cell_group_1: Vec::new(),
276 cell_group_2: Vec::new(),
277 cell_group_3: Vec::new(),
278 cell_group_4: Vec::new(),
279 has_fill_cells_u: false,
280 has_fill_cells_v: false,
281 has_group_3_cells_in_auto_rows: false,
282 }
283 }
284
285 fn decide_number_of_rows_and_columns(data: &Grid, children: &Children) -> (usize, usize) {
286 let mut max_row_from_attached = -1;
287 let mut max_column_from_attached = -1;
288 for child in children.into_iter() {
289 let child = child.borrow();
290 let map = child.get_context().get_attached_values();
291
292 let max_row = if let Some(row) = map.get::<Row>() {
293 if let Some(row_span) = map.get::<RowSpan>() {
294 row + row_span - 1
295 } else {
296 *row
297 }
298 } else {
299 -1
300 };
301 max_row_from_attached = max_row_from_attached.max(max_row);
302
303 let max_column = if let Some(column) = map.get::<Column>() {
304 if let Some(column_span) = map.get::<ColumnSpan>() {
305 column + column_span - 1
306 } else {
307 *column
308 }
309 } else {
310 -1
311 };
312 max_column_from_attached = max_column_from_attached.max(max_column);
313 }
314
315 let is_horizontal_flow = data.columns > 0;
316
317 let number_of_columns;
318 let number_of_rows;
319 if max_row_from_attached >= 0 || max_column_from_attached >= 0 {
320 number_of_rows = data.rows.max(max_row_from_attached + 1).max(0);
321 number_of_columns = data.columns.max(max_column_from_attached + 1).max(0);
322 } else {
323 if is_horizontal_flow {
324 number_of_columns = data.columns;
325 number_of_rows = if data.rows > 0 {
326 data.rows
327 } else {
328 (children.len() as i32 - 1) / number_of_columns + 1
329 };
330 } else if data.rows > 0 {
331 number_of_rows = data.rows;
332 number_of_columns = if data.columns > 0 {
333 data.columns
334 } else {
335 (children.len() as i32 - 1) / number_of_rows + 1
336 }
337 } else {
338 number_of_columns = 0;
339 number_of_rows = 0;
340 }
341 }
342
343 (number_of_rows as usize, number_of_columns as usize)
344 }
345
346 fn prepare_definitions(
347 &mut self,
348 data: &Grid,
349 _children: &Children,
350 number_of_rows: usize,
351 number_of_columns: usize,
352 size_to_content_u: bool,
353 size_to_content_v: bool,
354 ) {
355 let mut widths = Vec::with_capacity(number_of_columns);
356 let mut min_widths = Vec::with_capacity(number_of_columns);
357 let mut max_widths = Vec::with_capacity(number_of_columns);
358 let mut heights = Vec::with_capacity(number_of_rows);
359 let mut min_heights = Vec::with_capacity(number_of_rows);
360 let mut max_heights = Vec::with_capacity(number_of_rows);
361 for _ in 0..number_of_columns {
362 widths.push(data.default_width);
363 min_widths.push(data.default_min_width);
364 max_widths.push(data.default_max_width);
365 }
366 for _ in 0..number_of_rows {
367 heights.push(data.default_height);
368 min_heights.push(data.default_min_height);
369 max_heights.push(data.default_max_height);
370 }
371 for (column, width) in &data.widths {
372 if *column >= 0 && *column <= widths.len() as i32 {
373 widths[*column as usize] = *width;
374 }
375 }
376 for (column, min_width) in &data.min_widths {
377 if *column >= 0 && *column <= min_widths.len() as i32 {
378 min_widths[*column as usize] = *min_width;
379 }
380 }
381 for (column, max_width) in &data.max_widths {
382 if *column >= 0 && *column <= max_widths.len() as i32 {
383 max_widths[*column as usize] = *max_width;
384 }
385 }
386 for (row, height) in &data.heights {
387 if *row >= 0 && *row <= heights.len() as i32 {
388 heights[*row as usize] = *height;
389 }
390 }
391 for (row, min_height) in &data.min_heights {
392 if *row >= 0 && *row <= min_heights.len() as i32 {
393 min_heights[*row as usize] = *min_height;
394 }
395 }
396 for (row, max_height) in &data.max_heights {
397 if *row >= 0 && *row <= max_heights.len() as i32 {
398 max_heights[*row as usize] = *max_height;
399 }
400 }
401
402 self.definitions_u = Vec::new();
403 for i in 0..number_of_columns {
404 let definition =
405 DefinitionBase::new(widths[i], min_widths[i], max_widths[i], size_to_content_u);
406 self.definitions_u.push(definition);
407 }
408
409 self.definitions_v = Vec::new();
410 for i in 0..number_of_rows {
411 let definition = DefinitionBase::new(
412 heights[i],
413 min_heights[i],
414 max_heights[i],
415 size_to_content_v,
416 );
417 self.definitions_v.push(definition);
418 }
419 }
420
421 fn prepare_cell_cache(&mut self, data: &Grid, children: &Children) {
422 self.has_fill_cells_u = false;
423 self.has_fill_cells_v = false;
424 self.has_group_3_cells_in_auto_rows = false;
425
426 let mut child_index = 0;
427 let mut column_index = 0;
428 let mut row_index = 0;
429
430 self.cell_group_1 = Vec::new();
431 self.cell_group_2 = Vec::new();
432 self.cell_group_3 = Vec::new();
433 self.cell_group_4 = Vec::new();
434
435 for child in children.into_iter() {
436 let mut column_span = 1;
437 let mut row_span = 1;
438
439 let child = child.borrow();
440 let map = child.get_context().get_attached_values();
441 if let Some(row) = map.get::<Row>() {
442 row_index = *row;
443 }
444 if let Some(column) = map.get::<Column>() {
445 column_index = *column;
446 }
447 if let Some(rspan) = map.get::<RowSpan>() {
448 row_span = *rspan;
449 }
450 if let Some(cspan) = map.get::<ColumnSpan>() {
451 column_span = *cspan;
452 }
453
454 if column_index >= 0
455 && column_index < self.definitions_u.len() as i32
456 && row_index >= 0
457 && row_index < self.definitions_v.len() as i32
458 && column_span >= 1
459 && row_span >= 1
460 && column_index + column_span - 1 < self.definitions_u.len() as i32
461 && row_index + row_span - 1 < self.definitions_v.len() as i32
462 {
463 let mut is_fill_u = false;
464 let mut is_auto_u = false;
465 let mut is_fill_v = false;
466 let mut is_auto_v = false;
467
468 for i in column_index..column_index + column_span {
469 match self.definitions_u[i as usize].user_size {
470 Length::Fill(_) => is_fill_u = true,
471 Length::Auto => is_auto_u = true,
472 _ => (),
473 }
474 }
475
476 for i in row_index..row_index + row_span {
477 match self.definitions_v[i as usize].user_size {
478 Length::Fill(_) => is_fill_v = true,
479 Length::Auto => is_auto_v = true,
480 _ => (),
481 }
482 }
483
484 self.has_fill_cells_u |= is_fill_u;
485 self.has_fill_cells_v |= is_fill_v;
486
487 let cell_cache = CellCache {
488 child_index: child_index,
489 column_index: column_index as usize,
490 row_index: row_index as usize,
491 column_span: column_span as usize,
492 row_span: row_span as usize,
493 is_fill_u: is_fill_u,
494 is_auto_u: is_auto_u,
495 is_fill_v: is_fill_v,
496 is_auto_v: is_auto_v,
497 };
498
499 if !is_fill_v {
500 if !is_fill_u {
501 self.cell_group_1.push(cell_cache);
502 } else {
503 self.cell_group_3.push(cell_cache);
504 self.has_group_3_cells_in_auto_rows |= is_auto_v;
505 }
506 } else {
507 if is_auto_u && !is_fill_u {
508 self.cell_group_2.push(cell_cache);
509 } else {
510 self.cell_group_4.push(cell_cache);
511 }
512 }
513 }
514
515 child_index += 1;
516
517 let is_horizontal_flow = data.columns > 0;
518 if is_horizontal_flow {
519 column_index += 1;
520 if column_index >= self.definitions_u.len() as i32 {
521 column_index = 0;
522 row_index += 1;
523 }
524 } else {
525 row_index += 1;
526 if row_index >= self.definitions_v.len() as i32 {
527 row_index = 0;
528 column_index += 1;
529 }
530 }
531 }
532 }
533
534 fn measure_cells_group(
535 drawing_context: &mut dyn DrawingContext,
536 definitions_u: &mut Vec<DefinitionBase>,
537 definitions_v: &mut Vec<DefinitionBase>,
538 cells: &Vec<CellCache>,
539 children: &Children,
540 ignore_desired_size_u: bool,
541 force_infinity_v: bool,
542 ) -> bool {
543 let mut has_desired_size_u_changed = false;
544
545 let mut span_store = HashMap::new();
546 let ignore_desired_size_v = force_infinity_v;
547
548 for cell in cells {
549 let child = children.get(cell.child_index).unwrap();
550
551 let old_width = child.borrow().get_rect().width;
552 Self::measure_cell(
553 drawing_context,
554 &cell,
555 &child,
556 definitions_u,
557 definitions_v,
558 force_infinity_v,
559 );
560 let new_rect = child.borrow().get_rect();
561 let new_width = new_rect.width;
562 let new_height = new_rect.height;
563 has_desired_size_u_changed |=
564 new_width != old_width && (new_width - old_width).abs() > 0.0000015f32;
565
566 if !ignore_desired_size_u {
567 if cell.column_span == 1 {
568 let user_max_size = definitions_u[cell.column_index].user_max_size;
569 definitions_u[cell.column_index].update_min_size(new_width.min(user_max_size));
570 } else {
571 let key = (cell.column_index, cell.column_span, true);
572 span_store
573 .entry(key)
574 .and_modify(|v| {
575 if new_width > *v {
576 *v = new_width
577 }
578 })
579 .or_insert(new_width);
580 }
581 }
582
583 if !ignore_desired_size_v {
584 if cell.row_span == 1 {
585 let user_max_size = definitions_v[cell.row_index].user_max_size;
586 definitions_v[cell.row_index].update_min_size(new_height.min(user_max_size));
587 } else {
588 let key = (cell.row_index, cell.row_span, false);
589 span_store
590 .entry(key)
591 .and_modify(|v| {
592 if new_height > *v {
593 *v = new_height
594 }
595 })
596 .or_insert(new_height);
597 }
598 }
599 }
600
601 for (key, requested_size) in span_store.iter() {
602 Self::ensure_min_size_in_definition_range(
603 if key.2 { definitions_u } else { definitions_v },
604 key.0,
605 key.1,
606 *requested_size,
607 );
608 }
609
610 has_desired_size_u_changed
611 }
612
613 fn measure_cell(
614 drawing_context: &mut dyn DrawingContext,
615 cell: &CellCache,
616 child: &Rc<RefCell<dyn ControlObject>>,
617 definitions_u: &mut Vec<DefinitionBase>,
618 definitions_v: &mut Vec<DefinitionBase>,
619 force_infinity_v: bool,
620 ) {
621 let cell_measure_width;
622 let cell_measure_height;
623
624 if cell.is_auto_u && !cell.is_fill_u {
625 cell_measure_width = f32::INFINITY;
626 } else {
627 cell_measure_width = Self::get_measure_size_for_range(
628 definitions_u,
629 cell.column_index,
630 cell.column_span,
631 );
632 }
633
634 if force_infinity_v {
635 cell_measure_height = f32::INFINITY;
636 } else if cell.is_auto_v && !cell.is_fill_v {
637 cell_measure_height = f32::INFINITY;
638 } else {
639 cell_measure_height =
640 Self::get_measure_size_for_range(definitions_v, cell.row_index, cell.row_span);
641 }
642
643 child.borrow_mut().measure(
644 drawing_context,
645 Size::new(cell_measure_width, cell_measure_height),
646 );
647 }
648
649 fn get_measure_size_for_range(
650 definitions: &mut Vec<DefinitionBase>,
651 start: usize,
652 count: usize,
653 ) -> f32 {
654 let mut measure_size = 0.0f32;
655 for i in start..start + count {
656 measure_size += if let Length::Auto = definitions[i].size_type {
657 definitions[i].min_size
658 } else {
659 definitions[i].measure_size
660 };
661 }
662 measure_size
663 }
664
665 fn ensure_min_size_in_definition_range(
669 definitions: &mut Vec<DefinitionBase>,
670 start: usize,
671 count: usize,
672 requested_size: f32,
673 ) {
674 if requested_size.abs() < 0.00001 {
675 return;
676 }
677
678 let mut temp_definitions = Vec::with_capacity(count);
679 let end = start + count;
680 let mut auto_definitions_count = 0;
681 let mut range_min_size = 0.0f32;
682 let mut range_preferred_size = 0.0f32;
683 let mut range_max_size = 0.0f32;
684 let mut max_max_size = 0.0f32;
685
686 for i in start..end {
687 let min_size = definitions[i].min_size;
688 let preferred_size = definitions[i].get_preferred_size();
689 let max_size = definitions[i].user_max_size.max(min_size);
690
691 range_min_size += min_size;
692 range_preferred_size += preferred_size;
693 range_max_size += max_size;
694
695 definitions[i].size_cache = max_size;
696
697 if max_max_size < max_max_size {
698 max_max_size = max_size;
699 }
700 if let Length::Auto = definitions[i].user_size {
701 auto_definitions_count += 1;
702 }
703
704 temp_definitions.push(i);
705 }
706
707 if requested_size <= range_min_size {
708 return;
709 }
710
711 if requested_size <= range_preferred_size {
712 temp_definitions.sort_by(|x, y| {
713 if let Length::Auto = definitions[*x].user_size {
714 if let Length::Auto = definitions[*y].user_size {
715 definitions[*x]
716 .min_size
717 .partial_cmp(&definitions[*y].min_size)
718 .unwrap()
719 } else {
720 std::cmp::Ordering::Less
721 }
722 } else {
723 if let Length::Auto = definitions[*y].user_size {
724 std::cmp::Ordering::Greater
725 } else {
726 definitions[*x]
727 .get_preferred_size()
728 .partial_cmp(&definitions[*y].get_preferred_size())
729 .unwrap()
730 }
731 }
732 });
733
734 let mut size_to_distribute = requested_size;
735 for i in 0..auto_definitions_count {
736 size_to_distribute -= definitions[temp_definitions[i]].min_size;
737 }
738
739 for i in auto_definitions_count..count {
740 let new_min_size = definitions[temp_definitions[i]]
741 .get_preferred_size()
742 .min(size_to_distribute / ((count - i) as f32));
743 if new_min_size > definitions[temp_definitions[i]].min_size {
744 definitions[temp_definitions[i]].update_min_size(new_min_size);
745 }
746 size_to_distribute -= new_min_size;
747 }
748 } else if requested_size <= range_max_size {
749 temp_definitions.sort_by(|x, y| {
750 if let Length::Auto = definitions[*x].user_size {
751 if let Length::Auto = definitions[*y].user_size {
752 definitions[*x]
753 .size_cache
754 .partial_cmp(&definitions[*y].size_cache)
755 .unwrap()
756 } else {
757 std::cmp::Ordering::Greater
758 }
759 } else {
760 if let Length::Auto = definitions[*y].user_size {
761 std::cmp::Ordering::Less
762 } else {
763 definitions[*x]
764 .size_cache
765 .partial_cmp(&definitions[*y].size_cache)
766 .unwrap()
767 }
768 }
769 });
770
771 let mut size_to_distribute = requested_size - range_preferred_size;
772 for i in 0..count - auto_definitions_count {
773 let preferred_size = definitions[temp_definitions[i]].get_preferred_size();
774 let new_min_size = preferred_size
775 + size_to_distribute / ((count - auto_definitions_count - i) as f32);
776 let size_cache = definitions[temp_definitions[i]].size_cache;
777 definitions[temp_definitions[i]].update_min_size(new_min_size.min(size_cache));
778 size_to_distribute -= definitions[temp_definitions[i]].min_size - preferred_size;
779 }
780
781 for i in count - auto_definitions_count..count {
782 let preferred_size = definitions[temp_definitions[i]].min_size;
783 let new_min_size = preferred_size + size_to_distribute / ((count - i) as f32);
784 let size_cache = definitions[temp_definitions[i]].size_cache;
785 definitions[temp_definitions[i]].update_min_size(new_min_size.min(size_cache));
786 size_to_distribute -= definitions[temp_definitions[i]].min_size - preferred_size;
787 }
788 } else {
789 let equal_size = requested_size / (count as f32);
790 if equal_size < max_max_size && max_max_size - equal_size > 0.0000015f32 {
791 let total_remaining_size = max_max_size * (count as f32) - range_max_size;
792 let size_to_distribute = requested_size - range_max_size;
793
794 for i in 0..count {
795 let delta_size = (max_max_size - definitions[temp_definitions[i]].size_cache)
796 * size_to_distribute
797 / total_remaining_size;
798 let size_cache = definitions[temp_definitions[i]].size_cache;
799 definitions[temp_definitions[i]].update_min_size(size_cache + delta_size);
800 }
801 } else {
802 for i in 0..count {
803 definitions[temp_definitions[i]].update_min_size(equal_size);
804 }
805 }
806 }
807 }
808
809 fn resolve_fill(definitions: &mut Vec<DefinitionBase>, available_size: f32) {
810 let def_count = definitions.len();
811 let mut definition_indices_min = Vec::<i32>::with_capacity(def_count);
812 let mut definition_indices_max = Vec::<i32>::with_capacity(def_count);
813 let mut taken_size = 0.0f32;
814 let mut fill_count = 0;
815 let mut scale = 1.0f32;
816
817 let mut max_fill = 0.0f32;
819 for def in definitions.iter_mut() {
820 if let Length::Fill(v) = def.size_type {
821 fill_count += 1;
822 def.measure_size = 1.0f32;
823 max_fill = max_fill.max(v);
824 }
825 }
826
827 if max_fill.is_infinite() && max_fill.is_sign_positive() {
828 scale = -1.0f32;
829 } else if fill_count > 0 {
830 let power = (f32::MAX / max_fill / (fill_count as f32)).log2().floor();
831 if power < 0.0 {
832 scale = 2.0f32.powf(power - 4.0f32);
833 }
834 }
835
836 let mut run_phase_2_and_3 = true;
838 while run_phase_2_and_3 {
839 let mut total_fill_weight = 0.0f32;
841 taken_size = 0.0f32;
842 let mut min_count = 0;
843 let mut max_count = 0;
844 definition_indices_min.truncate(0);
845 definition_indices_max.truncate(0);
846
847 for (i, def) in definitions.iter_mut().enumerate() {
848 match def.size_type {
849 Length::Auto => {
850 taken_size += def.min_size;
851 }
852 Length::Exact(_) => {
853 taken_size += def.measure_size;
854 }
855 Length::Fill(v) => {
856 if def.measure_size < 0.0f32 {
857 taken_size += -def.measure_size;
858 } else {
859 let fill_weight = Self::get_fill_weight(v, scale);
860 total_fill_weight += fill_weight;
861
862 if def.min_size > 0.0f32 {
863 definition_indices_min.push(i as i32);
864 min_count += 1;
865 def.measure_size = fill_weight / def.min_size;
866 }
867
868 let effective_max_size = def.min_size.max(def.user_max_size);
869 if !effective_max_size.is_infinite()
870 || !effective_max_size.is_sign_positive()
871 {
872 definition_indices_max.push(i as i32);
873 max_count += 1;
874 def.size_cache = fill_weight / effective_max_size;
875 }
876 }
877 }
878 };
879 }
880
881 let min_count_phase2 = min_count;
883 let max_count_phase2 = max_count;
884 let mut taken_fill_weight = 0.0f32;
885 let mut remaining_available_size = available_size - taken_size;
886 let mut remaining_fill_weight = total_fill_weight - taken_fill_weight;
887
888 definition_indices_min.sort_by(|x, y| {
889 definitions[*y as usize]
890 .measure_size
891 .partial_cmp(&definitions[*x as usize].measure_size)
892 .unwrap()
893 });
894 definition_indices_max.sort_by(|x, y| {
895 definitions[*x as usize]
896 .size_cache
897 .partial_cmp(&definitions[*y as usize].size_cache)
898 .unwrap()
899 });
900
901 while min_count + max_count > 0 && remaining_available_size > 0.0f32 {
902 if remaining_fill_weight < total_fill_weight / 256.0f32 {
903 taken_fill_weight = 0.0f32;
904 total_fill_weight = 0.0f32;
905
906 for def in definitions.iter_mut() {
907 if let Length::Fill(v) = def.size_type {
908 if def.measure_size > 0.0f32 {
909 total_fill_weight += Self::get_fill_weight(v, scale);
910 }
911 }
912 }
913
914 remaining_fill_weight = total_fill_weight - taken_fill_weight;
915 }
916
917 let min_ratio = if min_count > 0 {
918 definitions[definition_indices_min[min_count - 1] as usize].measure_size
919 } else {
920 f32::INFINITY
921 };
922 let max_ratio = if max_count > 0 {
923 definitions[definition_indices_max[max_count - 1] as usize].size_cache
924 } else {
925 -1.0f32
926 };
927 let proportion = remaining_fill_weight / remaining_available_size;
928
929 let choose_min = if min_ratio < proportion {
930 if max_ratio > proportion {
931 let min_power = min_ratio.log2().floor();
932 let max_power = max_ratio.log2().floor();
933 let f = 2.0f32.powf(((min_power + max_power) / 2.0f32).floor());
934 if (proportion / f) * (proportion / f) > (min_ratio / f) * (min_ratio / f) {
935 true
936 } else {
937 false
938 }
939 } else {
940 true
941 }
942 } else if max_ratio > proportion {
943 false
944 } else {
945 break;
946 };
947
948 let (resolved_def, resolved_size) = if choose_min {
949 let index = definition_indices_min[min_count - 1] as usize;
950 min_count -= 1;
951 let def = &mut definitions[index];
952 let size = def.min_size;
953 (def, size)
954 } else {
955 let index = definition_indices_max[max_count - 1] as usize;
956 max_count -= 1;
957 let def = &mut definitions[index];
958 let size = def.min_size.max(def.user_max_size);
959 (def, size)
960 };
961
962 taken_size += resolved_size;
963 resolved_def.measure_size = -resolved_size;
964 if let Length::Fill(v) = resolved_def.user_size {
965 taken_fill_weight += Self::get_fill_weight(v, scale);
966 fill_count -= 1;
967 }
968
969 remaining_available_size = available_size - taken_size;
970 remaining_fill_weight = total_fill_weight - taken_fill_weight;
971
972 while min_count > 0
973 && definitions[definition_indices_min[min_count - 1] as usize].measure_size
974 < 0.0
975 {
976 min_count -= 1;
977 definition_indices_min[min_count] = -1;
978 }
979 while max_count > 0
980 && definitions[definition_indices_max[max_count - 1] as usize].measure_size
981 < 0.0
982 {
983 max_count -= 1;
984 definition_indices_max[max_count] = -1;
985 }
986 }
987
988 run_phase_2_and_3 = false;
989 if fill_count == 0 && taken_size < available_size {
990 for i in min_count..min_count_phase2 {
991 let index = definition_indices_min[i];
992 if index >= 0 {
993 definitions[index as usize].measure_size = 1.0f32;
994 fill_count += 1;
995 run_phase_2_and_3 = true;
996 }
997 }
998 }
999
1000 if taken_size > available_size {
1001 for i in max_count..max_count_phase2 {
1002 let index = definition_indices_max[i];
1003 if index >= 0 {
1004 definitions[index as usize].measure_size = 1.0f32;
1005 fill_count += 1;
1006 run_phase_2_and_3 = true;
1007 }
1008 }
1009 }
1010 }
1011
1012 fill_count = 0;
1014 definition_indices_min.truncate(0);
1015 for i in 0..def_count {
1016 let def = &mut definitions[i];
1017
1018 if let Length::Fill(v) = def.size_type {
1019 if def.measure_size < 0.0f32 {
1020 def.measure_size = -def.measure_size;
1021 } else {
1022 definition_indices_min.push(i as i32);
1023 fill_count += 1;
1024 def.measure_size = Self::get_fill_weight(v, scale);
1025 }
1026 }
1027 }
1028
1029 if fill_count > 0 {
1030 definition_indices_min.sort_by(|x, y| {
1031 definitions[*x as usize]
1032 .measure_size
1033 .partial_cmp(&definitions[*y as usize].measure_size)
1034 .unwrap()
1035 });
1036
1037 let mut total_fill_weight = 0.0f32;
1038 for i in 0..fill_count {
1039 let def = &mut definitions[definition_indices_min[i] as usize];
1040 total_fill_weight += def.measure_size;
1041 def.size_cache = total_fill_weight;
1042 }
1043
1044 for i in (0..fill_count).rev() {
1045 let def = &mut definitions[definition_indices_min[i] as usize];
1046 let mut resolved_size = if def.measure_size > 0.0f32 {
1047 (available_size - taken_size).max(0.0f32) * (def.measure_size / def.size_cache)
1048 } else {
1049 0.0f32
1050 };
1051
1052 resolved_size = resolved_size.min(def.user_max_size);
1053 resolved_size = resolved_size.max(def.min_size);
1054
1055 def.measure_size = resolved_size;
1056 taken_size += resolved_size;
1057 }
1058 }
1059 }
1060
1061 fn set_final_size(definitions: &mut Vec<DefinitionBase>, final_size: f32, columns: bool) {
1062 let def_count = definitions.len();
1063 let mut definition_indices_min = Vec::<i32>::with_capacity(def_count);
1064 let mut definition_indices_max = Vec::<i32>::with_capacity(def_count);
1065 let mut taken_size = 0.0f32;
1066 let mut fill_count = 0;
1067 let mut scale = 1.0f32;
1068
1069 let mut max_fill = 0.0f32;
1071 for def in definitions.iter_mut() {
1072 if let Length::Fill(v) = def.user_size {
1073 fill_count += 1;
1074 def.measure_size = 1.0f32;
1075 max_fill = max_fill.max(v);
1076 }
1077 }
1078
1079 if max_fill.is_infinite() && max_fill.is_sign_positive() {
1080 scale = -1.0f32;
1081 } else if fill_count > 0 {
1082 let power = (f32::MAX / (max_fill as f32) / (fill_count as f32))
1083 .log(2.0f32)
1084 .floor();
1085 if power < 0.0f32 {
1086 scale = 2.0f32.powf(power - 4.0f32);
1087 }
1088 }
1089
1090 let mut run_phase_2_and_3 = true;
1092 while run_phase_2_and_3 {
1093 let mut total_fill_weight = 0.0f32;
1095 taken_size = 0.0f32;
1096 let mut min_count = 0;
1097 let mut max_count = 0;
1098 definition_indices_min.truncate(0);
1099 definition_indices_max.truncate(0);
1100
1101 for (i, def) in definitions.iter_mut().enumerate() {
1102 if let Length::Fill(v) = def.user_size {
1103 if def.measure_size < 0.0f32 {
1104 taken_size += -def.measure_size;
1105 } else {
1106 let fill_weight = Self::get_fill_weight(v, scale);
1107 total_fill_weight += fill_weight;
1108
1109 let min_size_for_arrange = def.get_min_size_for_arrange();
1110 if min_size_for_arrange > 0.0f32 {
1111 definition_indices_min.push(i as i32);
1112 min_count += 1;
1113 def.measure_size = fill_weight / min_size_for_arrange;
1114 }
1115
1116 let effective_max_size = min_size_for_arrange.max(def.user_max_size);
1117 if !effective_max_size.is_infinite()
1118 || !effective_max_size.is_sign_positive()
1119 {
1120 definition_indices_max.push(i as i32);
1121 max_count += 1;
1122 def.size_cache = fill_weight / effective_max_size;
1123 }
1124 }
1125 } else {
1126 let min_size_for_arrange = def.get_min_size_for_arrange();
1127
1128 let user_size = match def.user_size {
1129 Length::Exact(v) => v,
1130 Length::Auto => min_size_for_arrange,
1131 _ => 0.0f32,
1132 };
1133
1134 let user_max_size = if def.is_shared() {
1135 user_size
1136 } else {
1137 def.user_max_size
1138 };
1139
1140 def.size_cache = min_size_for_arrange.max(user_size.min(user_max_size));
1141 taken_size += def.size_cache;
1142 }
1143 }
1144
1145 let min_count_phase2 = min_count;
1147 let max_count_phase2 = max_count;
1148 let mut taken_fill_weight = 0.0f32;
1149 let mut remaining_available_size = final_size - taken_size;
1150 let mut remaining_fill_weight = total_fill_weight - taken_fill_weight;
1151
1152 definition_indices_min.sort_by(|x, y| {
1153 definitions[*y as usize]
1154 .measure_size
1155 .partial_cmp(&definitions[*x as usize].measure_size)
1156 .unwrap()
1157 });
1158 definition_indices_max.sort_by(|x, y| {
1159 definitions[*x as usize]
1160 .size_cache
1161 .partial_cmp(&definitions[*y as usize].size_cache)
1162 .unwrap()
1163 });
1164
1165 while min_count + max_count > 0 && remaining_available_size > 0.0f32 {
1166 if remaining_fill_weight < total_fill_weight / 256.0f32 {
1167 taken_fill_weight = 0.0f32;
1168 total_fill_weight = 0.0f32;
1169
1170 for def in definitions.iter_mut() {
1171 if let Length::Fill(v) = def.user_size {
1172 if def.measure_size > 0.0f32 {
1173 total_fill_weight += Self::get_fill_weight(v, scale);
1174 }
1175 }
1176 }
1177
1178 remaining_fill_weight = total_fill_weight - taken_fill_weight;
1179 }
1180
1181 let min_ratio = if min_count > 0 {
1182 definitions[definition_indices_min[min_count - 1] as usize].measure_size
1183 } else {
1184 f32::INFINITY
1185 };
1186 let max_ratio = if max_count > 0 {
1187 definitions[definition_indices_max[max_count - 1] as usize].size_cache
1188 } else {
1189 -1.0f32
1190 };
1191 let proportion = remaining_fill_weight / remaining_available_size;
1192
1193 let choose_min = if min_ratio < proportion {
1194 if max_ratio > proportion {
1195 let min_power = min_ratio.log2().floor();
1196 let max_power = max_ratio.log2().floor();
1197 let f = 2.0f32.powf(((min_power + max_power) / 2.0f32).floor());
1198 if (proportion / f) * (proportion / f) > (min_ratio / f) * (min_ratio / f) {
1199 true
1200 } else {
1201 false
1202 }
1203 } else {
1204 true
1205 }
1206 } else if max_ratio > proportion {
1207 false
1208 } else {
1209 break;
1210 };
1211
1212 let (resolved_def, resolved_size) = if choose_min {
1213 let index = definition_indices_min[min_count - 1] as usize;
1214 min_count -= 1;
1215 let def = &mut definitions[index];
1216 let size = def.get_min_size_for_arrange();
1217 (def, size)
1218 } else {
1219 let index = definition_indices_max[max_count - 1] as usize;
1220 max_count -= 1;
1221 let def = &mut definitions[index];
1222 let size = def.get_min_size_for_arrange().max(def.user_max_size);
1223 (def, size)
1224 };
1225
1226 taken_size += resolved_size;
1227 resolved_def.measure_size = -resolved_size;
1228 if let Length::Fill(v) = resolved_def.user_size {
1229 taken_fill_weight += Self::get_fill_weight(v, scale);
1230 fill_count -= 1;
1231 }
1232
1233 remaining_available_size = final_size - taken_size;
1234 remaining_fill_weight = total_fill_weight - taken_fill_weight;
1235
1236 while min_count > 0
1237 && definitions[definition_indices_min[min_count - 1] as usize].measure_size
1238 < 0.0
1239 {
1240 min_count -= 1;
1241 definition_indices_min[min_count] = -1;
1242 }
1243 while max_count > 0
1244 && definitions[definition_indices_max[max_count - 1] as usize].measure_size
1245 < 0.0
1246 {
1247 max_count -= 1;
1248 definition_indices_max[max_count] = -1;
1249 }
1250 }
1251
1252 run_phase_2_and_3 = false;
1253 if fill_count == 0 && taken_size < final_size {
1254 for i in min_count..min_count_phase2 {
1255 let index = definition_indices_min[i];
1256 if index >= 0 {
1257 definitions[index as usize].measure_size = 1.0f32;
1258 fill_count += 1;
1259 run_phase_2_and_3 = true;
1260 }
1261 }
1262 }
1263
1264 if taken_size > final_size {
1265 for i in max_count..max_count_phase2 {
1266 let index = definition_indices_max[i];
1267 if index >= 0 {
1268 definitions[index as usize].measure_size = 1.0f32;
1269 fill_count += 1;
1270 run_phase_2_and_3 = true;
1271 }
1272 }
1273 }
1274 }
1275
1276 fill_count = 0;
1278 definition_indices_min.truncate(0);
1279 for i in 0..def_count {
1280 let def = &mut definitions[i];
1281
1282 if let Length::Fill(v) = def.user_size {
1283 if def.measure_size < 0.0f32 {
1284 def.size_cache = -def.measure_size;
1285 } else {
1286 definition_indices_min.push(i as i32);
1287 fill_count += 1;
1288 def.measure_size = Self::get_fill_weight(v, scale);
1289 }
1290 }
1291 }
1292
1293 if fill_count > 0 {
1294 definition_indices_min.sort_by(|x, y| {
1295 definitions[*x as usize]
1296 .measure_size
1297 .partial_cmp(&definitions[*y as usize].measure_size)
1298 .unwrap()
1299 });
1300
1301 let mut total_fill_weight = 0.0f32;
1302 for i in 0..fill_count {
1303 let def = &mut definitions[definition_indices_min[i] as usize];
1304 total_fill_weight += def.measure_size;
1305 def.size_cache = total_fill_weight;
1306 }
1307
1308 for i in (0..fill_count).rev() {
1309 let def = &mut definitions[definition_indices_min[i] as usize];
1310 let mut resolved_size = if def.measure_size > 0.0f32 {
1311 (final_size - taken_size).max(0.0f32) * (def.measure_size / def.size_cache)
1312 } else {
1313 0.0f32
1314 };
1315
1316 resolved_size = resolved_size.min(def.user_max_size);
1317 resolved_size = resolved_size.max(def.get_min_size_for_arrange());
1318
1319 taken_size += resolved_size;
1320 def.size_cache = resolved_size;
1321 }
1322 }
1323
1324 let use_layout_rounding = true;
1326 if use_layout_rounding {
1327 let dpi_scale_x = 1.0f32;
1329 let dpi_scale_y = 1.0f32;
1330 let dpi_scale = if columns { dpi_scale_x } else { dpi_scale_y };
1331
1332 let mut rounding_errors = Vec::with_capacity(definitions.len());
1333 let mut rounded_taken_size = 0.0f32;
1334 for def in definitions.iter_mut() {
1335 let rounded_size = round_layout_value(def.size_cache, dpi_scale);
1336 rounding_errors.push(rounded_size - def.size_cache);
1337 def.size_cache = rounded_size;
1338 rounded_taken_size += rounded_size;
1339 }
1340
1341 if (rounded_taken_size - final_size).abs() > 0.0000015f32 {
1342 definition_indices_min.truncate(0);
1343 for i in 0..definitions.len() {
1344 definition_indices_min.push(i as i32);
1345 }
1346
1347 definition_indices_min.sort_by(|x, y| {
1348 rounding_errors[*x as usize]
1349 .partial_cmp(&rounding_errors[*y as usize])
1350 .unwrap()
1351 });
1352
1353 let mut adjusted_size = rounded_taken_size;
1354 let dpi_increment = 1.0f32 / dpi_scale;
1355
1356 if rounded_taken_size > final_size {
1357 let mut i = (definitions.len() - 1) as i32;
1358 while adjusted_size > final_size
1359 && adjusted_size - final_size > 0.0000015f32
1360 && i >= 0
1361 {
1362 let definition =
1363 &mut definitions[definition_indices_min[i as usize] as usize];
1364 let mut final_size = definition.size_cache - dpi_increment;
1365 final_size = final_size.max(definition.get_min_size_for_arrange());
1366 if final_size < definition.size_cache {
1367 adjusted_size -= dpi_increment;
1368 }
1369 definition.size_cache = final_size;
1370 i -= 1;
1371 }
1372 } else if rounded_taken_size < final_size {
1373 let mut i = 0;
1374 while adjusted_size < final_size
1375 && final_size - adjusted_size > 0.0000015f32
1376 && i < definitions.len()
1377 {
1378 let definition = &mut definitions[definition_indices_min[i] as usize];
1379 let mut final_size = definition.size_cache + dpi_increment;
1380 final_size = final_size.max(definition.get_min_size_for_arrange());
1381 if final_size > definition.size_cache {
1382 adjusted_size += dpi_increment;
1383 }
1384 definition.size_cache = final_size;
1385 i += 1;
1386 }
1387 }
1388 }
1389 }
1390
1391 definitions[0].final_offset = 0.0f32;
1393 let length = definitions.len();
1394 for i in 0..length {
1395 definitions[(i + 1) % length].final_offset =
1396 definitions[i].final_offset + definitions[i].size_cache;
1397 }
1398 }
1399
1400 fn get_fill_weight(v: f32, scale: f32) -> f32 {
1402 if scale < 0.0f32 {
1403 if v.is_infinite() && v.is_sign_positive() {
1404 1.0f32
1405 } else {
1406 0.0f32
1407 }
1408 } else {
1409 v * scale
1410 }
1411 }
1412
1413 fn get_final_size_for_range(
1414 definitions: &mut Vec<DefinitionBase>,
1415 start: usize,
1416 count: usize,
1417 ) -> f32 {
1418 definitions
1419 .iter()
1420 .skip(start)
1421 .take(count)
1422 .map(|def| def.size_cache)
1423 .sum()
1424 }
1425
1426 fn cache_min_sizes(
1427 definitions: &Vec<DefinitionBase>,
1428 cells: &Vec<CellCache>,
1429 is_rows: bool,
1430 ) -> Vec<f32> {
1431 let mut min_sizes = Vec::with_capacity(definitions.len());
1432
1433 for _ in 0..definitions.len() {
1434 min_sizes.push(-1.0f32);
1435 }
1436
1437 for cell in cells {
1438 if is_rows {
1439 min_sizes[cell.row_index] = definitions[cell.row_index].min_size;
1440 } else {
1441 min_sizes[cell.column_index] = definitions[cell.column_index].min_size;
1442 }
1443 }
1444
1445 min_sizes
1446 }
1447
1448 fn apply_cached_min_sizes(definitions: &mut Vec<DefinitionBase>, min_sizes: &Vec<f32>) {
1449 for (i, min_size) in min_sizes.iter().enumerate() {
1450 if *min_size > 0.0f32 || min_size.abs() < 0.0000015f32 {
1451 definitions[i].min_size = *min_size;
1452 }
1453 }
1454 }
1455
1456 fn calculate_desired_size(definitions: &Vec<DefinitionBase>) -> f32 {
1457 return definitions.iter().map(|d| d.min_size).sum();
1458 }
1459}
1460
1461impl Style<Grid> for DefaultGridStyle {
1462 fn setup(&mut self, _data: &mut Grid, _control_context: &mut ControlContext) {}
1463
1464 fn handle_event(
1465 &mut self,
1466 _data: &mut Grid,
1467 _control_context: &mut ControlContext,
1468 _drawing_context: &mut dyn DrawingContext,
1469 _event_context: &mut dyn EventContext,
1470 _event: ControlEvent,
1471 ) {
1472 }
1473
1474 fn measure(
1475 &mut self,
1476 data: &mut Grid,
1477 control_context: &mut ControlContext,
1478 drawing_context: &mut dyn DrawingContext,
1479 size: Size,
1480 ) -> Size {
1481 let mut grid_desired_size = Size::new(0.0f32, 0.0f32);
1482
1483 let children = control_context.get_children();
1484
1485 let (number_of_rows, number_of_columns) =
1486 Self::decide_number_of_rows_and_columns(data, children);
1487
1488 if number_of_rows == 0 && number_of_columns == 0 {
1489 self.definitions_u = Vec::new();
1490 self.definitions_v = Vec::new();
1491
1492 for child in children.into_iter() {
1493 let mut child = child.borrow_mut();
1494 child.measure(drawing_context, size);
1495 let child_rc = child.get_rect();
1496 grid_desired_size.width = grid_desired_size.width.max(child_rc.width);
1497 grid_desired_size.height = grid_desired_size.height.max(child_rc.height);
1498 }
1499 } else {
1500 let size_to_content_u = size.width == f32::INFINITY;
1501 let size_to_content_v = size.height == f32::INFINITY;
1502
1503 self.prepare_definitions(
1504 &data,
1505 &children,
1506 number_of_rows,
1507 number_of_columns,
1508 size_to_content_u,
1509 size_to_content_v,
1510 );
1511 self.prepare_cell_cache(&data, &children);
1512
1513 Self::measure_cells_group(
1514 drawing_context,
1515 &mut self.definitions_u,
1516 &mut self.definitions_v,
1517 &self.cell_group_1,
1518 &children,
1519 false,
1520 false,
1521 );
1522
1523 if !self.has_group_3_cells_in_auto_rows {
1524 if self.has_fill_cells_v {
1525 Self::resolve_fill(&mut self.definitions_v, size.height);
1526 }
1527 Self::measure_cells_group(
1528 drawing_context,
1529 &mut self.definitions_u,
1530 &mut self.definitions_v,
1531 &self.cell_group_2,
1532 &children,
1533 false,
1534 false,
1535 );
1536
1537 if self.has_fill_cells_u {
1538 Self::resolve_fill(&mut self.definitions_u, size.width);
1539 }
1540 Self::measure_cells_group(
1541 drawing_context,
1542 &mut self.definitions_u,
1543 &mut self.definitions_v,
1544 &self.cell_group_3,
1545 &children,
1546 false,
1547 false,
1548 );
1549 } else {
1550 if self.cell_group_2.len() == 0 {
1551 if self.has_fill_cells_u {
1552 Self::resolve_fill(&mut self.definitions_u, size.width);
1553 }
1554 Self::measure_cells_group(
1555 drawing_context,
1556 &mut self.definitions_u,
1557 &mut self.definitions_v,
1558 &self.cell_group_3,
1559 &children,
1560 false,
1561 false,
1562 );
1563 if self.has_fill_cells_v {
1564 Self::resolve_fill(&mut self.definitions_v, size.height);
1565 }
1566 } else {
1567 let mut has_desired_size_u_changed = false;
1568 let mut cnt = 0;
1569
1570 let group_2_min_sizes =
1571 Self::cache_min_sizes(&self.definitions_u, &self.cell_group_3, false);
1572 let group_3_min_sizes =
1573 Self::cache_min_sizes(&self.definitions_v, &self.cell_group_3, true);
1574
1575 Self::measure_cells_group(
1576 drawing_context,
1577 &mut self.definitions_u,
1578 &mut self.definitions_v,
1579 &self.cell_group_2,
1580 &children,
1581 false,
1582 true,
1583 );
1584
1585 loop {
1586 if has_desired_size_u_changed {
1587 Self::apply_cached_min_sizes(
1588 &mut self.definitions_v,
1589 &group_3_min_sizes,
1590 );
1591 }
1592
1593 if self.has_fill_cells_u {
1594 Self::resolve_fill(&mut self.definitions_u, size.width);
1595 }
1596 Self::measure_cells_group(
1597 drawing_context,
1598 &mut self.definitions_u,
1599 &mut self.definitions_v,
1600 &self.cell_group_3,
1601 &children,
1602 false,
1603 false,
1604 );
1605
1606 Self::apply_cached_min_sizes(&mut self.definitions_u, &group_2_min_sizes);
1607
1608 if self.has_fill_cells_v {
1609 Self::resolve_fill(&mut self.definitions_v, size.height);
1610 }
1611 has_desired_size_u_changed = Self::measure_cells_group(
1612 drawing_context,
1613 &mut self.definitions_u,
1614 &mut self.definitions_v,
1615 &self.cell_group_2,
1616 &children,
1617 cnt == 5,
1618 false,
1619 );
1620
1621 cnt += 1;
1622 if !has_desired_size_u_changed || cnt > 5 {
1623 break;
1624 }
1625 }
1626 }
1627 }
1628
1629 Self::measure_cells_group(
1630 drawing_context,
1631 &mut self.definitions_u,
1632 &mut self.definitions_v,
1633 &self.cell_group_4,
1634 &children,
1635 false,
1636 false,
1637 );
1638
1639 grid_desired_size.width = Self::calculate_desired_size(&self.definitions_u);
1640 grid_desired_size.height = Self::calculate_desired_size(&self.definitions_v);
1641 }
1642
1643 grid_desired_size
1644 }
1645
1646 fn set_rect(
1647 &mut self,
1648 _data: &mut Grid,
1649 control_context: &mut ControlContext,
1650 drawing_context: &mut dyn DrawingContext,
1651 rect: Rect,
1652 ) {
1653 let children = control_context.get_children();
1654
1655 if self.definitions_u.len() == 0 && self.definitions_v.len() == 0 {
1656 for child in children.into_iter() {
1657 child.borrow_mut().set_rect(drawing_context, rect);
1658 }
1659 } else {
1660 Self::set_final_size(&mut self.definitions_u, rect.width, true);
1661 Self::set_final_size(&mut self.definitions_v, rect.height, false);
1662
1663 for cell in self
1664 .cell_group_1
1665 .iter()
1666 .chain(self.cell_group_2.iter())
1667 .chain(self.cell_group_3.iter())
1668 .chain(self.cell_group_4.iter())
1669 {
1670 let child = children.get(cell.child_index).unwrap();
1671 let column_index = cell.column_index;
1672 let row_index = cell.row_index;
1673 let column_span = cell.column_span;
1674 let row_span = cell.row_span;
1675 let rc = Rect::new(
1676 if column_index == 0 {
1677 rect.x
1678 } else {
1679 rect.x + self.definitions_u[column_index].final_offset
1680 },
1681 if row_index == 0 {
1682 rect.y
1683 } else {
1684 rect.y + self.definitions_v[row_index].final_offset
1685 },
1686 Self::get_final_size_for_range(
1687 &mut self.definitions_u,
1688 column_index,
1689 column_span,
1690 ),
1691 Self::get_final_size_for_range(&mut self.definitions_v, row_index, row_span),
1692 );
1693
1694 child.borrow_mut().set_rect(drawing_context, rc);
1695 }
1696 }
1697 }
1698
1699 fn hit_test(
1700 &self,
1701 _data: &Grid,
1702 control_context: &ControlContext,
1703 point: Point,
1704 ) -> Option<Rc<RefCell<dyn ControlObject>>> {
1705 if point.is_inside(&control_context.get_rect()) {
1706 let children = control_context.get_children();
1707 for child in children.into_iter().rev() {
1708 let c = child.borrow();
1709 let rect = c.get_rect();
1710 if point.is_inside(&rect) {
1711 let hit_control = c.hit_test(point);
1712 if hit_control.is_some() {
1713 return hit_control;
1714 }
1715 }
1716 }
1717 None
1718 } else {
1719 None
1720 }
1721 }
1722
1723 fn to_primitives(
1724 &self,
1725 _data: &Grid,
1726 control_context: &ControlContext,
1727 drawing_context: &mut dyn DrawingContext,
1728 ) -> (Vec<Primitive>, Vec<Primitive>) {
1729 let mut vec = Vec::new();
1730 let mut overlay = Vec::new();
1731
1732 let children = control_context.get_children();
1733 for child in children.into_iter() {
1734 let (mut vec2, mut overlay2) = child.borrow().to_primitives(drawing_context);
1735 vec.append(&mut vec2);
1736 overlay.append(&mut overlay2);
1737 }
1738
1739 (vec, overlay)
1740 }
1741}