selector
This commit is contained in:
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