mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-13 20:53:07 +00:00
Add StackView::move_layer
This commit is contained in:
parent
bd9b6a695b
commit
8da659367d
@ -82,7 +82,7 @@ pub use self::select_view::SelectView;
|
|||||||
pub use self::shadow_view::ShadowView;
|
pub use self::shadow_view::ShadowView;
|
||||||
pub use self::sized_view::SizedView;
|
pub use self::sized_view::SizedView;
|
||||||
pub use self::slider_view::SliderView;
|
pub use self::slider_view::SliderView;
|
||||||
pub use self::stack_view::StackView;
|
pub use self::stack_view::{StackView, LayerPosition};
|
||||||
pub use self::text_area::TextArea;
|
pub use self::text_area::TextArea;
|
||||||
pub use self::text_view::TextView;
|
pub use self::text_view::TextView;
|
||||||
pub use self::tracked_view::TrackedView;
|
pub use self::tracked_view::TrackedView;
|
||||||
|
@ -12,6 +12,7 @@ use views::{Layer, ShadowView};
|
|||||||
/// Simple stack of views.
|
/// Simple stack of views.
|
||||||
/// Only the top-most view is active and can receive input.
|
/// Only the top-most view is active and can receive input.
|
||||||
pub struct StackView {
|
pub struct StackView {
|
||||||
|
// Store layers from back to front.
|
||||||
layers: Vec<Child>,
|
layers: Vec<Child>,
|
||||||
last_size: Vec2,
|
last_size: Vec2,
|
||||||
}
|
}
|
||||||
@ -21,6 +22,15 @@ enum Placement {
|
|||||||
Fullscreen,
|
Fullscreen,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Identifies a layer in a `StackView`.
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum LayerPosition {
|
||||||
|
/// Starts from the back (bottom) of the stack.
|
||||||
|
FromBack(usize),
|
||||||
|
/// Starts from the front (top) of the stack.
|
||||||
|
FromFront(usize),
|
||||||
|
}
|
||||||
|
|
||||||
impl Placement {
|
impl Placement {
|
||||||
pub fn compute_offset<S, A, P>(
|
pub fn compute_offset<S, A, P>(
|
||||||
&self, size: S, available: A, parent: P
|
&self, size: S, available: A, parent: P
|
||||||
@ -236,6 +246,44 @@ impl StackView {
|
|||||||
pub fn layer_sizes(&self) -> Vec<Vec2> {
|
pub fn layer_sizes(&self) -> Vec<Vec2> {
|
||||||
self.layers.iter().map(|layer| layer.size).collect()
|
self.layers.iter().map(|layer| layer.size).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_index(&self, pos: LayerPosition) -> usize {
|
||||||
|
match pos {
|
||||||
|
LayerPosition::FromBack(i) => i,
|
||||||
|
LayerPosition::FromFront(i) => self.layers.len() - i - 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Moves a layer to a new position in the stack.
|
||||||
|
///
|
||||||
|
/// This only affects the elevation of a layer (whether it is drawn over
|
||||||
|
/// or under other views).
|
||||||
|
pub fn move_layer(&mut self, from: LayerPosition, to: LayerPosition) {
|
||||||
|
// Convert relative positions to indices in the array
|
||||||
|
let from_i = self.get_index(from);
|
||||||
|
let to_i = self.get_index(to);
|
||||||
|
|
||||||
|
let removed = self.layers.remove(from_i);
|
||||||
|
|
||||||
|
// Shift the position if needed
|
||||||
|
let to_i = if to_i > from_i {
|
||||||
|
to_i - 1
|
||||||
|
} else {
|
||||||
|
to_i
|
||||||
|
};
|
||||||
|
|
||||||
|
self.layers.insert(to_i, removed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Brings the given view to the front of the stack.
|
||||||
|
pub fn move_to_front(&mut self, layer: LayerPosition) {
|
||||||
|
self.move_layer(layer, LayerPosition::FromFront(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Pushes the given view to the back of the stack.
|
||||||
|
pub fn move_to_back(&mut self, layer: LayerPosition) {
|
||||||
|
self.move_layer(layer, LayerPosition::FromBack(0));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct StackPositionIterator<R: Deref<Target = Child>, I: Iterator<Item = R>> {
|
struct StackPositionIterator<R: Deref<Target = Child>, I: Iterator<Item = R>> {
|
||||||
|
Loading…
Reference in New Issue
Block a user