OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/exo/notification_surface.h" | |
6 | |
7 #include "components/exo/notification_surface_manager.h" | |
8 #include "components/exo/surface.h" | |
9 #include "ui/aura/window.h" | |
10 #include "ui/aura/window_delegate.h" | |
11 #include "ui/base/cursor/cursor.h" | |
12 #include "ui/base/hit_test.h" | |
13 #include "ui/gfx/geometry/rect.h" | |
14 #include "ui/views/widget/widget.h" | |
15 | |
16 namespace exo { | |
17 | |
18 namespace { | |
19 | |
20 class CustomWindowDelegate : public aura::WindowDelegate { | |
21 public: | |
22 explicit CustomWindowDelegate(Surface* surface) : surface_(surface) {} | |
23 ~CustomWindowDelegate() override {} | |
24 | |
25 // Overridden from aura::WindowDelegate: | |
26 gfx::Size GetMinimumSize() const override { return gfx::Size(); } | |
27 gfx::Size GetMaximumSize() const override { return gfx::Size(); } | |
28 void OnBoundsChanged(const gfx::Rect& old_bounds, | |
29 const gfx::Rect& new_bounds) override {} | |
30 gfx::NativeCursor GetCursor(const gfx::Point& point) override { | |
31 return surface_->GetCursor(); | |
32 } | |
33 int GetNonClientComponent(const gfx::Point& point) const override { | |
34 return HTNOWHERE; | |
35 } | |
36 bool ShouldDescendIntoChildForEventHandling( | |
37 aura::Window* child, | |
38 const gfx::Point& location) override { | |
39 return true; | |
40 } | |
41 bool CanFocus() override { return true; } | |
42 void OnCaptureLost() override {} | |
43 void OnPaint(const ui::PaintContext& context) override {} | |
44 void OnDeviceScaleFactorChanged(float device_scale_factor) override {} | |
45 void OnWindowDestroying(aura::Window* window) override {} | |
46 void OnWindowDestroyed(aura::Window* window) override { delete this; } | |
47 void OnWindowTargetVisibilityChanged(bool visible) override {} | |
48 bool HasHitTestMask() const override { return surface_->HasHitTestMask(); } | |
49 void GetHitTestMask(gfx::Path* mask) const override { | |
50 surface_->GetHitTestMask(mask); | |
51 } | |
52 void OnKeyEvent(ui::KeyEvent* event) override { | |
53 // Propagates the key event upto the top-level views Widget so that we can | |
54 // trigger proper events in the views/ash level there. Event handling for | |
55 // Surfaces is done in a post event handler in keyboard.cc. | |
56 views::Widget* widget = | |
57 views::Widget::GetTopLevelWidgetForNativeView(surface_->window()); | |
58 if (widget) | |
59 widget->OnKeyEvent(event); | |
60 } | |
61 | |
62 private: | |
63 Surface* const surface_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(CustomWindowDelegate); | |
66 }; | |
67 | |
68 } // namespace | |
69 | |
70 NotificationSurface::NotificationSurface(NotificationSurfaceManager* manager, | |
71 Surface* surface, | |
72 const std::string& notification_id) | |
73 : manager_(manager), | |
74 surface_(surface), | |
75 notification_id_(notification_id), | |
76 window_(new aura::Window(new CustomWindowDelegate(surface))) { | |
77 surface_->SetSurfaceDelegate(this); | |
78 surface_->AddSurfaceObserver(this); | |
79 | |
80 window_->SetType(ui::wm::WINDOW_TYPE_CONTROL); | |
81 window_->SetName("ExoNotificationSurface"); | |
82 window_->Init(ui::LAYER_NOT_DRAWN); | |
83 window_->set_owned_by_parent(false); | |
84 | |
85 // TODO(xiyuan): Fix after Surface no longer has an aura::Window. | |
86 window_->AddChild(surface_->window()); | |
87 surface_->window()->Show(); | |
88 } | |
89 | |
90 NotificationSurface::~NotificationSurface() { | |
91 if (surface_) { | |
92 surface_->SetSurfaceDelegate(nullptr); | |
93 surface_->RemoveSurfaceObserver(this); | |
94 } | |
95 if (added_to_manager_) | |
96 manager_->RemoveSurface(this); | |
97 } | |
98 | |
99 gfx::Size NotificationSurface::GetSize() const { | |
100 return surface_->content_size(); | |
101 } | |
102 | |
103 void NotificationSurface::OnSurfaceCommit() { | |
104 surface_->CheckIfSurfaceHierarchyNeedsCommitToNewSurfaces(); | |
105 surface_->CommitSurfaceHierarchy(); | |
106 | |
107 gfx::Rect bounds = window_->bounds(); | |
108 if (bounds.size() != surface_->content_size()) { | |
109 bounds.set_size(surface_->content_size()); | |
110 window_->SetBounds(bounds); | |
111 } | |
112 | |
113 // Defer AddSurface until there are contents to show. | |
114 if (!added_to_manager_ && !surface_->content_size().IsEmpty()) { | |
115 added_to_manager_ = true; | |
116 manager_->AddSurface(this); | |
117 } | |
118 } | |
119 | |
120 bool NotificationSurface::IsSurfaceSynchronized() const { | |
121 return false; | |
122 } | |
123 | |
124 void NotificationSurface::OnSurfaceDestroying(Surface* surface) { | |
125 window_.reset(); | |
126 surface->RemoveSurfaceObserver(this); | |
127 surface_ = nullptr; | |
128 } | |
129 | |
130 } // namespace exo | |
OLD | NEW |