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

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: Move on top of the recently committed local view property stuff. 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..7d2411e67436fed35fbb49c396a41f54e9ba65f1 100644
--- a/mojo/services/window_manager/view_target.cc
+++ b/mojo/services/window_manager/view_target.cc
@@ -4,23 +4,67 @@
#include "mojo/services/window_manager/view_target.h"
+#include "mojo/converters/geometry/geometry_type_converters.h"
#include "mojo/services/public/cpp/view_manager/view.h"
+#include "mojo/services/public/cpp/view_manager/view_property.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 {};
+namespace {
-ViewTarget::ViewTarget(WindowManagerApp* app, View* view_to_wrap)
- : app_(app),
- view_(view_to_wrap) {
+DEFINE_OWNED_VIEW_PROPERTY_KEY(ViewTarget, kViewTargetKey, nullptr);
+
+} // namespace
+
+ViewTarget::ViewTarget(View* view_to_wrap)
+ : view_(view_to_wrap) {
+ view_->SetLocalProperty(kViewTargetKey, this);
sky 2014/11/17 22:39:27 DCHECK GetLocalProperty(kViewTargetKey, this) == n
+ view_->AddObserver(this);
+}
+
+// static
+ViewTarget* ViewTarget::TargetFromView(View* view) {
sky 2014/11/17 22:39:27 Keep definitions & declarations in order.
+ return view ? view->GetLocalProperty(kViewTargetKey) : 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);
}
-ViewTarget::~ViewTarget() {
- // We don't own our children or |view_|.
+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 +75,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;
sky 2014/11/17 22:39:27 Should this be set to this first?
Elliot Glaysher 2014/11/17 23:33:01 Rewrote this to not use a parent temporary. (I th
+ for (const ViewTarget* parent = this; parent; parent = parent->GetParent())
+ root = parent;
+ return root;
}
scoped_ptr<ViewTargeter> ViewTarget::SetEventTargeter(
@@ -56,25 +103,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 +125,40 @@ 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);
sky 2014/11/17 22:39:27 Is it necessary to add an observer anymore?
Elliot Glaysher 2014/11/17 23:33:01 It isn't.
+}
+
+ViewTarget::~ViewTarget() {}
+
+bool ViewTarget::ConvertPointForAncestor(const ViewTarget* ancestor,
+ gfx::Point* point) const {
+ gfx::Vector2d offset;
+ bool result = GetTargetOffsetRelativeTo(ancestor, &offset);
+ *point += offset;
+ return result;
+}
+
+bool ViewTarget::ConvertPointFromAncestor(const ViewTarget* ancestor,
+ gfx::Point* point) const {
+ gfx::Vector2d offset;
+ bool result = GetTargetOffsetRelativeTo(ancestor, &offset);
+ *point -= offset;
+ return result;
+}
+
+bool ViewTarget::GetTargetOffsetRelativeTo(const ViewTarget* ancestor,
+ gfx::Vector2d* offset) const {
+ const ViewTarget* v = this;
+ for (; v && v != ancestor; v = v->GetParent()) {
+ gfx::Rect bounds = v->GetBounds();
+ *offset += gfx::Vector2d(bounds.x(), bounds.y());
+ }
+ return v == ancestor;
}
} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698