Skip to content

Commit

Permalink
[Merge to M90]bento_v1: Make sure the focused mini view can always be…
Browse files Browse the repository at this point in the history
… seen

TBR=minch@chromium.org

(cherry picked from commit a2bd272)

Bug: 1178942
Change-Id: I72b95d4cd4c5b8e782b71d03f60121b3d3637e8e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2725645
Commit-Queue: Min Chen <minch@chromium.org>
Reviewed-by: Ahmed Fakhry <afakhry@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#859943}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2740296
Reviewed-by: Min Chen <minch@chromium.org>
Auto-Submit: Min Chen <minch@chromium.org>
Cr-Commit-Position: refs/branch-heads/4430@{#186}
Cr-Branched-From: e5ce7dc-refs/heads/master@{#857950}
  • Loading branch information
minch authored and Chromium LUCI CQ committed Mar 6, 2021
1 parent 8228e69 commit 3fc6768
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 19 deletions.
9 changes: 6 additions & 3 deletions ash/wm/desks/desk_mini_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ DeskMiniView::DeskMiniView(DesksBarView* owner_bar,

desk_->AddObserver(this);

auto desk_name_view = std::make_unique<DeskNameView>();
auto desk_name_view = std::make_unique<DeskNameView>(this);
desk_name_view->AddObserver(this);
desk_name_view->set_controller(this);
desk_name_view->SetText(desk_->name());
Expand Down Expand Up @@ -183,9 +183,11 @@ gfx::Size DeskMiniView::CalculatePreferredSize() const {
return preview_bounds.size();

// The preferred size takes into account only the width of the preview
// view.
// view. Desk preview's bottom inset should be excluded to maintain
// |kLabelPreviewSpacing| between preview and desk name view.
return gfx::Size{preview_bounds.width(),
preview_bounds.height() + 2 * kLabelPreviewSpacing +
preview_bounds.height() - GetPreviewBorderInsets().bottom() +
2 * kLabelPreviewSpacing +
desk_name_view_->GetPreferredSize().height()};
}

Expand Down Expand Up @@ -286,6 +288,7 @@ void DeskMiniView::MaybeSwapHighlightedView(bool right) {

void DeskMiniView::OnViewHighlighted() {
UpdateBorderColor();
owner_bar_->ScrollToShowMiniViewIfNecessary(this);
}

void DeskMiniView::OnViewUnhighlighted() {
Expand Down
5 changes: 4 additions & 1 deletion ash/wm/desks/desk_name_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "ash/shell.h"
#include "ash/style/ash_color_provider.h"
#include "ash/wm/desks/desk_mini_view.h"
#include "ash/wm/desks/desks_bar_view.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/overview/overview_grid.h"
#include "ui/accessibility/ax_enums.mojom.h"
Expand Down Expand Up @@ -47,7 +49,7 @@ bool IsDesksBarWidget(const views::Widget* widget) {

} // namespace

DeskNameView::DeskNameView() {
DeskNameView::DeskNameView(DeskMiniView* mini_view) : mini_view_(mini_view) {
auto border = std::make_unique<WmHighlightItemBorder>(
/*corner_radius=*/4, gfx::Insets(0, kDeskNameViewHorizontalPadding));
border_ptr_ = border.get();
Expand Down Expand Up @@ -165,6 +167,7 @@ void DeskNameView::MaybeSwapHighlightedView(bool right) {}

void DeskNameView::OnViewHighlighted() {
UpdateBorderState();
mini_view_->owner_bar()->ScrollToShowMiniViewIfNecessary(mini_view_);
}

void DeskNameView::OnViewUnhighlighted() {
Expand Down
7 changes: 6 additions & 1 deletion ash/wm/desks/desk_name_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace ash {

class DeskMiniView;

// Defines a special textfield that allows modifying the name of its
// corresponding desk. When it's not focused, it looks like a normal label. It
// can be highlighted and activated by the OverviewHighlightController, and it
Expand All @@ -20,7 +22,7 @@ class ASH_EXPORT DeskNameView
: public views::Textfield,
public OverviewHighlightController::OverviewHighlightableView {
public:
DeskNameView();
explicit DeskNameView(DeskMiniView* mini_view);
DeskNameView(const DeskNameView&) = delete;
DeskNameView& operator=(const DeskNameView&) = delete;
~DeskNameView() override;
Expand Down Expand Up @@ -64,6 +66,9 @@ class ASH_EXPORT DeskNameView
// and if the mouse is entering/exiting the view.
SkColor GetBackgroundColor() const;

// The mini view that associated with this name view.
DeskMiniView* const mini_view_;

// Owned by this View via `View::border_`. This is just a convenient pointer
// to it.
WmHighlightItemBorder* border_ptr_;
Expand Down
16 changes: 16 additions & 0 deletions ash/wm/desks/desks_bar_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,22 @@ void DesksBarView::UpdateNewMiniViews(bool initializing_bar_view,
first_time_mini_views);
}

void DesksBarView::ScrollToShowMiniViewIfNecessary(
const DeskMiniView* mini_view) {
DCHECK(base::Contains(mini_views_, mini_view));
const gfx::Rect visible_bounds = scroll_view_->GetVisibleRect();
const gfx::Rect mini_view_bounds = mini_view->bounds();
const bool beyond_left = mini_view_bounds.x() < visible_bounds.x();
const bool beyond_right = mini_view_bounds.right() > visible_bounds.right();
auto* scroll_bar = scroll_view_->horizontal_scroll_bar();
if (beyond_left) {
scroll_view_->ScrollToPosition(
scroll_bar, mini_view_bounds.right() - scroll_view_->bounds().width());
} else if (beyond_right) {
scroll_view_->ScrollToPosition(scroll_bar, mini_view_bounds.x());
}
}

DeskMiniView* DesksBarView::FindMiniViewForDesk(const Desk* desk) const {
for (auto* mini_view : mini_views_) {
if (mini_view->desk() == desk)
Expand Down
5 changes: 5 additions & 0 deletions ash/wm/desks/desks_bar_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,17 @@ class ASH_EXPORT DesksBarView : public views::View,
// their final positions if |initializing_bar_view| is false.
void UpdateNewMiniViews(bool initializing_bar_view, bool expanding_bar_view);

// If the focused |mini_view| is outside of the scroll view's visible bounds,
// scrolls the bar to make sure it can always be seen.
void ScrollToShowMiniViewIfNecessary(const DeskMiniView* mini_view);

ScrollArrowButton* GetLeftScrollButtonForTesting() const {
return left_scroll_button_;
}
ScrollArrowButton* GetRightScrollButtonForTesting() const {
return right_scroll_button_;
}
views::ScrollView* GetScrollViewForTesting() const { return scroll_view_; }

private:
friend class BentoDesksBarLayout;
Expand Down
70 changes: 56 additions & 14 deletions ash/wm/desks/desks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4010,6 +4010,12 @@ class DesksBentoTest : public AshTestBase {
AshTestBase::SetUp();
}

void SendKey(ui::KeyboardCode key_code, int flags = 0) {
auto* generator = GetEventGenerator();
generator->PressKey(key_code, flags);
generator->ReleaseKey(key_code, flags);
}

private:
base::test::ScopedFeatureList scoped_feature_list_;
};
Expand Down Expand Up @@ -4156,6 +4162,48 @@ TEST_F(DesksBentoTest, ScrollButtons) {
EXPECT_FALSE(desks_bar->GetRightScrollButtonForTesting()->GetVisible());
}

// Tests that change the focused mini view should scroll the desks bar and put
// the focused mini view inside the visible bounds.
TEST_F(DesksBentoTest, FocusedMiniViewIsVisible) {
UpdateDisplay("501x600");
for (size_t i = 1; i < desks_util::GetMaxNumberOfDesks(); i++)
NewDesk();

EXPECT_EQ(DesksController::Get()->desks().size(),
desks_util::GetMaxNumberOfDesks());
auto window = CreateAppWindow(gfx::Rect(0, 0, 100, 100));
TabletModeControllerTestApi().EnterTabletMode();
// Snap the window to left and then right side of the display should enter
// overview mode.
auto* root_window = Shell::GetPrimaryRootWindow();
SplitViewController::Get(root_window)
->SnapWindow(window.get(), SplitViewController::LEFT);
auto* overview_controller = Shell::Get()->overview_controller();
EXPECT_TRUE(overview_controller->InOverviewSession());
auto* desks_bar = GetOverviewGridForRoot(root_window)->desks_bar_view();
auto mini_views = desks_bar->mini_views();
ASSERT_EQ(mini_views.size(), desks_util::GetMaxNumberOfDesks());
// Traverse all the desks mini views from left to right.
for (size_t i = 0; i < desks_util::GetMaxNumberOfDesks(); i++) {
// Move the focus to mini view.
SendKey(ui::VKEY_TAB);
EXPECT_TRUE(desks_bar->GetScrollViewForTesting()->GetVisibleRect().Contains(
mini_views[i]->bounds()));
// Move the focus to the mini view's associated name view.
SendKey(ui::VKEY_TAB);
}

// Traverse from all the desk mini views from right to left.
for (size_t i = desks_util::GetMaxNumberOfDesks() - 1; i > 0; i--) {
// Move the focus from desk name view to the associated mini view.
SendKey(ui::VKEY_LEFT);
// Move the focus to previous mini view's name view.
SendKey(ui::VKEY_LEFT);
EXPECT_TRUE(desks_bar->GetScrollViewForTesting()->GetVisibleRect().Contains(
mini_views[i - 1]->bounds()));
}
}

// Tests that the bounds of a window that is visible on all desks is shared
// across desks.
TEST_F(DesksBentoTest, VisibleOnAllDesksGlobalBounds) {
Expand Down Expand Up @@ -4537,18 +4585,12 @@ TEST_F(DesksBentoTest, ZeroStateDeskButtonText) {
event_generator);
EXPECT_TRUE(desks_bar_view->mini_views()[0]->desk_name_view()->HasFocus());

auto send_key = [this](ui::KeyboardCode key_code, int flags = 0) {
auto* generator = GetEventGenerator();
generator->PressKey(key_code, flags);
generator->ReleaseKey(key_code, flags);
};

// Change the desk name to "test".
send_key(ui::VKEY_T);
send_key(ui::VKEY_E);
send_key(ui::VKEY_S);
send_key(ui::VKEY_T);
send_key(ui::VKEY_RETURN);
SendKey(ui::VKEY_T);
SendKey(ui::VKEY_E);
SendKey(ui::VKEY_S);
SendKey(ui::VKEY_T);
SendKey(ui::VKEY_RETURN);
overview_controller->EndOverview();
overview_controller->StartOverview();

Expand All @@ -4561,7 +4603,7 @@ TEST_F(DesksBentoTest, ZeroStateDeskButtonText) {
// Create 'Desk 2'.
ClickOnView(desks_bar_view->zero_state_new_desk_button(), event_generator);
EXPECT_FALSE(desks_bar_view->IsZeroState());
send_key(ui::VKEY_RETURN);
SendKey(ui::VKEY_RETURN);
EXPECT_EQ(base::UTF8ToUTF16("Desk 2"),
DesksController::Get()->desks()[1].get()->name());

Expand All @@ -4576,8 +4618,8 @@ TEST_F(DesksBentoTest, ZeroStateDeskButtonText) {
ClickOnView(desks_bar_view->zero_state_default_desk_button(),
event_generator);
for (size_t i = 0; i < DeskNameView::kMaxLength + 5; i++)
send_key(ui::VKEY_A);
send_key(ui::VKEY_RETURN);
SendKey(ui::VKEY_A);
SendKey(ui::VKEY_RETURN);
overview_controller->EndOverview();
overview_controller->StartOverview();

Expand Down

0 comments on commit 3fc6768

Please sign in to comment.