selector
This commit is contained in:
@@ -5,6 +5,7 @@ mod ptr;
|
|||||||
mod rect;
|
mod rect;
|
||||||
mod text;
|
mod text;
|
||||||
mod trait_fns;
|
mod trait_fns;
|
||||||
|
mod selector;
|
||||||
|
|
||||||
pub use image::*;
|
pub use image::*;
|
||||||
pub use mask::*;
|
pub use mask::*;
|
||||||
@@ -13,3 +14,4 @@ pub use ptr::*;
|
|||||||
pub use rect::*;
|
pub use rect::*;
|
||||||
pub use text::*;
|
pub use text::*;
|
||||||
pub use trait_fns::*;
|
pub use trait_fns::*;
|
||||||
|
pub use selector::*;
|
||||||
|
|||||||
48
src/widget/selector.rs
Normal file
48
src/widget/selector.rs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
use std::hash::Hash;
|
||||||
|
|
||||||
|
use iris_core::util::HashMap;
|
||||||
|
|
||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
pub struct WidgetSelector<T> {
|
||||||
|
current: (T, StrongWidget),
|
||||||
|
map: HashMap<T, StrongWidget>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Hash + Eq> WidgetSelector<T> {
|
||||||
|
pub fn new(key: T, widget: StrongWidget) -> Self {
|
||||||
|
Self {
|
||||||
|
current: (key, widget),
|
||||||
|
map: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set(&mut self, key: T, widget: StrongWidget) {
|
||||||
|
self.map.insert(key, widget);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn select(&mut self, key: T) -> bool {
|
||||||
|
if let Some(val) = self.map.remove(&key) {
|
||||||
|
let mut new = (key, val);
|
||||||
|
std::mem::swap(&mut new, &mut self.current);
|
||||||
|
self.map.insert(new.0, new.1);
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: 'static> Widget for WidgetSelector<T> {
|
||||||
|
fn draw(&mut self, painter: &mut Painter) {
|
||||||
|
painter.widget(&self.current.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||||
|
ctx.width(&self.current.1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||||
|
ctx.height(&self.current.1)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user