Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(832)

Unified Diff: mojo/services/window_manager/view_target.cc

Issue 724973003: Get event targetting working for mouse events. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Patch beautification. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/services/window_manager/view_target.cc
diff --git a/mojo/services/window_manager/view_target.cc b/mojo/services/window_manager/view_target.cc
index cb378bf3c8c0a7b43fbafc5554a874c051d51f99..3e4afa0004eda6042eee57881f4e3573a27c5361 100644
--- a/mojo/services/window_manager/view_target.cc
+++ b/mojo/services/window_manager/view_target.cc
@@ -4,23 +4,68 @@
#include "mojo/services/window_manager/view_target.h"
+#include "base/lazy_instance.h"
+#include "mojo/converters/geometry/geometry_type_converters.h"
#include "mojo/services/public/cpp/view_manager/view.h"
+#include "mojo/services/window_manager/view_targeter.h"
#include "mojo/services/window_manager/window_manager_app.h"
#include "ui/events/event.h"
#include "ui/events/event_target_iterator.h"
#include "ui/events/event_targeter.h"
+#include "ui/gfx/geometry/rect.h"
+#include "ui/gfx/point3_f.h"
+#include "ui/gfx/transform.h"
namespace mojo {
-class ViewTargeter : public ui::EventTargeter {};
+static base::LazyInstance<std::map<View*, ViewTarget*>> g_target_map =
sky 2014/11/14 01:43:57 We may use the client lib on multiple threads. You
Elliot Glaysher 2014/11/14 21:04:29 Moved everything regarding the mapping to a ported
+ LAZY_INSTANCE_INITIALIZER;
-ViewTarget::ViewTarget(WindowManagerApp* app, View* view_to_wrap)
- : app_(app),
- view_(view_to_wrap) {
+ViewTarget::ViewTarget(View* view_to_wrap)
+ : view_(view_to_wrap) {
+ auto& target_map = g_target_map.Get();
+ DCHECK(target_map.find(view_) == target_map.end());
+ target_map[view_] = this;
+ view_->AddObserver(this);
}
-ViewTarget::~ViewTarget() {
- // We don't own our children or |view_|.
+// static
+ViewTarget* ViewTarget::TargetFromView(View* view) {
+ auto& target_map = g_target_map.Get();
+ auto it = target_map.find(view);
+ return (it != target_map.end()) ? it->second : nullptr;
+}
+
+void ViewTarget::ConvertPointToTarget(const ViewTarget* source,
+ const ViewTarget* target,
+ gfx::Point* point) {
+ // TODO(erg): Do we need to deal with |source| and |target| being in
+ // different trees?
+ if (source == target)
+ return;
+
+ const ViewTarget* root_target = source->GetRoot();
+ CHECK_EQ(root_target, target->GetRoot());
+
+ if (source != root_target)
+ source->ConvertPointForAncestor(root_target, point);
+ if (target != root_target)
+ target->ConvertPointFromAncestor(root_target, point);
+}
+
+std::vector<ViewTarget*> ViewTarget::GetChildren() const {
+ std::vector<ViewTarget*> targets;
+ for (View* child : view_->children())
+ targets.push_back(TargetFromView(child));
+ return targets;
+}
+
+ViewTarget* ViewTarget::GetParent() const {
+ return TargetFromView(view_->parent());
+}
+
+gfx::Rect ViewTarget::GetBounds() const {
+ return view_->bounds().To<gfx::Rect>();
}
bool ViewTarget::HasParent() const {
@@ -31,8 +76,11 @@ bool ViewTarget::IsVisible() const {
return view_->visible();
}
-void ViewTarget::AddChild(ViewTarget* view) {
- children_.push_back(view);
+const ViewTarget* ViewTarget::GetRoot() const {
+ const ViewTarget* root = nullptr;
+ for (const ViewTarget* parent = this; parent; parent = parent->GetParent())
+ root = parent;
+ return root;
}
scoped_ptr<ViewTargeter> ViewTarget::SetEventTargeter(
@@ -42,6 +90,19 @@ scoped_ptr<ViewTargeter> ViewTarget::SetEventTargeter(
return old_targeter.Pass();
}
+bool ViewTarget::GetTargetTransformRelativeTo(const ViewTarget* ancestor,
+ gfx::Transform* transform) const {
+ const ViewTarget* v = this;
+ for (; v && v != ancestor; v = v->GetParent()) {
+ gfx::Transform translation;
+ gfx::Rect bounds = v->GetBounds();
+ translation.Translate(static_cast<float>(bounds.x()),
sky 2014/11/14 01:43:57 Bounds are all ints and we don't support arbitrary
Elliot Glaysher 2014/11/14 21:04:29 Redid all of this operating on Vector2ds.
+ static_cast<float>(bounds.y()));
+ transform->ConcatTransform(translation);
+ }
+ return v == ancestor;
+}
+
bool ViewTarget::CanAcceptEvent(const ui::Event& event) {
// We need to make sure that a touch cancel event and any gesture events it
// creates can always reach the window. This ensures that we receive a valid
@@ -56,25 +117,20 @@ bool ViewTarget::CanAcceptEvent(const ui::Event& event) {
if (!view_->parent())
return true;
- // For located events (i.e. mouse, touch etc.), an assumption is made that
- // windows that don't have a default event-handler cannot process the event
- // (see more in GetWindowForPoint()). This assumption is not made for key
- // events.
- return event.IsKeyEvent() || target_handler();
+ // In aura, we only accept events if this is a key event or if the user
+ // supplied a TargetHandler, usually the aura::WindowDelegate. Here, we're
+ // just forwarding events to other Views which may be in other processes, so
+ // always accept.
+ return true;
}
ui::EventTarget* ViewTarget::GetParentTarget() {
- if (!view_->parent()) {
- // We are the root node.
- return nullptr;
- }
-
- return app_->GetViewTargetForViewId(view_->parent()->id());
+ return TargetFromView(view_->parent());
}
scoped_ptr<ui::EventTargetIterator> ViewTarget::GetChildIterator() const {
return scoped_ptr<ui::EventTargetIterator>(
- new ui::EventTargetIteratorImpl<ViewTarget>(children_));
+ new ui::CopyingEventTargetIteratorImpl<ViewTarget>(GetChildren()));
}
ui::EventTargeter* ViewTarget::GetEventTargeter() {
@@ -83,11 +139,37 @@ ui::EventTargeter* ViewTarget::GetEventTargeter() {
void ViewTarget::ConvertEventToTarget(ui::EventTarget* target,
ui::LocatedEvent* event) {
- // TODO(erg): Actually doing enabling this line requires doing some partially
- // specialized template cruft. Punt for now.
- //
- // event->ConvertLocationToTarget(this,
- // static_cast<ViewTarget*>(target));
+ event->ConvertLocationToTarget(this,
+ static_cast<ViewTarget*>(target));
+}
+
+void ViewTarget::OnViewDestroying(View* view) {
+ view->RemoveObserver(this);
+ delete this;
+}
+
+ViewTarget::~ViewTarget() {
+ g_target_map.Get().erase(view_);
+}
+
+bool ViewTarget::ConvertPointForAncestor(const ViewTarget* ancestor,
+ gfx::Point* point) const {
+ gfx::Transform transform;
+ bool result = GetTargetTransformRelativeTo(ancestor, &transform);
+ gfx::Point3F p(*point);
+ transform.TransformPoint(&p);
+ *point = gfx::ToFlooredPoint(p.AsPointF());
+ return result;
+}
+
+bool ViewTarget::ConvertPointFromAncestor(const ViewTarget* ancestor,
+ gfx::Point* point) const {
+ gfx::Transform transform;
+ bool result = GetTargetTransformRelativeTo(ancestor, &transform);
+ gfx::Point3F p(*point);
+ transform.TransformPointReverse(&p);
+ *point = gfx::ToFlooredPoint(p.AsPointF());
+ return result;
}
} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698