Commit add2aa14 authored by Manu Cornet's avatar Manu Cornet Committed by Commit Bot

CrOS Shelf: Put alignment selection logic in |Shelf| and remove duplicates

Change-Id: I42931af323cbfbd4da06eeb11c93cc06258ba74d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2025467
Commit-Queue: Alex Newcomer <newcomer@chromium.org>
Reviewed-by: default avatarAlex Newcomer <newcomer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#736076}
parent 9f56b1f5
...@@ -287,24 +287,6 @@ bool Shelf::IsHorizontalAlignment() const { ...@@ -287,24 +287,6 @@ bool Shelf::IsHorizontalAlignment() const {
return true; return true;
} }
int Shelf::SelectValueForShelfAlignment(int bottom, int left, int right) const {
switch (alignment_) {
case ShelfAlignment::kBottom:
case ShelfAlignment::kBottomLocked:
return bottom;
case ShelfAlignment::kLeft:
return left;
case ShelfAlignment::kRight:
return right;
}
NOTREACHED();
return bottom;
}
int Shelf::PrimaryAxisValue(int horizontal, int vertical) const {
return IsHorizontalAlignment() ? horizontal : vertical;
}
void Shelf::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide_behavior) { void Shelf::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide_behavior) {
DCHECK(shelf_layout_manager_); DCHECK(shelf_layout_manager_);
......
...@@ -104,10 +104,26 @@ class ASH_EXPORT Shelf : public ShelfLayoutManagerObserver { ...@@ -104,10 +104,26 @@ class ASH_EXPORT Shelf : public ShelfLayoutManagerObserver {
bool IsHorizontalAlignment() const; bool IsHorizontalAlignment() const;
// Returns a value based on shelf alignment. // Returns a value based on shelf alignment.
int SelectValueForShelfAlignment(int bottom, int left, int right) const; template <typename T>
T SelectValueForShelfAlignment(T bottom, T left, T right) const {
switch (alignment_) {
case ShelfAlignment::kBottom:
case ShelfAlignment::kBottomLocked:
return bottom;
case ShelfAlignment::kLeft:
return left;
case ShelfAlignment::kRight:
return right;
}
NOTREACHED();
return bottom;
}
// Returns |horizontal| if shelf is horizontal, otherwise |vertical|. // Returns |horizontal| if shelf is horizontal, otherwise |vertical|.
int PrimaryAxisValue(int horizontal, int vertical) const; template <typename T>
T PrimaryAxisValue(T horizontal, T vertical) const {
return IsHorizontalAlignment() ? horizontal : vertical;
}
void SetAutoHideBehavior(ShelfAutoHideBehavior behavior); void SetAutoHideBehavior(ShelfAutoHideBehavior behavior);
......
...@@ -458,7 +458,7 @@ gfx::Rect ShelfLayoutManager::GetIdealBounds() const { ...@@ -458,7 +458,7 @@ gfx::Rect ShelfLayoutManager::GetIdealBounds() const {
const int shelf_size = ShelfConfig::Get()->shelf_size(); const int shelf_size = ShelfConfig::Get()->shelf_size();
aura::Window* shelf_window = shelf_widget_->GetNativeWindow(); aura::Window* shelf_window = shelf_widget_->GetNativeWindow();
gfx::Rect rect(screen_util::GetDisplayBoundsInParent(shelf_window)); gfx::Rect rect(screen_util::GetDisplayBoundsInParent(shelf_window));
return SelectValueForShelfAlignment( return shelf_->SelectValueForShelfAlignment(
gfx::Rect(rect.x(), rect.bottom() - shelf_size, rect.width(), shelf_size), gfx::Rect(rect.x(), rect.bottom() - shelf_size, rect.width(), shelf_size),
gfx::Rect(rect.x(), rect.y(), shelf_size, rect.height()), gfx::Rect(rect.x(), rect.y(), shelf_size, rect.height()),
gfx::Rect(rect.right() - shelf_size, rect.y(), shelf_size, gfx::Rect(rect.right() - shelf_size, rect.y(), shelf_size,
...@@ -1603,13 +1603,15 @@ void ShelfLayoutManager::CalculateTargetBounds( ...@@ -1603,13 +1603,15 @@ void ShelfLayoutManager::CalculateTargetBounds(
screen_util::GetDisplayBoundsWithShelf(shelf_widget_->GetNativeWindow()); screen_util::GetDisplayBoundsWithShelf(shelf_widget_->GetNativeWindow());
available_bounds.Inset(work_area->GetAccessibilityInsets()); available_bounds.Inset(work_area->GetAccessibilityInsets());
int shelf_width = PrimaryAxisValue(available_bounds.width(), shelf_size); int shelf_width =
int shelf_height = PrimaryAxisValue(shelf_size, available_bounds.height()); shelf_->PrimaryAxisValue(available_bounds.width(), shelf_size);
const int shelf_primary_position = SelectValueForShelfAlignment( int shelf_height =
shelf_->PrimaryAxisValue(shelf_size, available_bounds.height());
const int shelf_primary_position = shelf_->SelectValueForShelfAlignment(
available_bounds.bottom() - shelf_in_screen_portion, available_bounds.bottom() - shelf_in_screen_portion,
available_bounds.x() - shelf_size + shelf_in_screen_portion, available_bounds.x() - shelf_size + shelf_in_screen_portion,
available_bounds.right() - shelf_in_screen_portion); available_bounds.right() - shelf_in_screen_portion);
gfx::Point shelf_origin = SelectValueForShelfAlignment( gfx::Point shelf_origin = shelf_->SelectValueForShelfAlignment(
gfx::Point(available_bounds.x(), shelf_primary_position), gfx::Point(available_bounds.x(), shelf_primary_position),
gfx::Point(shelf_primary_position, available_bounds.y()), gfx::Point(shelf_primary_position, available_bounds.y()),
gfx::Point(shelf_primary_position, available_bounds.y())); gfx::Point(shelf_primary_position, available_bounds.y()));
...@@ -1625,7 +1627,7 @@ void ShelfLayoutManager::CalculateTargetBounds( ...@@ -1625,7 +1627,7 @@ void ShelfLayoutManager::CalculateTargetBounds(
else else
status_size.set_width(shelf_size); status_size.set_width(shelf_size);
gfx::Point status_origin = SelectValueForShelfAlignment( gfx::Point status_origin = shelf_->SelectValueForShelfAlignment(
gfx::Point(0, 0), gfx::Point(0, 0),
gfx::Point(shelf_width - status_size.width(), gfx::Point(shelf_width - status_size.width(),
shelf_height - status_size.height()), shelf_height - status_size.height()),
...@@ -1684,7 +1686,7 @@ void ShelfLayoutManager::CalculateTargetBounds( ...@@ -1684,7 +1686,7 @@ void ShelfLayoutManager::CalculateTargetBounds(
if (drag_status_ == kDragInProgress) if (drag_status_ == kDragInProgress)
UpdateTargetBoundsForGesture(hotseat_target_state); UpdateTargetBoundsForGesture(hotseat_target_state);
target_bounds_.shelf_insets = SelectValueForShelfAlignment( target_bounds_.shelf_insets = shelf_->SelectValueForShelfAlignment(
gfx::Insets(0, 0, gfx::Insets(0, 0,
GetShelfInset(state.visibility_state, GetShelfInset(state.visibility_state,
IsHotseatEnabled() IsHotseatEnabled()
...@@ -1698,7 +1700,7 @@ void ShelfLayoutManager::CalculateTargetBounds( ...@@ -1698,7 +1700,7 @@ void ShelfLayoutManager::CalculateTargetBounds(
// that can change the size of the shelf. // that can change the size of the shelf.
const bool showing_login_shelf = !state.IsActiveSessionState(); const bool showing_login_shelf = !state.IsActiveSessionState();
if (chromeos::switches::ShouldShowScrollableShelf() && !showing_login_shelf) { if (chromeos::switches::ShouldShowScrollableShelf() && !showing_login_shelf) {
target_bounds_.shelf_bounds_in_shelf = SelectValueForShelfAlignment( target_bounds_.shelf_bounds_in_shelf = shelf_->SelectValueForShelfAlignment(
gfx::Rect(target_bounds_.nav_bounds_in_shelf.right(), 0, gfx::Rect(target_bounds_.nav_bounds_in_shelf.right(), 0,
shelf_width - status_size.width() - shelf_width - status_size.width() -
target_bounds_.nav_bounds_in_shelf.width() - target_bounds_.nav_bounds_in_shelf.width() -
...@@ -1715,7 +1717,7 @@ void ShelfLayoutManager::CalculateTargetBounds( ...@@ -1715,7 +1717,7 @@ void ShelfLayoutManager::CalculateTargetBounds(
target_bounds_.nav_bounds_in_shelf.height() - target_bounds_.nav_bounds_in_shelf.height() -
home_button_edge_spacing)); home_button_edge_spacing));
} else { } else {
target_bounds_.shelf_bounds_in_shelf = SelectValueForShelfAlignment( target_bounds_.shelf_bounds_in_shelf = shelf_->SelectValueForShelfAlignment(
gfx::Rect(0, 0, shelf_width - status_size.width(), gfx::Rect(0, 0, shelf_width - status_size.width(),
target_bounds_.shelf_bounds.height()), target_bounds_.shelf_bounds.height()),
gfx::Rect(0, 0, target_bounds_.shelf_bounds.width(), gfx::Rect(0, 0, target_bounds_.shelf_bounds.width(),
...@@ -1771,7 +1773,7 @@ void ShelfLayoutManager::UpdateTargetBoundsForGesture( ...@@ -1771,7 +1773,7 @@ void ShelfLayoutManager::UpdateTargetBoundsForGesture(
// because there should be a linear transition to the home launcher gesture. // because there should be a linear transition to the home launcher gesture.
translate = drag_amount_; translate = drag_amount_;
} else { } else {
const bool resist = SelectValueForShelfAlignment( const bool resist = shelf_->SelectValueForShelfAlignment(
drag_amount_<-resistance_free_region, drag_amount_> drag_amount_<-resistance_free_region, drag_amount_>
resistance_free_region, resistance_free_region,
drag_amount_ < -resistance_free_region); drag_amount_ < -resistance_free_region);
...@@ -1789,7 +1791,7 @@ void ShelfLayoutManager::UpdateTargetBoundsForGesture( ...@@ -1789,7 +1791,7 @@ void ShelfLayoutManager::UpdateTargetBoundsForGesture(
const gfx::Rect available_bounds = const gfx::Rect available_bounds =
screen_util::GetDisplayBoundsWithShelf(shelf_widget_->GetNativeWindow()); screen_util::GetDisplayBoundsWithShelf(shelf_widget_->GetNativeWindow());
const int baseline = SelectValueForShelfAlignment( const int baseline = shelf_->SelectValueForShelfAlignment(
available_bounds.bottom() - (shelf_hidden_at_start ? 0 : shelf_size), available_bounds.bottom() - (shelf_hidden_at_start ? 0 : shelf_size),
available_bounds.x() - (shelf_hidden_at_start ? shelf_size : 0), available_bounds.x() - (shelf_hidden_at_start ? shelf_size : 0),
available_bounds.right() - (shelf_hidden_at_start ? 0 : shelf_size)); available_bounds.right() - (shelf_hidden_at_start ? 0 : shelf_size));
...@@ -1880,7 +1882,7 @@ gfx::Rect ShelfLayoutManager::GetVisibleShelfBounds() const { ...@@ -1880,7 +1882,7 @@ gfx::Rect ShelfLayoutManager::GetVisibleShelfBounds() const {
gfx::Rect ShelfLayoutManager::GetAutoHideShowShelfRegionInScreen() const { gfx::Rect ShelfLayoutManager::GetAutoHideShowShelfRegionInScreen() const {
gfx::Rect shelf_bounds_in_screen = GetVisibleShelfBounds(); gfx::Rect shelf_bounds_in_screen = GetVisibleShelfBounds();
gfx::Vector2d offset = SelectValueForShelfAlignment( gfx::Vector2d offset = shelf_->SelectValueForShelfAlignment(
gfx::Vector2d(0, shelf_bounds_in_screen.height()), gfx::Vector2d(0, shelf_bounds_in_screen.height()),
gfx::Vector2d(-kMaxAutoHideShowShelfRegionSize, 0), gfx::Vector2d(-kMaxAutoHideShowShelfRegionSize, 0),
gfx::Vector2d(shelf_bounds_in_screen.width(), 0)); gfx::Vector2d(shelf_bounds_in_screen.width(), 0));
...@@ -2446,7 +2448,7 @@ void ShelfLayoutManager::UpdateDrag(const ui::LocatedEvent& event_in_screen, ...@@ -2446,7 +2448,7 @@ void ShelfLayoutManager::UpdateDrag(const ui::LocatedEvent& event_in_screen,
} else { } else {
if (drag_start_point_in_screen_ == gfx::Point()) if (drag_start_point_in_screen_ == gfx::Point())
drag_start_point_in_screen_ = event_in_screen.location(); drag_start_point_in_screen_ = event_in_screen.location();
drag_amount_ += PrimaryAxisValue(scroll_y, scroll_x); drag_amount_ += shelf_->PrimaryAxisValue(scroll_y, scroll_x);
if (event_in_screen.type() == ui::ET_SCROLL_FLING_START) { if (event_in_screen.type() == ui::ET_SCROLL_FLING_START) {
last_drag_velocity_ = last_drag_velocity_ =
event_in_screen.AsGestureEvent()->details().velocity_y(); event_in_screen.AsGestureEvent()->details().velocity_y();
......
...@@ -268,28 +268,6 @@ class ASH_EXPORT ShelfLayoutManager ...@@ -268,28 +268,6 @@ class ASH_EXPORT ShelfLayoutManager
return drag_status_ == kDragAppListInProgress; return drag_status_ == kDragAppListInProgress;
} }
// TODO(harrym|oshima): These templates will be moved to a new Shelf class.
// A helper function for choosing values specific to a shelf alignment.
template <typename T>
T SelectValueForShelfAlignment(T bottom, T left, T right) const {
switch (shelf_->alignment()) {
case ShelfAlignment::kBottom:
case ShelfAlignment::kBottomLocked:
return bottom;
case ShelfAlignment::kLeft:
return left;
case ShelfAlignment::kRight:
return right;
}
NOTREACHED();
return right;
}
template <typename T>
T PrimaryAxisValue(T horizontal, T vertical) const {
return shelf_->IsHorizontalAlignment() ? horizontal : vertical;
}
private: private:
class UpdateShelfObserver; class UpdateShelfObserver;
friend class DimShelfLayoutManagerTestBase; friend class DimShelfLayoutManagerTestBase;
......
...@@ -74,22 +74,19 @@ class ShelfDragCallback { ...@@ -74,22 +74,19 @@ class ShelfDragCallback {
Shelf* shelf = AshTestBase::GetPrimaryShelf(); Shelf* shelf = AshTestBase::GetPrimaryShelf();
gfx::Rect shelf_bounds = GetShelfWidget()->GetWindowBoundsInScreen(); gfx::Rect shelf_bounds = GetShelfWidget()->GetWindowBoundsInScreen();
float scroll_delta = float scroll_delta = shelf->PrimaryAxisValue(scroll_.y(), scroll_.x());
GetShelfLayoutManager()->PrimaryAxisValue(scroll_.y(), scroll_.x()); bool increasing_drag = shelf->SelectValueForShelfAlignment(
bool increasing_drag = scroll_delta<0, scroll_delta> 0, scroll_delta < 0);
GetShelfLayoutManager()->SelectValueForShelfAlignment( const int shelf_size =
scroll_delta<0, scroll_delta> 0, scroll_delta < 0); shelf->PrimaryAxisValue(shelf_bounds.height(), shelf_bounds.width());
const int shelf_size = GetShelfLayoutManager()->PrimaryAxisValue(
shelf_bounds.height(), shelf_bounds.width());
if (was_visible_on_drag_start_) { if (was_visible_on_drag_start_) {
if (increasing_drag) { if (increasing_drag) {
// If dragging inwards from the visible state, then the shelf should // If dragging inwards from the visible state, then the shelf should
// 'overshoot', but not by more than the scroll delta. // 'overshoot', but not by more than the scroll delta.
const int bounds_delta = const int bounds_delta = shelf->SelectValueForShelfAlignment(
GetShelfLayoutManager()->SelectValueForShelfAlignment( visible_shelf_bounds_.y() - shelf_bounds.y(),
visible_shelf_bounds_.y() - shelf_bounds.y(), shelf_bounds.x() - visible_shelf_bounds_.x(),
shelf_bounds.x() - visible_shelf_bounds_.x(), visible_shelf_bounds_.x() - shelf_bounds.x());
visible_shelf_bounds_.x() - shelf_bounds.x());
EXPECT_GE(bounds_delta, 0); EXPECT_GE(bounds_delta, 0);
EXPECT_LE(bounds_delta, std::abs(scroll_delta)); EXPECT_LE(bounds_delta, std::abs(scroll_delta));
} else { } else {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment