remove context generic

This commit is contained in:
2025-08-25 18:53:21 -04:00
parent d4b1a56467
commit e8b255c8f9
17 changed files with 167 additions and 217 deletions

View File

@@ -5,8 +5,8 @@ pub struct Regioned {
pub inner: WidgetId,
}
impl<Ctx: 'static> Widget<Ctx> for Regioned {
fn draw(&self, painter: &mut Painter<Ctx>) {
impl Widget for Regioned {
fn draw(&self, painter: &mut Painter) {
painter.draw_within(&self.inner, self.region);
}
}

View File

@@ -5,8 +5,8 @@ pub struct Image {
handle: Option<TextureHandle>,
}
impl<Ctx> Widget<Ctx> for Image {
fn draw(&self, painter: &mut Painter<Ctx>) {
impl Widget for Image {
fn draw(&self, painter: &mut Painter) {
if let Some(handle) = &self.handle {
painter.draw_texture(handle);
} else {
@@ -20,7 +20,7 @@ impl<Ctx> Widget<Ctx> for Image {
}
}
pub fn image<Ctx>(image: impl LoadableImage) -> WidgetFnRet!(Image, Ctx) {
pub fn image(image: impl LoadableImage) -> WidgetFnRet!(Image) {
let image = match image.get_image() {
Ok(image) => Some(image),
Err(e) => {

View File

@@ -27,8 +27,8 @@ impl Rect {
}
}
impl<Ctx> Widget<Ctx> for Rect {
fn draw(&self, painter: &mut Painter<Ctx>) {
impl Widget for Rect {
fn draw(&self, painter: &mut Painter) {
painter.write(RectPrimitive {
color: self.color,
radius: self.radius,

View File

@@ -1,25 +1,21 @@
use crate::prelude::*;
pub trait Sensable<W, Ctx, Tag> {
fn on(self, sense: Sense, f: impl SenseFn<Ctx>) -> WidgetIdFnRet!(W, Ctx);
pub trait Sensable<W, Tag> {
fn on(self, sense: Sense, f: impl SenseFn) -> WidgetIdFnRet!(W);
fn id_on(
self,
sense: Sense,
f: impl FnMut(&WidgetId<W>, SenseCtx<Ctx>) + 'static + Clone,
) -> WidgetIdFnRet!(W, Ctx)
f: impl FnMut(&WidgetId<W>, &mut Ui) + 'static + Clone,
) -> WidgetIdFnRet!(W)
where
W: Widget<Ctx>;
fn edit_on(
self,
sense: Sense,
f: impl FnMut(&mut W, &mut Ctx) + 'static + Clone,
) -> WidgetIdFnRet!(W, Ctx)
W: Widget;
fn edit_on(self, sense: Sense, f: impl FnMut(&mut W) + 'static + Clone) -> WidgetIdFnRet!(W)
where
W: Widget<Ctx>;
W: Widget;
}
impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
fn on(self, sense: Sense, f: impl SenseFn<Ctx>) -> WidgetIdFnRet!(W::Widget, Ctx) {
impl<W: WidgetLike<Tag>, Tag> Sensable<W::Widget, Tag> for W {
fn on(self, sense: Sense, f: impl SenseFn) -> WidgetIdFnRet!(W::Widget) {
move |ui| {
let id = self.add(ui);
ui.add_sensor(
@@ -35,10 +31,10 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
fn id_on(
self,
sense: Sense,
mut f: impl FnMut(&WidgetId<W::Widget>, SenseCtx<Ctx>) + 'static + Clone,
) -> WidgetIdFnRet!(W::Widget, Ctx)
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ui) + 'static + Clone,
) -> WidgetIdFnRet!(W::Widget)
where
W::Widget: Widget<Ctx>,
W::Widget: Widget,
{
self.with_id(move |ui, id| {
let id2 = id.clone();
@@ -48,11 +44,11 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
fn edit_on(
self,
sense: Sense,
mut f: impl FnMut(&mut W::Widget, &mut Ctx) + 'static + Clone,
) -> WidgetIdFnRet!(W::Widget, Ctx)
mut f: impl FnMut(&mut W::Widget) + 'static + Clone,
) -> WidgetIdFnRet!(W::Widget)
where
W::Widget: Widget<Ctx>,
W::Widget: Widget,
{
self.id_on(sense, move |id, ctx| f(&mut ctx.ui[id], ctx.app))
self.id_on(sense, move |id, ui| f(&mut ui[id]))
}
}

View File

@@ -5,8 +5,8 @@ pub struct Span {
pub dir: Dir,
}
impl<Ctx: 'static> Widget<Ctx> for Span {
fn draw(&self, painter: &mut Painter<Ctx>) {
impl Widget for Span {
fn draw(&self, painter: &mut Painter) {
let total = self.sums();
let mut start = UIScalar::min();
for (child, length) in &self.children {

View File

@@ -4,8 +4,8 @@ pub struct Stack {
pub children: Vec<WidgetId>,
}
impl<Ctx> Widget<Ctx> for Stack {
fn draw(&self, painter: &mut Painter<Ctx>) {
impl Widget for Stack {
fn draw(&self, painter: &mut Painter) {
for child in &self.children {
painter.draw(child);
}

View File

@@ -20,8 +20,8 @@ impl Text {
}
}
impl<Ctx> Widget<Ctx> for Text {
fn draw(&self, painter: &mut Painter<Ctx>) {
impl Widget for Text {
fn draw(&self, painter: &mut Painter) {
// TODO: when on_update is added to painter,
// return & store TextureHandle to reuse
painter.draw_text(&self.content, &self.attrs);

View File

@@ -3,36 +3,36 @@ use crate::layout::{
Dir, UiPos, UiRegion, Vec2, WidgetArrLike, WidgetFnRet, WidgetIdFnRet, WidgetLike,
};
pub trait CoreWidget<W, Ctx: 'static, Tag> {
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);
fn label(self, label: impl Into<String>) -> WidgetIdFnRet!(W, Ctx);
pub trait CoreWidget<W, Tag> {
fn pad(self, padding: impl Into<Padding>) -> WidgetFnRet!(Regioned);
fn center(self, size: impl Into<Vec2>) -> WidgetFnRet!(Regioned);
fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned);
fn label(self, label: impl Into<String>) -> WidgetIdFnRet!(W);
}
impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> CoreWidget<W::Widget, Ctx, Tag> for W {
fn pad(self, padding: impl Into<Padding>) -> WidgetFnRet!(Regioned, Ctx) {
impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
fn pad(self, padding: impl Into<Padding>) -> WidgetFnRet!(Regioned) {
|ui| Regioned {
region: padding.into().region(),
inner: self.add(ui).erase_type(),
}
}
fn center(self, size: impl Into<Vec2>) -> WidgetFnRet!(Regioned, Ctx) {
fn center(self, size: impl Into<Vec2>) -> WidgetFnRet!(Regioned) {
|ui| Regioned {
region: UiPos::center().expand(size.into()),
inner: self.add(ui).erase_type(),
}
}
fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned, Ctx) {
fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned) {
move |ui| Regioned {
region,
inner: self.add(ui).erase_type(),
}
}
fn label(self, label: impl Into<String>) -> WidgetIdFnRet!(W::Widget, Ctx) {
fn label(self, label: impl Into<String>) -> WidgetIdFnRet!(W::Widget) {
|ui| {
let id = self.add(ui);
ui.set_label(&id, label.into());
@@ -41,22 +41,20 @@ 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]) -> WidgetFnRet!(Span, Ctx);
fn stack(self) -> WidgetFnRet!(Stack, Ctx);
pub trait CoreWidgetArr<const LEN: usize, Tag> {
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> WidgetFnRet!(Span);
fn stack(self) -> WidgetFnRet!(Stack);
}
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]) -> WidgetFnRet!(Span, Ctx) {
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> CoreWidgetArr<LEN, Tag> for Wa {
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> WidgetFnRet!(Span) {
let lengths = lengths.map(Into::into);
move |ui| Span {
dir,
children: self.ui(ui).arr.into_iter().zip(lengths).collect(),
}
}
fn stack(self) -> WidgetFnRet!(Stack, Ctx) {
fn stack(self) -> WidgetFnRet!(Stack) {
move |ui| Stack {
children: self.ui(ui).arr.to_vec(),
}