File: arc_ghost_window_delegate.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (196 lines) | stat: -rw-r--r-- 6,298 bytes parent folder | download | duplicates (3)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/app_restore/arc_ghost_window_delegate.h"

#include "base/notreached.h"
#include "chrome/browser/ash/app_restore/arc_ghost_window_shell_surface.h"
#include "chrome/browser/ash/app_restore/arc_window_utils.h"
#include "chrome/browser/ash/arc/window_predictor/window_predictor_utils.h"
#include "chromeos/ui/base/window_state_type.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"

namespace ash::full_restore {

namespace {

const int kNullWindowState = -1;

}  // namespace

ArcGhostWindowDelegate::ArcGhostWindowDelegate(
    exo::ClientControlledShellSurface* shell_surface,
    int window_id,
    const std::string& app_id,
    int64_t display_id,
    const gfx::Rect& bounds,
    chromeos::WindowStateType window_state)
    : window_id_(window_id),
      app_id_(app_id),
      bounds_(gfx::Rect(bounds)),
      pending_close_(false),
      window_state_(window_state),
      shell_surface_(shell_surface) {
  DCHECK(shell_surface);

  observation_.Observe(ArcGhostWindowHandler::Get());
  SetDisplayId(display_id);
}
ArcGhostWindowDelegate::~ArcGhostWindowDelegate() = default;

void ArcGhostWindowDelegate::OnGeometryChanged(const gfx::Rect& geometry) {}

void ArcGhostWindowDelegate::OnStateChanged(
    chromeos::WindowStateType old_state_type,
    chromeos::WindowStateType new_state) {
  if (old_state_type == new_state)
    return;

  auto* window_state =
      WindowState::Get(shell_surface_->GetWidget()->GetNativeWindow());

  if (!window_state || !shell_surface_->host_window()->GetRootWindow())
    return;

  display::Display display;
  const display::Screen* screen = display::Screen::GetScreen();
  auto display_existed = screen->GetDisplayWithDisplayId(display_id_, &display);
  DCHECK(display_existed);

  switch (new_state) {
    case chromeos::WindowStateType::kNormal:
    case chromeos::WindowStateType::kDefault:
      // Reset geometry for previous bounds.
      shell_surface_->SetBounds(display_id_, bounds_);
      shell_surface_->SetRestored();
      break;
    case chromeos::WindowStateType::kMinimized:
      shell_surface_->SetMinimized();
      break;
    case chromeos::WindowStateType::kMaximized:
      shell_surface_->SetMaximized();
      // Update geometry for showing overlay as maximized bounds.
      shell_surface_->SetBounds(display_id_, display.bounds());
      break;
    case chromeos::WindowStateType::kFullscreen:
      // TODO(sstan): Adjust bounds like maximized state.
      shell_surface_->SetFullscreen(true, display::kInvalidDisplayId);
      break;
    default:
      NOTIMPLEMENTED();
      break;
  }
  shell_surface_->OnSurfaceCommit();
  window_state_ = new_state;
}

void ArcGhostWindowDelegate::OnBoundsChanged(
    chromeos::WindowStateType current_state,
    chromeos::WindowStateType requested_state,
    int64_t display_id,
    const gfx::Rect& bounds_in_screen,
    bool is_resize,
    int bounds_change,
    bool is_adjusted_bounds) {
  auto* window_state =
      WindowState::Get(shell_surface_->GetWidget()->GetNativeWindow());

  if (!window_state || !shell_surface_->host_window()->GetRootWindow())
    return;

  display::Display target_display;
  const display::Screen* screen = display::Screen::GetScreen();

  if (!screen->GetDisplayWithDisplayId(display_id, &target_display))
    return;

  if (display_id_ != display_id) {
    if (!SetDisplayId(display_id))
      return;
  }

  // Don't change the bounds in maximize/fullscreen/pinned state.
  if (window_state->IsMaximizedOrFullscreenOrPinned() &&
      requested_state == window_state->GetStateType()) {
    return;
  }

  switch (requested_state) {
    case chromeos::WindowStateType::kPrimarySnapped:
      // TODO(b/279530665): Maybe sync to ARC.
      shell_surface_->SetSnapPrimary(chromeos::kDefaultSnapRatio);
      break;
    case chromeos::WindowStateType::kSecondarySnapped:
      // TODO(b/279530665): Maybe sync to ARC.
      shell_surface_->SetSnapSecondary(chromeos::kDefaultSnapRatio);
      break;
    case chromeos::WindowStateType::kFloated:
      // Ignore the unsupported request.
      return;
    default:
      if (requested_state != window_state->GetStateType()) {
        NOTREACHED();
      }
  }

  shell_surface_->SetBounds(display_id, bounds_in_screen);
  shell_surface_->OnSurfaceCommit();
  bounds_ = gfx::Rect(bounds_in_screen);
  UpdateWindowInfoToArc();
}

void ArcGhostWindowDelegate::OnDragStarted(int component) {}

void ArcGhostWindowDelegate::OnDragFinished(int x, int y, bool canceled) {}

void ArcGhostWindowDelegate::OnZoomLevelChanged(exo::ZoomChange zoom_change) {}

// ArcGhostWindowHandler::Observer
void ArcGhostWindowDelegate::OnAppInstanceConnected() {
  // Update window info to ARC when app instance connected, since the previous
  // window info may not be delivered.
  UpdateWindowInfoToArc();
}

void ArcGhostWindowDelegate::OnWindowCloseRequested(int window_id) {
  if (window_id != window_id_)
    return;
  pending_close_ = true;
  UpdateWindowInfoToArc();
}

void ArcGhostWindowDelegate::OnAppStatesUpdate(const std::string& app_id,
                                               bool ready,
                                               bool need_fixup) {
  if (app_id != app_id_)
    return;

  // Currently the type update is oneway. If an App need fixup, is not able to
  // become another state before it's ready.
  if (need_fixup) {
    static_cast<ArcGhostWindowShellSurface*>(shell_surface_)
        ->SetWindowType(arc::GhostWindowType::kFixup);
  }
}

bool ArcGhostWindowDelegate::SetDisplayId(int64_t display_id) {
  std::optional<double> scale_factor = GetDisplayScaleFactor(display_id);
  if (!scale_factor.has_value()) {
    LOG(ERROR) << "Invalid display id for ARC Ghost Window";
    scale_factor_ = 1.;
    return false;
  }
  scale_factor_ = scale_factor.value();
  display_id_ = display_id;
  return true;
}

void ArcGhostWindowDelegate::UpdateWindowInfoToArc() {
  ArcGhostWindowHandler::Get()->OnWindowInfoUpdated(
      window_id_, pending_close_ ? kNullWindowState : (int)window_state_,
      display_id_, gfx::ScaleToRoundedRect(bounds_, scale_factor_));
}

}  // namespace ash::full_restore