You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Make GridPlacement's fields non-zero and add accessor functions. (#9486)
# Objective
* There is no way to read the fields of `GridPlacement` once set.
* Values of `0` for `GridPlacement`'s fields are invalid but can be set.
* A non-zero representation would be half the size.
fixes#9474
## Solution
* Add `get_start`, `get_end` and `get_span` accessor methods.
* Change`GridPlacement`'s constructor functions to panic on arguments of
zero.
* Use non-zero types instead of primitives for `GridPlacement`'s fields.
---
## Changelog
`bevy_ui::ui_node::GridPlacement`:
* Field types have been changed to `Option<NonZeroI16>` and
`Option<NonZeroU16>`. This is because zero values are not valid for
`GridPlacement`. Previously, Taffy interpreted these as auto variants.
* Constructor functions for `GridPlacement` panic on arguments of `0`.
* Added accessor functions: `get_start`, `get_end`, and `get_span`.
These return the inner primitive value (if present) of the respective
fields.
## Migration Guide
`GridPlacement`'s constructor functions no longer accept values of `0`.
Given any argument of `0` they will panic with a `GridPlacementError`.
/// The grid line at which the item should start. Lines are 1-indexed. Negative indexes count backwards from the end of the grid. Zero is not a valid index.
1473
-
pub(crate)start:Option<i16>,
1476
+
pub(crate)start:Option<NonZeroI16>,
1474
1477
/// How many grid tracks the item should span. Defaults to 1.
1475
-
pub(crate)span:Option<u16>,
1476
-
/// The grid line at which the node should end. Lines are 1-indexed. Negative indexes count backwards from the end of the grid. Zero is not a valid index.
1477
-
pub(crate)end:Option<i16>,
1478
+
pub(crate)span:Option<NonZeroU16>,
1479
+
/// The grid line at which the item should end. Lines are 1-indexed. Negative indexes count backwards from the end of the grid. Zero is not a valid index.
1480
+
pub(crate)end:Option<NonZeroI16>,
1478
1481
}
1479
1482
1480
1483
implGridPlacement{
1481
1484
pubconstDEFAULT:Self = Self{
1482
1485
start:None,
1483
-
span:Some(1),
1486
+
span:Some(unsafe{NonZeroU16::new_unchecked(1)}),
1484
1487
end:None,
1485
1488
};
1486
1489
1487
1490
/// Place the grid item automatically (letting the `span` default to `1`).
1488
1491
pubfnauto() -> Self{
1489
-
Self{
1490
-
start:None,
1491
-
end:None,
1492
-
span:Some(1),
1493
-
}
1492
+
Self::DEFAULT
1494
1493
}
1495
1494
1496
1495
/// Place the grid item automatically, specifying how many tracks it should `span`.
1496
+
///
1497
+
/// # Panics
1498
+
///
1499
+
/// Panics if `span` is `0`
1497
1500
pubfnspan(span:u16) -> Self{
1498
1501
Self{
1499
1502
start:None,
1500
1503
end:None,
1501
-
span:Some(span),
1504
+
span:try_into_grid_span(span).expect("Invalid span value of 0."),
1502
1505
}
1503
1506
}
1504
1507
1505
1508
/// Place the grid item specifying the `start` grid line (letting the `span` default to `1`).
1509
+
///
1510
+
/// # Panics
1511
+
///
1512
+
/// Panics if `start` is `0`
1506
1513
pubfnstart(start:i16) -> Self{
1507
1514
Self{
1508
-
start:Some(start),
1509
-
end:None,
1510
-
span:Some(1),
1515
+
start:try_into_grid_index(start).expect("Invalid start value of 0."),
1516
+
..Self::DEFAULT
1511
1517
}
1512
1518
}
1513
1519
1514
1520
/// Place the grid item specifying the `end` grid line (letting the `span` default to `1`).
1521
+
///
1522
+
/// # Panics
1523
+
///
1524
+
/// Panics if `end` is `0`
1515
1525
pubfnend(end:i16) -> Self{
1516
1526
Self{
1517
-
start:None,
1518
-
end:Some(end),
1519
-
span:Some(1),
1527
+
end:try_into_grid_index(end).expect("Invalid end value of 0."),
1528
+
..Self::DEFAULT
1520
1529
}
1521
1530
}
1522
1531
1523
1532
/// Place the grid item specifying the `start` grid line and how many tracks it should `span`.
1533
+
///
1534
+
/// # Panics
1535
+
///
1536
+
/// Panics if `start` or `span` is `0`
1524
1537
pubfnstart_span(start:i16,span:u16) -> Self{
1525
1538
Self{
1526
-
start:Some(start),
1539
+
start:try_into_grid_index(start).expect("Invalid start value of 0."),
1527
1540
end:None,
1528
-
span:Some(span),
1541
+
span:try_into_grid_span(span).expect("Invalid span value of 0."),
1529
1542
}
1530
1543
}
1531
1544
1532
1545
/// Place the grid item specifying `start` and `end` grid lines (`span` will be inferred)
1546
+
///
1547
+
/// # Panics
1548
+
///
1549
+
/// Panics if `start` or `end` is `0`
1533
1550
pubfnstart_end(start:i16,end:i16) -> Self{
1534
1551
Self{
1535
-
start:Some(start),
1536
-
end:Some(end),
1552
+
start:try_into_grid_index(start).expect("Invalid start value of 0."),
1553
+
end:try_into_grid_index(end).expect("Invalid end value of 0."),
1537
1554
span:None,
1538
1555
}
1539
1556
}
1540
1557
1541
1558
/// Place the grid item specifying the `end` grid line and how many tracks it should `span`.
1559
+
///
1560
+
/// # Panics
1561
+
///
1562
+
/// Panics if `end` or `span` is `0`
1542
1563
pubfnend_span(end:i16,span:u16) -> Self{
1543
1564
Self{
1544
1565
start:None,
1545
-
end:Some(end),
1546
-
span:Some(span),
1566
+
end:try_into_grid_index(end).expect("Invalid end value of 0."),
1567
+
span:try_into_grid_span(span).expect("Invalid span value of 0."),
1547
1568
}
1548
1569
}
1549
1570
1550
1571
/// Mutate the item, setting the `start` grid line
1572
+
///
1573
+
/// # Panics
1574
+
///
1575
+
/// Panics if `start` is `0`
1551
1576
pubfnset_start(mutself,start:i16) -> Self{
1552
-
self.start = Some(start);
1577
+
self.start = try_into_grid_index(start).expect("Invalid start value of 0.");
1553
1578
self
1554
1579
}
1555
1580
1556
1581
/// Mutate the item, setting the `end` grid line
1582
+
///
1583
+
/// # Panics
1584
+
///
1585
+
/// Panics if `end` is `0`
1557
1586
pubfnset_end(mutself,end:i16) -> Self{
1558
-
self.end = Some(end);
1587
+
self.end = try_into_grid_index(end).expect("Invalid end value of 0.");
1559
1588
self
1560
1589
}
1561
1590
1562
1591
/// Mutate the item, setting the number of tracks the item should `span`
1592
+
///
1593
+
/// # Panics
1594
+
///
1595
+
/// Panics if `span` is `0`
1563
1596
pubfnset_span(mutself,span:u16) -> Self{
1564
-
self.span = Some(span);
1597
+
self.span = try_into_grid_span(span).expect("Invalid span value of 0.");
1565
1598
self
1566
1599
}
1600
+
1601
+
/// Returns the grid line at which the item should start, or `None` if not set.
1602
+
pubfnget_start(self) -> Option<i16>{
1603
+
self.start.map(NonZeroI16::get)
1604
+
}
1605
+
1606
+
/// Returns the grid line at which the item should end, or `None` if not set.
1607
+
pubfnget_end(self) -> Option<i16>{
1608
+
self.end.map(NonZeroI16::get)
1609
+
}
1610
+
1611
+
/// Returns span for this grid item, or `None` if not set.
1612
+
pubfnget_span(self) -> Option<u16>{
1613
+
self.span.map(NonZeroU16::get)
1614
+
}
1567
1615
}
1568
1616
1569
1617
implDefaultforGridPlacement{
@@ -1572,6 +1620,29 @@ impl Default for GridPlacement {
1572
1620
}
1573
1621
}
1574
1622
1623
+
/// Convert an `i16` to `NonZeroI16`, fails on `0` and returns the `InvalidZeroIndex` error.
0 commit comments