FINALLY transition events to global; slow text sending bug tho
This commit is contained in:
1 parent
2537284372
commit
baaeb6b027
21 files changed
+650
-557
No files matched your search
@@ -58,7 +58,7 @@ impl DefaultAppState for Client {
|
||||
.on(CursorSense::click(), move |ctx| {
|
||||
let child = image(include_bytes!("assets/sungals.png"))
|
||||
.center()
|
||||
.add(ctx.ui);
|
||||
.add(ctx.data.ui);
|
||||
span_add_.get_mut().children.push(child);
|
||||
})
|
||||
.sized((150, 150))
|
||||
@@ -113,7 +113,9 @@ impl DefaultAppState for Client {
|
||||
.text_align(Align::LEFT)
|
||||
.wrap(true)
|
||||
.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(&mut Ui::new());
|
||||
texts_.get_mut().children.push(msg_box);
|
||||
})
|
||||
.add(ui);
|
||||
@@ -126,7 +128,7 @@ impl DefaultAppState for Client {
|
||||
add_text.width(rest(1)),
|
||||
Rect::new(Color::GREEN)
|
||||
.on(CursorSense::click(), move |ctx| {
|
||||
ctx.ui.run_event(ctx.state, add_text_, Submit, ());
|
||||
Events::<Submit, _>::run(add_text_.id(), &mut (), ctx.state);
|
||||
})
|
||||
.sized((40, 40)),
|
||||
)
|
||||
|
||||
+26
-11
@@ -2,20 +2,22 @@ use crate::{default::UiState, prelude::*};
|
||||
use std::time::{Duration, Instant};
|
||||
use winit::dpi::{LogicalPosition, LogicalSize};
|
||||
|
||||
event_ctx!(UiState);
|
||||
|
||||
pub struct Selector;
|
||||
|
||||
impl<W: 'static> WidgetAttr<W> for Selector {
|
||||
impl<W: Widget + 'static> WidgetAttr<W> for Selector {
|
||||
type Input = WidgetRef<TextEdit>;
|
||||
|
||||
fn run(ui: &mut Ui, container: WidgetRef<W>, id: Self::Input) {
|
||||
ui.register_event(container, CursorSense::click_or_drag(), move |mut ctx| {
|
||||
let ui = ctx.ui;
|
||||
container.on(CursorSense::click_or_drag(), move |ctx| {
|
||||
let ui = &mut ctx.data.ui;
|
||||
let region = ui.window_region(&id).unwrap();
|
||||
let id_pos = region.top_left;
|
||||
let container_pos = ui.window_region(&container).unwrap().top_left;
|
||||
ctx.data.cursor += container_pos - id_pos;
|
||||
ctx.data.size = region.size();
|
||||
select(ui, id, ctx.state, ctx.data);
|
||||
let pos = ctx.data.pos + container_pos - id_pos;
|
||||
let size = region.size();
|
||||
select(ui, id, ctx.state, pos, size, ctx.data.sense.is_dragging());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -26,18 +28,31 @@ impl WidgetAttr<TextEdit> for Selectable {
|
||||
type Input = ();
|
||||
|
||||
fn run(ui: &mut Ui, id: WidgetRef<TextEdit>, _: Self::Input) {
|
||||
ui.register_event(id, CursorSense::click_or_drag(), move |ctx| {
|
||||
select(ctx.ui, id, ctx.state, ctx.data);
|
||||
id.on(CursorSense::click_or_drag(), move |ctx| {
|
||||
select(
|
||||
ctx.data.ui,
|
||||
id,
|
||||
ctx.state,
|
||||
ctx.data.pos,
|
||||
ctx.data.size,
|
||||
ctx.data.sense.is_dragging(),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn select(ui: &mut Ui, id: WidgetRef<TextEdit>, state: &mut UiState, data: CursorData) {
|
||||
fn select(
|
||||
ui: &mut Ui,
|
||||
id: WidgetRef<TextEdit>,
|
||||
state: &mut UiState,
|
||||
pos: Vec2,
|
||||
size: Vec2,
|
||||
dragging: bool,
|
||||
) {
|
||||
let now = Instant::now();
|
||||
let recent = (now - state.last_click) < Duration::from_millis(300);
|
||||
state.last_click = now;
|
||||
id.get_mut()
|
||||
.select(data.cursor, data.size, data.sense.is_dragging(), recent);
|
||||
id.get_mut().select(pos, size, dragging, recent);
|
||||
if let Some(region) = ui.window_region(&id) {
|
||||
state.window.set_ime_allowed(true);
|
||||
state.window.set_ime_cursor_area(
|
||||
|
||||
+5
-11
@@ -1,15 +1,9 @@
|
||||
use crate::layout::DefaultEvent;
|
||||
use iris_core::layout::Event;
|
||||
|
||||
#[derive(Eq, PartialEq, Hash, Clone)]
|
||||
#[derive(Clone)]
|
||||
pub struct Submit;
|
||||
impl Event for Submit {}
|
||||
|
||||
#[derive(Eq, PartialEq, Hash, Clone)]
|
||||
#[derive(Clone)]
|
||||
pub struct Edited;
|
||||
|
||||
impl DefaultEvent for Submit {
|
||||
type Data = ();
|
||||
}
|
||||
|
||||
impl DefaultEvent for Edited {
|
||||
type Data = ();
|
||||
}
|
||||
impl Event for Edited {}
|
||||
+3
-3
@@ -130,13 +130,13 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
|
||||
ui_state.window.set_ime_allowed(false);
|
||||
}
|
||||
TextInputResult::Submit => {
|
||||
ui.run_event(app_state, sel, Submit, ());
|
||||
Events::<Submit, _>::run(sel.id(), &mut (), app_state);
|
||||
}
|
||||
TextInputResult::Paste => {
|
||||
if let Ok(t) = ui_state.clipboard.get_text() {
|
||||
sel.get_mut().insert(&t);
|
||||
}
|
||||
ui.run_event(app_state, sel, Edited, ());
|
||||
Events::<Edited, _>::run(sel.id(), &mut (), app_state);
|
||||
}
|
||||
TextInputResult::Copy(text) => {
|
||||
if let Err(err) = ui_state.clipboard.set_text(text) {
|
||||
@@ -144,7 +144,7 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
|
||||
}
|
||||
}
|
||||
TextInputResult::Used => {
|
||||
ui.run_event(app_state, sel, Edited, ());
|
||||
Events::<Edited, _>::run(sel.id(), &mut (), app_state);
|
||||
}
|
||||
TextInputResult::Unused => {}
|
||||
}
|
||||
|
||||
+19
-35
@@ -6,14 +6,14 @@ pub mod eventable {
|
||||
|
||||
widget_trait! {
|
||||
pub trait Eventable;
|
||||
fn on<E: Event, Ctx: 'static>(
|
||||
fn on<E: EventAlias, Ctx: 'static>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl WidgetEventFn<Ctx, E::Data, WL::Widget>,
|
||||
f: impl for<'a> EventIdFn<Ctx, <E::Event as Event>::Data<'a>, WL::Widget>,
|
||||
) -> impl WidgetIdFn<WL::Widget> {
|
||||
move |ui| {
|
||||
let id = self.add(ui);
|
||||
ui.register_widget_event(id.weak(), event, f);
|
||||
Events::<E::Event, Ctx>::register(id.weak(), event.into_event(), f);
|
||||
id
|
||||
}
|
||||
}
|
||||
@@ -26,53 +26,37 @@ macro_rules! event_ctx {
|
||||
($ty: ty) => {
|
||||
mod local_event_trait {
|
||||
use super::*;
|
||||
use std::marker::Sized;
|
||||
#[allow(unused_imports)]
|
||||
use $crate::prelude::*;
|
||||
|
||||
#[allow(unused)]
|
||||
pub trait EventableCtx<W: ?Sized, Tag, Ctx: 'static> {
|
||||
fn on<E: Event>(
|
||||
widget_trait! {
|
||||
#[allow(unused)]
|
||||
pub trait EventableCtx;
|
||||
fn on<E: EventAlias>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl WidgetEventFn<Ctx, E::Data, W>,
|
||||
) -> impl WidgetIdFn<W> + EventableCtx<W, IdFnTag, Ctx>;
|
||||
}
|
||||
|
||||
impl<WL: WidgetLike<Tag>, Tag> EventableCtx<WL::Widget, Tag, $ty> for WL {
|
||||
fn on<E: Event>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl WidgetEventFn<$ty, E::Data, WL::Widget>,
|
||||
) -> impl WidgetIdFn<WL::Widget> + EventableCtx<WL::Widget, IdFnTag, $ty> {
|
||||
f: impl for<'a> EventIdFn<$ty, <E::Event as Event>::Data<'a>, WL::Widget>,
|
||||
) -> impl WidgetIdFn<WL::Widget> {
|
||||
eventable::Eventable::on(self, event, f)
|
||||
}
|
||||
}
|
||||
|
||||
use std::marker::Sized;
|
||||
#[allow(unused)]
|
||||
pub trait EventableCtxUi<W: ?Sized, Tag, Ctx: 'static>
|
||||
where
|
||||
WidgetRef<W>: EventableCtx<W, Tag, Ctx>,
|
||||
{
|
||||
fn on<E: Event>(
|
||||
&mut self,
|
||||
widget: WidgetRef<W>,
|
||||
pub trait EventableCtxRef<W: Widget + ?Sized> {
|
||||
fn on<E: EventAlias>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl WidgetEventFn<Ctx, E::Data, W>,
|
||||
f: impl for<'a> EventIdFn<$ty, <E::Event as Event>::Data<'a>, W>,
|
||||
);
|
||||
}
|
||||
|
||||
impl<W: ?Sized + 'static, Tag> EventableCtxUi<W, Tag, $ty> for Ui
|
||||
where
|
||||
WidgetRef<W>: EventableCtx<W, Tag, $ty>,
|
||||
{
|
||||
fn on<E: Event>(
|
||||
&mut self,
|
||||
widget: WidgetRef<W>,
|
||||
impl<W: Widget + ?Sized> EventableCtxRef<W> for WidgetRef<W> {
|
||||
fn on<E: EventAlias>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl WidgetEventFn<$ty, E::Data, W>,
|
||||
f: impl for<'a> EventIdFn<$ty, <E::Event as Event>::Data<'a>, W>,
|
||||
) {
|
||||
self.register_widget_event(widget, event, f);
|
||||
Events::<E::Event, $ty>::register(self, event.into_event(), f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Whitespace-only changes.
+60
-163
@@ -1,8 +1,5 @@
|
||||
use crate::layout::{UiRegion, Vec2};
|
||||
use crate::prelude::*;
|
||||
use crate::{
|
||||
layout::{UiModule, UiRegion, Vec2},
|
||||
util::HashMap,
|
||||
};
|
||||
use std::{
|
||||
ops::{BitOr, Deref, DerefMut},
|
||||
rc::Rc,
|
||||
@@ -26,8 +23,22 @@ pub enum CursorSense {
|
||||
Scroll,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CursorSenses(Vec<CursorSense>);
|
||||
|
||||
impl Event for CursorSenses {
|
||||
type Data<'a> = CursorData<'a>;
|
||||
type State = SensorState;
|
||||
fn should_run(&self, data: &mut Self::Data<'_>) -> bool {
|
||||
if let Some(sense) = should_run(self, data.cursor, data.hover) {
|
||||
data.sense = sense;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CursorSense {
|
||||
pub fn click() -> Self {
|
||||
Self::PressStart(CursorButton::Left)
|
||||
@@ -72,6 +83,16 @@ impl CursorButtons {
|
||||
self.middle.end_frame();
|
||||
self.right.end_frame();
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = (CursorButton, &ActivationState)> {
|
||||
[
|
||||
CursorButton::Left,
|
||||
CursorButton::Middle,
|
||||
CursorButton::Right,
|
||||
]
|
||||
.into_iter()
|
||||
.map(|b| (b, self.select(&b)))
|
||||
}
|
||||
}
|
||||
|
||||
impl CursorState {
|
||||
@@ -100,70 +121,25 @@ pub struct Sensor<Ctx, Data> {
|
||||
pub f: Rc<dyn EventFn<Ctx, Data>>,
|
||||
}
|
||||
|
||||
pub type SensorMap<Ctx, Data> = HashMap<WidgetId, SensorGroup<Ctx, Data>>;
|
||||
pub type SenseShape = UiRegion;
|
||||
pub struct SensorGroup<Ctx, Data> {
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct SensorState {
|
||||
pub hover: ActivationState,
|
||||
pub sensors: Vec<Sensor<Ctx, Data>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CursorData {
|
||||
pub cursor: Vec2,
|
||||
pub struct CursorData<'a> {
|
||||
/// where this widget was hit
|
||||
pub pos: Vec2,
|
||||
pub size: Vec2,
|
||||
pub scroll_delta: Vec2,
|
||||
/// the (first) sense that triggered this event
|
||||
/// the senses are checked in order
|
||||
pub hover: ActivationState,
|
||||
pub cursor: &'a CursorState,
|
||||
pub ui: &'a mut Ui,
|
||||
/// the first sense that triggered this
|
||||
pub sense: CursorSense,
|
||||
}
|
||||
|
||||
pub struct CursorModule<Ctx> {
|
||||
map: SensorMap<Ctx, CursorData>,
|
||||
active: HashMap<usize, HashMap<WidgetId, SenseShape>>,
|
||||
}
|
||||
|
||||
impl<Ctx: 'static> UiModule for CursorModule<Ctx> {
|
||||
fn on_draw(&mut self, inst: &WidgetInstance) {
|
||||
if self.map.contains_key(&inst.id) {
|
||||
self.active
|
||||
.entry(inst.layer)
|
||||
.or_default()
|
||||
.insert(inst.id, inst.region);
|
||||
}
|
||||
}
|
||||
|
||||
fn on_undraw(&mut self, inst: &WidgetInstance) {
|
||||
if let Some(layer) = self.active.get_mut(&inst.layer) {
|
||||
layer.remove(&inst.id);
|
||||
}
|
||||
}
|
||||
|
||||
fn on_remove(&mut self, id: &WidgetId) {
|
||||
self.map.remove(id);
|
||||
for layer in self.active.values_mut() {
|
||||
layer.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
fn on_move(&mut self, inst: &WidgetInstance) {
|
||||
if let Some(map) = self.active.get_mut(&inst.layer)
|
||||
&& let Some(region) = map.get_mut(&inst.id)
|
||||
{
|
||||
*region = inst.region;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx> CursorModule<Ctx> {
|
||||
pub fn merge(&mut self, other: Self) {
|
||||
for (id, group) in other.map {
|
||||
for sensor in group.sensors {
|
||||
self.map.entry(id).or_default().sensors.push(sensor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SensorUi {
|
||||
fn run_sensors<Ctx: 'static>(&mut self, ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2);
|
||||
}
|
||||
@@ -175,55 +151,41 @@ impl SensorUi for Ui {
|
||||
cursor: &CursorState,
|
||||
window_size: Vec2,
|
||||
) {
|
||||
CursorModule::<Ctx>::run(self, ctx, cursor, window_size);
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx: 'static> CursorModule<Ctx> {
|
||||
pub fn run(ui: &mut Ui, ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) {
|
||||
let layers = std::mem::take(&mut ui.data_mut().layers);
|
||||
let mut module = std::mem::take(ui.data_mut().modules.get_mut::<Self>());
|
||||
|
||||
let ui_id = self.id();
|
||||
let layers = std::mem::take(&mut self.data_mut().layers);
|
||||
let mut active = std::mem::take(&mut *Events::<CursorSenses, Ctx>::active(ui_id));
|
||||
for i in layers.indices().rev() {
|
||||
let Some(list) = module.active.get_mut(&i) else {
|
||||
continue;
|
||||
};
|
||||
let mut sensed = false;
|
||||
for (id, shape) in list.iter() {
|
||||
let group = module.map.get_mut(id).unwrap();
|
||||
for (id, state) in active.get_mut(&i).into_iter().flatten() {
|
||||
let shape = self.active().get(id).unwrap().region;
|
||||
let region = shape.to_px(window_size);
|
||||
let in_shape = cursor.exists && region.contains(cursor.pos);
|
||||
group.hover.update(in_shape);
|
||||
if group.hover == ActivationState::Off {
|
||||
state.hover.update(in_shape);
|
||||
if state.hover == ActivationState::Off {
|
||||
continue;
|
||||
}
|
||||
sensed = true;
|
||||
|
||||
for sensor in &mut group.sensors {
|
||||
if let Some(sense) = should_run(&sensor.senses, cursor, group.hover) {
|
||||
let data = CursorData {
|
||||
cursor: cursor.pos - region.top_left,
|
||||
size: region.bot_right - region.top_left,
|
||||
scroll_delta: cursor.scroll_delta,
|
||||
sense,
|
||||
};
|
||||
(sensor.f)(EventCtx {
|
||||
ui,
|
||||
state: ctx,
|
||||
data,
|
||||
});
|
||||
}
|
||||
}
|
||||
let mut data = CursorData {
|
||||
pos: cursor.pos - region.top_left,
|
||||
size: region.bot_right - region.top_left,
|
||||
scroll_delta: cursor.scroll_delta,
|
||||
hover: state.hover,
|
||||
cursor,
|
||||
ui: self,
|
||||
// this does not have any meaning;
|
||||
// might wanna set up Event to have a prepare stage
|
||||
sense: CursorSense::Hovering,
|
||||
};
|
||||
Events::<CursorSenses, Ctx>::run(*id, &mut data, ctx);
|
||||
}
|
||||
if sensed {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let ui_mod = ui.data_mut().modules.get_mut::<Self>();
|
||||
std::mem::swap(ui_mod, &mut module);
|
||||
ui_mod.merge(module);
|
||||
ui.data_mut().layers = layers;
|
||||
*Events::<CursorSenses, Ctx>::active(ui_id) = active;
|
||||
self.data_mut().layers = layers;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -291,66 +253,10 @@ impl ActivationState {
|
||||
}
|
||||
}
|
||||
|
||||
impl Event for CursorSenses {
|
||||
type Module<Ctx: 'static> = CursorModule<Ctx>;
|
||||
type Data = CursorData;
|
||||
}
|
||||
|
||||
impl Event for CursorSense {
|
||||
type Module<Ctx: 'static> = CursorModule<Ctx>;
|
||||
type Data = CursorData;
|
||||
}
|
||||
|
||||
impl<E: Event<Data = <CursorSenses as Event>::Data> + Into<CursorSenses>, Ctx: 'static>
|
||||
EventModule<E, Ctx> for CursorModule<Ctx>
|
||||
{
|
||||
fn register(&mut self, id: WidgetId, senses: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
|
||||
// TODO: does not add to active if currently active
|
||||
self.map.entry(id).or_default().sensors.push(Sensor {
|
||||
senses: senses.into(),
|
||||
f: Rc::new(f),
|
||||
});
|
||||
}
|
||||
|
||||
fn run<'a>(
|
||||
&mut self,
|
||||
id: WidgetId,
|
||||
event: E,
|
||||
) -> Option<impl FnOnce(EventCtx<Ctx, <E as Event>::Data>) + use<'a, Ctx, E>> {
|
||||
let senses = event.into();
|
||||
if let Some(group) = self.map.get_mut(&id) {
|
||||
let fs: Vec<_> = group
|
||||
.sensors
|
||||
.iter_mut()
|
||||
.filter_map(|sensor| {
|
||||
if sensor.senses.iter().any(|s| senses.contains(s)) {
|
||||
Some(sensor.f.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
Some(move |ctx: EventCtx<Ctx, CursorData>| {
|
||||
for f in fs {
|
||||
f(EventCtx {
|
||||
state: ctx.state,
|
||||
ui: ctx.ui,
|
||||
data: ctx.data.clone(),
|
||||
});
|
||||
}
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx, Data> Default for SensorGroup<Ctx, Data> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
hover: Default::default(),
|
||||
sensors: Default::default(),
|
||||
}
|
||||
impl EventAlias for CursorSense {
|
||||
type Event = CursorSenses;
|
||||
fn into_event(self) -> Self::Event {
|
||||
CursorSenses::from(self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -390,12 +296,3 @@ impl BitOr<CursorSense> for CursorSenses {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx> Default for CursorModule<Ctx> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
map: Default::default(),
|
||||
active: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user