highlighter_controller.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright 2017 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. #include "ash/highlighter/highlighter_controller.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "ash/highlighter/highlighter_gesture_util.h"
  8. #include "ash/highlighter/highlighter_result_view.h"
  9. #include "ash/highlighter/highlighter_view.h"
  10. #include "ash/public/cpp/shell_window_ids.h"
  11. #include "ash/shell.h"
  12. #include "ash/system/palette/palette_utils.h"
  13. #include "base/bind.h"
  14. #include "base/metrics/histogram_macros.h"
  15. #include "base/timer/timer.h"
  16. #include "ui/aura/window.h"
  17. #include "ui/aura/window_tree_host.h"
  18. #include "ui/events/base_event_utils.h"
  19. #include "ui/views/widget/widget.h"
  20. namespace ash {
  21. namespace {
  22. // Bezel stroke detection margin, in DP.
  23. const int kScreenEdgeMargin = 2;
  24. const int kInterruptedStrokeTimeoutMs = 500;
  25. // Adjust the height of the bounding box to match the pen tip height,
  26. // while keeping the same vertical center line. Adjust the width to
  27. // account for the pen tip width.
  28. gfx::RectF AdjustHorizontalStroke(const gfx::RectF& box,
  29. const gfx::SizeF& pen_tip_size) {
  30. return gfx::RectF(box.x() - pen_tip_size.width() / 2,
  31. box.CenterPoint().y() - pen_tip_size.height() / 2,
  32. box.width() + pen_tip_size.width(), pen_tip_size.height());
  33. }
  34. } // namespace
  35. HighlighterController::HighlighterController() {
  36. Shell::Get()->AddPreTargetHandler(this);
  37. }
  38. HighlighterController::~HighlighterController() {
  39. Shell::Get()->RemovePreTargetHandler(this);
  40. }
  41. void HighlighterController::AddObserver(Observer* observer) {
  42. DCHECK(observer);
  43. observers_.AddObserver(observer);
  44. }
  45. void HighlighterController::RemoveObserver(Observer* observer) {
  46. DCHECK(observer);
  47. observers_.RemoveObserver(observer);
  48. }
  49. void HighlighterController::SetExitCallback(base::OnceClosure exit_callback,
  50. bool require_success) {
  51. exit_callback_ = std::move(exit_callback);
  52. require_success_ = require_success;
  53. }
  54. void HighlighterController::UpdateEnabledState(
  55. HighlighterEnabledState enabled_state) {
  56. if (enabled_state_ == enabled_state)
  57. return;
  58. enabled_state_ = enabled_state;
  59. SetEnabled(enabled_state == HighlighterEnabledState::kEnabled);
  60. for (auto& observer : observers_)
  61. observer.OnHighlighterEnabledChanged(enabled_state);
  62. }
  63. void HighlighterController::AbortSession() {
  64. if (enabled_state_ == HighlighterEnabledState::kEnabled)
  65. UpdateEnabledState(HighlighterEnabledState::kDisabledBySessionAbort);
  66. }
  67. void HighlighterController::SetEnabled(bool enabled) {
  68. FastInkPointerController::SetEnabled(enabled);
  69. if (enabled) {
  70. session_start_ = ui::EventTimeForNow();
  71. gesture_counter_ = 0;
  72. recognized_gesture_counter_ = 0;
  73. } else {
  74. UMA_HISTOGRAM_COUNTS_100("Ash.Shelf.Palette.Assistant.GesturesPerSession",
  75. gesture_counter_);
  76. UMA_HISTOGRAM_COUNTS_100(
  77. "Ash.Shelf.Palette.Assistant.GesturesPerSession.Recognized",
  78. recognized_gesture_counter_);
  79. // If |highlighter_view_widget_| is animating it will delete itself when
  80. // done animating. |result_view_widget_| will exist only if
  81. // |highlighter_view_widget_| is animating, and it will also delete itself
  82. // when done animating.
  83. if (highlighter_view_widget_ && !GetHighlighterView()->animating())
  84. DestroyPointerView();
  85. }
  86. }
  87. views::View* HighlighterController::GetPointerView() const {
  88. return const_cast<HighlighterController*>(this)->GetHighlighterView();
  89. }
  90. void HighlighterController::CreatePointerView(
  91. base::TimeDelta presentation_delay,
  92. aura::Window* root_window) {
  93. highlighter_view_widget_ = HighlighterView::Create(
  94. presentation_delay,
  95. Shell::GetContainer(root_window, kShellWindowId_OverlayContainer));
  96. result_view_widget_.reset();
  97. }
  98. void HighlighterController::UpdatePointerView(ui::TouchEvent* event) {
  99. interrupted_stroke_timer_.reset();
  100. GetHighlighterView()->AddNewPoint(event->root_location_f(),
  101. event->time_stamp());
  102. if (event->type() != ui::ET_TOUCH_RELEASED)
  103. return;
  104. gfx::Rect bounds =
  105. highlighter_view_widget_->GetNativeWindow()->GetRootWindow()->bounds();
  106. bounds.Inset(kScreenEdgeMargin);
  107. const gfx::PointF pos = GetHighlighterView()->points().GetNewest().location;
  108. if (bounds.Contains(
  109. gfx::Point(static_cast<int>(pos.x()), static_cast<int>(pos.y())))) {
  110. // The stroke has ended far enough from the screen edge, process it
  111. // immediately.
  112. RecognizeGesture();
  113. return;
  114. }
  115. // The stroke has ended close to the screen edge. Delay gesture recognition
  116. // a little to give the pen a chance to re-enter the screen.
  117. GetHighlighterView()->AddGap();
  118. interrupted_stroke_timer_ = std::make_unique<base::OneShotTimer>();
  119. interrupted_stroke_timer_->Start(
  120. FROM_HERE, base::Milliseconds(kInterruptedStrokeTimeoutMs),
  121. base::BindOnce(&HighlighterController::RecognizeGesture,
  122. base::Unretained(this)));
  123. }
  124. void HighlighterController::RecognizeGesture() {
  125. interrupted_stroke_timer_.reset();
  126. aura::Window* current_window =
  127. highlighter_view_widget_->GetNativeWindow()->GetRootWindow();
  128. const gfx::Rect bounds = current_window->bounds();
  129. const fast_ink::FastInkPoints& points = GetHighlighterView()->points();
  130. gfx::RectF box = points.GetBoundingBoxF();
  131. const HighlighterGestureType gesture_type =
  132. DetectHighlighterGesture(box, HighlighterView::kPenTipSize, points);
  133. if (gesture_type == HighlighterGestureType::kHorizontalStroke) {
  134. UMA_HISTOGRAM_COUNTS_10000("Ash.Shelf.Palette.Assistant.HighlighterLength",
  135. static_cast<int>(box.width()));
  136. box = AdjustHorizontalStroke(box, HighlighterView::kPenTipSize);
  137. } else if (gesture_type == HighlighterGestureType::kClosedShape) {
  138. const float fraction =
  139. box.width() * box.height() / (bounds.width() * bounds.height());
  140. UMA_HISTOGRAM_PERCENTAGE("Ash.Shelf.Palette.Assistant.CircledPercentage",
  141. static_cast<int>(fraction * 100));
  142. }
  143. GetHighlighterView()->Animate(
  144. box.CenterPoint(), gesture_type,
  145. base::BindOnce(&HighlighterController::DestroyHighlighterView,
  146. base::Unretained(this)));
  147. // |box| is not guaranteed to be inside the screen bounds, clip it.
  148. // Not converting |box| to gfx::Rect here to avoid accumulating rounding
  149. // errors, instead converting |bounds| to gfx::RectF.
  150. box.Intersect(
  151. gfx::RectF(bounds.x(), bounds.y(), bounds.width(), bounds.height()));
  152. if (!box.IsEmpty() &&
  153. gesture_type != HighlighterGestureType::kNotRecognized) {
  154. // The window for selection should be the root window to show assistant.
  155. Shell::SetRootWindowForNewWindows(current_window->GetRootWindow());
  156. const gfx::Rect selection_rect = gfx::ToEnclosingRect(box);
  157. for (auto& observer : observers_)
  158. observer.OnHighlighterSelectionRecognized(selection_rect);
  159. result_view_widget_ = HighlighterResultView::Create(current_window);
  160. static_cast<HighlighterResultView*>(result_view_widget_->GetContentsView())
  161. ->Animate(box, gesture_type,
  162. base::BindOnce(&HighlighterController::DestroyResultView,
  163. base::Unretained(this)));
  164. recognized_gesture_counter_++;
  165. CallExitCallback();
  166. } else {
  167. if (!require_success_)
  168. CallExitCallback();
  169. }
  170. gesture_counter_++;
  171. const base::TimeTicks gesture_start = points.GetOldest().time;
  172. if (gesture_counter_ > 1) {
  173. // Up to 3 minutes.
  174. UMA_HISTOGRAM_MEDIUM_TIMES("Ash.Shelf.Palette.Assistant.GestureInterval",
  175. gesture_start - previous_gesture_end_);
  176. }
  177. previous_gesture_end_ = points.GetNewest().time;
  178. // Up to 10 seconds.
  179. UMA_HISTOGRAM_TIMES("Ash.Shelf.Palette.Assistant.GestureDuration",
  180. points.GetNewest().time - gesture_start);
  181. UMA_HISTOGRAM_ENUMERATION("Ash.Shelf.Palette.Assistant.GestureType",
  182. gesture_type,
  183. HighlighterGestureType::kGestureCount);
  184. }
  185. void HighlighterController::DestroyPointerView() {
  186. DestroyHighlighterView();
  187. DestroyResultView();
  188. }
  189. bool HighlighterController::CanStartNewGesture(ui::LocatedEvent* event) {
  190. // Ignore events over the palette.
  191. if (palette_utils::PaletteContainsPointInScreen(event->root_location()))
  192. return false;
  193. return !interrupted_stroke_timer_ &&
  194. FastInkPointerController::CanStartNewGesture(event);
  195. }
  196. bool HighlighterController::ShouldProcessEvent(ui::LocatedEvent* event) {
  197. // Allow mouse clicking when Assistant tool is enabled.
  198. if (event->type() == ui::ET_MOUSE_PRESSED ||
  199. event->type() == ui::ET_MOUSE_RELEASED) {
  200. return false;
  201. }
  202. return FastInkPointerController::ShouldProcessEvent(event);
  203. }
  204. void HighlighterController::DestroyHighlighterView() {
  205. highlighter_view_widget_.reset();
  206. // |interrupted_stroke_timer_| should never be non null when
  207. // |highlighter_view_widget_| is null.
  208. interrupted_stroke_timer_.reset();
  209. }
  210. void HighlighterController::DestroyResultView() {
  211. result_view_widget_.reset();
  212. }
  213. void HighlighterController::CallExitCallback() {
  214. if (!exit_callback_.is_null())
  215. std::move(exit_callback_).Run();
  216. }
  217. HighlighterView* HighlighterController::GetHighlighterView() {
  218. return highlighter_view_widget_
  219. ? static_cast<HighlighterView*>(
  220. highlighter_view_widget_->GetContentsView())
  221. : nullptr;
  222. }
  223. } // namespace ash