OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/magnifier/magnification_controller.h" | 5 #include "ash/magnifier/magnification_controller.h" |
6 | 6 |
7 #include "ash/accelerators/accelerator_controller.h" | 7 #include "ash/accelerators/accelerator_controller.h" |
8 #include "ash/accessibility_delegate.h" | 8 #include "ash/accessibility_delegate.h" |
9 #include "ash/ash_switches.h" | 9 #include "ash/ash_switches.h" |
10 #include "ash/display/root_window_transformers.h" | 10 #include "ash/display/root_window_transformers.h" |
11 #include "ash/host/ash_window_tree_host.h" | 11 #include "ash/host/ash_window_tree_host.h" |
12 #include "ash/host/root_window_transformer.h" | 12 #include "ash/host/root_window_transformer.h" |
13 #include "ash/root_window_controller.h" | 13 #include "ash/root_window_controller.h" |
14 #include "ash/screen_util.h" | |
14 #include "ash/shell.h" | 15 #include "ash/shell.h" |
15 #include "ash/system/tray/system_tray_delegate.h" | 16 #include "ash/system/tray/system_tray_delegate.h" |
16 #include "base/command_line.h" | 17 #include "base/command_line.h" |
17 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
18 #include "ui/aura/client/aura_constants.h" | 19 #include "ui/aura/client/aura_constants.h" |
19 #include "ui/aura/client/cursor_client.h" | 20 #include "ui/aura/client/cursor_client.h" |
20 #include "ui/aura/window.h" | 21 #include "ui/aura/window.h" |
21 #include "ui/aura/window_tree_host.h" | 22 #include "ui/aura/window_tree_host.h" |
22 #include "ui/base/ime/input_method.h" | 23 #include "ui/base/ime/input_method.h" |
23 #include "ui/base/ime/input_method_observer.h" | 24 #include "ui/base/ime/input_method_observer.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 void SetEnabled(bool enabled) override; | 82 void SetEnabled(bool enabled) override; |
82 bool IsEnabled() const override; | 83 bool IsEnabled() const override; |
83 void SetScale(float scale, bool animate) override; | 84 void SetScale(float scale, bool animate) override; |
84 float GetScale() const override { return scale_; } | 85 float GetScale() const override { return scale_; } |
85 void MoveWindow(int x, int y, bool animate) override; | 86 void MoveWindow(int x, int y, bool animate) override; |
86 void MoveWindow(const gfx::Point& point, bool animate) override; | 87 void MoveWindow(const gfx::Point& point, bool animate) override; |
87 gfx::Point GetWindowPosition() const override { | 88 gfx::Point GetWindowPosition() const override { |
88 return gfx::ToFlooredPoint(origin_); | 89 return gfx::ToFlooredPoint(origin_); |
89 } | 90 } |
90 void SetScrollDirection(ScrollDirection direction) override; | 91 void SetScrollDirection(ScrollDirection direction) override; |
92 gfx::Rect GetViewportRect() const override; | |
93 void HandleFocusedNodeChanged( | |
94 bool is_editable_node, | |
95 const gfx::Rect& node_bounds_in_screen) override; | |
91 | 96 |
92 // For test | 97 // For test |
93 gfx::Point GetPointOfInterestForTesting() override { | 98 gfx::Point GetPointOfInterestForTesting() override { |
94 return point_of_interest_; | 99 return point_of_interest_; |
95 } | 100 } |
96 | 101 |
97 private: | 102 private: |
98 // ui::ImplicitAnimationObserver overrides: | 103 // ui::ImplicitAnimationObserver overrides: |
99 void OnImplicitAnimationsCompleted() override; | 104 void OnImplicitAnimationsCompleted() override; |
100 | 105 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 bool redraw_original_root_window); | 143 bool redraw_original_root_window); |
139 | 144 |
140 // Returns if the magnification scale is 1.0 or not (larger then 1.0). | 145 // Returns if the magnification scale is 1.0 or not (larger then 1.0). |
141 bool IsMagnified() const; | 146 bool IsMagnified() const; |
142 | 147 |
143 // Returns the rect of the magnification window. | 148 // Returns the rect of the magnification window. |
144 gfx::RectF GetWindowRectDIP(float scale) const; | 149 gfx::RectF GetWindowRectDIP(float scale) const; |
145 // Returns the size of the root window. | 150 // Returns the size of the root window. |
146 gfx::Size GetHostSizeDIP() const; | 151 gfx::Size GetHostSizeDIP() const; |
147 | 152 |
148 // Correct the givin scale value if nessesary. | 153 // Correct the given scale value if necessary. |
149 void ValidateScale(float* scale); | 154 void ValidateScale(float* scale); |
150 | 155 |
151 // ui::EventHandler overrides: | 156 // ui::EventHandler overrides: |
152 void OnMouseEvent(ui::MouseEvent* event) override; | 157 void OnMouseEvent(ui::MouseEvent* event) override; |
153 void OnScrollEvent(ui::ScrollEvent* event) override; | 158 void OnScrollEvent(ui::ScrollEvent* event) override; |
154 void OnTouchEvent(ui::TouchEvent* event) override; | 159 void OnTouchEvent(ui::TouchEvent* event) override; |
155 | 160 |
156 // Moves the view port when |point| is located within | 161 // Moves the view port when |point| is located within |
157 // |x_panning_margin| and |y_pannin_margin| to the edge of the visible | 162 // |x_panning_margin| and |y_pannin_margin| to the edge of the visible |
158 // window region. The view port will be moved so that the |point| will be | 163 // window region. The view port will be moved so that the |point| will be |
159 // moved to the point where it has |x_target_margin| and |y_target_margin| | 164 // moved to the point where it has |x_target_margin| and |y_target_margin| |
160 // to the edge of the visible region. | 165 // to the edge of the visible region. |
161 void MoveMagnifierWindow(const gfx::Point& point, | 166 void MoveMagnifierWindowFollowPoint(const gfx::Point& point, |
162 int x_panning_margin, | 167 int x_panning_margin, |
163 int y_panning_margin, | 168 int y_panning_margin, |
164 int x_target_margin, | 169 int x_target_margin, |
165 int y_target_margin); | 170 int y_target_margin); |
171 | |
172 // Moves the viewport so that |rect| is fully visible. If |rect| is larger | |
173 // than the viewport horizontally or vertically, the viewport will be moved | |
174 // to center the |rect| in that dimension. | |
175 void MoveMaginifierWindowFollowRect(const gfx::Rect& rect); | |
dcheng
2014/12/15 23:43:29
MoveMagnifierWindowFollowRect.
jennyz
2014/12/16 00:08:57
Done.
| |
166 | 176 |
167 // ui::InputMethodObserver: | 177 // ui::InputMethodObserver: |
168 void OnTextInputTypeChanged(const ui::TextInputClient* client) override {} | 178 void OnTextInputTypeChanged(const ui::TextInputClient* client) override {} |
169 void OnFocus() override {} | 179 void OnFocus() override {} |
170 void OnBlur() override {} | 180 void OnBlur() override {} |
171 void OnTextInputStateChanged(const ui::TextInputClient* client) override {} | 181 void OnTextInputStateChanged(const ui::TextInputClient* client) override {} |
172 void OnInputMethodDestroyed(const ui::InputMethod* input_method) override {} | 182 void OnInputMethodDestroyed(const ui::InputMethod* input_method) override {} |
173 void OnShowImeIfNeeded() override {} | 183 void OnShowImeIfNeeded() override {} |
174 void OnCaretBoundsChanged(const ui::TextInputClient* client) override; | 184 void OnCaretBoundsChanged(const ui::TextInputClient* client) override; |
175 | 185 |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
346 break; | 356 break; |
347 } | 357 } |
348 RedrawDIP(new_origin, scale_, true); | 358 RedrawDIP(new_origin, scale_, true); |
349 } | 359 } |
350 | 360 |
351 void MagnificationControllerImpl::OnMouseMove(const gfx::Point& location) { | 361 void MagnificationControllerImpl::OnMouseMove(const gfx::Point& location) { |
352 DCHECK(root_window_); | 362 DCHECK(root_window_); |
353 | 363 |
354 gfx::Point mouse(location); | 364 gfx::Point mouse(location); |
355 int margin = kPanningMergin / scale_; // No need to consider DPI. | 365 int margin = kPanningMergin / scale_; // No need to consider DPI. |
356 MoveMagnifierWindow(mouse, margin, margin, margin, margin); | 366 MoveMagnifierWindowFollowPoint(mouse, margin, margin, margin, margin); |
367 } | |
368 | |
369 gfx::Rect MagnificationControllerImpl::GetViewportRect() const { | |
370 return gfx::ToEnclosingRect(GetWindowRectDIP(scale_)); | |
371 } | |
372 | |
373 void MagnificationControllerImpl::HandleFocusedNodeChanged( | |
374 bool is_editable_node, | |
375 const gfx::Rect& node_bounds_in_screen) { | |
376 // The editable node is handled by OnCaretBoundsChanged. | |
377 if (is_editable_node) | |
378 return; | |
379 | |
380 gfx::Rect node_bounds_in_root = | |
381 ScreenUtil::ConvertRectFromScreen(root_window_, node_bounds_in_screen); | |
382 if (GetViewportRect().Contains(node_bounds_in_root)) | |
383 return; | |
384 | |
385 MoveMaginifierWindowFollowRect(node_bounds_in_root); | |
357 } | 386 } |
358 | 387 |
359 void MagnificationControllerImpl::AfterAnimationMoveCursorTo( | 388 void MagnificationControllerImpl::AfterAnimationMoveCursorTo( |
360 const gfx::Point& location) { | 389 const gfx::Point& location) { |
361 DCHECK(root_window_); | 390 DCHECK(root_window_); |
362 | 391 |
363 aura::client::CursorClient* cursor_client = | 392 aura::client::CursorClient* cursor_client = |
364 aura::client::GetCursorClient(root_window_); | 393 aura::client::GetCursorClient(root_window_); |
365 if (cursor_client) { | 394 if (cursor_client) { |
366 // When cursor is invisible, do not move or show the cursor after the | 395 // When cursor is invisible, do not move or show the cursor after the |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
590 void MagnificationControllerImpl::OnTouchEvent(ui::TouchEvent* event) { | 619 void MagnificationControllerImpl::OnTouchEvent(ui::TouchEvent* event) { |
591 aura::Window* target = static_cast<aura::Window*>(event->target()); | 620 aura::Window* target = static_cast<aura::Window*>(event->target()); |
592 aura::Window* current_root = target->GetRootWindow(); | 621 aura::Window* current_root = target->GetRootWindow(); |
593 if (current_root == root_window_) { | 622 if (current_root == root_window_) { |
594 gfx::Rect root_bounds = current_root->bounds(); | 623 gfx::Rect root_bounds = current_root->bounds(); |
595 if (root_bounds.Contains(event->root_location())) | 624 if (root_bounds.Contains(event->root_location())) |
596 point_of_interest_ = event->root_location(); | 625 point_of_interest_ = event->root_location(); |
597 } | 626 } |
598 } | 627 } |
599 | 628 |
600 void MagnificationControllerImpl::MoveMagnifierWindow(const gfx::Point& point, | 629 void MagnificationControllerImpl::MoveMagnifierWindowFollowPoint( |
601 int x_panning_margin, | 630 const gfx::Point& point, |
602 int y_panning_margin, | 631 int x_panning_margin, |
603 int x_target_margin, | 632 int y_panning_margin, |
604 int y_target_margin) { | 633 int x_target_margin, |
634 int y_target_margin) { | |
605 DCHECK(root_window_); | 635 DCHECK(root_window_); |
606 int x = origin_.x(); | |
607 int y = origin_.y(); | |
608 bool start_zoom = false; | 636 bool start_zoom = false; |
609 | 637 |
610 const gfx::Rect window_rect = gfx::ToEnclosingRect(GetWindowRectDIP(scale_)); | 638 const gfx::Rect window_rect = GetViewportRect(); |
611 const int left = window_rect.x(); | 639 const int left = window_rect.x(); |
612 const int right = window_rect.right(); | 640 const int right = window_rect.right(); |
613 | 641 |
614 int x_diff = 0; | 642 int x_diff = 0; |
615 if (point.x() < left + x_panning_margin) { | 643 if (point.x() < left + x_panning_margin) { |
616 // Panning left. | 644 // Panning left. |
617 x_diff = point.x() - (left + x_target_margin); | 645 x_diff = point.x() - (left + x_target_margin); |
618 start_zoom = true; | 646 start_zoom = true; |
619 } else if (right - x_panning_margin < point.x()) { | 647 } else if (right - x_panning_margin < point.x()) { |
620 // Panning right. | 648 // Panning right. |
621 x_diff = point.x() - (right - x_target_margin); | 649 x_diff = point.x() - (right - x_target_margin); |
622 start_zoom = true; | 650 start_zoom = true; |
623 } | 651 } |
624 x = left + x_diff; | 652 int x = left + x_diff; |
625 | 653 |
626 const int top = window_rect.y(); | 654 const int top = window_rect.y(); |
627 const int bottom = window_rect.bottom(); | 655 const int bottom = window_rect.bottom(); |
628 | 656 |
629 int y_diff = 0; | 657 int y_diff = 0; |
630 if (point.y() < top + y_panning_margin) { | 658 if (point.y() < top + y_panning_margin) { |
631 // Panning up. | 659 // Panning up. |
632 y_diff = point.y() - (top + y_target_margin); | 660 y_diff = point.y() - (top + y_target_margin); |
633 start_zoom = true; | 661 start_zoom = true; |
634 } else if (bottom - y_panning_margin < point.y()) { | 662 } else if (bottom - y_panning_margin < point.y()) { |
635 // Panning down. | 663 // Panning down. |
636 y_diff = point.y() - (bottom - y_target_margin); | 664 y_diff = point.y() - (bottom - y_target_margin); |
637 start_zoom = true; | 665 start_zoom = true; |
638 } | 666 } |
639 y = top + y_diff; | 667 int y = top + y_diff; |
640 if (start_zoom && !is_on_animation_) { | 668 if (start_zoom && !is_on_animation_) { |
641 // No animation on panning. | 669 // No animation on panning. |
642 bool animate = false; | 670 bool animate = false; |
643 bool ret = RedrawDIP(gfx::Point(x, y), scale_, animate); | 671 bool ret = RedrawDIP(gfx::Point(x, y), scale_, animate); |
644 | 672 |
645 if (ret) { | 673 if (ret) { |
646 // If the magnified region is moved, hides the mouse cursor and moves it. | 674 // If the magnified region is moved, hides the mouse cursor and moves it. |
647 if (x_diff != 0 || y_diff != 0) | 675 if (x_diff != 0 || y_diff != 0) |
648 MoveCursorTo(root_window_->GetHost(), point); | 676 MoveCursorTo(root_window_->GetHost(), point); |
649 } | 677 } |
650 } | 678 } |
651 } | 679 } |
652 | 680 |
681 void MagnificationControllerImpl::MoveMaginifierWindowFollowRect( | |
682 const gfx::Rect& rect) { | |
683 DCHECK(root_window_); | |
684 bool should_pan = false; | |
685 | |
686 const gfx::Rect viewport_rect = GetViewportRect(); | |
687 const int left = viewport_rect.x(); | |
688 const int right = viewport_rect.right(); | |
689 const gfx::Point rect_center = rect.CenterPoint(); | |
690 const gfx::Point window_center = viewport_rect.CenterPoint(); | |
691 | |
692 int x = left; | |
693 if (rect.x() < left || right < rect.right()) { | |
694 // Panning horizontally. | |
695 x = rect_center.x() - viewport_rect.width() / 2; | |
696 should_pan = true; | |
697 } | |
698 | |
699 const int top = viewport_rect.y(); | |
700 const int bottom = viewport_rect.bottom(); | |
701 | |
702 int y = top; | |
703 if (rect.y() < top || bottom < rect.bottom()) { | |
704 // Panning vertically. | |
705 y = rect_center.y() - viewport_rect.height() / 2; | |
706 should_pan = true; | |
707 } | |
708 | |
709 if (should_pan) { | |
710 if (is_on_animation_) { | |
711 root_window_->layer()->GetAnimator()->StopAnimating(); | |
712 is_on_animation_ = false; | |
713 } | |
714 RedrawDIP(gfx::Point(x, y), scale_, false); // No animation on panning. | |
715 } | |
716 } | |
717 | |
653 void MagnificationControllerImpl::OnCaretBoundsChanged( | 718 void MagnificationControllerImpl::OnCaretBoundsChanged( |
654 const ui::TextInputClient* client) { | 719 const ui::TextInputClient* client) { |
655 // caret bounds in screen coordinates. | 720 // caret bounds in screen coordinates. |
656 const gfx::Rect caret_bounds = client->GetCaretBounds(); | 721 const gfx::Rect caret_bounds = client->GetCaretBounds(); |
657 // Note: OnCaretBoundsChanged could be fired OnTextInputTypeChanged during | 722 // Note: OnCaretBoundsChanged could be fired OnTextInputTypeChanged during |
658 // which the caret position is not set a meaning position, and we do not | 723 // which the caret position is not set a meaning position, and we do not |
659 // need to adjust the view port position based on the bogus caret position. | 724 // need to adjust the view port position based on the bogus caret position. |
660 // This is only a transition period, the caret position will be fixed upon | 725 // This is only a transition period, the caret position will be fixed upon |
661 // focusing right after. | 726 // focusing right after. |
662 if (caret_bounds.width() == 0 && caret_bounds.height() == 0) | 727 if (caret_bounds.width() == 0 && caret_bounds.height() == 0) |
663 return; | 728 return; |
664 | 729 |
665 gfx::Point caret_origin = caret_bounds.origin(); | 730 gfx::Point caret_origin = caret_bounds.origin(); |
666 // caret_origin in |root_window_| coordinates. | 731 // caret_origin in |root_window_| coordinates. |
667 wm::ConvertPointFromScreen(root_window_, &caret_origin); | 732 wm::ConvertPointFromScreen(root_window_, &caret_origin); |
668 | 733 |
669 // Visible window_rect in |root_window_| coordinates. | 734 // Visible window_rect in |root_window_| coordinates. |
670 const gfx::Rect visible_window_rect = | 735 const gfx::Rect visible_window_rect = GetViewportRect(); |
671 gfx::ToEnclosingRect(GetWindowRectDIP(scale_)); | |
672 | 736 |
673 const int panning_margin = kCaretPanningMargin / scale_; | 737 const int panning_margin = kCaretPanningMargin / scale_; |
674 MoveMagnifierWindow(caret_origin, | 738 MoveMagnifierWindowFollowPoint(caret_origin, panning_margin, panning_margin, |
675 panning_margin, | 739 visible_window_rect.width() / 2, |
676 panning_margin, | 740 visible_window_rect.height() / 2); |
677 visible_window_rect.width() / 2, | |
678 visible_window_rect.height() / 2); | |
679 } | 741 } |
680 | 742 |
681 //////////////////////////////////////////////////////////////////////////////// | 743 //////////////////////////////////////////////////////////////////////////////// |
682 // MagnificationController: | 744 // MagnificationController: |
683 | 745 |
684 // static | 746 // static |
685 MagnificationController* MagnificationController::CreateInstance() { | 747 MagnificationController* MagnificationController::CreateInstance() { |
686 return new MagnificationControllerImpl(); | 748 return new MagnificationControllerImpl(); |
687 } | 749 } |
688 | 750 |
689 } // namespace ash | 751 } // namespace ash |
OLD | NEW |