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

Side by Side Diff: ash/shelf/shelf_layout_manager.cc

Issue 1742953002: Simplify ash shelf layout code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Restore ConvertRectToScreen calls. Created 4 years, 9 months 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 unified diff | Download patch
« no previous file with comments | « ash/shelf/shelf.cc ('k') | ash/shelf/shelf_layout_manager_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/shelf/shelf_layout_manager.h" 5 #include "ash/shelf/shelf_layout_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <cstring> 9 #include <cstring>
10 #include <string> 10 #include <string>
(...skipping 30 matching lines...) Expand all
41 #include "ui/aura/window_event_dispatcher.h" 41 #include "ui/aura/window_event_dispatcher.h"
42 #include "ui/base/ui_base_switches.h" 42 #include "ui/base/ui_base_switches.h"
43 #include "ui/compositor/layer.h" 43 #include "ui/compositor/layer.h"
44 #include "ui/compositor/layer_animation_observer.h" 44 #include "ui/compositor/layer_animation_observer.h"
45 #include "ui/compositor/layer_animator.h" 45 #include "ui/compositor/layer_animator.h"
46 #include "ui/compositor/scoped_layer_animation_settings.h" 46 #include "ui/compositor/scoped_layer_animation_settings.h"
47 #include "ui/events/event.h" 47 #include "ui/events/event.h"
48 #include "ui/events/event_handler.h" 48 #include "ui/events/event_handler.h"
49 #include "ui/gfx/screen.h" 49 #include "ui/gfx/screen.h"
50 #include "ui/keyboard/keyboard_util.h" 50 #include "ui/keyboard/keyboard_util.h"
51 #include "ui/views/border.h"
51 #include "ui/views/widget/widget.h" 52 #include "ui/views/widget/widget.h"
52 #include "ui/wm/public/activation_client.h" 53 #include "ui/wm/public/activation_client.h"
53 54
54 namespace ash { 55 namespace ash {
55 namespace { 56 namespace {
56 57
57 // Delay before showing the shelf. This is after the mouse stops moving. 58 // Delay before showing the shelf. This is after the mouse stops moving.
58 const int kAutoHideDelayMS = 200; 59 const int kAutoHideDelayMS = 200;
59 60
60 // To avoid hiding the shelf when the mouse transitions from a message bubble 61 // To avoid hiding the shelf when the mouse transitions from a message bubble
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 gfx::Rect(rect.right() - kShelfSize, rect.y(), kShelfSize, rect.height()), 280 gfx::Rect(rect.right() - kShelfSize, rect.y(), kShelfSize, rect.height()),
280 gfx::Rect(rect.x(), rect.y(), rect.width(), kShelfSize)); 281 gfx::Rect(rect.x(), rect.y(), rect.width(), kShelfSize));
281 } 282 }
282 283
283 void ShelfLayoutManager::LayoutShelf() { 284 void ShelfLayoutManager::LayoutShelf() {
284 TargetBounds target_bounds; 285 TargetBounds target_bounds;
285 CalculateTargetBounds(state_, &target_bounds); 286 CalculateTargetBounds(state_, &target_bounds);
286 UpdateBoundsAndOpacity(target_bounds, false, NULL); 287 UpdateBoundsAndOpacity(target_bounds, false, NULL);
287 288
288 if (shelf_->shelf()) { 289 if (shelf_->shelf()) {
289 // This is not part of UpdateBoundsAndOpacity() because
290 // SetShelfViewBounds() sets the bounds immediately and does not animate.
291 // The height of the ShelfView for a horizontal shelf and the width of
292 // the ShelfView for a vertical shelf are set when |shelf_|'s bounds
293 // are changed via UpdateBoundsAndOpacity(). This sets the origin and the
294 // dimension in the other direction.
295 shelf_->shelf()->SetShelfViewBounds(target_bounds.shelf_bounds_in_shelf);
296 // Update insets in ShelfWindowTargeter when shelf bounds change. 290 // Update insets in ShelfWindowTargeter when shelf bounds change.
297 FOR_EACH_OBSERVER(ShelfLayoutManagerObserver, observers_, 291 FOR_EACH_OBSERVER(ShelfLayoutManagerObserver, observers_,
298 WillChangeVisibilityState(visibility_state())); 292 WillChangeVisibilityState(visibility_state()));
299 } 293 }
300 } 294 }
301 295
302 ShelfVisibilityState ShelfLayoutManager::CalculateShelfVisibility() { 296 ShelfVisibilityState ShelfLayoutManager::CalculateShelfVisibility() {
303 switch(auto_hide_behavior_) { 297 switch(auto_hide_behavior_) {
304 case SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS: 298 case SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS:
305 #if defined(OS_WIN) 299 #if defined(OS_WIN)
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 } 400 }
407 401
408 void ShelfLayoutManager::StartGestureDrag(const ui::GestureEvent& gesture) { 402 void ShelfLayoutManager::StartGestureDrag(const ui::GestureEvent& gesture) {
409 gesture_drag_status_ = GESTURE_DRAG_IN_PROGRESS; 403 gesture_drag_status_ = GESTURE_DRAG_IN_PROGRESS;
410 gesture_drag_amount_ = 0.f; 404 gesture_drag_amount_ = 0.f;
411 gesture_drag_auto_hide_state_ = visibility_state() == SHELF_AUTO_HIDE ? 405 gesture_drag_auto_hide_state_ = visibility_state() == SHELF_AUTO_HIDE ?
412 auto_hide_state() : SHELF_AUTO_HIDE_SHOWN; 406 auto_hide_state() : SHELF_AUTO_HIDE_SHOWN;
413 UpdateShelfBackground(BACKGROUND_CHANGE_ANIMATE); 407 UpdateShelfBackground(BACKGROUND_CHANGE_ANIMATE);
414 } 408 }
415 409
416 void ShelfLayoutManager::UpdateGestureDrag( 410 void ShelfLayoutManager::UpdateGestureDrag(const ui::GestureEvent& gesture) {
417 const ui::GestureEvent& gesture) { 411 gesture_drag_amount_ += PrimaryAxisValue(gesture.details().scroll_y(),
418 bool horizontal = IsHorizontalAlignment(); 412 gesture.details().scroll_x());
419 gesture_drag_amount_ += horizontal ? gesture.details().scroll_y() :
420 gesture.details().scroll_x();
421 LayoutShelf(); 413 LayoutShelf();
422 } 414 }
423 415
424 void ShelfLayoutManager::CompleteGestureDrag(const ui::GestureEvent& gesture) { 416 void ShelfLayoutManager::CompleteGestureDrag(const ui::GestureEvent& gesture) {
425 bool horizontal = IsHorizontalAlignment(); 417 bool horizontal = IsHorizontalAlignment();
426 bool should_change = false; 418 bool should_change = false;
427 if (gesture.type() == ui::ET_GESTURE_SCROLL_END) { 419 if (gesture.type() == ui::ET_GESTURE_SCROLL_END) {
428 // The visibility of the shelf changes only if the shelf was dragged X% 420 // The visibility of the shelf changes only if the shelf was dragged X%
429 // along the correct axis. If the shelf was already visible, then the 421 // along the correct axis. If the shelf was already visible, then the
430 // direction of the drag does not matter. 422 // direction of the drag does not matter.
431 const float kDragHideThreshold = 0.4f; 423 const float kDragHideThreshold = 0.4f;
432 gfx::Rect bounds = GetIdealBounds(); 424 gfx::Rect bounds = GetIdealBounds();
433 float drag_ratio = fabs(gesture_drag_amount_) / 425 float drag_ratio = fabs(gesture_drag_amount_) /
434 (horizontal ? bounds.height() : bounds.width()); 426 (horizontal ? bounds.height() : bounds.width());
435 if (gesture_drag_auto_hide_state_ == SHELF_AUTO_HIDE_SHOWN) { 427 if (gesture_drag_auto_hide_state_ == SHELF_AUTO_HIDE_SHOWN) {
436 should_change = drag_ratio > kDragHideThreshold; 428 should_change = drag_ratio > kDragHideThreshold;
437 } else { 429 } else {
438 bool correct_direction = false; 430 bool correct_direction = false;
439 switch (GetAlignment()) { 431 switch (GetAlignment()) {
440 case SHELF_ALIGNMENT_BOTTOM: 432 case SHELF_ALIGNMENT_BOTTOM:
441 case SHELF_ALIGNMENT_RIGHT: 433 case SHELF_ALIGNMENT_RIGHT:
442 correct_direction = gesture_drag_amount_ < 0; 434 correct_direction = gesture_drag_amount_ < 0;
443 break; 435 break;
444 case SHELF_ALIGNMENT_LEFT: 436 case SHELF_ALIGNMENT_LEFT:
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
688 // illegal state. We therefore hide the shelf here if required. 680 // illegal state. We therefore hide the shelf here if required.
689 if (!target_bounds.status_opacity) 681 if (!target_bounds.status_opacity)
690 shelf_->status_area_widget()->Hide(); 682 shelf_->status_area_widget()->Hide();
691 // Setting visibility during an animation causes the visibility property to 683 // Setting visibility during an animation causes the visibility property to
692 // animate. Override the animation settings to immediately set the 684 // animate. Override the animation settings to immediately set the
693 // visibility property. Opacity will still animate. 685 // visibility property. Opacity will still animate.
694 686
695 // TODO(harrym): Once status area widget is a child view of shelf 687 // TODO(harrym): Once status area widget is a child view of shelf
696 // this can be simplified. 688 // this can be simplified.
697 gfx::Rect status_bounds = target_bounds.status_bounds_in_shelf; 689 gfx::Rect status_bounds = target_bounds.status_bounds_in_shelf;
698 status_bounds.set_x(status_bounds.x() + 690 status_bounds.Offset(target_bounds.shelf_bounds_in_root.OffsetFromOrigin());
699 target_bounds.shelf_bounds_in_root.x());
700 status_bounds.set_y(status_bounds.y() +
701 target_bounds.shelf_bounds_in_root.y());
702 // mash::wm::ShelfLayout manages window bounds when running mash_shell. 691 // mash::wm::ShelfLayout manages window bounds when running mash_shell.
703 if (!aura::GetMusWindow(shelf_->GetNativeWindow())) { 692 if (!aura::GetMusWindow(shelf_->GetNativeWindow())) {
704 shelf_->status_area_widget()->SetBounds( 693 shelf_->status_area_widget()->SetBounds(
705 ScreenUtil::ConvertRectToScreen( 694 ScreenUtil::ConvertRectToScreen(
706 shelf_->status_area_widget()->GetNativeView()->parent(), 695 shelf_->status_area_widget()->GetNativeView()->parent(),
707 status_bounds)); 696 status_bounds));
708 } 697 }
709 if (!state_.is_screen_locked) { 698 if (!state_.is_screen_locked) {
710 gfx::Insets insets; 699 gfx::Insets insets;
711 // If user session is blocked (login to new user session or add user to 700 // If user session is blocked (login to new user session or add user to
712 // the existing session - multi-profile) then give 100% of work area only 701 // the existing session - multi-profile) then give 100% of work area only
713 // if keyboard is not shown. 702 // if keyboard is not shown.
714 if (!state_.is_adding_user_screen || !keyboard_bounds_.IsEmpty()) { 703 if (!state_.is_adding_user_screen || !keyboard_bounds_.IsEmpty())
715 insets = target_bounds.work_area_insets; 704 insets = target_bounds.work_area_insets;
716 }
717 Shell::GetInstance()->SetDisplayWorkAreaInsets(root_window_, insets); 705 Shell::GetInstance()->SetDisplayWorkAreaInsets(root_window_, insets);
718 } 706 }
719 } 707 }
720 708
709 // Set an empty border to avoid the shelf view and status area overlapping.
710 // TODO(msw): Avoid setting bounds of views within the shelf widget here.
711 gfx::Rect shelf_bounds = gfx::Rect(target_bounds.shelf_bounds_in_root.size());
712 shelf_->GetContentsView()->SetBorder(views::Border::CreateEmptyBorder(
713 shelf_bounds.InsetsFrom(target_bounds.shelf_bounds_in_shelf)));
714 shelf_->GetContentsView()->Layout();
715
721 // Setting visibility during an animation causes the visibility property to 716 // Setting visibility during an animation causes the visibility property to
722 // animate. Set the visibility property without an animation. 717 // animate. Set the visibility property without an animation.
723 if (target_bounds.status_opacity) 718 if (target_bounds.status_opacity)
724 shelf_->status_area_widget()->Show(); 719 shelf_->status_area_widget()->Show();
725 } 720 }
726 721
727 void ShelfLayoutManager::StopAnimating() { 722 void ShelfLayoutManager::StopAnimating() {
728 GetLayer(shelf_)->GetAnimator()->StopAnimating(); 723 GetLayer(shelf_)->GetAnimator()->StopAnimating();
729 GetLayer(shelf_->status_area_widget())->GetAnimator()->StopAnimating(); 724 GetLayer(shelf_->status_area_widget())->GetAnimator()->StopAnimating();
730 } 725 }
731 726
732 void ShelfLayoutManager::CalculateTargetBounds(const State& state, 727 void ShelfLayoutManager::CalculateTargetBounds(const State& state,
733 TargetBounds* target_bounds) { 728 TargetBounds* target_bounds) {
734 gfx::Rect available_bounds = 729 int shelf_size = kShelfSize;
735 ScreenUtil::GetShelfDisplayBoundsInRoot(root_window_);
736 gfx::Rect status_size(
737 shelf_->status_area_widget()->GetWindowBoundsInScreen().size());
738 int shelf_width = PrimaryAxisValue(available_bounds.width(), kShelfSize);
739 int shelf_height = PrimaryAxisValue(kShelfSize, available_bounds.height());
740
741 if (state.visibility_state == SHELF_AUTO_HIDE && 730 if (state.visibility_state == SHELF_AUTO_HIDE &&
742 state.auto_hide_state == SHELF_AUTO_HIDE_HIDDEN) { 731 state.auto_hide_state == SHELF_AUTO_HIDE_HIDDEN) {
743 // Auto-hidden shelf always starts with the default size. If a gesture-drag 732 // Auto-hidden shelf always starts with the default size. If a gesture-drag
744 // is in progress, then the call to UpdateTargetBoundsForGesture() below 733 // is in progress, then the call to UpdateTargetBoundsForGesture() below
745 // takes care of setting the height properly. 734 // takes care of setting the height properly.
746 if (IsHorizontalAlignment()) 735 shelf_size = kAutoHideSize;
747 shelf_height = kAutoHideSize;
748 else
749 shelf_width = kAutoHideSize;
750 } else if (state.visibility_state == SHELF_HIDDEN || 736 } else if (state.visibility_state == SHELF_HIDDEN ||
751 (!keyboard_bounds_.IsEmpty() && !keyboard::IsKeyboardOverscrollEnabled())) 737 (!keyboard_bounds_.IsEmpty() &&
752 { 738 !keyboard::IsKeyboardOverscrollEnabled())) {
753 if (IsHorizontalAlignment()) 739 shelf_size = 0;
754 shelf_height = 0;
755 else
756 shelf_width = 0;
757 } 740 }
758 741
742 gfx::Rect available_bounds =
743 ScreenUtil::GetShelfDisplayBoundsInRoot(root_window_);
744 int shelf_width = PrimaryAxisValue(available_bounds.width(), shelf_size);
745 int shelf_height = PrimaryAxisValue(shelf_size, available_bounds.height());
759 int bottom_shelf_vertical_offset = available_bounds.bottom(); 746 int bottom_shelf_vertical_offset = available_bounds.bottom();
760 if (keyboard_bounds_.IsEmpty()) 747 if (keyboard_bounds_.IsEmpty())
761 bottom_shelf_vertical_offset -= shelf_height; 748 bottom_shelf_vertical_offset -= shelf_height;
762 else 749 else
763 bottom_shelf_vertical_offset -= keyboard_bounds_.height(); 750 bottom_shelf_vertical_offset -= keyboard_bounds_.height();
764 751
765 target_bounds->shelf_bounds_in_root = SelectValueForShelfAlignment( 752 gfx::Point shelf_origin = SelectValueForShelfAlignment(
766 gfx::Rect(available_bounds.x(), bottom_shelf_vertical_offset, 753 gfx::Point(available_bounds.x(), bottom_shelf_vertical_offset),
767 available_bounds.width(), shelf_height), 754 gfx::Point(available_bounds.x(), available_bounds.y()),
768 gfx::Rect(available_bounds.x(), available_bounds.y(), 755 gfx::Point(available_bounds.right() - shelf_width, available_bounds.y()),
769 shelf_width, available_bounds.height()), 756 gfx::Point(available_bounds.x(), available_bounds.y()));
770 gfx::Rect(available_bounds.right() - shelf_width, available_bounds.y(), 757 target_bounds->shelf_bounds_in_root =
771 shelf_width, available_bounds.height()), 758 gfx::Rect(shelf_origin.x(), shelf_origin.y(), shelf_width, shelf_height);
772 gfx::Rect(available_bounds.x(), available_bounds.y(),
773 available_bounds.width(), shelf_height));
774 759
760 gfx::Size status_size(
761 shelf_->status_area_widget()->GetWindowBoundsInScreen().size());
775 if (IsHorizontalAlignment()) 762 if (IsHorizontalAlignment())
776 status_size.set_height(kShelfSize); 763 status_size.set_height(kShelfSize);
777 else 764 else
778 status_size.set_width(kShelfSize); 765 status_size.set_width(kShelfSize);
779 766
780 target_bounds->status_bounds_in_shelf = SelectValueForShelfAlignment( 767 gfx::Point status_origin = SelectValueForShelfAlignment(
781 gfx::Rect(base::i18n::IsRTL() ? 0 : shelf_width - status_size.width(), 768 gfx::Point(0, 0),
782 0, status_size.width(), status_size.height()), 769 gfx::Point(shelf_width - status_size.width(),
783 gfx::Rect(shelf_width - status_size.width(), 770 shelf_height - status_size.height()),
784 shelf_height - status_size.height(), status_size.width(), 771 gfx::Point(0, shelf_height - status_size.height()),
785 status_size.height()), 772 gfx::Point(0, shelf_height - status_size.height()));
786 gfx::Rect(0, shelf_height - status_size.height(), 773 if (IsHorizontalAlignment() && !base::i18n::IsRTL())
787 status_size.width(), status_size.height()), 774 status_origin.set_x(shelf_width - status_size.width());
788 gfx::Rect(base::i18n::IsRTL() ? 0 : shelf_width - status_size.width(), 775 target_bounds->status_bounds_in_shelf = gfx::Rect(status_origin, status_size);
789 shelf_height - status_size.height(),
790 status_size.width(), status_size.height()));
791 776
792 target_bounds->work_area_insets = SelectValueForShelfAlignment( 777 target_bounds->work_area_insets = SelectValueForShelfAlignment(
793 gfx::Insets(0, 0, GetWorkAreaSize(state, shelf_height), 0), 778 gfx::Insets(0, 0, GetWorkAreaSize(state, shelf_height), 0),
794 gfx::Insets(0, GetWorkAreaSize(state, shelf_width), 0, 0), 779 gfx::Insets(0, GetWorkAreaSize(state, shelf_width), 0, 0),
795 gfx::Insets(0, 0, 0, GetWorkAreaSize(state, shelf_width)), 780 gfx::Insets(0, 0, 0, GetWorkAreaSize(state, shelf_width)),
796 gfx::Insets(GetWorkAreaSize(state, shelf_height), 0, 0, 0)); 781 gfx::Insets(GetWorkAreaSize(state, shelf_height), 0, 0, 0));
797 782
798 // TODO(varkha): The functionality of managing insets for display areas 783 // TODO(varkha): The functionality of managing insets for display areas
799 // should probably be pushed to a separate component. This would simplify or 784 // should probably be pushed to a separate component. This would simplify or
800 // remove entirely the dependency on keyboard and dock. 785 // remove entirely the dependency on keyboard and dock.
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
849 available_bounds.Subtract(target_bounds->shelf_bounds_in_root); 834 available_bounds.Subtract(target_bounds->shelf_bounds_in_root);
850 available_bounds.Subtract(keyboard_bounds_); 835 available_bounds.Subtract(keyboard_bounds_);
851 user_work_area_bounds_ = 836 user_work_area_bounds_ =
852 ScreenUtil::ConvertRectToScreen(root_window_, available_bounds); 837 ScreenUtil::ConvertRectToScreen(root_window_, available_bounds);
853 } 838 }
854 839
855 void ShelfLayoutManager::UpdateTargetBoundsForGesture( 840 void ShelfLayoutManager::UpdateTargetBoundsForGesture(
856 TargetBounds* target_bounds) const { 841 TargetBounds* target_bounds) const {
857 CHECK_EQ(GESTURE_DRAG_IN_PROGRESS, gesture_drag_status_); 842 CHECK_EQ(GESTURE_DRAG_IN_PROGRESS, gesture_drag_status_);
858 bool horizontal = IsHorizontalAlignment(); 843 bool horizontal = IsHorizontalAlignment();
859 const gfx::Rect& available_bounds(root_window_->bounds()); 844 gfx::Rect available_bounds =
845 ScreenUtil::GetShelfDisplayBoundsInRoot(root_window_);
860 int resistance_free_region = 0; 846 int resistance_free_region = 0;
861 847
862 if (gesture_drag_auto_hide_state_ == SHELF_AUTO_HIDE_HIDDEN && 848 if (gesture_drag_auto_hide_state_ == SHELF_AUTO_HIDE_HIDDEN &&
863 visibility_state() == SHELF_AUTO_HIDE && 849 visibility_state() == SHELF_AUTO_HIDE &&
864 auto_hide_state() != SHELF_AUTO_HIDE_SHOWN) { 850 auto_hide_state() != SHELF_AUTO_HIDE_SHOWN) {
865 // If the shelf was hidden when the drag started (and the state hasn't 851 // If the shelf was hidden when the drag started (and the state hasn't
866 // changed since then, e.g. because the tray-menu was shown because of the 852 // changed since then, e.g. because the tray-menu was shown because of the
867 // drag), then allow the drag some resistance-free region at first to make 853 // drag), then allow the drag some resistance-free region at first to make
868 // sure the shelf sticks with the finger until the shelf is visible. 854 // sure the shelf sticks with the finger until the shelf is visible.
869 resistance_free_region = kShelfSize - kAutoHideSize; 855 resistance_free_region = kShelfSize - kAutoHideSize;
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
1178 return false; 1164 return false;
1179 } 1165 }
1180 if (session_state_delegate->IsUserSessionBlocked() || 1166 if (session_state_delegate->IsUserSessionBlocked() ||
1181 state_.is_adding_user_screen) { 1167 state_.is_adding_user_screen) {
1182 return true; 1168 return true;
1183 } 1169 }
1184 return false; 1170 return false;
1185 } 1171 }
1186 1172
1187 } // namespace ash 1173 } // namespace ash
OLDNEW
« no previous file with comments | « ash/shelf/shelf.cc ('k') | ash/shelf/shelf_layout_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698