remove context generic
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user