File: client_controlled_state.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (430 lines) | stat: -rw-r--r-- 17,194 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/wm/client_controlled_state.h"

#include "ash/root_window_controller.h"
#include "ash/screen_util.h"
#include "ash/shell.h"
#include "ash/wm/float/float_controller.h"
#include "ash/wm/pip/pip_positioner.h"
#include "ash/wm/screen_pinning_controller.h"
#include "ash/wm/splitview/split_view_controller.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "ash/wm/window_positioning_utils.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_state_delegate.h"
#include "ash/wm/window_state_util.h"
#include "ash/wm/wm_metrics.h"
#include "chromeos/ui/base/window_state_type.h"
#include "chromeos/ui/wm/window_util.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
#include "ui/aura/window_delegate.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/display/screen.h"
#include "ui/wm/core/window_util.h"

namespace ash {

namespace {

using ::chromeos::WindowStateType;

// |kMinimumOnScreenArea + 1| is used to avoid adjusting loop.
constexpr int kClientControlledWindowMinimumOnScreenArea =
    kMinimumOnScreenArea + 1;
}  // namespace

ClientControlledState::ClientControlledState(std::unique_ptr<Delegate> delegate)
    : BaseState(WindowStateType::kDefault), delegate_(std::move(delegate)) {}

ClientControlledState::~ClientControlledState() = default;

// static
void ClientControlledState::AdjustBoundsForMinimumWindowVisibility(
    const gfx::Rect& display_bounds,
    gfx::Rect* bounds) {
  AdjustBoundsToEnsureWindowVisibility(
      display_bounds, kClientControlledWindowMinimumOnScreenArea,
      kClientControlledWindowMinimumOnScreenArea, bounds);
}

void ClientControlledState::ResetDelegate() {
  delegate_.reset();
}

void ClientControlledState::HandleTransitionEvents(WindowState* window_state,
                                                   const WMEvent* event) {
  if (!delegate_)
    return;

  const WMEventType event_type = event->type();
  bool pin_transition = window_state->IsTrustedPinned() ||
                        window_state->IsPinned() || event->IsPinEvent();
  // Pinned State transition is handled on server side.
  if (pin_transition) {
    // Only one window can be pinned.
    if (event->IsPinEvent() &&
        Shell::Get()->screen_pinning_controller()->IsPinned()) {
      return;
    }
    WindowStateType next_state_type =
        GetStateForTransitionEvent(window_state, event);
    delegate_->HandleWindowStateRequest(window_state, next_state_type);
    return;
  }

  switch (event_type) {
    case WM_EVENT_NORMAL:
    case WM_EVENT_MAXIMIZE:
    case WM_EVENT_MINIMIZE:
    case WM_EVENT_FULLSCREEN:
    case WM_EVENT_SNAP_PRIMARY:
    case WM_EVENT_SNAP_SECONDARY:
    case WM_EVENT_FLOAT: {
      WindowStateType next_state =
          GetResolvedNextWindowStateType(window_state, event);
      UpdateWindowForTransitionEvents(window_state, next_state, event);
      break;
    }
    case WM_EVENT_RESTORE:
      UpdateWindowForTransitionEvents(
          window_state, window_state->GetRestoreWindowState(), event);
      break;
    case WM_EVENT_SHOW_INACTIVE:
      NOTREACHED();
      break;
    default:
      NOTREACHED() << "Unknown event :" << event->type();
  }
}

void ClientControlledState::AttachState(
    WindowState* window_state,
    WindowState::State* state_in_previous_mode) {}

void ClientControlledState::DetachState(WindowState* window_state) {}

void ClientControlledState::HandleWorkspaceEvents(WindowState* window_state,
                                                  const WMEvent* event) {
  if (!delegate_)
    return;
  // Client is responsible for adjusting bounds after workspace bounds change.
  if (window_state->IsSnapped()) {
    const aura::Window* window = window_state->window();
    // If `SplitViewController` is aware of `window` (e.g. in tablet), let the
    // controller handle the workspace event.
    if (SplitViewController::Get(window)->IsWindowInSplitView(window)) {
      return;
    }

    gfx::Rect bounds = window->bounds();
    window_state->AdjustSnappedBoundsForDisplayWorkspaceChange(&bounds);

    // Then ask delegate to set the desired bounds for the snap state.
    delegate_->HandleBoundsRequest(window_state, window_state->GetStateType(),
                                   bounds, window_state->GetDisplay().id());
  } else if (window_state->IsFloated()) {
    const gfx::Rect bounds =
        Shell::Get()->tablet_mode_controller()->InTabletMode()
            ? FloatController::GetFloatWindowTabletBounds(
                  window_state->window())
            : FloatController::GetFloatWindowClamshellBounds(
                  window_state->window(),
                  // TODO(b/292579250): Add a mechanism to float as close to the
                  // previous bounds in the event of a workspace event. For now,
                  // use the default float location.
                  chromeos::FloatStartLocation::kBottomRight);
    delegate_->HandleBoundsRequest(window_state, window_state->GetStateType(),
                                   bounds, window_state->GetDisplay().id());
  } else if (event->type() == WM_EVENT_DISPLAY_METRICS_CHANGED) {
    // Explicitly handle the primary change because it can change the display id
    // with no bounds change.
    if (event->AsDisplayMetricsChangedWMEvent()->primary_changed()) {
      const gfx::Rect bounds = window_state->window()->bounds();
      delegate_->HandleBoundsRequest(window_state, window_state->GetStateType(),
                                     bounds, window_state->GetDisplay().id());
    }
  } else if (event->type() == WM_EVENT_ADDED_TO_WORKSPACE) {
    aura::Window* window = window_state->window();
    gfx::Rect bounds = window->bounds();
    AdjustBoundsForMinimumWindowVisibility(window->GetRootWindow()->bounds(),
                                           &bounds);

    if (window->bounds() != bounds)
      window_state->SetBoundsConstrained(bounds);
  }
}

void ClientControlledState::HandleCompoundEvents(WindowState* window_state,
                                                 const WMEvent* event) {
  if (!delegate_)
    return;
  switch (event->type()) {
    case WM_EVENT_TOGGLE_MAXIMIZE_CAPTION:
      if (window_state->IsFullscreen()) {
        const WMEvent wm_event(WM_EVENT_TOGGLE_FULLSCREEN);
        window_state->OnWMEvent(&wm_event);
      } else if (window_state->IsMaximized()) {
        window_state->Restore();
      } else if (window_state->IsNormalOrSnapped()) {
        if (window_state->CanMaximize())
          window_state->Maximize();
      }
      break;
    case WM_EVENT_TOGGLE_MAXIMIZE:
      if (window_state->IsFullscreen()) {
        const WMEvent wm_event(WM_EVENT_TOGGLE_FULLSCREEN);
        window_state->OnWMEvent(&wm_event);
      } else if (window_state->IsMaximized()) {
        window_state->Restore();
      } else if (window_state->CanMaximize()) {
        window_state->Maximize();
      }
      break;
    case WM_EVENT_TOGGLE_VERTICAL_MAXIMIZE:
      // TODO(oshima): Implement this.
      break;
    case WM_EVENT_TOGGLE_HORIZONTAL_MAXIMIZE:
      // TODO(oshima): Implement this.
      break;
    case WM_EVENT_TOGGLE_FULLSCREEN:
      ToggleFullScreen(window_state, window_state->delegate());
      break;
    case WM_EVENT_CYCLE_SNAP_PRIMARY:
    case WM_EVENT_CYCLE_SNAP_SECONDARY:
      CycleSnap(window_state, event->type());
      break;
    default:
      NOTREACHED() << "Invalid event :" << event->type();
      break;
  }
}

void ClientControlledState::HandleBoundsEvents(WindowState* window_state,
                                               const WMEvent* event) {
  if (!delegate_)
    return;
  auto* const window = window_state->window();
  switch (event->type()) {
    case WM_EVENT_SET_BOUNDS: {
      const auto* set_bounds_event = event->AsSetBoundsWMEvent();
      const gfx::Rect& bounds = set_bounds_event->requested_bounds();
      if (set_bounds_locally_) {
        // Don’t preempt on-going animation (e.g. tucking) for floated windows.
        if (window_state->IsFloated() && window->layer() &&
            window->layer()->GetAnimator()->is_animating()) {
          return;
        }

        switch (next_bounds_change_animation_type_) {
          case WindowState::BoundsChangeAnimationType::kNone:
            window_state->SetBoundsDirect(bounds);
            break;
          case WindowState::BoundsChangeAnimationType::kCrossFade:
            window_state->SetBoundsDirectCrossFade(bounds);
            break;
          case WindowState::BoundsChangeAnimationType::kCrossFadeFloat:
            window_state->SetBoundsDirectCrossFade(bounds, true);
            break;
          case WindowState::BoundsChangeAnimationType::kCrossFadeUnfloat:
            window_state->SetBoundsDirectCrossFade(bounds, false);
            break;
          case WindowState::BoundsChangeAnimationType::kAnimate:
            window_state->SetBoundsDirectAnimated(
                bounds, bounds_change_animation_duration_);
            break;
          case WindowState::BoundsChangeAnimationType::kAnimateZero:
            NOTREACHED();
            break;
        }
        next_bounds_change_animation_type_ =
            WindowState::BoundsChangeAnimationType::kNone;
      } else if (!window_state->IsPinned()) {
        // TODO(oshima): Define behavior for pinned app.
        bounds_change_animation_duration_ = set_bounds_event->duration();
        int64_t display_id = set_bounds_event->display_id();
        if (display_id == display::kInvalidDisplayId) {
          display_id = display::Screen::GetScreen()
                           ->GetDisplayNearestWindow(window)
                           .id();
        }
#if DCHECK_IS_ON()
        gfx::Rect bounds_in_display(bounds);
        // The coordinates of the WindowState's parent must be same as display
        // coordinates. The following code is only to verify this condition.
        const aura::Window* root = window->GetRootWindow();
        aura::Window::ConvertRectToTarget(window->parent(), root,
                                          &bounds_in_display);
        DCHECK_EQ(bounds_in_display.x(), bounds.x());
        DCHECK_EQ(bounds_in_display.y(), bounds.y());
#endif
        delegate_->HandleBoundsRequest(
            window_state, window_state->GetStateType(), bounds, display_id);
      }
      break;
    }
    default:
      NOTREACHED() << "Unknown event:" << event->type();
  }
}

void ClientControlledState::OnWindowDestroying(WindowState* window_state) {
  ResetDelegate();
}

bool ClientControlledState::EnterNextState(WindowState* window_state,
                                           WindowStateType next_state_type) {
  // Do nothing if  we're already in the same state, or delegate has already
  // been deleted.
  if (state_type_ == next_state_type || !delegate_)
    return false;
  WindowStateType previous_state_type = state_type_;
  state_type_ = next_state_type;

  window_state->UpdateWindowPropertiesFromStateType();
  window_state->NotifyPreStateTypeChange(previous_state_type);

  auto* const window = window_state->window();

  // Calling order matters. We need to handle the floated state before handling
  // the minimized state because FloatController may change the visibility of
  // the window.
  auto* const float_controller = Shell::Get()->float_controller();
  if (next_state_type == WindowStateType::kFloated) {
    if (Shell::Get()->tablet_mode_controller()->InTabletMode()) {
      float_controller->FloatForTablet(window, previous_state_type);
    } else {
      float_controller->FloatImpl(window);
    }
  }
  if (previous_state_type == WindowStateType::kFloated) {
    float_controller->UnfloatImpl(window);
  }

  // Don't update the window if the window is detached from parent.
  // This can happen during dragging.
  // TODO(oshima): This was added for DOCKED windows. Investigate if
  // we still need this.
  if (window->parent()) {
    UpdateMinimizedState(window_state, previous_state_type);
  }

  window_state->NotifyPostStateTypeChange(previous_state_type);

  if (IsPinnedWindowStateType(next_state_type) ||
      IsPinnedWindowStateType(previous_state_type)) {
    set_next_bounds_change_animation_type(
        WindowState::BoundsChangeAnimationType::kCrossFade);
    Shell::Get()->screen_pinning_controller()->SetPinnedWindow(window);
  }
  return true;
}

WindowStateType ClientControlledState::GetResolvedNextWindowStateType(
    WindowState* window_state,
    const WMEvent* event) {
  DCHECK(event->IsTransitionEvent());

  const WindowStateType next = GetStateForTransitionEvent(window_state, event);

  if (Shell::Get()->tablet_mode_controller()->InTabletMode() &&
      next == WindowStateType::kNormal && window_state->CanMaximize())
    return WindowStateType::kMaximized;

  return next;
}

void ClientControlledState::UpdateWindowForTransitionEvents(
    WindowState* window_state,
    chromeos::WindowStateType next_state_type,
    const WMEvent* event) {
  const WMEventType event_type = event->type();
  aura::Window* window = window_state->window();

  if (chromeos::IsSnappedWindowStateType(next_state_type)) {
    if (window_state->CanSnap()) {
      const bool is_restoring =
          window->GetProperty(aura::client::kIsRestoringKey) ||
          event_type == WM_EVENT_RESTORE;
      CHECK(is_restoring || event->IsSnapEvent());

      const WindowSnapActionSource snap_action_source =
          is_restoring ? WindowSnapActionSource::kSnapByWindowStateRestore
                       : event->AsSnapEvent()->snap_action_source();
      HandleWindowSnapping(window_state,
                           next_state_type == WindowStateType::kPrimarySnapped
                               ? WM_EVENT_SNAP_PRIMARY
                               : WM_EVENT_SNAP_SECONDARY,
                           snap_action_source);
      window_state->RecordWindowSnapActionSource(snap_action_source);

      // Get the desired window bounds for the snap state.
      // TODO(b/246683799): Investigate why window_state->snap_ratio() can be
      // empty.
      // Use the saved `window_state->snap_ratio()` if restoring, otherwise use
      // the event requested snap ratio, which has a default value.
      const float next_snap_ratio =
          is_restoring
              ? window_state->snap_ratio().value_or(chromeos::kDefaultSnapRatio)
              : event->AsSnapEvent()->snap_ratio();

      // We don't want Unminimize() to restore the pre-snapped state during the
      // transition. See crbug.com/1031313 for why we need this.
      // kRestoreShowStateKey property will be updated properly after the window
      // is snapped correctly.
      if (window_state->IsMinimized())
        window->ClearProperty(aura::client::kRestoreShowStateKey);

      window_state->UpdateWindowPropertiesFromStateType();
      VLOG(1) << "Processing State Transtion: event=" << event_type
              << ", state=" << state_type_
              << ", next_state=" << next_state_type;

      // Then ask delegate to set the desired bounds for the snap state.
      delegate_->HandleBoundsRequest(
          window_state, next_state_type,
          GetSnappedWindowBoundsInParent(window, next_state_type,
                                         next_snap_ratio),
          window_state->GetDisplay().id());
    }
  } else if (next_state_type == WindowStateType::kFloated) {
    if (chromeos::wm::CanFloatWindow(window)) {
      const gfx::Rect bounds =
          Shell::Get()->tablet_mode_controller()->InTabletMode()
              ? FloatController::GetFloatWindowTabletBounds(window)
              : FloatController::GetFloatWindowClamshellBounds(
                    window, event_type == WM_EVENT_FLOAT
                                ? event->AsFloatEvent()->float_start_location()
                                : chromeos::FloatStartLocation::kBottomRight);

      window_state->UpdateWindowPropertiesFromStateType();
      VLOG(1) << "Processing State Transtion: event=" << event_type
              << ", state=" << state_type_
              << ", next_state=" << next_state_type;

      // Then ask delegate to set the desired bounds for the float state.
      delegate_->HandleBoundsRequest(window_state, next_state_type, bounds,
                                     window_state->GetDisplay().id());
    }
  } else {
    // Clients handle a window state change asynchronously. So in the case
    // that the window is in a transitional state (already snapped but not
    // applied to its window state yet), we here skip to pass WM_EVENT.
    if (SplitViewController::Get(window)->IsWindowInTransitionalState(window))
      return;

    // Reset window state.
    window_state->UpdateWindowPropertiesFromStateType();
    VLOG(1) << "Processing State Transtion: event=" << event_type
            << ", state=" << state_type_ << ", next_state=" << next_state_type;

    // Then ask delegate to handle the window state change.
    delegate_->HandleWindowStateRequest(window_state, next_state_type);
  }
}

}  // namespace ash