Merge lp:~3v1n0/unity/input-monitor-panel into lp:unity

Proposed by Marco Trevisan (Treviño)
Status: Merged
Approved by: Andrea Azzarone
Approved revision: no longer in the source branch.
Merged at revision: 4185
Proposed branch: lp:~3v1n0/unity/input-monitor-panel
Merge into: lp:unity
Prerequisite: lp:~3v1n0/unity/input-monitor-decorations
Diff against target: 277 lines (+51/-58)
4 files modified
panel/PanelView.cpp (+43/-52)
panel/PanelView.h (+4/-6)
tests/test_panel_controller.cpp (+2/-0)
tests/test_panel_view.cpp (+2/-0)
To merge this branch: bzr merge lp:~3v1n0/unity/input-monitor-panel
Reviewer Review Type Date Requested Status
Andrea Azzarone (community) Approve
Review via email: mp+303550@code.launchpad.net

Commit message

PanelView: use InputMonitor to track menu events

Description of the change

In order to get menus properly working with this branch, this one is also needed:
https://code.launchpad.net/~3v1n0/unity/panel-service-menus-deactivation-discard/+merge/303549

To post a comment you must log in.
Revision history for this message
Andrea Azzarone (azzar1) wrote :

Thanks

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'panel/PanelView.cpp'
2--- panel/PanelView.cpp 2016-08-12 13:57:19 +0000
3+++ panel/PanelView.cpp 2016-08-22 13:06:46 +0000
4@@ -23,6 +23,7 @@
5
6 #include <UnityCore/GLibWrapper.h>
7
8+#include "unity-shared/InputMonitor.h"
9 #include "unity-shared/PanelStyle.h"
10 #include "unity-shared/RawPixel.h"
11 #include "unity-shared/TextureCache.h"
12@@ -42,6 +43,7 @@
13 namespace
14 {
15 const RawPixel TRIANGLE_THRESHOLD = 5_em;
16+const double SCRUB_VELOCITY_THRESHOLD = 0.05;
17 const int refine_gradient_midpoint = 959;
18 }
19
20@@ -52,6 +54,7 @@
21 : View(NUX_FILE_LINE_PARAM)
22 , parent_(parent)
23 , remote_(menus->Indicators())
24+ , last_pointer_time_(0)
25 , is_dirty_(true)
26 , opacity_maximized_toggle_(false)
27 , needs_geo_sync_(false)
28@@ -648,7 +651,9 @@
29 }
30 }
31
32-static bool PointInTriangle(nux::Point const& p, nux::Point const& t0, nux::Point const& t1, nux::Point const& t2)
33+namespace
34+{
35+bool PointInTriangle(nux::Point const& p, nux::Point const& t0, nux::Point const& t1, nux::Point const& t2)
36 {
37 int s = t0.y * t2.x - t0.x * t2.y + (t2.y - t0.y) * p.x + (t0.x - t2.x) * p.y;
38 int t = t0.x * t1.y - t0.y * t1.x + (t0.y - t1.y) * p.x + (t1.x - t0.x) * p.y;
39@@ -667,47 +672,43 @@
40 return s > 0 && t > 0 && (s + t) < A;
41 }
42
43-static double GetMouseVelocity(nux::Point const& p0, nux::Point const& p1, util::Timer &timer)
44+double GetMouseVelocity(nux::Point const& p0, nux::Point const& p1, Time time_delta)
45 {
46 int dx, dy;
47 double speed;
48- auto millis = timer.ElapsedMicroSeconds();
49
50- if (millis == 0)
51+ if (time_delta == 0)
52 return 1;
53
54 dx = p0.x - p1.x;
55 dy = p0.y - p1.y;
56
57- speed = sqrt(dx * dx + dy * dy) / millis * 1000;
58+ speed = sqrt(dx * dx + dy * dy) / time_delta;
59
60 return speed;
61 }
62+} // anonymous namespace
63
64-bool PanelView::TrackMenuPointer()
65+void PanelView::OnActiveEntryEvent(XEvent const& e)
66 {
67- nux::Point const& mouse = nux::GetGraphicsDisplay()->GetMouseScreenCoord();
68- double speed = GetMouseVelocity(mouse, tracked_pointer_pos_, mouse_tracker_timer_);
69-
70- mouse_tracker_timer_.Reset();
71+ if (e.type != MotionNotify)
72+ return;
73+
74+ double scale = Settings::Instance().em(monitor_)->DPIScale();
75+ nux::Point mouse(e.xmotion.x_root, e.xmotion.y_root);
76+ double speed = GetMouseVelocity(mouse, tracked_pointer_pos_, e.xmotion.time - last_pointer_time_);
77+
78 tracked_pointer_pos_ = mouse;
79-
80- double scale = Settings::Instance().em(monitor_)->DPIScale();
81- if (speed > 0 && PointInTriangle(mouse,
82- nux::Point(triangle_top_corner_.x, std::max(triangle_top_corner_.y - TRIANGLE_THRESHOLD.CP(scale), 0)),
83- nux::Point(menu_geo_.x, menu_geo_.y),
84- nux::Point(menu_geo_.x + menu_geo_.width, menu_geo_.y)))
85- {
86- return true;
87- }
88-
89- if (mouse != triangle_top_corner_)
90- {
91- triangle_top_corner_ = mouse;
92- OnMenuPointerMoved(mouse.x, mouse.y);
93- }
94-
95- return true;
96+ last_pointer_time_ = e.xmotion.time;
97+
98+ if (speed > SCRUB_VELOCITY_THRESHOLD &&
99+ PointInTriangle(mouse, {mouse.x, std::max(mouse.y - TRIANGLE_THRESHOLD.CP(scale), 0)},
100+ menu_geo_.GetPosition(), {menu_geo_.x + menu_geo_.width, menu_geo_.y}))
101+ {
102+ return;
103+ }
104+
105+ OnMenuPointerMoved(mouse.x, mouse.y);
106 }
107
108 void PanelView::OnEntryActivated(std::string const& panel, std::string const& entry_id, nux::Rect const& menu_geo)
109@@ -715,41 +716,33 @@
110 if (!panel.empty() && panel != GetPanelName())
111 return;
112
113+ bool active = !entry_id.empty();
114+ auto const& activation_cb = sigc::mem_fun(this, &PanelView::OnActiveEntryEvent);
115 menu_geo_ = menu_geo;
116
117- bool active = !entry_id.empty();
118- if (active && !track_menu_pointer_timeout_)
119+ if (active)
120 {
121- //
122- // Track menus being scrubbed at 60Hz (about every 16 millisec)
123- // It might sound ugly, but it's far nicer (and more responsive) than the
124- // code it replaces which used to capture motion events in another process
125- // (unity-panel-service) and send them to us over dbus.
126- // NOTE: The reason why we have to use a timer instead of tracking motion
127- // events is because the motion events will never be delivered to this
128- // process. All the motion events will go to unity-panel-service while
129- // scrubbing because the active panel menu has (needs) the pointer grab.
130- //
131- mouse_tracker_timer_.Reset();
132- triangle_top_corner_ = nux::GetGraphicsDisplay()->GetMouseScreenCoord();
133- track_menu_pointer_timeout_.reset(new glib::Timeout(16));
134- track_menu_pointer_timeout_->Run(sigc::mem_fun(this, &PanelView::TrackMenuPointer));
135+ auto& im = input::Monitor::Get();
136+ if (im.RegisterClient(input::Events::POINTER, activation_cb))
137+ {
138+ last_pointer_time_ = 0;
139+ ActivateEntry(entry_id);
140+ }
141+
142+ if (overlay_is_open_)
143+ ubus_manager_.SendMessage(UBUS_OVERLAY_CLOSE_REQUEST);
144 }
145- else if (!active)
146+ else
147 {
148- track_menu_pointer_timeout_.reset();
149+ input::Monitor::Get().UnregisterClient(activation_cb);
150 menu_view_->NotifyAllMenusClosed();
151- tracked_pointer_pos_ = {-1, -1};
152 }
153-
154- if (overlay_is_open_)
155- ubus_manager_.SendMessage(UBUS_OVERLAY_CLOSE_REQUEST);
156 }
157
158 void PanelView::OnEntryShowMenu(std::string const& entry_id, unsigned xid,
159 int x, int y, unsigned button)
160 {
161- if (!track_menu_pointer_timeout_)
162+ if (menu_geo_.IsNull())
163 {
164 // This is ugly... But Nux fault!
165 menu_view_->IgnoreLeaveEvents(true);
166@@ -768,7 +761,6 @@
167 {
168 // Since this only happens on keyboard events, we need to prevent that the
169 // pointer tracker would select another entry.
170- tracked_pointer_pos_ = nux::GetGraphicsDisplay()->GetMouseScreenCoord();
171 return true;
172 }
173
174@@ -785,7 +777,6 @@
175 {
176 // Since this only happens on keyboard events, we need to prevent that the
177 // pointer tracker would select another entry.
178- tracked_pointer_pos_ = nux::GetGraphicsDisplay()->GetMouseScreenCoord();
179 return true;
180 }
181
182
183=== modified file 'panel/PanelView.h'
184--- panel/PanelView.h 2016-03-30 18:18:07 +0000
185+++ panel/PanelView.h 2016-08-22 13:06:46 +0000
186@@ -35,7 +35,6 @@
187 #include "unity-shared/Introspectable.h"
188 #include "unity-shared/MenuManager.h"
189 #include "unity-shared/MockableBaseWindow.h"
190-#include "unity-shared/Timer.h"
191 #include "PanelMenuView.h"
192 #include "PanelTray.h"
193 #include "PanelIndicatorsView.h"
194@@ -88,7 +87,6 @@
195 void OnObjectAdded(indicator::Indicator::Ptr const& proxy);
196 void OnObjectRemoved(indicator::Indicator::Ptr const& proxy);
197 void OnIndicatorViewUpdated();
198- void OnMenuPointerMoved(int x, int y);
199 void OnEntryActivated(std::string const& panel, std::string const& entry_id, nux::Rect const& geo);
200 void OnEntryShowMenu(std::string const& entry_id, unsigned xid, int x, int y, unsigned button);
201
202@@ -100,6 +98,8 @@
203 void OnSpreadInitiate();
204 void OnSpreadTerminate();
205 void OnLowGfxChanged();
206+ void OnMenuPointerMoved(int x, int y);
207+ void OnActiveEntryEvent(XEvent const&);
208 void EnableOverlayMode(bool);
209 void LoadTextures();
210
211@@ -109,7 +109,6 @@
212 bool IsTransparent();
213 void UpdateBackground();
214 void ForceUpdateBackground();
215- bool TrackMenuPointer();
216 void SyncGeometries();
217 void AddPanelView(PanelIndicatorsView* child, unsigned int stretchFactor);
218
219@@ -134,8 +133,8 @@
220 std::unique_ptr<nux::AbstractPaintLayer> bg_refine_single_column_layer_;
221
222 std::string active_overlay_;
223- nux::Point tracked_pointer_pos_, triangle_top_corner_;
224- util::Timer mouse_tracker_timer_;
225+ nux::Point tracked_pointer_pos_;
226+ Time last_pointer_time_;
227
228 bool is_dirty_;
229 bool opacity_maximized_toggle_;
230@@ -152,7 +151,6 @@
231 BackgroundEffectHelper bg_effect_helper_;
232 nux::ObjectPtr<nux::IOpenGLBaseTexture> bg_blur_texture_;
233 UBusManager ubus_manager_;
234- glib::Source::UniquePtr track_menu_pointer_timeout_;
235 };
236
237 } // namespace panel
238
239=== modified file 'tests/test_panel_controller.cpp'
240--- tests/test_panel_controller.cpp 2014-03-21 04:40:12 +0000
241+++ tests/test_panel_controller.cpp 2016-08-22 13:06:46 +0000
242@@ -19,6 +19,7 @@
243
244 #include <gmock/gmock.h>
245
246+#include "InputMonitor.h"
247 #include "PanelController.h"
248 #include "PanelStyle.h"
249 #include "PanelView.h"
250@@ -46,6 +47,7 @@
251 menu::MockManager::Ptr menus;
252 ui::EdgeBarrierController::Ptr edge_barriers;
253 launcher::Options::Ptr options;
254+ input::Monitor im;
255 };
256
257 TEST_F(TestPanelController, Construction)
258
259=== modified file 'tests/test_panel_view.cpp'
260--- tests/test_panel_view.cpp 2014-12-12 22:33:24 +0000
261+++ tests/test_panel_view.cpp 2016-08-22 13:06:46 +0000
262@@ -25,6 +25,7 @@
263 #include "unity-shared/PanelStyle.h"
264 #include "unity-shared/UBusMessages.h"
265 #include "unity-shared/UBusWrapper.h"
266+ #include "InputMonitor.h"
267
268 #include "mock_menu_manager.h"
269 #include "test_standalone_wm.h"
270@@ -43,6 +44,7 @@
271 nux::ObjectPtr<MockableBaseWindow> window_;
272 nux::ObjectPtr<PanelView> panel_view_;
273 testwrapper::StandaloneWM WM;
274+ input::Monitor im;
275
276 TestPanelView()
277 : window_(new MockableBaseWindow())