Compare commits
3 Commits
17c436d944
...
dab6cf298a
| Author | SHA1 | Date | |
|---|---|---|---|
| dab6cf298a | |||
| 38d896d44d | |||
| 7b54aaf3c4 |
@@ -4,12 +4,12 @@ My experimental attempt at a rust ui library (also my first ui library).
|
|||||||
|
|
||||||
It's currently designed around using retained data structures (widgets), rather than diffing generated trees from data like xilem or iced. This is an experiment and I'm not sure if it's a good idea or not.
|
It's currently designed around using retained data structures (widgets), rather than diffing generated trees from data like xilem or iced. This is an experiment and I'm not sure if it's a good idea or not.
|
||||||
|
|
||||||
There's a `main.rs` that runs a testing window, so you can just `cargo run` to see it working.
|
Examples are in `examples`, eg. `cargo run --example tabs`.
|
||||||
|
|
||||||
Goals, in general order:
|
Goals, in general order:
|
||||||
1. does what I want it to (text, images, video, animations)
|
1. does what I want it to (text, images, video, animations)
|
||||||
2. very easy to use ignoring ergonomic ref counting
|
2. very easy to use ignoring ergonomic ref counting
|
||||||
3. reasonably fast / efficient (a lot faster than electron, save battery life)
|
3. reasonably fast / efficient (a lot faster than electron, save battery life, try to beat iced and xilem)
|
||||||
|
|
||||||
## dev details
|
## dev details
|
||||||
|
|
||||||
|
|||||||
@@ -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