From eed7159a065266fd7d70513c81fd6064286f4dcf Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Sun, 13 Oct 2013 20:33:42 +0200 Subject: [PATCH] Improve event processing semantics a little. Add a safety check I'm not sure is needed. --- ui/view.cpp | 28 +++++++++++++++++++++------- ui/view.h | 8 +++++--- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/ui/view.cpp b/ui/view.cpp index 59ab6ca961..54745f8b35 100644 --- a/ui/view.cpp +++ b/ui/view.cpp @@ -26,7 +26,7 @@ struct DispatchQueueItem { EventParams params; }; -std::queue g_dispatchQueue; +std::deque g_dispatchQueue; void EventTriggered(Event *e, EventParams params) { @@ -35,7 +35,7 @@ void EventTriggered(Event *e, EventParams params) { DispatchQueueItem item; item.e = e; item.params = params; - g_dispatchQueue.push(item); + g_dispatchQueue.push_front(item); } void DispatchEvents() { @@ -43,11 +43,19 @@ void DispatchEvents() { while (!g_dispatchQueue.empty()) { DispatchQueueItem item = g_dispatchQueue.back(); - g_dispatchQueue.pop(); - item.e->Dispatch(item.params); + g_dispatchQueue.pop_back(); + if (item.e) { + item.e->Dispatch(item.params); + } } } +void RemoveQueuedEvents(View *v) { + for (int i = 0; i < g_dispatchQueue.size(); i++) { + if (g_dispatchQueue[i].params.v == v) + g_dispatchQueue.erase(g_dispatchQueue.begin() + i); + } +} View *GetFocusedView() { return focusedView; @@ -107,10 +115,16 @@ EventReturn Event::Dispatch(EventParams &e) { for (auto iter = handlers_.begin(); iter != handlers_.end(); ++iter) { if ((iter->func)(e) == UI::EVENT_DONE) { // Event is handled, stop looping immediately. This event might even have gotten deleted. - eventHandled = true; + return UI::EVENT_DONE; } } - return eventHandled ? UI::EVENT_DONE : UI::EVENT_SKIPPED; + return UI::EVENT_SKIPPED; +} + +View::~View() { + if (HasFocus()) + SetFocusedView(0); + RemoveQueuedEvents(this); } void View::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) { @@ -374,7 +388,7 @@ void PopupHeader::Draw(UIContext &dc) { EventReturn CheckBox::OnClicked(EventParams &e) { if (toggle_) *toggle_ = !(*toggle_); - return EVENT_DONE; + return EVENT_CONTINUE; // It's safe to keep processing events. } void CheckBox::Draw(UIContext &dc) { diff --git a/ui/view.h b/ui/view.h index 9e6a07b45d..9e2aa1085f 100644 --- a/ui/view.h +++ b/ui/view.h @@ -180,9 +180,11 @@ enum MeasureSpecType { AT_MOST, }; +// I hope I can find a way to simplify this one day. enum EventReturn { - EVENT_DONE, - EVENT_SKIPPED, + EVENT_DONE, // Return this when no other view may process this event, for example if you changed the view hierarchy + EVENT_SKIPPED, // Return this if you ignored an event + EVENT_CONTINUE, // Return this if it's safe to send this event along. This hsould normally be the default choice but often EVENT_DONE is necessary. }; enum FocusFlags { @@ -292,7 +294,7 @@ public: layoutParams_.reset(new LayoutParams()); } - virtual ~View() { if (HasFocus()) SetFocusedView(0); } + virtual ~View(); // Please note that Touch is called ENTIRELY asynchronously from drawing! // Can even be called on a different thread! This is to really minimize latency, and decouple