Skip to content

Commit

Permalink
FloatWindow: Make float state restorable.
Browse files Browse the repository at this point in the history
Allow float state to be restore in window restore.
Also as floated window can be restored, in cases like desk template,
float is allowed on inactive desk, modify the check to cover the case.

Test: Manual and unit test.
Change-Id: I9c5d86eb898450cc1afd571ddf54eb7fb11e1980
Fixed: b/256232589
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4021846
Commit-Queue: Judy Wang <shidi@chromium.org>
Reviewed-by: Sammie Quon <sammiequon@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1070600}
  • Loading branch information
Judy Wang authored and Chromium LUCI CQ committed Nov 12, 2022
1 parent 66ee196 commit a79aa04
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 7 deletions.
17 changes: 17 additions & 0 deletions ash/wm/desks/desks_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/overview/overview_grid.h"
#include "ash/wm/overview/overview_session.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_util.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
Expand Down Expand Up @@ -141,6 +142,22 @@ aura::Window* GetDeskContainerForContext(aura::Window* context) {
return nullptr;
}

const Desk* GetDeskForContext(aura::Window* context) {
DCHECK(context);

for (const auto& desk : DesksController::Get()->desks()) {
if (desk.get()->container_id() ==
GetDeskContainerForContext(context)->GetId()) {
return desk.get();
}
}

if (WindowState::Get(context)->IsFloated())
return Shell::Get()->float_controller()->FindDeskOfFloatedWindow(context);

return nullptr;
}

