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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
// https://www.apache.org/licenses/LICENSE-2.0
//! Layout solver — storage
use super::SizeRules;
use kas_macros::impl_scope;
use std::fmt::Debug;
/// Requirements of row solver storage type
///
/// Usually this is set by a [`crate::layout::RowSolver`] from
/// [`crate::Layout::size_rules`], then used by [`crate::Layout::set_rect`] to
/// divide the assigned rect between children.
///
/// It may be useful to access this directly if not solving size rules normally;
/// specifically this allows a different size solver to replace `size_rules` and
/// influence `set_rect`.
///
/// Note: some implementations allocate when [`Self::set_dim`] is first called.
/// It is expected that this method is called before other methods.
pub trait RowStorage: Clone + Debug + sealed::Sealed {
/// Set dimension: number of columns or rows
fn set_dim(&mut self, cols: usize);
/// Access [`SizeRules`] for each column/row
fn rules(&mut self) -> &mut [SizeRules] {
self.widths_and_rules().1
}
/// Access widths for each column/row
///
/// Widths are calculated from rules when `set_rect` is called. Assigning
/// to widths before `set_rect` is called only has any effect when the available
/// size exceeds the minimum required (see [`SizeRules::solve_seq`]).
fn widths(&mut self) -> &mut [i32] {
self.widths_and_rules().0
}
/// Access widths and rules simultaneously
fn widths_and_rules(&mut self) -> (&mut [i32], &mut [SizeRules]);
}
/// Fixed-length row storage
///
/// Uses const-generics argument `C` (the number of columns).
#[derive(Clone, Debug)]
pub struct FixedRowStorage<const C: usize> {
rules: [SizeRules; C],
widths: [i32; C],
}
impl<const C: usize> Default for FixedRowStorage<C> {
fn default() -> Self {
FixedRowStorage {
rules: [SizeRules::default(); C],
widths: [0; C],
}
}
}
impl<const C: usize> RowStorage for FixedRowStorage<C> {
fn set_dim(&mut self, cols: usize) {
assert_eq!(self.rules.as_ref().len(), cols);
assert_eq!(self.widths.as_ref().len(), cols);
}
fn widths_and_rules(&mut self) -> (&mut [i32], &mut [SizeRules]) {
(self.widths.as_mut(), self.rules.as_mut())
}
}
/// Variable-length row storage
#[derive(Clone, Debug, Default)]
pub struct DynRowStorage {
rules: Vec<SizeRules>,
widths: Vec<i32>,
}
impl RowStorage for DynRowStorage {
fn set_dim(&mut self, cols: usize) {
self.rules.resize(cols, SizeRules::EMPTY);
self.widths.resize(cols, 0);
}
fn widths_and_rules(&mut self) -> (&mut [i32], &mut [SizeRules]) {
(&mut self.widths, &mut self.rules)
}
}
/// Temporary storage type.
///
/// For dynamic-length rows and fixed-length rows with more than 16 items use
/// `Vec<i32>`. For fixed-length rows up to 16 items, use `[i32; rows]`.
pub trait RowTemp: AsMut<[i32]> + Default + Debug + sealed::Sealed {
#[doc(hidden)]
fn set_len(&mut self, len: usize);
}
impl RowTemp for Vec<i32> {
fn set_len(&mut self, len: usize) {
self.resize(len, 0);
}
}
impl<const L: usize> RowTemp for [i32; L]
where
[i32; L]: Default,
{
fn set_len(&mut self, len: usize) {
assert_eq!(self.len(), len);
}
}
/// Requirements of grid solver storage type
///
/// Usually this is set by a [`crate::layout::GridSolver`] from
/// [`crate::Layout::size_rules`], then used by [`crate::Layout::set_rect`] to
/// divide the assigned rect between children.
///
/// It may be useful to access this directly if not solving size rules normally;
/// specifically this allows a different size solver to replace `size_rules` and
/// influence `set_rect`.
///
/// Note: some implementations allocate when [`Self::set_dims`] is first called.
/// It is expected that this method is called before other methods.
pub trait GridStorage: sealed::Sealed + Clone + std::fmt::Debug {
/// Set dimension: number of columns and rows
fn set_dims(&mut self, cols: usize, rows: usize);
/// Access [`SizeRules`] for each column
fn width_rules(&mut self) -> &mut [SizeRules] {
self.widths_and_rules().1
}
/// Access [`SizeRules`] for each row
fn height_rules(&mut self) -> &mut [SizeRules] {
self.heights_and_rules().1
}
/// Access widths for each column
///
/// Widths are calculated from rules when `set_rect` is called. Assigning
/// to widths before `set_rect` is called only has any effect when the available
/// size exceeds the minimum required (see [`SizeRules::solve_seq`]).
fn widths(&mut self) -> &mut [i32] {
self.widths_and_rules().0
}
/// Access heights for each row
///
/// Heights are calculated from rules when `set_rect` is called. Assigning
/// to heights before `set_rect` is called only has any effect when the available
/// size exceeds the minimum required (see [`SizeRules::solve_seq`]).
fn heights(&mut self) -> &mut [i32] {
self.heights_and_rules().0
}
/// Access column widths and rules simultaneously
fn widths_and_rules(&mut self) -> (&mut [i32], &mut [SizeRules]);
/// Access row heights and rules simultaneously
fn heights_and_rules(&mut self) -> (&mut [i32], &mut [SizeRules]);
}
impl_scope! {
/// Fixed-length grid storage
///
/// Uses const-generics arguments `R, C` (the number of rows and columns).
#[impl_default]
#[derive(Clone, Debug)]
pub struct FixedGridStorage<const C: usize, const R: usize> {
width_rules: [SizeRules; C] = [SizeRules::default(); C],
height_rules: [SizeRules; R] = [SizeRules::default(); R],
widths: [i32; C] = [0; C],
heights: [i32; R] = [0; R],
}
impl GridStorage for Self {
fn set_dims(&mut self, cols: usize, rows: usize) {
assert_eq!(self.width_rules.as_ref().len(), cols);
assert_eq!(self.height_rules.as_ref().len(), rows);
assert_eq!(self.widths.len(), cols);
assert_eq!(self.heights.len(), rows);
}
#[doc(hidden)]
fn widths_and_rules(&mut self) -> (&mut [i32], &mut [SizeRules]) {
(
self.widths.as_mut(),
self.width_rules.as_mut(),
)
}
#[doc(hidden)]
fn heights_and_rules(&mut self) -> (&mut [i32], &mut [SizeRules]) {
(
self.heights.as_mut(),
self.height_rules.as_mut(),
)
}
}
}
/// Variable-length grid storage
#[derive(Clone, Debug, Default)]
pub struct DynGridStorage {
width_rules: Vec<SizeRules>,
height_rules: Vec<SizeRules>,
widths: Vec<i32>,
heights: Vec<i32>,
}
impl GridStorage for DynGridStorage {
fn set_dims(&mut self, cols: usize, rows: usize) {
self.width_rules.resize(cols, SizeRules::EMPTY);
self.height_rules.resize(rows, SizeRules::EMPTY);
self.widths.resize(cols, 0);
self.heights.resize(rows, 0);
}
#[doc(hidden)]
fn widths_and_rules(&mut self) -> (&mut [i32], &mut [SizeRules]) {
(self.widths.as_mut(), self.width_rules.as_mut())
}
#[doc(hidden)]
fn heights_and_rules(&mut self) -> (&mut [i32], &mut [SizeRules]) {
(self.heights.as_mut(), self.height_rules.as_mut())
}
}
mod sealed {
pub trait Sealed {}
impl<const C: usize> Sealed for super::FixedRowStorage<C> {}
impl Sealed for super::DynRowStorage {}
impl Sealed for Vec<i32> {}
impl<const L: usize> Sealed for [i32; L] {}
impl<const C: usize, const R: usize> Sealed for super::FixedGridStorage<C, R> {}
impl Sealed for super::DynGridStorage {}
}