please rust analyzer with macros

This commit is contained in:
2025-08-16 02:34:44 -04:00
parent 166394d8d9
commit b0fe7310eb
5 changed files with 45 additions and 32 deletions

View File

@@ -1,29 +1,25 @@
use crate::{Sense, SenseFn, Sensor, Ui, Widget, WidgetId, WidgetIdFn, WidgetLike};
use crate::{Sense, SenseFn, Sensor, Ui, Widget, WidgetId, WidgetIdFnRet, WidgetLike};
pub trait Sensable<W, Ctx, Tag> {
fn on(
self,
sense: Sense,
f: impl SenseFn<Ctx>,
) -> impl WidgetIdFn<W, Ctx>;
fn on(self, sense: Sense, f: impl SenseFn<Ctx>) -> WidgetIdFnRet!(W, Ctx);
fn id_on(
self,
sense: Sense,
f: impl FnMut(&WidgetId<W>, &mut Ui<Ctx>, &mut Ctx) + 'static + Clone,
) -> impl WidgetIdFn<W, Ctx>
) -> WidgetIdFnRet!(W, Ctx)
where
W: Widget<Ctx>;
fn edit_on(
self,
sense: Sense,
f: impl FnMut(&mut W, &mut Ctx) + 'static + Clone,
) -> impl WidgetIdFn<W, Ctx>
) -> WidgetIdFnRet!(W, Ctx)
where
W: Widget<Ctx>;
}
impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
fn on(self, sense: Sense, f: impl SenseFn<Ctx>) -> impl WidgetIdFn<W::Widget, Ctx> {
fn on(self, sense: Sense, f: impl SenseFn<Ctx>) -> WidgetIdFnRet!(W::Widget, Ctx) {
move |ui| {
let id = self.add(ui);
ui.add_sensor(
@@ -41,7 +37,7 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
sense: Sense,
// trait copied here bc rust analyzer skill issue
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ui<Ctx>, &mut Ctx) + 'static + Clone,
) -> impl WidgetIdFn<W::Widget, Ctx>
) -> WidgetIdFnRet!(W::Widget, Ctx)
where
W::Widget: Widget<Ctx>,
{
@@ -55,7 +51,7 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
sense: Sense,
// trait copied here bc rust analyzer skill issue
mut f: impl FnMut(&mut W::Widget, &mut Ctx) + 'static + Clone,
) -> impl WidgetIdFn<W::Widget, Ctx>
) -> WidgetIdFnRet!(W::Widget, Ctx)
where
W::Widget: Widget<Ctx>,
{

View File

@@ -1,28 +1,28 @@
use super::*;
use crate::{UiRegion, Vec2, WidgetArrLike, WidgetFn, WidgetLike};
use crate::{UiRegion, Vec2, WidgetArrLike, WidgetFnRet, WidgetLike};
pub trait CoreWidget<W: 'static, Ctx: 'static, Tag> {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Regioned, Ctx>;
fn center(self, size: impl Into<Vec2>) -> impl WidgetFn<Regioned, Ctx>;
fn region(self, region: UiRegion) -> impl WidgetFn<Regioned, Ctx>;
fn pad(self, padding: impl Into<Padding>) -> WidgetFnRet!(Regioned, Ctx);
fn center(self, size: impl Into<Vec2>) -> WidgetFnRet!(Regioned, Ctx);
fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned, Ctx);
}
impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> CoreWidget<W::Widget, Ctx, Tag> for W {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Regioned, Ctx> {
fn pad(self, padding: impl Into<Padding>) -> WidgetFnRet!(Regioned, Ctx) {
|ui| Regioned {
region: padding.into().region(),
inner: self.add(ui).erase_type(),
}
}
fn center(self, size: impl Into<Vec2>) -> impl WidgetFn<Regioned, Ctx> {
fn center(self, size: impl Into<Vec2>) -> WidgetFnRet!(Regioned, Ctx) {
|ui| Regioned {
region: UiRegion::center().size(size.into()),
inner: self.add(ui).erase_type(),
}
}
fn region(self, region: UiRegion) -> impl WidgetFn<Regioned, Ctx> {
fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned, Ctx) {
move |ui| Regioned {
region,
inner: self.add(ui).erase_type(),
@@ -31,21 +31,21 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> CoreWidget<W::Widget, Ctx, Tag>
}
pub trait CoreWidgetArr<const LEN: usize, Ctx: 'static, Tag> {
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> impl WidgetFn<Span, Ctx>;
fn stack(self) -> impl WidgetFn<Stack, Ctx>;
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> WidgetFnRet!(Span, Ctx);
fn stack(self) -> WidgetFnRet!(Stack, Ctx);
}
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Ctx, Tag>, Ctx: 'static, Tag>
CoreWidgetArr<LEN, Ctx, Tag> for Wa
{
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> impl WidgetFn<Span, Ctx> {
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> WidgetFnRet!(Span, Ctx) {
let lengths = lengths.map(Into::into);
move |ui| Span {
dir,
children: self.ui(ui).arr.into_iter().zip(lengths).collect(),
}
}
fn stack(self) -> impl WidgetFn<Stack, Ctx> {
fn stack(self) -> WidgetFnRet!(Stack, Ctx) {
move |ui| Stack {
children: self.ui(ui).arr.to_vec(),
}

View File

@@ -33,7 +33,6 @@ pub struct Sensor<Ctx> {
pub type SensorMap<Ctx> = HashMap<Id, SensorGroup<Ctx>>;
pub type ActiveSensors = Vec<(SenseShape, Id)>;
pub trait SenseFn_<Ctx> = FnMut(&mut Ui<Ctx>, &mut Ctx) + 'static;
pub type SenseShape = UiRegion;
#[derive(Clone)]
pub struct SenseTrigger {
@@ -45,10 +44,10 @@ pub struct SensorGroup<Ctx> {
pub cursor: ActivationState,
pub sensors: Vec<Sensor<Ctx>>,
}
pub trait SenseFn<Ctx>: SenseFn_<Ctx> {
pub trait SenseFn<Ctx>: FnMut(&mut Ui<Ctx>, &mut Ctx) + 'static {
fn box_clone(&self) -> Box<dyn SenseFn<Ctx>>;
}
impl<F: SenseFn_<Ctx> + Clone, Ctx> SenseFn<Ctx> for F {
impl<F: FnMut(&mut Ui<Ctx>, &mut Ctx) + 'static + Clone, Ctx> SenseFn<Ctx> for F {
fn box_clone(&self) -> Box<dyn SenseFn<Ctx>> {
Box::new(self.clone())
}

View File

@@ -74,7 +74,7 @@ pub trait WidgetLike<Ctx, Tag> {
fn with_id<W2>(
self,
f: impl FnOnce(&mut Ui<Ctx>, WidgetId<Self::Widget>) -> WidgetId<W2>,
) -> impl WidgetIdFn<W2, Ctx>
) -> WidgetIdFnRet!(W2, Ctx)
where
Self: Sized,
{
@@ -85,11 +85,28 @@ pub trait WidgetLike<Ctx, Tag> {
}
}
/// A function that returns a widget given a UI.
/// Useful for defining trait functions on widgets that create a parent widget so that the children
/// don't need to be IDs yet
pub trait WidgetFn<W: Widget<Ctx>, Ctx> = FnOnce(&mut Ui<Ctx>) -> W;
pub trait WidgetIdFn<W, Ctx> = FnOnce(&mut Ui<Ctx>) -> WidgetId<W>;
// A function that returns a widget given a UI.
// Useful for defining trait functions on widgets that create a parent widget so that the children
// don't need to be IDs yet
// pub trait WidgetFn<W: Widget<Ctx>, Ctx> = FnOnce(&mut Ui<Ctx>) -> W;
// pub trait WidgetIdFn<W, Ctx> = FnOnce(&mut Ui<Ctx>) -> WidgetId<W>;
// copium for rust analyzer
macro_rules! WidgetFnRet {
($W:ty, $Ctx:ty) => {
impl FnOnce(&mut $crate::Ui<$Ctx>) -> $W
};
}
pub(crate) use WidgetFnRet;
macro_rules! WidgetIdFnRet {
($W:ty, $Ctx:ty) => {
impl FnOnce(&mut $crate::Ui<$Ctx>) -> $crate::WidgetId<$W>
};
($W:ty, $Ctx:ty, $($use:tt)*) => {
impl FnOnce(&mut $crate::Ui<$Ctx>) -> $crate::WidgetId<$W> + use<$($use)*>
};
}
pub(crate) use WidgetIdFnRet;
pub trait Idable<Ctx, Tag> {
type Widget: Widget<Ctx>;
@@ -97,7 +114,7 @@ pub trait Idable<Ctx, Tag> {
fn id<'a>(
self,
id: &WidgetId<Self::Widget>,
) -> impl WidgetIdFn<Self::Widget, Ctx> + use<'a, Self, Ctx, Tag>
) -> WidgetIdFnRet!(Self::Widget, Ctx, 'a, Self, Ctx, Tag)
where
Self: Sized,
{

View File

@@ -84,6 +84,7 @@ impl Client {
let main = main.clone();
let to = to.clone().erase_type();
Rect::new(color)
.on(Sense::PressEnd, |_, _| {})
.id_on(Sense::PressStart, move |id, ui, _| {
ui[&main].inner = to.clone();
ui[id].color = color.add_rgb(-0.2);