2016-03-15 22:37:57 +00:00
|
|
|
use view::{View, SizeRequest, DimensionRequest};
|
2015-06-08 03:58:10 +00:00
|
|
|
use vec::Vec2;
|
|
|
|
use printer::Printer;
|
2015-06-08 19:23:36 +00:00
|
|
|
use orientation::Orientation;
|
2016-03-15 22:37:57 +00:00
|
|
|
use event::{Event, EventResult, Key};
|
2015-06-08 03:58:10 +00:00
|
|
|
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Arranges its children linearly according to its orientation.
|
2015-06-08 03:58:10 +00:00
|
|
|
pub struct LinearLayout {
|
|
|
|
children: Vec<Child>,
|
|
|
|
orientation: Orientation,
|
2015-06-08 22:11:44 +00:00
|
|
|
focus: usize,
|
2015-06-08 03:58:10 +00:00
|
|
|
}
|
|
|
|
|
2015-06-08 22:38:10 +00:00
|
|
|
struct Child {
|
|
|
|
view: Box<View>,
|
|
|
|
size: Vec2,
|
|
|
|
weight: usize,
|
|
|
|
}
|
2015-06-08 03:58:10 +00:00
|
|
|
|
|
|
|
impl LinearLayout {
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Creates a new layout with the given orientation.
|
2015-06-08 03:58:10 +00:00
|
|
|
pub fn new(orientation: Orientation) -> Self {
|
|
|
|
LinearLayout {
|
|
|
|
children: Vec::new(),
|
|
|
|
orientation: orientation,
|
2015-06-08 22:11:44 +00:00
|
|
|
focus: 0,
|
2015-06-08 03:58:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Modifies the weight of the last child added.
|
|
|
|
///
|
|
|
|
/// It is an error to call this before adding a child (and it will panic).
|
2015-06-08 03:58:10 +00:00
|
|
|
pub fn weight(mut self, weight: usize) -> Self {
|
|
|
|
self.children.last_mut().unwrap().weight = weight;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Adds a child to the layout.
|
2015-06-08 03:58:10 +00:00
|
|
|
pub fn child<V: View + 'static>(mut self, view: V) -> Self {
|
|
|
|
self.children.push(Child {
|
|
|
|
view: Box::new(view),
|
|
|
|
size: Vec2::zero(),
|
|
|
|
weight: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Creates a new vertical layout.
|
2015-06-08 03:58:10 +00:00
|
|
|
pub fn vertical() -> Self {
|
|
|
|
LinearLayout::new(Orientation::Vertical)
|
|
|
|
}
|
2015-06-08 22:38:10 +00:00
|
|
|
|
|
|
|
/// Creates a new horizontal layout.
|
2015-06-08 03:58:10 +00:00
|
|
|
pub fn horizontal() -> Self {
|
|
|
|
LinearLayout::new(Orientation::Horizontal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Returns the index of the maximum element.
|
|
|
|
/// WTF isn't it part of standard library??
|
2015-06-08 03:58:10 +00:00
|
|
|
fn find_max(list: &Vec<usize>) -> usize {
|
|
|
|
let mut max_value = 0;
|
|
|
|
let mut max = 0;
|
2016-03-15 22:37:57 +00:00
|
|
|
for (i, &x) in list.iter().enumerate() {
|
2015-06-08 03:58:10 +00:00
|
|
|
if x > max_value {
|
|
|
|
max_value = x;
|
|
|
|
max = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
max
|
|
|
|
}
|
|
|
|
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Given a total number of points and a list of weights,
|
|
|
|
/// try to share the points according to the weight,
|
|
|
|
/// rounding properly and conserving the sum of points.
|
2015-06-08 03:58:10 +00:00
|
|
|
fn share(total: usize, weights: Vec<usize>) -> Vec<usize> {
|
2015-06-08 22:38:10 +00:00
|
|
|
// It first give a base value to everyone, which is their truncated share.
|
|
|
|
// Then, it gives the rest to the most deserving.
|
2016-03-15 22:37:57 +00:00
|
|
|
if weights.len() == 0 {
|
|
|
|
return Vec::new();
|
|
|
|
}
|
2015-06-08 03:58:10 +00:00
|
|
|
|
2016-03-15 22:37:57 +00:00
|
|
|
let sum_weight = weights.iter().fold(0, |a, b| a + b);
|
2015-06-08 03:58:10 +00:00
|
|
|
if sum_weight == 0 {
|
|
|
|
return (0..weights.len()).map(|_| 0).collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut base = Vec::with_capacity(weights.len());
|
|
|
|
let mut rest = Vec::with_capacity(weights.len());
|
|
|
|
let mut extra = total;
|
|
|
|
|
|
|
|
for weight in weights.iter() {
|
|
|
|
let b = total * weight / sum_weight;
|
|
|
|
extra -= b;
|
|
|
|
base.push(b);
|
2016-03-15 22:37:57 +00:00
|
|
|
rest.push(total * weight - b * sum_weight);
|
2015-06-08 03:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: better to sort (base,rest) as one array and pick the extra first.
|
|
|
|
for _ in 0..extra {
|
|
|
|
let i = find_max(&rest);
|
|
|
|
rest[i] = 0;
|
|
|
|
base[i] += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
base
|
|
|
|
}
|
|
|
|
|
|
|
|
impl View for LinearLayout {
|
|
|
|
fn draw(&mut self, printer: &Printer) {
|
|
|
|
// Use pre-computed sizes
|
|
|
|
let mut offset = Vec2::zero();
|
2016-03-15 22:37:57 +00:00
|
|
|
for (i, child) in self.children.iter_mut().enumerate() {
|
2015-06-08 22:47:09 +00:00
|
|
|
child.view.draw(&printer.sub_printer(offset, child.size, i == self.focus));
|
2015-06-08 03:58:10 +00:00
|
|
|
|
|
|
|
*self.orientation.get_ref(&mut offset) += self.orientation.get(&child.size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(&mut self, size: Vec2) {
|
|
|
|
// Compute the very minimal required size
|
2016-03-15 22:37:57 +00:00
|
|
|
let req = SizeRequest {
|
2015-06-08 03:58:10 +00:00
|
|
|
w: DimensionRequest::AtMost(size.x),
|
|
|
|
h: DimensionRequest::AtMost(size.y),
|
|
|
|
};
|
2016-03-15 22:37:57 +00:00
|
|
|
let min_sizes: Vec<Vec2> = self.children
|
|
|
|
.iter()
|
|
|
|
.map(|child| child.view.get_min_size(req))
|
|
|
|
.collect();
|
2015-06-08 03:58:10 +00:00
|
|
|
let min_size = self.orientation.stack(min_sizes.iter());
|
|
|
|
|
|
|
|
// Emulate 'non-strict inequality' on integers
|
|
|
|
// (default comparison on Vec2 is strict)
|
2016-03-15 22:37:57 +00:00
|
|
|
if !(min_size < size + (1, 1)) {
|
2015-06-08 03:58:10 +00:00
|
|
|
// Error! Not enough space! Emergency procedures!
|
2016-03-15 22:37:57 +00:00
|
|
|
return;
|
2015-06-08 03:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now share this extra space among everyone
|
|
|
|
|
|
|
|
let extras = {
|
|
|
|
let extra = size - min_size;
|
|
|
|
let space = self.orientation.get(&extra);
|
2016-03-15 22:37:57 +00:00
|
|
|
share(space,
|
|
|
|
self.children.iter().map(|child| child.weight).collect())
|
2015-06-08 03:58:10 +00:00
|
|
|
};
|
|
|
|
|
2015-06-08 19:23:36 +00:00
|
|
|
|
2016-03-15 22:37:57 +00:00
|
|
|
for (child, (child_size, extra)) in self.children
|
|
|
|
.iter_mut()
|
|
|
|
.zip(min_sizes.iter().zip(extras.iter())) {
|
2015-06-08 03:58:10 +00:00
|
|
|
let mut child_size = *child_size;
|
|
|
|
*self.orientation.get_ref(&mut child_size) += *extra;
|
|
|
|
*self.orientation.swap().get_ref(&mut child_size) = self.orientation.swap().get(&size);
|
|
|
|
child.size = child_size;
|
|
|
|
child.view.layout(child_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_min_size(&self, req: SizeRequest) -> Vec2 {
|
|
|
|
// First, make a naive scenario: everything will work fine.
|
2016-03-15 22:37:57 +00:00
|
|
|
let sizes: Vec<Vec2> = self.children
|
|
|
|
.iter()
|
|
|
|
.map(|view| view.view.get_min_size(req))
|
|
|
|
.collect();
|
2015-06-08 03:58:10 +00:00
|
|
|
self.orientation.stack(sizes.iter())
|
|
|
|
|
|
|
|
|
|
|
|
// Did it work? Champagne!
|
|
|
|
|
|
|
|
|
|
|
|
// Ok, so maybe it didn't.
|
|
|
|
// Last chance: did someone lie about his needs?
|
|
|
|
// Could we squash him a little?
|
|
|
|
|
|
|
|
// Find out who's fluid, if any.
|
|
|
|
}
|
2015-06-08 22:11:44 +00:00
|
|
|
|
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
|
|
|
match self.children[self.focus].view.on_event(event) {
|
|
|
|
EventResult::Ignored => match event {
|
|
|
|
Event::KeyEvent(Key::Tab) if self.focus > 0 => {
|
|
|
|
self.focus -= 1;
|
|
|
|
EventResult::Consumed(None)
|
2016-03-15 22:37:57 +00:00
|
|
|
}
|
|
|
|
Event::KeyEvent(Key::ShiftTab) if self.focus + 1 < self.children.len() => {
|
2015-06-08 22:11:44 +00:00
|
|
|
self.focus += 1;
|
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
2016-03-15 22:37:57 +00:00
|
|
|
Event::KeyEvent(Key::Left) if self.orientation == Orientation::Horizontal &&
|
|
|
|
self.focus > 0 => {
|
2015-06-08 22:11:44 +00:00
|
|
|
self.focus -= 1;
|
|
|
|
EventResult::Consumed(None)
|
2016-03-15 22:37:57 +00:00
|
|
|
}
|
|
|
|
Event::KeyEvent(Key::Up) if self.orientation == Orientation::Vertical &&
|
|
|
|
self.focus > 0 => {
|
2015-06-08 22:11:44 +00:00
|
|
|
self.focus -= 1;
|
|
|
|
EventResult::Consumed(None)
|
2016-03-15 22:37:57 +00:00
|
|
|
}
|
|
|
|
Event::KeyEvent(Key::Right) if self.orientation == Orientation::Horizontal &&
|
|
|
|
self.focus + 1 < self.children.len() => {
|
2015-06-08 22:11:44 +00:00
|
|
|
self.focus += 1;
|
|
|
|
EventResult::Consumed(None)
|
2016-03-15 22:37:57 +00:00
|
|
|
}
|
|
|
|
Event::KeyEvent(Key::Down) if self.orientation == Orientation::Vertical &&
|
|
|
|
self.focus + 1 < self.children.len() => {
|
2015-06-08 22:11:44 +00:00
|
|
|
self.focus += 1;
|
|
|
|
EventResult::Consumed(None)
|
2016-03-15 22:37:57 +00:00
|
|
|
}
|
2015-06-08 22:11:44 +00:00
|
|
|
_ => EventResult::Ignored,
|
|
|
|
},
|
|
|
|
res => res,
|
|
|
|
}
|
|
|
|
}
|
2015-06-08 03:58:10 +00:00
|
|
|
}
|