initial global widget store

This commit is contained in:
iris committed 2025-12-10 01:42:39 -05:00
1 parent 7f4846a2d3
commit 6156c66a20
31 files changed
+536 -352

No files matched your search

+31 -18
View File
@@ -51,7 +51,7 @@ impl DefaultAppState for Client {
.add(ui);
let span_add = Span::empty(Dir::RIGHT).add(ui);
let span_add_ = span_add.clone();
let span_add_ = span_add.weak();
let add_button = rect(Color::LIME)
.radius(30)
@@ -64,7 +64,6 @@ impl DefaultAppState for Client {
.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 |_| {
@@ -99,7 +98,8 @@ impl DefaultAppState for Client {
.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_ = texts.weak();
let msg_area = texts.scroll().masked().background(rect(Color::SKY));
let add_text = wtext("add")
.editable(false)
.text_align(Align::LEFT)
@@ -114,18 +114,19 @@ impl DefaultAppState for Client {
.wrap(true)
.attr::<Selectable>(());
let msg_box = text.background(rect(Color::WHITE.darker(0.5))).add(ctx.ui);
texts.get_mut().children.push(msg_box);
texts_.get_mut().children.push(msg_box);
})
.add(ui);
let add_text_ = add_text.weak();
let text_edit_scroll = (
msg_area.height(rest(1)),
(
Rect::new(Color::WHITE.darker(0.9)),
(
add_text.clone().width(rest(1)),
add_text.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_, Submit, ());
})
.sized((40, 40)),
)
@@ -140,13 +141,24 @@ impl DefaultAppState for Client {
.span(Dir::DOWN)
.add(ui);
let main = pad_test.clone().pad(10).add(ui);
let main = pad_test.pad(10).add(ui);
let main_ = main.weak();
let switch_button = |color, to: WidgetRef, label| {
let main_ = main.clone();
let tab_handles = Handle::from((0, Vec::new()));
let switch_button = |color, to: Option<WidgetHandle>, label| {
let tab_handles = tab_handles.clone();
let i = tab_handles.get().1.len();
tab_handles.get_mut().1.push(to);
let rect = rect(color)
.on(CursorSense::click(), move |ctx| {
main_.get_mut().inner = to.clone();
let (prev, all) = &mut *tab_handles.get_mut();
if let Some(to) = &mut all[i] {
let mut main = main_.get_mut();
std::mem::swap(&mut main.inner, to);
all.swap(*prev, i);
*prev = i;
}
ctx.widget.get_mut().color = color.darker(0.3);
})
.on(
@@ -162,20 +174,21 @@ impl DefaultAppState for Client {
};
let tabs = (
switch_button(Color::RED, pad_test, "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::RED, None, "pad"),
switch_button(Color::GREEN, Some(span_test), "span"),
switch_button(Color::BLUE, Some(span_add_test), "image span"),
switch_button(Color::MAGENTA, Some(text_test), "text layout"),
switch_button(
Color::YELLOW.mul_rgb(0.5),
text_edit_scroll,
Some(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);
let info = info_.weak();
let info_sect = info_.pad(10).align(Align::RIGHT);
((tabs.height(40), main).span(Dir::DOWN), info_sect)
.stack()
@@ -187,7 +200,7 @@ impl DefaultAppState for Client {
fn window_event(&mut self, _: WindowEvent, ui: &mut Ui, state: &UiState) {
let new = format!(
"widgets: {}\nactive:{}\nviews: {}",
ui.num_widgets(),
total_widgets(),
ui.active_widgets(),
state.renderer.ui.view_count()
);
+14 -20
View File
@@ -1,4 +1,4 @@
use crate::{prelude::*, default::UiState};
use crate::{default::UiState, prelude::*};
use std::time::{Duration, Instant};
use winit::dpi::{LogicalPosition, LogicalSize};
@@ -7,21 +7,16 @@ pub struct Selector;
impl<W: 'static> WidgetAttr<W> for Selector {
type Input = WidgetRef<TextEdit>;
fn run(ui: &mut Ui, container: &WidgetRef<W>, id: Self::Input) {
let container = container.clone();
ui.register_event(
&container.clone(),
CursorSense::click_or_drag(),
move |mut ctx| {
let ui = ctx.ui;
let region = ui.window_region(&id).unwrap();
let id_pos = region.top_left;
let container_pos = ui.window_region(&container).unwrap().top_left;
ctx.data.cursor += container_pos - id_pos;
ctx.data.size = region.size();
select(ui, id.clone(), ctx.state, ctx.data);
},
);
fn run(ui: &mut Ui, container: WidgetRef<W>, id: Self::Input) {
ui.register_event(container, CursorSense::click_or_drag(), move |mut ctx| {
let ui = ctx.ui;
let region = ui.window_region(&id).unwrap();
let id_pos = region.top_left;
let container_pos = ui.window_region(&container).unwrap().top_left;
ctx.data.cursor += container_pos - id_pos;
ctx.data.size = region.size();
select(ui, id, ctx.state, ctx.data);
});
}
}
@@ -30,10 +25,9 @@ pub struct Selectable;
impl WidgetAttr<TextEdit> for Selectable {
type Input = ();
fn run(ui: &mut Ui, id: &WidgetRef<TextEdit>, _: Self::Input) {
let id = id.clone();
ui.register_event(&id.clone(), CursorSense::click_or_drag(), move |ctx| {
select(ctx.ui, id.clone(), ctx.state, ctx.data);
fn run(ui: &mut Ui, id: WidgetRef<TextEdit>, _: Self::Input) {
ui.register_event(id, CursorSense::click_or_drag(), move |ctx| {
select(ctx.ui, id, ctx.state, ctx.data);
});
}
}
+1 -2
View File
@@ -120,10 +120,9 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
ui_state.renderer.resize(size)
}
WindowEvent::KeyboardInput { event, .. } => {
if let Some(sel) = &ui_state.focus
if let Some(sel) = ui_state.focus
&& event.state.is_pressed()
{
let sel = &sel.clone();
let res = sel.get_mut().apply_event(event, &ui_state.input.modifiers);
match res {
TextInputResult::Unfocus => {
+4 -4
View File
@@ -13,7 +13,7 @@ pub mod eventable {
) -> impl WidgetIdFn<WL::Widget> {
move |ui| {
let id = self.add(ui);
ui.register_widget_event(&id, event, f);
ui.register_widget_event(id.weak(), event, f);
id
}
}
@@ -56,7 +56,7 @@ macro_rules! event_ctx {
{
fn on<E: Event>(
&mut self,
widget: &WidgetRef<W>,
widget: WidgetRef<W>,
event: E,
f: impl WidgetEventFn<Ctx, E::Data, W>,
);
@@ -68,11 +68,11 @@ macro_rules! event_ctx {
{
fn on<E: Event>(
&mut self,
widget: &WidgetRef<W>,
widget: WidgetRef<W>,
event: E,
f: impl WidgetEventFn<$ty, E::Data, W>,
) {
self.register_widget_event(&widget, event, f);
self.register_widget_event(widget, event, f);
}
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
use crate::prelude::*;
pub struct Masked {
pub inner: WidgetRef,
pub inner: WidgetHandle,
}
impl Widget for Masked {
+1 -1
View File
@@ -1,7 +1,7 @@
use crate::prelude::*;
pub struct Aligned {
pub inner: WidgetRef,
pub inner: WidgetHandle,
pub align: Align,
}
+1 -1
View File
@@ -1,7 +1,7 @@
use crate::prelude::*;
pub struct LayerOffset {
pub inner: WidgetRef,
pub inner: WidgetHandle,
pub offset: usize,
}
+1 -1
View File
@@ -1,7 +1,7 @@
use crate::prelude::*;
pub struct MaxSize {
pub inner: WidgetRef,
pub inner: WidgetHandle,
pub x: Option<Len>,
pub y: Option<Len>,
}
+1 -1
View File
@@ -1,7 +1,7 @@
use crate::prelude::*;
pub struct Offset {
pub inner: WidgetRef,
pub inner: WidgetHandle,
pub amt: UiVec2,
}
+1 -1
View File
@@ -2,7 +2,7 @@ use crate::prelude::*;
pub struct Pad {
pub padding: Padding,
pub inner: WidgetRef,
pub inner: WidgetHandle,
}
impl Widget for Pad {
+2 -2
View File
@@ -1,7 +1,7 @@
use crate::prelude::*;
pub struct Scroll {
inner: WidgetRef,
inner: WidgetHandle,
axis: Axis,
amt: f32,
snap_end: bool,
@@ -41,7 +41,7 @@ impl Widget for Scroll {
}
impl Scroll {
pub fn new(inner: WidgetRef, axis: Axis) -> Self {
pub fn new(inner: WidgetHandle, axis: Axis) -> Self {
Self {
inner,
axis,
+1 -1
View File
@@ -1,7 +1,7 @@
use crate::prelude::*;
pub struct Sized {
pub inner: WidgetRef,
pub inner: WidgetHandle,
pub x: Option<Len>,
pub y: Option<Len>,
}
+3 -3
View File
@@ -2,7 +2,7 @@ use crate::prelude::*;
use std::marker::PhantomData;
pub struct Span {
pub children: Vec<WidgetRef>,
pub children: Vec<WidgetHandle>,
pub dir: Dir,
pub gap: f32,
}
@@ -158,7 +158,7 @@ impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> FnOnce<(&mut Ui,)>
extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> Self::Output {
Span {
children: self.children.ui(args.0).arr.to_vec(),
children: self.children.ui(args.0).arr.into_iter().collect(),
dir: self.dir,
gap: self.gap,
}
@@ -182,7 +182,7 @@ impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> SpanBuilder<LEN, Wa, Ta
}
impl std::ops::Deref for Span {
type Target = Vec<WidgetRef>;
type Target = Vec<WidgetHandle>;
fn deref(&self) -> &Self::Target {
&self.children
+2 -2
View File
@@ -3,7 +3,7 @@ use std::marker::PhantomData;
use crate::prelude::*;
pub struct Stack {
pub children: Vec<WidgetRef>,
pub children: Vec<WidgetHandle>,
pub size: StackSize,
}
@@ -55,7 +55,7 @@ impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> FnOnce<(&mut Ui,)>
extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> Self::Output {
Stack {
children: self.children.ui(args.0).arr.to_vec(),
children: self.children.ui(args.0).arr.into_iter().collect(),
size: self.size,
}
}
+1 -1
View File
@@ -2,7 +2,7 @@ use crate::prelude::*;
#[derive(Default)]
pub struct WidgetPtr {
pub inner: Option<WidgetRef>,
pub inner: Option<WidgetHandle>,
}
impl Widget for WidgetPtr {
+18 -21
View File
@@ -1,15 +1,12 @@
use crate::prelude::*;
use crate::util::Id;
use std::{
ops::{BitOr, Deref, DerefMut},
rc::Rc,
};
use crate::{
layout::{UiModule, UiRegion, Vec2},
util::HashMap,
};
use std::{
ops::{BitOr, Deref, DerefMut},
rc::Rc,
};
#[derive(Clone, Copy, PartialEq)]
pub enum CursorButton {
@@ -100,10 +97,10 @@ pub enum ActivationState {
/// but I'm not sure how or if worth it
pub struct Sensor<Ctx, Data> {
pub senses: CursorSenses,
pub f: Rc<dyn EventFn<Ctx, Data>>,
pub f: Box<dyn EventFn<Ctx, Data>>,
}
pub type SensorMap<Ctx, Data> = HashMap<Id, SensorGroup<Ctx, Data>>;
pub type SensorMap<Ctx, Data> = HashMap<WidgetId, SensorGroup<Ctx, Data>>;
pub type SenseShape = UiRegion;
pub struct SensorGroup<Ctx, Data> {
pub hover: ActivationState,
@@ -122,7 +119,7 @@ pub struct CursorData {
pub struct CursorModule<Ctx> {
map: SensorMap<Ctx, CursorData>,
active: HashMap<usize, HashMap<Id, SenseShape>>,
active: HashMap<usize, HashMap<WidgetId, SenseShape>>,
}
impl<Ctx: 'static> UiModule for CursorModule<Ctx> {
@@ -141,7 +138,7 @@ impl<Ctx: 'static> UiModule for CursorModule<Ctx> {
}
}
fn on_remove(&mut self, id: &Id) {
fn on_remove(&mut self, id: &WidgetId) {
self.map.remove(id);
for layer in self.active.values_mut() {
layer.remove(id);
@@ -307,34 +304,34 @@ impl Event for CursorSense {
impl<E: Event<Data = <CursorSenses as Event>::Data> + Into<CursorSenses>, Ctx: 'static>
EventModule<E, Ctx> for CursorModule<Ctx>
{
fn register(&mut self, id: Id, senses: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
fn register(&mut self, id: WidgetId, senses: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
// TODO: does not add to active if currently active
self.map.entry(id).or_default().sensors.push(Sensor {
senses: senses.into(),
f: Rc::new(f),
f: Box::new(f),
});
}
fn run<'a>(
&self,
id: &Id,
fn run(
&mut self,
id: WidgetId,
event: E,
) -> Option<impl Fn(EventCtx<Ctx, <E as Event>::Data>) + use<'a, E, Ctx>> {
) -> Option<impl FnOnce(EventCtx<Ctx, <E as Event>::Data>)> {
let senses = event.into();
if let Some(group) = self.map.get(id) {
if let Some(group) = self.map.get_mut(&id) {
let fs: Vec<_> = group
.sensors
.iter()
.iter_mut()
.filter_map(|sensor| {
if sensor.senses.iter().any(|s| senses.contains(s)) {
Some(sensor.f.clone())
Some(&mut sensor.f)
} else {
None
}
})
.collect();
Some(move |ctx: EventCtx<Ctx, CursorData>| {
for f in &fs {
for f in fs {
f(EventCtx {
state: ctx.state,
ui: ctx.ui,
+2 -2
View File
@@ -21,11 +21,11 @@ pub struct TextView {
// cache
tex: Option<RenderedText>,
width: Option<f32>,
pub hint: Option<WidgetRef>,
pub hint: Option<WidgetHandle>,
}
impl TextView {
pub fn new(buf: TextBuffer, attrs: TextAttrs, hint: Option<WidgetRef>) -> Self {
pub fn new(buf: TextBuffer, attrs: TextAttrs, hint: Option<WidgetHandle>) -> Self {
Self {
attrs: attrs.into(),
buf: buf.into(),
+1 -1
View File
@@ -127,7 +127,7 @@ widget_trait! {
|ui| self.add(ui)
}
fn set_ptr(self, ptr: &WidgetRef<WidgetPtr>, ui: &mut Ui) {
fn set_ptr(self, ptr: &WidgetHandle<WidgetPtr>, ui: &mut Ui) {
ptr.get_mut().inner = Some(self.add(ui));
}
}