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(),
}

View File

@@ -119,24 +119,24 @@ impl<W> Drop for WidgetId<W> {
pub struct IdTag;
pub struct IdFnTag;
// pub trait WidgetIdFn<W, Ctx> = FnOnce(&mut Ui<Ctx>) -> WidgetId<W>;
// pub trait WidgetIdFn<W, Ctx> = FnOnce(&mut Ui) -> WidgetId<W>;
macro_rules! WidgetIdFnRet {
($W:ty, $Ctx:ty) => {
impl FnOnce(&mut $crate::layout::Ui<$Ctx>) -> $crate::layout::WidgetId<$W>
($W:ty) => {
impl FnOnce(&mut $crate::layout::Ui) -> $crate::layout::WidgetId<$W>
};
($W:ty, $Ctx:ty, $($use:tt)*) => {
impl FnOnce(&mut $crate::layout::Ui<$Ctx>) -> $crate::layout::WidgetId<$W> + use<$($use)*>
($W:ty, $($use:tt)*) => {
impl FnOnce(&mut $crate::layout::Ui) -> $crate::layout::WidgetId<$W> + use<$($use)*>
};
}
pub(crate) use WidgetIdFnRet;
pub trait Idable<Ctx, Tag> {
type Widget: Widget<Ctx>;
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>);
pub trait Idable<Tag> {
type Widget: Widget;
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>);
fn id<'a>(
self,
id: &WidgetId<Self::Widget>,
) -> WidgetIdFnRet!(Self::Widget, Ctx, 'a, Self, Ctx, Tag)
) -> WidgetIdFnRet!(Self::Widget, 'a, Self, Tag)
where
Self: Sized,
{
@@ -149,7 +149,7 @@ pub trait Idable<Ctx, Tag> {
fn id_static<'a>(
self,
id: StaticWidgetId<Self::Widget>,
) -> WidgetIdFnRet!(Self::Widget, Ctx, 'a, Self, Ctx, Tag)
) -> WidgetIdFnRet!(Self::Widget, 'a, Self, Tag)
where
Self: Sized,
{
@@ -161,33 +161,33 @@ pub trait Idable<Ctx, Tag> {
}
}
impl<W: Widget<Ctx>, Ctx> Idable<Ctx, WidgetTag> for W {
impl<W: Widget> Idable<WidgetTag> for W {
type Widget = W;
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) {
ui.set(id, self);
}
}
impl<F: FnOnce(&mut Ui<Ctx>) -> W, W: Widget<Ctx>, Ctx> Idable<Ctx, FnTag> for F {
impl<F: FnOnce(&mut Ui) -> W, W: Widget> Idable<FnTag> for F {
type Widget = W;
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) {
let w = self(ui);
ui.set(id, w);
}
}
impl<W: 'static, Ctx> WidgetLike<Ctx, IdTag> for WidgetId<W> {
impl<W: 'static> WidgetLike<IdTag> for WidgetId<W> {
type Widget = W;
fn add(self, _: &mut Ui<Ctx>) -> WidgetId<W> {
fn add(self, _: &mut Ui) -> WidgetId<W> {
self
}
}
impl<W: 'static, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetLike<Ctx, IdFnTag> for F {
impl<W: 'static, F: FnOnce(&mut Ui) -> WidgetId<W>> WidgetLike<IdFnTag> for F {
type Widget = W;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
fn add(self, ui: &mut Ui) -> WidgetId<W> {
self(ui)
}
}
@@ -198,9 +198,9 @@ impl<W> StaticWidgetId<W> {
}
}
impl<W: 'static, Ctx> WidgetLike<Ctx, IdTag> for StaticWidgetId<W> {
impl<W: 'static> WidgetLike<IdTag> for StaticWidgetId<W> {
type Widget = W;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
fn add(self, ui: &mut Ui) -> WidgetId<W> {
self.id(&ui.send)
}
}

View File

@@ -18,10 +18,9 @@ struct State {
id: Option<Id>,
}
pub struct Painter<'a, Ctx: 'static> {
widgets: &'a Widgets<Ctx>,
ctx: &'a mut Ctx,
sensors_map: &'a SensorMap<Ctx>,
pub struct Painter<'a> {
widgets: &'a Widgets,
sensors_map: &'a SensorMap,
pub(super) active: &'a mut Active,
pub(super) primitives: &'a mut Primitives,
textures: &'a mut Textures,
@@ -31,13 +30,12 @@ pub struct Painter<'a, Ctx: 'static> {
state: State,
}
impl<'a, Ctx> Painter<'a, Ctx> {
impl<'a> Painter<'a> {
#[allow(clippy::too_many_arguments)]
pub(super) fn new(
nodes: &'a Widgets<Ctx>,
nodes: &'a Widgets,
primitives: &'a mut Primitives,
ctx: &'a mut Ctx,
sensors_map: &'a SensorMap<Ctx>,
sensors_map: &'a SensorMap,
active: &'a mut Active,
text: &'a mut TextData,
textures: &'a mut Textures,
@@ -45,7 +43,6 @@ impl<'a, Ctx> Painter<'a, Ctx> {
) -> Self {
Self {
widgets: nodes,
ctx,
active,
sensors_map,
primitives,
@@ -68,34 +65,22 @@ impl<'a, Ctx> Painter<'a, Ctx> {
}
/// Draws a widget within this widget's region.
pub fn draw<W>(&mut self, id: &WidgetId<W>)
where
Ctx: 'static,
{
pub fn draw<W>(&mut self, id: &WidgetId<W>) {
self.draw_at(id, self.state.region);
}
/// Draws a widget somewhere within this one.
/// Useful for drawing child widgets in select areas.
pub fn draw_within<W>(&mut self, id: &WidgetId<W>, region: UiRegion)
where
Ctx: 'static,
{
pub fn draw_within<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
self.draw_at(id, region.within(&self.state.region));
}
/// Draws a widget in an arbitrary region.
pub fn draw_at<W>(&mut self, id: &WidgetId<W>, region: UiRegion)
where
Ctx: 'static,
{
pub fn draw_at<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
self.draw_raw_at(&id.id, region);
}
fn draw_raw_at(&mut self, id: &Id, region: UiRegion)
where
Ctx: 'static,
{
fn draw_raw_at(&mut self, id: &Id, region: UiRegion) {
if self.active.widgets.contains_key(id) {
panic!("widget drawn twice!");
}
@@ -158,10 +143,6 @@ impl<'a, Ctx> Painter<'a, Ctx> {
self.state.region
}
pub fn ctx(&mut self) -> &mut Ctx {
self.ctx
}
pub fn region_size(&self) -> Vec2 {
self.state.region.in_size(self.screen_size)
}

View File

@@ -30,12 +30,12 @@ pub enum ActivationState {
Off,
}
pub struct Sensor<Ctx> {
pub struct Sensor {
pub sense: Sense,
pub f: Box<dyn SenseFn<Ctx>>,
pub f: Box<dyn SenseFn>,
}
pub type SensorMap<Ctx> = HashMap<Id, SensorGroup<Ctx>>;
pub type SensorMap = HashMap<Id, SensorGroup>;
pub type ActiveSensors = HashMap<Id, SenseShape>;
pub type SenseShape = UiRegion;
#[derive(Clone)]
@@ -43,26 +43,22 @@ pub struct SenseTrigger {
pub shape: SenseShape,
pub sense: Sense,
}
pub struct SensorGroup<Ctx> {
pub struct SensorGroup {
pub hover: ActivationState,
pub cursor: ActivationState,
pub sensors: Vec<Sensor<Ctx>>,
pub sensors: Vec<Sensor>,
}
pub struct SenseCtx<'a, Ctx> {
pub ui: &'a mut Ui<Ctx>,
pub app: &'a mut Ctx,
pub trait SenseFn: FnMut(&mut Ui) + 'static {
fn box_clone(&self) -> Box<dyn SenseFn>;
}
pub trait SenseFn<Ctx>: FnMut(SenseCtx<Ctx>) + 'static {
fn box_clone(&self) -> Box<dyn SenseFn<Ctx>>;
}
impl<F: FnMut(SenseCtx<Ctx>) + 'static + Clone, Ctx> SenseFn<Ctx> for F {
fn box_clone(&self) -> Box<dyn SenseFn<Ctx>> {
impl<F: FnMut(&mut Ui) + 'static + Clone> SenseFn for F {
fn box_clone(&self) -> Box<dyn SenseFn> {
Box::new(self.clone())
}
}
impl<Ctx> Ui<Ctx> {
pub fn add_sensor<W>(&mut self, id: &WidgetId<W>, f: Sensor<Ctx>) {
impl Ui {
pub fn add_sensor<W>(&mut self, id: &WidgetId<W>, f: Sensor) {
self.sensor_map
.entry(id.id.duplicate())
.or_default()
@@ -70,10 +66,7 @@ impl<Ctx> Ui<Ctx> {
.push(f);
}
pub fn run_sensors(&mut self, ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2)
where
Ctx: 'static,
{
pub fn run_sensors(&mut self, cursor: &CursorState, window_size: Vec2) {
let active = std::mem::take(&mut self.active.sensors);
let mut map = std::mem::take(&mut self.sensor_map);
for (id, shape) in active.iter() {
@@ -85,7 +78,7 @@ impl<Ctx> Ui<Ctx> {
for sensor in &mut group.sensors {
if should_run(sensor.sense, group.cursor, group.hover) {
(sensor.f.box_clone())(SenseCtx { ui: self, app: ctx });
(sensor.f.box_clone())(self);
}
}
}
@@ -141,7 +134,7 @@ impl ActivationState {
}
}
impl<Ctx> Default for SensorGroup<Ctx> {
impl Default for SensorGroup {
fn default() -> Self {
Self {
hover: Default::default(),
@@ -150,7 +143,7 @@ impl<Ctx> Default for SensorGroup<Ctx> {
}
}
}
impl<Ctx: 'static> Clone for SensorGroup<Ctx> {
impl Clone for SensorGroup {
fn clone(&self) -> Self {
Self {
hover: self.hover,
@@ -159,7 +152,7 @@ impl<Ctx: 'static> Clone for SensorGroup<Ctx> {
}
}
}
impl<Ctx: 'static> Clone for Sensor<Ctx> {
impl Clone for Sensor {
fn clone(&self) -> Self {
Self {
sense: self.sense,

View File

@@ -14,9 +14,9 @@ use std::{
sync::mpsc::{Receiver, Sender, channel},
};
pub struct Ui<Ctx> {
pub struct Ui {
base: Option<WidgetId>,
widgets: Widgets<Ctx>,
widgets: Widgets,
labels: HashMap<Id, String>,
updates: Vec<Id>,
recv: Receiver<Id>,
@@ -29,26 +29,23 @@ pub struct Ui<Ctx> {
full_redraw: bool,
pub(super) active: Active,
pub(super) sensor_map: SensorMap<Ctx>,
pub(super) sensor_map: SensorMap,
}
#[derive(Default)]
pub struct Widgets<Ctx> {
pub struct Widgets {
ids: IdTracker,
map: HashMap<Id, Box<dyn Widget<Ctx>>>,
map: HashMap<Id, Box<dyn Widget>>,
}
impl<Ctx> Ui<Ctx> {
pub fn add<W: Widget<Ctx>, Tag>(
&mut self,
w: impl WidgetLike<Ctx, Tag, Widget = W>,
) -> WidgetId<W> {
impl Ui {
pub fn add<W: Widget, Tag>(&mut self, w: impl WidgetLike<Tag, Widget = W>) -> WidgetId<W> {
w.add(self)
}
pub fn add_static<W: Widget<Ctx>, Tag>(
pub fn add_static<W: Widget, Tag>(
&mut self,
w: impl WidgetLike<Ctx, Tag, Widget = W>,
w: impl WidgetLike<Tag, Widget = W>,
) -> StaticWidgetId<W> {
let id = w.add(self);
id.into_static()
@@ -59,11 +56,11 @@ impl<Ctx> Ui<Ctx> {
self.labels.insert(id.id.duplicate(), label);
}
pub fn add_widget<W: Widget<Ctx>>(&mut self, w: W) -> WidgetId<W> {
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetId<W> {
self.push(w)
}
pub fn push<W: Widget<Ctx>>(&mut self, w: W) -> WidgetId<W> {
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
let id = self.id();
self.labels
.insert(id.id.duplicate(), std::any::type_name::<W>().to_string());
@@ -71,31 +68,28 @@ impl<Ctx> Ui<Ctx> {
id
}
pub fn set<W: Widget<Ctx>>(&mut self, id: &WidgetId<W>, w: W) {
pub fn set<W: Widget>(&mut self, id: &WidgetId<W>, w: W) {
self.widgets.insert(id.id.duplicate(), w);
}
pub fn set_base<Tag>(&mut self, w: impl WidgetLike<Ctx, Tag>) {
pub fn set_base<Tag>(&mut self, w: impl WidgetLike<Tag>) {
self.base = Some(w.add(self).erase_type());
self.full_redraw = true;
}
pub fn new() -> Self
where
Ctx: 'static,
{
pub fn new() -> Self {
Self::default()
}
pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> {
self.widgets.get(id)
}
pub fn get_mut<W: Widget<Ctx>>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
self.widgets.get_mut(id)
}
pub fn id<W: Widget<Ctx>>(&mut self) -> WidgetId<W> {
pub fn id<W: Widget>(&mut self) -> WidgetId<W> {
WidgetId::new(
self.widgets.reserve(),
TypeId::of::<W>(),
@@ -104,7 +98,7 @@ impl<Ctx> Ui<Ctx> {
)
}
pub fn id_static<W: Widget<Ctx>>(&mut self) -> StaticWidgetId<W> {
pub fn id_static<W: Widget>(&mut self) -> StaticWidgetId<W> {
let id = self.id();
id.into_static()
}
@@ -117,10 +111,7 @@ impl<Ctx> Ui<Ctx> {
self.size = size.into();
}
pub fn redraw_all(&mut self, ctx: &mut Ctx)
where
Ctx: 'static,
{
pub fn redraw_all(&mut self) {
self.active.clear();
self.primitives.clear();
// free before bc nothing should exist
@@ -128,7 +119,6 @@ impl<Ctx> Ui<Ctx> {
let mut painter = Painter::new(
&self.widgets,
&mut self.primitives,
ctx,
&self.sensor_map,
&mut self.active,
&mut self.text,
@@ -141,26 +131,19 @@ impl<Ctx> Ui<Ctx> {
self.primitives.replace();
}
pub fn update(&mut self, ctx: &mut Ctx)
where
Ctx: 'static,
{
pub fn update(&mut self) {
if self.full_redraw {
self.redraw_all(ctx);
self.redraw_all();
self.full_redraw = false;
} else if !self.updates.is_empty() {
self.redraw_updates(ctx);
self.redraw_updates();
}
}
fn redraw_updates(&mut self, ctx: &mut Ctx)
where
Ctx: 'static,
{
fn redraw_updates(&mut self) {
let mut painter = Painter::new(
&self.widgets,
&mut self.primitives,
ctx,
&self.sensor_map,
&mut self.active,
&mut self.text,
@@ -191,7 +174,7 @@ impl<Ctx> Ui<Ctx> {
}
}
impl<W: Widget<Ctx>, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
impl<W: Widget> Index<&WidgetId<W>> for Ui {
type Output = W;
fn index(&self, id: &WidgetId<W>) -> &Self::Output {
@@ -199,14 +182,14 @@ impl<W: Widget<Ctx>, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
}
}
impl<W: Widget<Ctx>, Ctx> IndexMut<&WidgetId<W>> for Ui<Ctx> {
impl<W: Widget> IndexMut<&WidgetId<W>> for Ui {
fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output {
self.updates.push(id.id.duplicate());
self.get_mut(id).unwrap()
}
}
impl<W: Widget<Ctx>, Ctx> Index<StaticWidgetId<W>> for Ui<Ctx> {
impl<W: Widget> Index<StaticWidgetId<W>> for Ui {
type Output = W;
fn index(&self, id: StaticWidgetId<W>) -> &Self::Output {
@@ -214,14 +197,14 @@ impl<W: Widget<Ctx>, Ctx> Index<StaticWidgetId<W>> for Ui<Ctx> {
}
}
impl<W: Widget<Ctx>, Ctx> IndexMut<StaticWidgetId<W>> for Ui<Ctx> {
impl<W: Widget> IndexMut<StaticWidgetId<W>> for Ui {
fn index_mut(&mut self, id: StaticWidgetId<W>) -> &mut Self::Output {
self.updates.push(id.id.id());
self.widgets.get_static_mut(&id).unwrap()
}
}
impl<Ctx> Widgets<Ctx> {
impl Widgets {
pub fn new() -> Self {
Self {
ids: IdTracker::default(),
@@ -229,15 +212,15 @@ impl<Ctx> Widgets<Ctx> {
}
}
pub fn get_dyn(&self, id: &Id) -> &dyn Widget<Ctx> {
pub fn get_dyn(&self, id: &Id) -> &dyn Widget {
self.map.get(id).unwrap().as_ref()
}
pub fn get_static<W: Widget<Ctx>>(&self, id: &StaticWidgetId<W>) -> Option<&W> {
pub fn get_static<W: Widget>(&self, id: &StaticWidgetId<W>) -> Option<&W> {
self.map.get(&id.id.id()).unwrap().as_any().downcast_ref()
}
pub fn get_static_mut<W: Widget<Ctx>>(&mut self, id: &StaticWidgetId<W>) -> Option<&mut W> {
pub fn get_static_mut<W: Widget>(&mut self, id: &StaticWidgetId<W>) -> Option<&mut W> {
self.map
.get_mut(&id.id.id())
.unwrap()
@@ -245,11 +228,11 @@ impl<Ctx> Widgets<Ctx> {
.downcast_mut()
}
pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> {
self.map.get(&id.id).unwrap().as_any().downcast_ref()
}
pub fn get_mut<W: Widget<Ctx>>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
self.map
.get_mut(&id.id)
.unwrap()
@@ -257,11 +240,11 @@ impl<Ctx> Widgets<Ctx> {
.downcast_mut()
}
pub fn insert(&mut self, id: Id, widget: impl Widget<Ctx>) {
pub fn insert(&mut self, id: Id, widget: impl Widget) {
self.map.insert(id, Box::new(widget));
}
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget<Ctx>>) {
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget>) {
self.map.insert(id, widget);
}
@@ -283,7 +266,7 @@ impl<Ctx> Widgets<Ctx> {
}
}
impl<Ctx> dyn Widget<Ctx> {
impl dyn Widget {
pub fn as_any(&self) -> &dyn Any {
self
}
@@ -293,7 +276,7 @@ impl<Ctx> dyn Widget<Ctx> {
}
}
impl<Ctx: 'static> Default for Ui<Ctx> {
impl Default for Ui {
fn default() -> Self {
let (send, recv) = channel();
Self {
@@ -341,7 +324,7 @@ impl Active {
instance
}
pub fn add<Ctx>(&mut self, id: &Id, instance: WidgetInstance, sensors_map: &SensorMap<Ctx>) {
pub fn add(&mut self, id: &Id, instance: WidgetInstance, sensors_map: &SensorMap) {
if sensors_map.get(id).is_some() {
self.sensors.insert(id.duplicate(), instance.region);
}

View File

@@ -2,20 +2,20 @@ use crate::layout::{Painter, Ui, WidgetId, WidgetIdFnRet};
use std::{any::Any, marker::PhantomData};
pub trait Widget<Ctx>: Any {
fn draw(&self, painter: &mut Painter<Ctx>);
pub trait Widget: Any {
fn draw(&self, painter: &mut Painter);
}
pub struct WidgetTag;
pub struct FnTag;
pub trait WidgetLike<Ctx, Tag> {
pub trait WidgetLike<Tag> {
type Widget: 'static;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<Self::Widget>;
fn add(self, ui: &mut Ui) -> WidgetId<Self::Widget>;
fn with_id<W2>(
self,
f: impl FnOnce(&mut Ui<Ctx>, WidgetId<Self::Widget>) -> WidgetId<W2>,
) -> WidgetIdFnRet!(W2, Ctx)
f: impl FnOnce(&mut Ui, WidgetId<Self::Widget>) -> WidgetId<W2>,
) -> WidgetIdFnRet!(W2)
where
Self: Sized,
{
@@ -33,22 +33,22 @@ pub trait WidgetLike<Ctx, Tag> {
/// don't need to be IDs yet
/// currently a macro for rust analyzer (doesn't support trait aliases atm)
macro_rules! WidgetFnRet {
($W:ty, $Ctx:ty) => {
impl FnOnce(&mut $crate::layout::Ui<$Ctx>) -> $W
($W:ty) => {
impl FnOnce(&mut $crate::layout::Ui) -> $W
};
}
pub(crate) use WidgetFnRet;
impl<W: Widget<Ctx>, Ctx, F: FnOnce(&mut Ui<Ctx>) -> W> WidgetLike<Ctx, FnTag> for F {
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike<FnTag> for F {
type Widget = W;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
fn add(self, ui: &mut Ui) -> WidgetId<W> {
self(ui).add(ui)
}
}
impl<W: Widget<Ctx>, Ctx> WidgetLike<Ctx, WidgetTag> for W {
impl<W: Widget> WidgetLike<WidgetTag> for W {
type Widget = W;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
fn add(self, ui: &mut Ui) -> WidgetId<W> {
ui.add_widget(self)
}
}
@@ -68,21 +68,21 @@ impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
}
pub struct ArrTag;
pub trait WidgetArrLike<const LEN: usize, Ctx, Tags> {
pub trait WidgetArrLike<const LEN: usize, Tags> {
type Ws;
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<LEN, Self::Ws>;
fn ui(self, ui: &mut Ui) -> WidgetArr<LEN, Self::Ws>;
}
impl<const LEN: usize, Ws, Ctx> WidgetArrLike<LEN, Ctx, ArrTag> for WidgetArr<LEN, Ws> {
impl<const LEN: usize, Ws> WidgetArrLike<LEN, ArrTag> for WidgetArr<LEN, Ws> {
type Ws = Ws;
fn ui(self, _: &mut Ui<Ctx>) -> WidgetArr<LEN, Ws> {
fn ui(self, _: &mut Ui) -> WidgetArr<LEN, Ws> {
self
}
}
impl<W: WidgetLike<Ctx, WidgetTag>, Ctx> WidgetArrLike<1, Ctx, WidgetTag> for W {
impl<W: WidgetLike<WidgetTag>> WidgetArrLike<1, WidgetTag> for W {
type Ws = (W::Widget,);
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<1, (W::Widget,)> {
fn ui(self, ui: &mut Ui) -> WidgetArr<1, (W::Widget,)> {
WidgetArr::new([self.add(ui).erase_type()])
}
}
@@ -93,9 +93,9 @@ macro_rules! impl_widget_arr {
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
};
($n:expr;$($W:ident)*;$($Tag:ident)*) => {
impl<$($W: WidgetLike<Ctx, $Tag>,$Tag,)* Ctx> WidgetArrLike<$n, Ctx, ($($Tag,)*)> for ($($W,)*) {
impl<$($W: WidgetLike<$Tag>,$Tag,)*> WidgetArrLike<$n, ($($Tag,)*)> for ($($W,)*) {
type Ws = ($($W::Widget,)*);
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<$n, ($($W::Widget,)*)> {
fn ui(self, ui: &mut Ui) -> WidgetArr<$n, ($($W::Widget,)*)> {
#[allow(non_snake_case)]
let ($($W,)*) = self;
WidgetArr::new(

View File

@@ -48,7 +48,7 @@ impl UiRenderer {
pass.draw(0..4, 0..self.instance.len() as u32);
}
pub fn update<Ctx>(&mut self, device: &Device, queue: &Queue, ui: &mut Ui<Ctx>) {
pub fn update(&mut self, device: &Device, queue: &Queue, ui: &mut Ui) {
if ui.primitives.updated {
self.instance
.update(device, queue, &ui.primitives.instances);

View File

@@ -10,7 +10,7 @@ use super::Client;
#[derive(Default)]
pub struct App {
client: Option<(Client, Ui<Client>)>,
client: Option<(Client, Ui)>,
}
impl App {

View File

@@ -27,7 +27,7 @@ pub struct ClientUi {
}
impl Client {
pub fn create_ui() -> (Ui<Self>, ClientUi) {
pub fn create_ui() -> (Ui, ClientUi) {
let mut ui = Ui::new();
let rect = Rect {
color: UiColor::WHITE,
@@ -78,17 +78,17 @@ impl Client {
let switch_button = |color, to, label| {
let rect = Rect::new(color)
.id_on(Sense::PressStart, move |id, ctx| {
ctx.ui[main].inner.set_static(to);
ctx.ui[id].color = color.add_rgb(-0.2);
.id_on(Sense::PressStart, move |id, ui| {
ui[main].inner.set_static(to);
ui[id].color = color.add_rgb(-0.2);
})
.edit_on(Sense::HoverStart, move |r, _| {
.edit_on(Sense::HoverStart, move |r| {
r.color = color.add_rgb(0.4);
})
.edit_on(Sense::PressEnd, move |r, _| {
.edit_on(Sense::PressEnd, move |r| {
r.color = color.add_rgb(0.4);
})
.edit_on(Sense::HoverEnd, move |r, _| {
.edit_on(Sense::HoverEnd, move |r| {
r.color = color;
});
(rect, text(label).size(30)).stack()
@@ -104,12 +104,11 @@ impl Client {
);
let add_button = Rect::new(Color::LIME)
.radius(30)
.on(Sense::PressStart, move |ctx| {
let child = ctx
.ui
.on(Sense::PressStart, move |ui| {
let child = ui
.add(image(include_bytes!("assets/sungals.png")))
.erase_type();
ctx.ui[span_add].children.push((child, ratio(1)));
ui[span_add].children.push((child, ratio(1)));
})
.region(
UiPos::corner(Corner::BotRight)
@@ -119,8 +118,8 @@ impl Client {
let del_button = Rect::new(Color::RED)
.radius(30)
.on(Sense::PressStart, move |ctx| {
ctx.ui[span_add].children.pop();
.on(Sense::PressStart, move |ui| {
ui[span_add].children.pop();
})
.region(
UiPos::corner(Corner::BotLeft)
@@ -167,15 +166,15 @@ impl Client {
}
}
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop, ui: &mut Ui<Self>) {
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop, ui: &mut Ui) {
self.input.event(&event);
let cursor_state = self.cursor_state();
let window_size = self.window_size();
ui.run_sensors(self, &cursor_state, window_size);
ui.run_sensors(&cursor_state, window_size);
match event {
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::RedrawRequested => {
ui.update(self);
ui.update();
self.renderer.update(ui);
self.renderer.draw()
}

View File

@@ -21,7 +21,7 @@ pub struct Renderer {
}
impl Renderer {
pub fn update<Ctx>(&mut self, updates: &mut Ui<Ctx>) {
pub fn update(&mut self, updates: &mut Ui) {
self.ui.update(&self.device, &self.queue, updates);
}