| 1 |
|
| 2 |
|
| 3 |
use std::cell::RefCell; |
| 4 |
use std::collections::HashMap; |
| 5 |
|
| •6 |
use kasuari::Strength; |
| 7 |
use kasuari::WeightedRelation::*; |
| 8 |
use kasuari::{Constraint as KasuariConstraint, Expression, Solver, Variable}; |
| 9 |
|
| 10 |
use helix_view::graphics::{Margin, Rect}; |
| 11 |
|
| 12 |
|
| 13 |
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)] |
| 14 |
pub enum Corner { |
| 15 |
TopLeft, |
| 16 |
TopRight, |
| 17 |
BottomRight, |
| 18 |
BottomLeft, |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
#[derive(Debug, Hash, Clone, PartialEq, Eq)] |
| 23 |
pub enum Direction { |
| 24 |
Horizontal, |
| 25 |
Vertical, |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 30 |
pub enum Constraint { |
| 31 |
|
| 32 |
Percentage(u16), |
| 33 |
Ratio(u32, u32), |
| 34 |
Length(u16), |
| 35 |
Max(u16), |
| 36 |
Min(u16), |
| 37 |
} |
| 38 |
|
| 39 |
impl Constraint { |
| 40 |
pub fn apply(&self, length: u16) -> u16 { |
| 41 |
match *self { |
| 42 |
Constraint::Percentage(p) => length * p / 100, |
| 43 |
Constraint::Ratio(num, den) => { |
| 44 |
let r = num * u32::from(length) / den; |
| 45 |
r as u16 |
| 46 |
} |
| 47 |
Constraint::Length(l) => length.min(l), |
| 48 |
Constraint::Max(m) => length.min(m), |
| 49 |
Constraint::Min(m) => length.max(m), |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 56 |
pub enum Alignment { |
| 57 |
Left, |
| 58 |
Center, |
| 59 |
Right, |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 64 |
pub struct Layout { |
| 65 |
direction: Direction, |
| 66 |
margin: Margin, |
| 67 |
constraints: Vec<Constraint>, |
| 68 |
} |
| 69 |
|
| 70 |
thread_local! { |
| 71 |
static LAYOUT_CACHE: RefCell<HashMap<(Rect, Layout), Vec<Rect>>> = RefCell::new(HashMap::new()); |
| 72 |
} |
| 73 |
|
| 74 |
impl Default for Layout { |
| 75 |
fn default() -> Layout { |
| 76 |
Layout { |
| 77 |
direction: Direction::Vertical, |
| 78 |
margin: Margin::none(), |
| 79 |
constraints: Vec::new(), |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
impl Layout { |
| 85 |
|
| 86 |
pub fn constraints<C>(mut self, constraints: C) -> Layout |
| 87 |
where |
| 88 |
C: Into<Vec<Constraint>>, |
| 89 |
{ |
| 90 |
self.constraints = constraints.into(); |
| 91 |
self |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
pub const fn margin(mut self, margin: u16) -> Layout { |
| 96 |
self.margin = Margin::all(margin); |
| 97 |
self |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
pub const fn horizontal_margin(mut self, horizontal: u16) -> Layout { |
| 102 |
self.margin.horizontal = horizontal; |
| 103 |
self |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
pub const fn vertical_margin(mut self, vertical: u16) -> Layout { |
| 108 |
self.margin.vertical = vertical; |
| 109 |
self |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
pub const fn direction(mut self, direction: Direction) -> Layout { |
| 114 |
self.direction = direction; |
| 115 |
self |
| 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 |
pub fn split(&self, area: Rect) -> Vec<Rect> { |
| 180 |
|
| 181 |
LAYOUT_CACHE.with(|c| { |
| 182 |
c.borrow_mut() |
| 183 |
.entry((area, self.clone())) |
| 184 |
.or_insert_with(|| split(area, self)) |
| 185 |
.clone() |
| 186 |
}) |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
fn split(area: Rect, layout: &Layout) -> Vec<Rect> { |
| 191 |
let mut solver = Solver::new(); |
| 192 |
let mut vars: HashMap<Variable, (usize, usize)> = HashMap::new(); |
| 193 |
let elements = layout |
| 194 |
.constraints |
| 195 |
.iter() |
| 196 |
.map(|_| Element::new()) |
| 197 |
.collect::<Vec<Element>>(); |
| 198 |
let mut results = layout |
| 199 |
.constraints |
| 200 |
.iter() |
| 201 |
.map(|_| Rect::default()) |
| 202 |
.collect::<Vec<Rect>>(); |
| 203 |
|
| 204 |
let dest_area = area.inner(layout.margin); |
| 205 |
for (i, e) in elements.iter().enumerate() { |
| 206 |
vars.insert(e.x, (i, 0)); |
| 207 |
vars.insert(e.y, (i, 1)); |
| 208 |
vars.insert(e.width, (i, 2)); |
| 209 |
vars.insert(e.height, (i, 3)); |
| 210 |
} |
| •211 |
let mut ccs: Vec<KasuariConstraint> = |
| 212 |
Vec::with_capacity(elements.len() * 4 + layout.constraints.len() * 6); |
| 213 |
for elt in &elements { |
| •214 |
ccs.push(elt.width | GE(Strength::REQUIRED) | 0f64); |
| 215 |
ccs.push(elt.height | GE(Strength::REQUIRED) | 0f64); |
| 216 |
ccs.push(elt.left() | GE(Strength::REQUIRED) | f64::from(dest_area.left())); |
| 217 |
ccs.push(elt.top() | GE(Strength::REQUIRED) | f64::from(dest_area.top())); |
| 218 |
ccs.push(elt.right() | LE(Strength::REQUIRED) | f64::from(dest_area.right())); |
| 219 |
ccs.push(elt.bottom() | LE(Strength::REQUIRED) | f64::from(dest_area.bottom())); |
| 220 |
} |
| 221 |
if let Some(first) = elements.first() { |
| 222 |
ccs.push(match layout.direction { |
| •223 |
Direction::Horizontal => first.left() | EQ(Strength::REQUIRED) | f64::from(dest_area.left()), |
| 224 |
Direction::Vertical => first.top() | EQ(Strength::REQUIRED) | f64::from(dest_area.top()), |
| 225 |
}); |
| 226 |
} |
| 227 |
if let Some(last) = elements.last() { |
| 228 |
ccs.push(match layout.direction { |
| •229 |
Direction::Horizontal => last.right() | EQ(Strength::REQUIRED) | f64::from(dest_area.right()), |
| 230 |
Direction::Vertical => last.bottom() | EQ(Strength::REQUIRED) | f64::from(dest_area.bottom()), |
| 231 |
}); |
| 232 |
} |
| 233 |
match layout.direction { |
| 234 |
Direction::Horizontal => { |
| 235 |
for pair in elements.windows(2) { |
| •236 |
ccs.push((pair[0].x + pair[0].width) | EQ(Strength::REQUIRED) | pair[1].x); |
| 237 |
} |
| 238 |
for (i, size) in layout.constraints.iter().enumerate() { |
| •239 |
ccs.push(elements[i].y | EQ(Strength::REQUIRED) | f64::from(dest_area.y)); |
| 240 |
ccs.push(elements[i].height | EQ(Strength::REQUIRED) | f64::from(dest_area.height)); |
| 241 |
ccs.push(match *size { |
| •242 |
Constraint::Length(v) => elements[i].width | EQ(Strength::WEAK) | f64::from(v), |
| 243 |
Constraint::Percentage(v) => { |
| •244 |
elements[i].width | EQ(Strength::WEAK) | (f64::from(v * dest_area.width) / 100.0) |
| 245 |
} |
| 246 |
Constraint::Ratio(n, d) => { |
| 247 |
elements[i].width |
| •248 |
| EQ(Strength::WEAK) |
| 249 |
| (f64::from(dest_area.width) * f64::from(n) / f64::from(d)) |
| 250 |
} |
| •251 |
Constraint::Min(v) => elements[i].width | GE(Strength::WEAK) | f64::from(v), |
| 252 |
Constraint::Max(v) => elements[i].width | LE(Strength::WEAK) | f64::from(v), |
| 253 |
}); |
| 254 |
} |
| 255 |
} |
| 256 |
Direction::Vertical => { |
| 257 |
for pair in elements.windows(2) { |
| •258 |
ccs.push((pair[0].y + pair[0].height) | EQ(Strength::REQUIRED) | pair[1].y); |
| 259 |
} |
| 260 |
for (i, size) in layout.constraints.iter().enumerate() { |
| •261 |
ccs.push(elements[i].x | EQ(Strength::REQUIRED) | f64::from(dest_area.x)); |
| 262 |
ccs.push(elements[i].width | EQ(Strength::REQUIRED) | f64::from(dest_area.width)); |
| 263 |
ccs.push(match *size { |
| •264 |
Constraint::Length(v) => elements[i].height | EQ(Strength::WEAK) | f64::from(v), |
| 265 |
Constraint::Percentage(v) => { |
| •266 |
elements[i].height | EQ(Strength::WEAK) | (f64::from(v * dest_area.height) / 100.0) |
| 267 |
} |
| 268 |
Constraint::Ratio(n, d) => { |
| 269 |
elements[i].height |
| •270 |
| EQ(Strength::WEAK) |
| 271 |
| (f64::from(dest_area.height) * f64::from(n) / f64::from(d)) |
| 272 |
} |
| •273 |
Constraint::Min(v) => elements[i].height | GE(Strength::WEAK) | f64::from(v), |
| 274 |
Constraint::Max(v) => elements[i].height | LE(Strength::WEAK) | f64::from(v), |
| 275 |
}); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| •279 |
solver.add_constraints(ccs).unwrap(); |
| 280 |
for &(var, value) in solver.fetch_changes() { |
| 281 |
let (index, attr) = vars[&var]; |
| 282 |
let value = if value.is_sign_negative() { |
| 283 |
0 |
| 284 |
} else { |
| 285 |
value as u16 |
| 286 |
}; |
| 287 |
match attr { |
| 288 |
0 => { |
| 289 |
results[index].x = value; |
| 290 |
} |
| 291 |
1 => { |
| 292 |
results[index].y = value; |
| 293 |
} |
| 294 |
2 => { |
| 295 |
results[index].width = value; |
| 296 |
} |
| 297 |
3 => { |
| 298 |
results[index].height = value; |
| 299 |
} |
| 300 |
_ => {} |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
if let Some(last) = results.last_mut() { |
| 306 |
match layout.direction { |
| 307 |
Direction::Vertical => { |
| 308 |
last.height = dest_area.bottom() - last.y; |
| 309 |
} |
| 310 |
Direction::Horizontal => { |
| 311 |
last.width = dest_area.right() - last.x; |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
results |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
struct Element { |
| 320 |
x: Variable, |
| 321 |
y: Variable, |
| 322 |
width: Variable, |
| 323 |
height: Variable, |
| 324 |
} |
| 325 |
|
| 326 |
impl Element { |
| 327 |
fn new() -> Element { |
| 328 |
Element { |
| 329 |
x: Variable::new(), |
| 330 |
y: Variable::new(), |
| 331 |
width: Variable::new(), |
| 332 |
height: Variable::new(), |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
fn left(&self) -> Variable { |
| 337 |
self.x |
| 338 |
} |
| 339 |
|
| 340 |
fn top(&self) -> Variable { |
| 341 |
self.y |
| 342 |
} |
| 343 |
|
| 344 |
fn right(&self) -> Expression { |
| 345 |
self.x + self.width |
| 346 |
} |
| 347 |
|
| 348 |
fn bottom(&self) -> Expression { |
| 349 |
self.y + self.height |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
#[cfg(test)] |
| 354 |
mod tests { |
| 355 |
use super::*; |
| 356 |
|
| 357 |
#[test] |
| 358 |
fn test_vertical_split_by_height() { |
| 359 |
let target = Rect { |
| 360 |
x: 2, |
| 361 |
y: 2, |
| 362 |
width: 10, |
| 363 |
height: 10, |
| 364 |
}; |
| 365 |
|
| 366 |
let chunks = Layout::default() |
| 367 |
.direction(Direction::Vertical) |
| 368 |
.constraints( |
| 369 |
[ |
| 370 |
Constraint::Percentage(10), |
| 371 |
Constraint::Max(5), |
| 372 |
Constraint::Min(1), |
| 373 |
] |
| 374 |
.as_ref(), |
| 375 |
) |
| 376 |
.split(target); |
| 377 |
|
| 378 |
assert_eq!(target.height, chunks.iter().map(|r| r.height).sum::<u16>()); |
| 379 |
chunks.windows(2).for_each(|w| assert!(w[0].y <= w[1].y)); |
| 380 |
} |
| 381 |
} |