bool ShouldDesksBarBeCreated() {
return !TabletMode::Get()->InTabletMode() ||
DesksController::Get()->desks().size() > 1;
Expand Down
7 changes: 7 additions & 0 deletions ash/wm/desks/desks_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Compositor;

namespace ash {

class Desk;
namespace desks_util {

// Note: the max number of desks depends on a runtime flag and the function
Expand Down Expand Up @@ -61,6 +62,12 @@ ASH_EXPORT bool BelongsToActiveDesk(aura::Window* window);
// with a desk container.
ASH_EXPORT aura::Window* GetDeskContainerForContext(aura::Window* context);

// If `context` belong to a desk, return the desk pointer, otherwise return
// nullptr. Note that floated window is not in the desk container but still
// considered as "belonging" to the desk, as it's only visible on a particular
// desk.
const Desk* GetDeskForContext(aura::Window* context);

// Returns true if the DesksBar widget should be created in overview mode.
ASH_EXPORT bool ShouldDesksBarBeCreated();

Expand Down
12 changes: 8 additions & 4 deletions ash/wm/float/float_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "ash/wm/wm_event.h"
#include "ash/wm/work_area_insets.h"
#include "ash/wm/workspace/workspace_event_handler.h"
#include "base/check.h"
#include "base/check_op.h"
#include "chromeos/ui/base/window_properties.h"
#include "chromeos/ui/wm/constants.h"
Expand Down Expand Up @@ -613,11 +614,11 @@ void FloatController::FloatImpl(aura::Window* window) {
// If a floated window already exists at current desk, unfloat it before
// floating `window`.
auto* desk_controller = DesksController::Get();
// Get the active desk where the window belongs to before moving it to float
// Get the desk where the window belongs to before moving it to float
// container.
DCHECK(desks_util::IsActiveDeskContainer(
desks_util::GetDeskContainerForContext(window)));
const Desk* desk = desk_controller->GetTargetActiveDesk();
const Desk* desk = desks_util::GetDeskForContext(window);
DCHECK(desk);

auto* previously_floated_window = FindFloatedWindowOfDesk(desk);
// Add floated window to `floated_window_info_map_`.
// Note: this has to be called before `ResetFloatedWindow`. Because in the
Expand All @@ -634,6 +635,9 @@ void FloatController::FloatImpl(aura::Window* window) {
DCHECK_NE(window->parent(), floated_container);
floated_container->AddChild(window);

if (!desk->is_active())
HideFloatedWindow(window);

if (!tablet_mode_observation_.IsObserving())
tablet_mode_observation_.Observe(Shell::Get()->tablet_mode_controller());
if (!desks_controller_observation_.IsObserving())
Expand Down
8 changes: 7 additions & 1 deletion ash/wm/window_restore/window_restore_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "ash/shell.h"
#include "ash/wm/container_finder.h"
#include "ash/wm/desks/desks_util.h"
#include "ash/wm/float/float_controller.h"
#include "ash/wm/mru_window_tracker.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "ash/wm/window_positioning_utils.h"
Expand Down Expand Up @@ -513,6 +514,7 @@ void WindowRestoreController::RestoreStateTypeAndClearLaunchedKey(
// Snap the window if necessary.
auto state_type = window_info->window_state_type;
if (state_type) {
auto* window_state = WindowState::Get(window);
// Add the window to be tracked by the tablet mode window manager
// manually. It is normally tracked when it becomes visible, but in snap
// case we want to track it before it becomes visible. This will allow us
Expand All @@ -531,7 +533,11 @@ void WindowRestoreController::RestoreStateTypeAndClearLaunchedKey(
*state_type == chromeos::WindowStateType::kPrimarySnapped
? WM_EVENT_SNAP_PRIMARY
: WM_EVENT_SNAP_SECONDARY);
WindowState::Get(window)->OnWMEvent(&snap_event);
window_state->OnWMEvent(&snap_event);
}
if (*state_type == chromeos::WindowStateType::kFloated) {
const WMEvent float_event(WM_EVENT_FLOAT);
window_state->OnWMEvent(&float_event);
}
}
}
Expand Down
39 changes: 37 additions & 2 deletions ash/wm/window_restore/window_restore_controller_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "ash/test/test_widget_builder.h"
#include "ash/wm/desks/desks_controller.h"
#include "ash/wm/desks/desks_util.h"
#include "ash/wm/float/float_controller.h"
#include "ash/wm/overview/overview_test_util.h"
#include "ash/wm/splitview/split_view_controller.h"
#include "ash/wm/tablet_mode/tablet_mode_controller_test_api.h"
Expand All @@ -25,6 +26,8 @@
#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/scoped_observation.h"
#include "base/test/scoped_feature_list.h"
#include "chromeos/ui/wm/features.h"
#include "components/account_id/account_id.h"
#include "components/app_restore/app_restore_info.h"
#include "components/app_restore/full_restore_utils.h"
Expand Down Expand Up @@ -253,6 +256,9 @@ class WindowRestoreControllerTest : public AshTestBase,

// AshTestBase:
void SetUp() override {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/{chromeos::wm::features::kFloatWindow},
/*disabled_features=*/{});
AshTestBase::SetUp();

WindowRestoreController::Get()->SetSaveWindowCallbackForTesting(
Expand Down Expand Up @@ -328,6 +334,8 @@ class WindowRestoreControllerTest : public AshTestBase,
base::flat_map<int32_t, WindowInfo> fake_window_restore_file_;

base::ScopedObservation<aura::Env, aura::EnvObserver> env_observation_{this};

base::test::ScopedFeatureList scoped_feature_list_;
};

// Tests window save with setting on or off.
Expand Down Expand Up @@ -394,7 +402,7 @@ TEST_F(WindowRestoreControllerTest, WindowMovedDesks) {
ASSERT_EQ(0, desks_controller->GetDeskIndex(
desks_controller->GetTargetActiveDesk()));

auto window = CreateAppWindow(gfx::Rect(100, 100), AppType::BROWSER);
auto window = CreateAppWindow(gfx::Rect(200, 200), AppType::BROWSER);
aura::Window* previous_parent = window->parent();
ResetSaveWindowsCount();

Expand Down Expand Up @@ -433,7 +441,7 @@ TEST_F(WindowRestoreControllerTest, AssignToAllDesks) {
TEST_F(WindowRestoreControllerTest, WindowMovedDisplay) {
UpdateDisplay("800x700,801+0-800x700");

auto window = CreateAppWindow(gfx::Rect(50, 50, 100, 100), AppType::BROWSER);
auto window = CreateAppWindow(gfx::Rect(50, 50, 200, 200), AppType::BROWSER);
ResetSaveWindowsCount();

// Move the window to the next display. Test that we save the window in
Expand Down Expand Up @@ -699,6 +707,33 @@ TEST_F(WindowRestoreControllerTest, ClamshellSnapWindow) {
EXPECT_EQ(restored_bounds, right_window->GetBoundsInScreen());
}

// Tests clamshell Floated window functionality when creating a window from
// window restore.
TEST_F(WindowRestoreControllerTest, ClamshellFloatWindow) {
// Add one floated window entry to our fake window restore file.
const gfx::Rect restored_bounds(200, 200);
AddEntryToFakeFile(/*restore_window_id=*/1, restored_bounds,
chromeos::WindowStateType::kFloated);

// Create one window restore window with the same restore window id as the
// entries we added. Test they are floated and have moved floated bounds.
aura::Window* floated_window =
CreateTestWindowRestoredWidgetFromRestoreId(/*restore_window_id=*/1)
->GetNativeWindow();
auto* floated_window_state = WindowState::Get(floated_window);
EXPECT_TRUE(floated_window_state->IsFloated());

auto* float_controller = Shell::Get()->float_controller();
EXPECT_EQ(
float_controller->GetPreferredFloatWindowClamshellBounds(floated_window),
floated_window->GetBoundsInScreen());

// Test that after restoring the floated windows, they have the bounds we
// saved into the fake file.
floated_window_state->Restore();
EXPECT_EQ(restored_bounds, floated_window->GetBoundsInScreen());
}

// Tests window restore behavior when a display is disconnected before
// restoration.
TEST_F(WindowRestoreControllerTest, DisconnectedDisplay) {
Expand Down

0 comments on commit a79aa04

Please sign in to comment.