idk work (r + h)

This commit is contained in:
2025-12-11 23:05:27 -05:00
parent a70d09e162
commit a708813ce7
11 changed files with 152 additions and 102 deletions

View File

@@ -10,7 +10,7 @@ fn main() {
}
pub struct Client {
info: WidgetView<Text>,
info: WidgetRef<Text>,
}
event_ctx!(Client);
@@ -56,24 +56,24 @@ impl DefaultAppState for Client {
let add_button = rect(Color::LIME)
.radius(30)
.on(CursorSense::click(), move |ctx| {
.on(CursorSense::click(), move |mut ctx| {
let child = ctx
.ui
.add(image(include_bytes!("assets/sungals.png")).center());
ctx.ui[span_add.1].children.push(child);
ctx[span_add.r].children.push(child);
})
.sized((150, 150))
.align(Align::BOT_RIGHT);
let del_button = rect(Color::RED)
.radius(30)
.on(CursorSense::click(), move |ctx| {
ctx.ui[span_add.1].children.pop();
.on(CursorSense::click(), move |mut ctx| {
ctx[span_add.r].children.pop();
})
.sized((150, 150))
.align(Align::BOT_LEFT);
let span_add_test = (span_add.0, add_button, del_button).stack().add(ui);
let span_add_test = (span_add.h, add_button, del_button).stack().add(ui);
let btext = |content| wtext(content).size(30);
@@ -99,14 +99,14 @@ impl DefaultAppState for Client {
.add(ui);
let texts = Span::empty(Dir::DOWN).gap(10).handles(ui);
let msg_area = texts.0.scroll().masked().background(rect(Color::SKY));
let msg_area = texts.h.scroll().masked().background(rect(Color::SKY));
let add_text = wtext("add")
.editable(false)
.text_align(Align::LEFT)
.size(30)
.attr::<Selectable>(())
.on(Submit, move |ctx| {
let content = ctx.id.edit(ctx.ui).take();
let content = ctx.widget.edit(ctx.ui).take();
let text = wtext(content)
.editable(false)
.size(30)
@@ -114,7 +114,7 @@ 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.1].children.push(msg_box);
ctx.ui[texts.r].children.push(msg_box);
})
.handles(ui);
let text_edit_scroll = (
@@ -122,10 +122,10 @@ impl DefaultAppState for Client {
(
Rect::new(Color::WHITE.darker(0.9)),
(
add_text.0.width(rest(1)),
add_text.h.width(rest(1)),
Rect::new(Color::GREEN)
.on(CursorSense::click(), move |ctx| {
ctx.ui.run_event(ctx.state, add_text.1, Submit, ());
ctx.ui.run_event(ctx.state, add_text.r, Submit, ());
})
.sized((40, 40)),
)
@@ -148,34 +148,34 @@ impl DefaultAppState for Client {
let i = vec.len();
if vec.is_empty() {
vec.push(None);
ui[main.1].set(to);
ui[main.r].set(to);
} else {
vec.push(Some(to));
}
let vals = vals.clone();
let rect = rect(color)
.on(CursorSense::click(), move |ctx| {
.on(CursorSense::click(), move |mut ctx| {
let (prev, vec) = &mut *vals.borrow_mut();
if let Some(h) = vec[i].take() {
vec[*prev] = ctx.ui[main.1].replace(h);
vec[*prev] = ctx[main.r].replace(h);
*prev = i;
}
ctx.ui[ctx.id].color = color.darker(0.3);
ctx.widget().color = color.darker(0.3);
})
.on(
CursorSense::HoverStart | CursorSense::unclick(),
move |ctx| {
ctx.ui[ctx.id].color = color.brighter(0.2);
move |mut ctx| {
ctx.widget().color = color.brighter(0.2);
},
)
.on(CursorSense::HoverEnd, move |ctx| {
ctx.ui[ctx.id].color = color;
.on(CursorSense::HoverEnd, move |mut ctx| {
ctx.widget().color = color;
});
(rect, wtext(label).size(30).text_align(Align::CENTER)).stack()
};
let tabs = (
switch_button(Color::RED, pad_test.0, "pad"),
switch_button(Color::RED, pad_test.h, "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"),
@@ -188,13 +188,13 @@ impl DefaultAppState for Client {
.span(Dir::RIGHT);
let info = wtext("").handles(ui);
let info_sect = info.0.pad(10).align(Align::RIGHT);
let info_sect = info.h.pad(10).align(Align::RIGHT);
((tabs.height(40), main.0.pad(10)).span(Dir::DOWN), info_sect)
((tabs.height(40), main.h.pad(10)).span(Dir::DOWN), info_sect)
.stack()
.set_root(ui);
Self { info: info.1 }
Self { info: info.r }
}
fn window_event(&mut self, _: WindowEvent, ui: &mut Ui, state: &UiState) {

View File

@@ -5,9 +5,9 @@ use winit::dpi::{LogicalPosition, LogicalSize};
pub struct Selector;
impl<W: 'static + Widget> WidgetAttr<W> for Selector {
type Input = WidgetView<TextEdit>;
type Input = WidgetRef<TextEdit>;
fn run(ui: &mut Ui, container: WidgetView<W>, id: Self::Input) {
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();
@@ -25,14 +25,14 @@ pub struct Selectable;
impl WidgetAttr<TextEdit> for Selectable {
type Input = ();
fn run(ui: &mut Ui, id: WidgetView<TextEdit>, _: Self::Input) {
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);
});
}
}
fn select(ui: &mut Ui, id: WidgetView<TextEdit>, state: &mut UiState, data: CursorData) {
fn select(ui: &mut Ui, id: WidgetRef<TextEdit>, state: &mut UiState, data: CursorData) {
let now = Instant::now();
let recent = (now - state.last_click) < Duration::from_millis(300);
state.last_click = now;

View File

@@ -30,7 +30,7 @@ pub struct DefaultState<AppState> {
pub struct UiState {
pub renderer: UiRenderer,
pub input: Input,
pub focus: Option<WidgetView<TextEdit>>,
pub focus: Option<WidgetRef<TextEdit>>,
pub clipboard: Clipboard,
pub window: Arc<Window>,
pub ime: usize,

View File

@@ -1,5 +1,31 @@
use crate::prelude::*;
pub mod eventable {
use super::*;
widget_trait! {
pub trait Eventable;
fn on<E: Event, Ctx: 'static>(
self,
event: E,
f: impl WidgetEventFn<Ctx, E::Data, WL::Widget>,
) -> impl WidgetIdFn<WL::Widget> {
move |ui| {
let id = self.handles(ui);
ui.register_event(&id.r, event, move |ctx| {
f(EventIdCtx {
widget: id.r,
state: ctx.state,
data: ctx.data,
ui: ctx.ui,
});
});
id.h
}
}
}
}
// TODO: naming in here is a bit weird like eventable
#[macro_export]
macro_rules! event_ctx {
@@ -31,29 +57,3 @@ macro_rules! event_ctx {
};
}
pub use event_ctx;
pub mod eventable {
use super::*;
widget_trait! {
pub trait Eventable;
fn on<E: Event, Ctx: 'static>(
self,
event: E,
f: impl WidgetEventFn<Ctx, E::Data, WL::Widget>,
) -> impl WidgetIdFn<WL::Widget> {
move |ui| {
let id = self.handles(ui);
ui.register_event(&id.1, event, move |ctx| {
f(EventIdCtx {
id: id.1,
state: ctx.state,
data: ctx.data,
ui: ctx.ui,
});
});
id.0
}
}
}
}

View File

@@ -6,7 +6,7 @@ use std::{
};
#[derive(Clone, Copy, PartialEq)]
pub enum Button {
pub enum CursorButton {
Left,
Right,
Middle,
@@ -14,9 +14,9 @@ pub enum Button {
#[derive(Clone, Copy, PartialEq)]
pub enum CursorSense {
PressStart(Button),
Pressing(Button),
PressEnd(Button),
PressStart(CursorButton),
Pressing(CursorButton),
PressEnd(CursorButton),
HoverStart,
Hovering,
HoverEnd,
@@ -27,16 +27,16 @@ pub struct CursorSenses(Vec<CursorSense>);
impl CursorSense {
pub fn click() -> Self {
Self::PressStart(Button::Left)
Self::PressStart(CursorButton::Left)
}
pub fn click_or_drag() -> CursorSenses {
Self::click() | Self::Pressing(Button::Left)
Self::click() | Self::Pressing(CursorButton::Left)
}
pub fn unclick() -> Self {
Self::PressEnd(Button::Left)
Self::PressEnd(CursorButton::Left)
}
pub fn is_dragging(&self) -> bool {
matches!(self, CursorSense::Pressing(Button::Left))
matches!(self, CursorSense::Pressing(CursorButton::Left))
}
}
@@ -56,11 +56,11 @@ pub struct CursorButtons {
}
impl CursorButtons {
pub fn select(&self, button: &Button) -> &ActivationState {
pub fn select(&self, button: &CursorButton) -> &ActivationState {
match button {
Button::Left => &self.left,
Button::Right => &self.right,
Button::Middle => &self.middle,
CursorButton::Left => &self.left,
CursorButton::Right => &self.right,
CursorButton::Middle => &self.middle,
}
}

View File

@@ -89,7 +89,7 @@ widget_trait! {
move |ui| {
Scroll::new(self.add(ui), Axis::Y)
.on(CursorSense::Scroll, |ctx| {
let s = &mut ctx.ui[ctx.id];
let s = &mut ctx.ui[ctx.widget];
s.scroll(ctx.data.scroll_delta.y * 50.0);
})
.add(ui)