idk work (r + h)
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
use crate::{Ui, WidgetIdFn, WidgetLike, WidgetView};
|
use crate::{Ui, WidgetIdFn, WidgetLike, WidgetRef};
|
||||||
|
|
||||||
pub trait WidgetAttr<W: ?Sized> {
|
pub trait WidgetAttr<W: ?Sized> {
|
||||||
type Input;
|
type Input;
|
||||||
fn run(ui: &mut Ui, id: WidgetView<W>, input: Self::Input);
|
fn run(ui: &mut Ui, id: WidgetRef<W>, input: Self::Input);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Attrable<W: ?Sized, Tag> {
|
pub trait Attrable<W: ?Sized, Tag> {
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
use crate::{Ui, UiModule, WidgetId, WidgetView, util::HashMap};
|
use crate::{Ui, UiModule, Widget, WidgetId, WidgetRef, util::HashMap};
|
||||||
use std::{hash::Hash, rc::Rc};
|
use std::{
|
||||||
|
hash::Hash,
|
||||||
|
ops::{Index, IndexMut},
|
||||||
|
rc::Rc,
|
||||||
|
};
|
||||||
|
|
||||||
pub trait Event: Sized {
|
pub trait Event: Sized {
|
||||||
type Module<Ctx: 'static>: EventModule<Self, Ctx>;
|
type Module<Ctx: 'static>: EventModule<Self, Ctx>;
|
||||||
@@ -13,12 +17,32 @@ pub struct EventCtx<'a, Ctx, Data> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct EventIdCtx<'a, Ctx, Data, W: ?Sized> {
|
pub struct EventIdCtx<'a, Ctx, Data, W: ?Sized> {
|
||||||
pub id: WidgetView<W>,
|
pub widget: WidgetRef<W>,
|
||||||
pub ui: &'a mut Ui,
|
pub ui: &'a mut Ui,
|
||||||
pub state: &'a mut Ctx,
|
pub state: &'a mut Ctx,
|
||||||
pub data: Data,
|
pub data: Data,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, Ctx, Data, W2, W: Widget> Index<WidgetRef<W>> for EventIdCtx<'a, Ctx, Data, W2> {
|
||||||
|
type Output = W;
|
||||||
|
|
||||||
|
fn index(&self, index: WidgetRef<W>) -> &Self::Output {
|
||||||
|
&self.ui[index]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Ctx, Data, W2, W: Widget> IndexMut<WidgetRef<W>> for EventIdCtx<'a, Ctx, Data, W2> {
|
||||||
|
fn index_mut(&mut self, index: WidgetRef<W>) -> &mut Self::Output {
|
||||||
|
&mut self.ui[index]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Ctx, Data, W: Widget> EventIdCtx<'a, Ctx, Data, W> {
|
||||||
|
pub fn widget(&mut self) -> &mut W {
|
||||||
|
&mut self.ui[self.widget]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait EventFn<Ctx, Data>: Fn(EventCtx<Ctx, Data>) + 'static {}
|
pub trait EventFn<Ctx, Data>: Fn(EventCtx<Ctx, Data>) + 'static {}
|
||||||
impl<F: Fn(EventCtx<Ctx, Data>) + 'static, Ctx, Data> EventFn<Ctx, Data> for F {}
|
impl<F: Fn(EventCtx<Ctx, Data>) + 'static, Ctx, Data> EventFn<Ctx, Data> for F {}
|
||||||
|
|
||||||
@@ -127,7 +151,7 @@ impl Ui {
|
|||||||
pub fn run_event<E: Event, Ctx: 'static, W>(
|
pub fn run_event<E: Event, Ctx: 'static, W>(
|
||||||
&mut self,
|
&mut self,
|
||||||
ctx: &mut Ctx,
|
ctx: &mut Ctx,
|
||||||
id: WidgetView<W>,
|
id: WidgetRef<W>,
|
||||||
event: E,
|
event: E,
|
||||||
data: E::Data,
|
data: E::Data,
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#![allow(clippy::multiple_bound_locations)]
|
use std::marker::Destruct;
|
||||||
|
|
||||||
/// stored in linear for sane manipulation
|
/// stored in linear for sane manipulation
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Clone, Copy, Hash, PartialEq, Eq, bytemuck::Zeroable, Debug)]
|
#[derive(Clone, Copy, Hash, PartialEq, Eq, bytemuck::Zeroable, Debug)]
|
||||||
pub struct Color<T: ColorNum> {
|
pub struct Color<T> {
|
||||||
pub r: T,
|
pub r: T,
|
||||||
pub g: T,
|
pub g: T,
|
||||||
pub b: T,
|
pub b: T,
|
||||||
@@ -56,23 +56,47 @@ pub trait ColorNum {
|
|||||||
const MAX: Self;
|
const MAX: Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: ColorNum + F32Conversion> Color<T> {
|
macro_rules! map_rgb {
|
||||||
pub fn mul_rgb(self, amt: impl F32Conversion) -> Self {
|
($x:ident,$self:ident, $e:tt) => {
|
||||||
let amt = amt.to();
|
#[allow(unused_braces)]
|
||||||
self.map_rgb(|x| T::from(x.to() * amt))
|
Self {
|
||||||
|
r: {
|
||||||
|
let $x = $self.r;
|
||||||
|
$e
|
||||||
|
},
|
||||||
|
g: {
|
||||||
|
let $x = $self.g;
|
||||||
|
$e
|
||||||
|
},
|
||||||
|
b: {
|
||||||
|
let $x = $self.b;
|
||||||
|
$e
|
||||||
|
},
|
||||||
|
a: $self.a,
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_rgb(self, amt: impl F32Conversion) -> Self {
|
impl<T: ColorNum + const F32Conversion> Color<T>
|
||||||
|
where
|
||||||
|
Self: const Destruct,
|
||||||
|
{
|
||||||
|
pub const fn mul_rgb(self, amt: impl const F32Conversion) -> Self {
|
||||||
let amt = amt.to();
|
let amt = amt.to();
|
||||||
self.map_rgb(|x| T::from(x.to() + amt))
|
map_rgb!(x, self, { T::from(x.to() * amt) })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn darker(self, amt: f32) -> Self {
|
pub const fn add_rgb(self, amt: impl const F32Conversion) -> Self {
|
||||||
|
let amt = amt.to();
|
||||||
|
map_rgb!(x, self, { T::from(x.to() + amt) })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn darker(self, amt: f32) -> Self {
|
||||||
self.mul_rgb(1.0 - amt)
|
self.mul_rgb(1.0 - amt)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn brighter(self, amt: f32) -> Self {
|
pub const fn brighter(self, amt: f32) -> Self {
|
||||||
self.map_rgb(|x| {
|
map_rgb!(x, self, {
|
||||||
let x = x.to();
|
let x = x.to();
|
||||||
T::from(x + (1.0 - x) * amt)
|
T::from(x + (1.0 - x) * amt)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -7,12 +7,9 @@ use crate::{
|
|||||||
|
|
||||||
pub type WidgetId = SlotId;
|
pub type WidgetId = SlotId;
|
||||||
|
|
||||||
/// An identifier for a widget that can index a UI to get the associated widget.
|
/// An identifier for a widget that can index a UI or event ctx to get it.
|
||||||
/// It should always remain valid; it keeps a ref count and removes the widget from the UI if all
|
/// This is a strong handle that does not impl Clone, and when it is dropped,
|
||||||
/// references are dropped.
|
/// a signal is sent to the owning UI to clean up the resources.
|
||||||
///
|
|
||||||
/// W does not need to implement widget so that AnyWidget is valid;
|
|
||||||
/// Instead, add generic bounds on methods that take an ID if they need specific data.
|
|
||||||
///
|
///
|
||||||
/// TODO: ergonomic clones when they get put in rust-analyzer & don't cause ICEs?
|
/// TODO: ergonomic clones when they get put in rust-analyzer & don't cause ICEs?
|
||||||
pub struct WidgetHandle<W: ?Sized = dyn Widget> {
|
pub struct WidgetHandle<W: ?Sized = dyn Widget> {
|
||||||
@@ -22,13 +19,18 @@ pub struct WidgetHandle<W: ?Sized = dyn Widget> {
|
|||||||
ty: *const W,
|
ty: *const W,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WidgetView<W: ?Sized = dyn Widget> {
|
/// A weak handle to a widget.
|
||||||
|
/// Will not keep it alive, but can still be used for indexing like WidgetHandle.
|
||||||
|
pub struct WidgetRef<W: ?Sized = dyn Widget> {
|
||||||
pub(super) id: WidgetId,
|
pub(super) id: WidgetId,
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
ty: *const W,
|
ty: *const W,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type WidgetHandles<W> = (WidgetHandle<W>, WidgetView<W>);
|
pub struct WidgetHandles<W: ?Sized = dyn Widget> {
|
||||||
|
pub h: WidgetHandle<W>,
|
||||||
|
pub r: WidgetRef<W>,
|
||||||
|
}
|
||||||
|
|
||||||
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetHandle<W> {
|
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetHandle<W> {
|
||||||
pub fn any(self) -> WidgetHandle<dyn Widget> {
|
pub fn any(self) -> WidgetHandle<dyn Widget> {
|
||||||
@@ -59,18 +61,18 @@ impl<W: ?Sized> WidgetHandle<W> {
|
|||||||
self.counter.refs()
|
self.counter.refs()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn weak(&self) -> WidgetView<W> {
|
pub fn weak(&self) -> WidgetRef<W> {
|
||||||
let Self { ty, id, .. } = *self;
|
let Self { ty, id, .. } = *self;
|
||||||
WidgetView { ty, id }
|
WidgetRef { ty, id }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handles(self) -> WidgetHandles<W> {
|
pub fn handles(self) -> WidgetHandles<W> {
|
||||||
let weak = self.weak();
|
let r = self.weak();
|
||||||
(self, weak)
|
WidgetHandles { h: self, r }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: ?Sized> WidgetView<W> {
|
impl<W: ?Sized> WidgetRef<W> {
|
||||||
pub fn id(&self) -> WidgetId {
|
pub fn id(&self) -> WidgetId {
|
||||||
self.id
|
self.id
|
||||||
}
|
}
|
||||||
@@ -99,7 +101,7 @@ impl<W: Widget + ?Sized> IdLike for WidgetHandle<W> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Widget + ?Sized> IdLike for WidgetView<W> {
|
impl<W: Widget + ?Sized> IdLike for WidgetRef<W> {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn id(&self) -> WidgetId {
|
fn id(&self) -> WidgetId {
|
||||||
self.id
|
self.id
|
||||||
@@ -107,15 +109,15 @@ impl<W: Widget + ?Sized> IdLike for WidgetView<W> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetHandle<U>> for WidgetHandle<T> {}
|
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetHandle<U>> for WidgetHandle<T> {}
|
||||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetView<U>> for WidgetView<T> {}
|
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetRef<U>> for WidgetRef<T> {}
|
||||||
|
|
||||||
impl<W: ?Sized> Clone for WidgetView<W> {
|
impl<W: ?Sized> Clone for WidgetRef<W> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
*self
|
*self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<W: ?Sized> Copy for WidgetView<W> {}
|
impl<W: ?Sized> Copy for WidgetRef<W> {}
|
||||||
impl<W: ?Sized> PartialEq for WidgetView<W> {
|
impl<W: ?Sized> PartialEq for WidgetRef<W> {
|
||||||
fn eq(&self, other: &Self) -> bool {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
self.id == other.id
|
self.id == other.id
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ impl Widgets {
|
|||||||
|
|
||||||
/// get_dyn but dynamic borrow checking of widgets
|
/// get_dyn but dynamic borrow checking of widgets
|
||||||
/// lets you do recursive (tree) operations, like the painter does
|
/// lets you do recursive (tree) operations, like the painter does
|
||||||
pub fn get_dyn_dynamic(&self, id: WidgetId) -> WidgetWrapper<'_> {
|
pub(crate) fn get_dyn_dynamic(&self, id: WidgetId) -> WidgetWrapper<'_> {
|
||||||
// SAFETY: must guarantee no other mutable references to this widget exist
|
// SAFETY: must guarantee no other mutable references to this widget exist
|
||||||
// done through the borrow variable
|
// done through the borrow variable
|
||||||
#[allow(mutable_transmutes)]
|
#[allow(mutable_transmutes)]
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
info: WidgetView<Text>,
|
info: WidgetRef<Text>,
|
||||||
}
|
}
|
||||||
|
|
||||||
event_ctx!(Client);
|
event_ctx!(Client);
|
||||||
@@ -56,24 +56,24 @@ impl DefaultAppState for Client {
|
|||||||
|
|
||||||
let add_button = rect(Color::LIME)
|
let add_button = rect(Color::LIME)
|
||||||
.radius(30)
|
.radius(30)
|
||||||
.on(CursorSense::click(), move |ctx| {
|
.on(CursorSense::click(), move |mut ctx| {
|
||||||
let child = ctx
|
let child = ctx
|
||||||
.ui
|
.ui
|
||||||
.add(image(include_bytes!("assets/sungals.png")).center());
|
.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))
|
.sized((150, 150))
|
||||||
.align(Align::BOT_RIGHT);
|
.align(Align::BOT_RIGHT);
|
||||||
|
|
||||||
let del_button = rect(Color::RED)
|
let del_button = rect(Color::RED)
|
||||||
.radius(30)
|
.radius(30)
|
||||||
.on(CursorSense::click(), move |ctx| {
|
.on(CursorSense::click(), move |mut ctx| {
|
||||||
ctx.ui[span_add.1].children.pop();
|
ctx[span_add.r].children.pop();
|
||||||
})
|
})
|
||||||
.sized((150, 150))
|
.sized((150, 150))
|
||||||
.align(Align::BOT_LEFT);
|
.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);
|
let btext = |content| wtext(content).size(30);
|
||||||
|
|
||||||
@@ -99,14 +99,14 @@ impl DefaultAppState for Client {
|
|||||||
.add(ui);
|
.add(ui);
|
||||||
|
|
||||||
let texts = Span::empty(Dir::DOWN).gap(10).handles(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")
|
let add_text = wtext("add")
|
||||||
.editable(false)
|
.editable(false)
|
||||||
.text_align(Align::LEFT)
|
.text_align(Align::LEFT)
|
||||||
.size(30)
|
.size(30)
|
||||||
.attr::<Selectable>(())
|
.attr::<Selectable>(())
|
||||||
.on(Submit, move |ctx| {
|
.on(Submit, move |ctx| {
|
||||||
let content = ctx.id.edit(ctx.ui).take();
|
let content = ctx.widget.edit(ctx.ui).take();
|
||||||
let text = wtext(content)
|
let text = wtext(content)
|
||||||
.editable(false)
|
.editable(false)
|
||||||
.size(30)
|
.size(30)
|
||||||
@@ -114,7 +114,7 @@ impl DefaultAppState for Client {
|
|||||||
.wrap(true)
|
.wrap(true)
|
||||||
.attr::<Selectable>(());
|
.attr::<Selectable>(());
|
||||||
let msg_box = text.background(rect(Color::WHITE.darker(0.5))).add(ctx.ui);
|
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);
|
.handles(ui);
|
||||||
let text_edit_scroll = (
|
let text_edit_scroll = (
|
||||||
@@ -122,10 +122,10 @@ impl DefaultAppState for Client {
|
|||||||
(
|
(
|
||||||
Rect::new(Color::WHITE.darker(0.9)),
|
Rect::new(Color::WHITE.darker(0.9)),
|
||||||
(
|
(
|
||||||
add_text.0.width(rest(1)),
|
add_text.h.width(rest(1)),
|
||||||
Rect::new(Color::GREEN)
|
Rect::new(Color::GREEN)
|
||||||
.on(CursorSense::click(), move |ctx| {
|
.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)),
|
.sized((40, 40)),
|
||||||
)
|
)
|
||||||
@@ -148,34 +148,34 @@ impl DefaultAppState for Client {
|
|||||||
let i = vec.len();
|
let i = vec.len();
|
||||||
if vec.is_empty() {
|
if vec.is_empty() {
|
||||||
vec.push(None);
|
vec.push(None);
|
||||||
ui[main.1].set(to);
|
ui[main.r].set(to);
|
||||||
} else {
|
} else {
|
||||||
vec.push(Some(to));
|
vec.push(Some(to));
|
||||||
}
|
}
|
||||||
let vals = vals.clone();
|
let vals = vals.clone();
|
||||||
let rect = rect(color)
|
let rect = rect(color)
|
||||||
.on(CursorSense::click(), move |ctx| {
|
.on(CursorSense::click(), move |mut ctx| {
|
||||||
let (prev, vec) = &mut *vals.borrow_mut();
|
let (prev, vec) = &mut *vals.borrow_mut();
|
||||||
if let Some(h) = vec[i].take() {
|
if let Some(h) = vec[i].take() {
|
||||||
vec[*prev] = ctx.ui[main.1].replace(h);
|
vec[*prev] = ctx[main.r].replace(h);
|
||||||
*prev = i;
|
*prev = i;
|
||||||
}
|
}
|
||||||
ctx.ui[ctx.id].color = color.darker(0.3);
|
ctx.widget().color = color.darker(0.3);
|
||||||
})
|
})
|
||||||
.on(
|
.on(
|
||||||
CursorSense::HoverStart | CursorSense::unclick(),
|
CursorSense::HoverStart | CursorSense::unclick(),
|
||||||
move |ctx| {
|
move |mut ctx| {
|
||||||
ctx.ui[ctx.id].color = color.brighter(0.2);
|
ctx.widget().color = color.brighter(0.2);
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.on(CursorSense::HoverEnd, move |ctx| {
|
.on(CursorSense::HoverEnd, move |mut ctx| {
|
||||||
ctx.ui[ctx.id].color = color;
|
ctx.widget().color = color;
|
||||||
});
|
});
|
||||||
(rect, wtext(label).size(30).text_align(Align::CENTER)).stack()
|
(rect, wtext(label).size(30).text_align(Align::CENTER)).stack()
|
||||||
};
|
};
|
||||||
|
|
||||||
let tabs = (
|
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::GREEN, span_test, "span"),
|
||||||
switch_button(Color::BLUE, span_add_test, "image span"),
|
switch_button(Color::BLUE, span_add_test, "image span"),
|
||||||
switch_button(Color::MAGENTA, text_test, "text layout"),
|
switch_button(Color::MAGENTA, text_test, "text layout"),
|
||||||
@@ -188,13 +188,13 @@ impl DefaultAppState for Client {
|
|||||||
.span(Dir::RIGHT);
|
.span(Dir::RIGHT);
|
||||||
|
|
||||||
let info = wtext("").handles(ui);
|
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()
|
.stack()
|
||||||
.set_root(ui);
|
.set_root(ui);
|
||||||
|
|
||||||
Self { info: info.1 }
|
Self { info: info.r }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn window_event(&mut self, _: WindowEvent, ui: &mut Ui, state: &UiState) {
|
fn window_event(&mut self, _: WindowEvent, ui: &mut Ui, state: &UiState) {
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ use winit::dpi::{LogicalPosition, LogicalSize};
|
|||||||
pub struct Selector;
|
pub struct Selector;
|
||||||
|
|
||||||
impl<W: 'static + Widget> WidgetAttr<W> for 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| {
|
ui.register_event(&container, CursorSense::click_or_drag(), move |mut ctx| {
|
||||||
let ui = ctx.ui;
|
let ui = ctx.ui;
|
||||||
let region = ui.window_region(&id).unwrap();
|
let region = ui.window_region(&id).unwrap();
|
||||||
@@ -25,14 +25,14 @@ pub struct Selectable;
|
|||||||
impl WidgetAttr<TextEdit> for Selectable {
|
impl WidgetAttr<TextEdit> for Selectable {
|
||||||
type Input = ();
|
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| {
|
ui.register_event(&id, CursorSense::click_or_drag(), move |ctx| {
|
||||||
select(ctx.ui, id, ctx.state, ctx.data);
|
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 now = Instant::now();
|
||||||
let recent = (now - state.last_click) < Duration::from_millis(300);
|
let recent = (now - state.last_click) < Duration::from_millis(300);
|
||||||
state.last_click = now;
|
state.last_click = now;
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ pub struct DefaultState<AppState> {
|
|||||||
pub struct UiState {
|
pub struct UiState {
|
||||||
pub renderer: UiRenderer,
|
pub renderer: UiRenderer,
|
||||||
pub input: Input,
|
pub input: Input,
|
||||||
pub focus: Option<WidgetView<TextEdit>>,
|
pub focus: Option<WidgetRef<TextEdit>>,
|
||||||
pub clipboard: Clipboard,
|
pub clipboard: Clipboard,
|
||||||
pub window: Arc<Window>,
|
pub window: Arc<Window>,
|
||||||
pub ime: usize,
|
pub ime: usize,
|
||||||
|
|||||||
52
src/event.rs
52
src/event.rs
@@ -1,5 +1,31 @@
|
|||||||
use crate::prelude::*;
|
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
|
// TODO: naming in here is a bit weird like eventable
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! event_ctx {
|
macro_rules! event_ctx {
|
||||||
@@ -31,29 +57,3 @@ macro_rules! event_ctx {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
pub use 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq)]
|
#[derive(Clone, Copy, PartialEq)]
|
||||||
pub enum Button {
|
pub enum CursorButton {
|
||||||
Left,
|
Left,
|
||||||
Right,
|
Right,
|
||||||
Middle,
|
Middle,
|
||||||
@@ -14,9 +14,9 @@ pub enum Button {
|
|||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq)]
|
#[derive(Clone, Copy, PartialEq)]
|
||||||
pub enum CursorSense {
|
pub enum CursorSense {
|
||||||
PressStart(Button),
|
PressStart(CursorButton),
|
||||||
Pressing(Button),
|
Pressing(CursorButton),
|
||||||
PressEnd(Button),
|
PressEnd(CursorButton),
|
||||||
HoverStart,
|
HoverStart,
|
||||||
Hovering,
|
Hovering,
|
||||||
HoverEnd,
|
HoverEnd,
|
||||||
@@ -27,16 +27,16 @@ pub struct CursorSenses(Vec<CursorSense>);
|
|||||||
|
|
||||||
impl CursorSense {
|
impl CursorSense {
|
||||||
pub fn click() -> Self {
|
pub fn click() -> Self {
|
||||||
Self::PressStart(Button::Left)
|
Self::PressStart(CursorButton::Left)
|
||||||
}
|
}
|
||||||
pub fn click_or_drag() -> CursorSenses {
|
pub fn click_or_drag() -> CursorSenses {
|
||||||
Self::click() | Self::Pressing(Button::Left)
|
Self::click() | Self::Pressing(CursorButton::Left)
|
||||||
}
|
}
|
||||||
pub fn unclick() -> Self {
|
pub fn unclick() -> Self {
|
||||||
Self::PressEnd(Button::Left)
|
Self::PressEnd(CursorButton::Left)
|
||||||
}
|
}
|
||||||
pub fn is_dragging(&self) -> bool {
|
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 {
|
impl CursorButtons {
|
||||||
pub fn select(&self, button: &Button) -> &ActivationState {
|
pub fn select(&self, button: &CursorButton) -> &ActivationState {
|
||||||
match button {
|
match button {
|
||||||
Button::Left => &self.left,
|
CursorButton::Left => &self.left,
|
||||||
Button::Right => &self.right,
|
CursorButton::Right => &self.right,
|
||||||
Button::Middle => &self.middle,
|
CursorButton::Middle => &self.middle,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ widget_trait! {
|
|||||||
move |ui| {
|
move |ui| {
|
||||||
Scroll::new(self.add(ui), Axis::Y)
|
Scroll::new(self.add(ui), Axis::Y)
|
||||||
.on(CursorSense::Scroll, |ctx| {
|
.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);
|
s.scroll(ctx.data.scroll_delta.y * 50.0);
|
||||||
})
|
})
|
||||||
.add(ui)
|
.add(ui)
|
||||||
|
|||||||
Reference in New Issue
Block a user