strong & weak widgets

This commit is contained in:
iris committed 2025-12-11 07:16:06 -05:00
1 parent a85e129026
commit 36668c82f4
19 files changed
+293 -294

No files matched your search

+44 -29
View File
@@ -1,3 +1,5 @@
use std::{cell::RefCell, rc::Rc};
use cosmic_text::Family;
use iris::prelude::*;
use len_fns::*;
@@ -8,7 +10,7 @@ fn main() {
}
pub struct Client {
info: WidgetHandle<Text>,
info: WidgetView<Text>,
}
event_ctx!(Client);
@@ -37,7 +39,8 @@ impl DefaultAppState for Client {
.width(rest(3)),
)
.span(Dir::RIGHT)
.add(ui);
.add(ui)
.handles();
let span_test = (
rrect.color(Color::GREEN).width(100),
@@ -50,8 +53,7 @@ impl DefaultAppState for Client {
.span(Dir::LEFT)
.add(ui);
let span_add = Span::empty(Dir::RIGHT).add(ui);
let span_add_ = span_add.clone();
let span_add = Span::empty(Dir::RIGHT).add(ui).handles();
let add_button = rect(Color::LIME)
.radius(30)
@@ -60,21 +62,20 @@ impl DefaultAppState for Client {
.ui
.add(image(include_bytes!("assets/sungals.png")).center())
.any();
ctx.ui[&span_add_].children.push(child);
ctx.ui[span_add.1].children.push(child);
})
.sized((150, 150))
.align(Align::BOT_RIGHT);
let span_add_ = span_add.clone();
let del_button = rect(Color::RED)
.radius(30)
.on(CursorSense::click(), move |ctx| {
ctx.ui[&span_add_].children.pop();
ctx.ui[span_add.1].children.pop();
})
.sized((150, 150))
.align(Align::BOT_LEFT);
let span_add_test = (span_add, add_button, del_button).stack().add(ui);
let span_add_test = (span_add.0, add_button, del_button).stack().add(ui);
let btext = |content| wtext(content).size(30);
@@ -99,8 +100,8 @@ impl DefaultAppState for Client {
.span(Dir::DOWN)
.add(ui);
let texts = Span::empty(Dir::DOWN).gap(10).add(ui);
let msg_area = texts.clone().scroll().masked().background(rect(Color::SKY));
let texts = Span::empty(Dir::DOWN).gap(10).add(ui).handles();
let msg_area = texts.0.scroll().masked().background(rect(Color::SKY));
let add_text = wtext("add")
.editable(false)
.text_align(Align::LEFT)
@@ -115,18 +116,19 @@ impl DefaultAppState for Client {
.wrap(true)
.attr::<Selectable>(());
let msg_box = text.background(rect(Color::WHITE.darker(0.5))).add(ctx.ui);
ctx.ui[&texts].children.push(msg_box.any());
ctx.ui[texts.1].children.push(msg_box.any());
})
.add(ui);
.add(ui)
.handles();
let text_edit_scroll = (
msg_area.height(rest(1)),
(
Rect::new(Color::WHITE.darker(0.9)),
(
add_text.clone().width(rest(1)),
add_text.0.width(rest(1)),
Rect::new(Color::GREEN)
.on(CursorSense::click(), move |ctx| {
ctx.ui.run_event(ctx.state, &add_text, Submit, ());
ctx.ui.run_event(ctx.state, add_text.1, Submit, ());
})
.sized((40, 40)),
)
@@ -141,13 +143,26 @@ impl DefaultAppState for Client {
.span(Dir::DOWN)
.add(ui);
let main = pad_test.clone().pad(10).add(ui);
let main = WidgetPtr::new().add(ui).handles();
let switch_button = |color, to: WidgetHandle, label| {
let main_ = main.clone();
let vals = Rc::new(RefCell::new((0, Vec::new())));
let mut switch_button = |color, to: WidgetHandle, label| {
let vec = &mut vals.borrow_mut().1;
let i = vec.len();
if vec.is_empty() {
vec.push(None);
ui[main.1].set(to);
} else {
vec.push(Some(to));
}
let vals = vals.clone();
let rect = rect(color)
.on(CursorSense::click(), move |ctx| {
ctx.ui[&main_.clone()].inner = to.clone();
let (prev, vec) = &mut *vals.borrow_mut();
if let Some(h) = vec[i].take() {
vec[*prev] = ctx.ui[main.1].replace(h);
*prev = i;
}
ctx.ui[ctx.id].color = color.darker(0.3);
})
.on(
@@ -163,26 +178,26 @@ impl DefaultAppState for Client {
};
let tabs = (
switch_button(Color::RED, pad_test.any(), "pad"),
switch_button(Color::GREEN, span_test.any(), "span"),
switch_button(Color::BLUE, span_add_test.any(), "image span"),
switch_button(Color::MAGENTA, text_test.any(), "text layout"),
switch_button(Color::RED, pad_test.0, "pad"),
switch_button(Color::GREEN, span_test, "span"),
switch_button(Color::BLUE, span_add_test, "image span"),
switch_button(Color::MAGENTA, text_test, "text layout"),
switch_button(
Color::YELLOW.mul_rgb(0.5),
text_edit_scroll.any(),
text_edit_scroll,
"text edit scroll",
),
)
.span(Dir::RIGHT);
let info = wtext("").add(ui);
let info_sect = info.clone().pad(10).align(Align::RIGHT);
let info = wtext("").add(ui).handles();
let info_sect = info.0.pad(10).align(Align::RIGHT);
((tabs.height(40), main).span(Dir::DOWN), info_sect)
((tabs.height(40), main.0.pad(10)).span(Dir::DOWN), info_sect)
.stack()
.set_root(ui);
Self { info }
Self { info: info.1 }
}
fn window_event(&mut self, _: WindowEvent, ui: &mut Ui, state: &UiState) {
@@ -192,8 +207,8 @@ impl DefaultAppState for Client {
ui.active_widgets(),
state.renderer.ui.view_count()
);
if new != *ui[&self.info].content {
*ui[&self.info].content = new;
if new != *ui[self.info].content {
*ui[self.info].content = new;
}
if ui.needs_redraw() {
state.window.request_redraw();