File: arc_ghost_window_shell_surface.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (236 lines) | stat: -rw-r--r-- 9,138 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
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
// 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_shell_surface.h"

#include "ash/wm/desks/desks_util.h"
#include "ash/wm/window_state.h"
#include "base/check_op.h"
#include "chrome/browser/ash/app_list/arc/arc_app_list_prefs.h"
#include "chrome/browser/ash/app_restore/arc_ghost_window_delegate.h"
#include "chrome/browser/ash/app_restore/arc_ghost_window_view.h"
#include "chrome/browser/ash/app_restore/arc_window_utils.h"
#include "chrome/browser/ash/arc/window_predictor/window_predictor_utils.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chromeos/ui/base/window_state_type.h"
#include "chromeos/ui/frame/frame_utils.h"
#include "components/app_restore/app_restore_data.h"
#include "components/app_restore/window_properties.h"
#include "components/exo/buffer.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/caption_button_types.h"

namespace ash::full_restore {

ArcGhostWindowShellSurface::ArcGhostWindowShellSurface(
    std::unique_ptr<exo::Surface> surface,
    int container,
    const std::string& application_id)
    : ClientControlledShellSurface(surface.get(),
                                   /*can_minimize=*/true,
                                   container,
                                   /*default_scale_cancellation=*/true,
                                   /*supports_floated_state=*/false) {
  controller_surface_ = std::move(surface);
  buffer_ = exo::Buffer::CreateBuffer(
      gfx::Size(1, 1), gfx::BufferFormat::RGBA_8888, gfx::BufferUsage::GPU_READ,
      "ArcGhostWindowShellSurface", gpu::kNullSurfaceHandle,
      /*shutdown_event=*/nullptr);
  SetApplicationId(application_id.c_str());
  controller_surface_->Attach(buffer_.get());
  controller_surface_->SetFrame(exo::SurfaceFrameType::NORMAL);
}

ArcGhostWindowShellSurface::~ArcGhostWindowShellSurface() {
  controller_surface_.reset();
  buffer_.reset();
}

// static
std::unique_ptr<ArcGhostWindowShellSurface> ArcGhostWindowShellSurface::Create(
    const std::string& app_id,
    arc::GhostWindowType type,
    int window_id,
    const gfx::Rect& bounds,
    app_restore::AppRestoreData* restore_data,
    base::RepeatingClosure close_callback) {
  // ArcGhostWindowShellSurface need a valid display id, or it cannot be
  // created.
  int64_t display_id_value =
      restore_data->display_id.value_or(display::kInvalidDisplayId);

  const chromeos::WindowStateType window_state =
      restore_data->window_info.window_state_type.value_or(
          chromeos::WindowStateType::kDefault);

  gfx::Rect local_bounds = bounds;
  // If the window is maximize / minimized, the initial bounds will be
  // unnecessary. Here set it as display size to ensure the content render is
  // correct.
  if (local_bounds.IsEmpty()) {
    DCHECK(chromeos::IsMaximizedOrFullscreenWindowStateType(window_state) ||
           chromeos::IsMinimizedWindowStateType(window_state));
    display::Display disp;
    display::Screen::GetScreen()->GetDisplayWithDisplayId(display_id_value,
                                                          &disp);
    local_bounds = disp.work_area();
  }

  // TODO(sstan): Fallback to system default color or other topic color, if
  // the task hasn't valid theme color.
  uint32_t theme_color =
      restore_data->status_bar_color.has_value() &&
              IsValidThemeColor(restore_data->status_bar_color.value())
          ? restore_data->status_bar_color.value()
          : SK_ColorWHITE;

  // TODO(sstan): Handle the desk container from full_restore data.
  int container = desks_util::GetActiveDeskContainerId();

  auto surface = std::make_unique<exo::Surface>();
  std::unique_ptr<ArcGhostWindowShellSurface> shell_surface(
      new ArcGhostWindowShellSurface(std::move(surface), container,
                                     WrapSessionAppIdFromWindowId(window_id)));

  // TODO(sstan): Add set_surface_destroyed_callback.
  shell_surface->set_delegate(std::make_unique<ArcGhostWindowDelegate>(
      shell_surface.get(), window_id, app_id, display_id_value, local_bounds,
      window_state));
  shell_surface->set_close_callback(std::move(close_callback));

  shell_surface->SetAppId(app_id);
  shell_surface->SetBounds(display_id_value, local_bounds);

  const std::optional<app_restore::WindowInfo::ArcExtraInfo>& arc_info =
      restore_data->window_info.arc_extra_info;
  if (arc_info) {
    if (arc_info->maximum_size.has_value()) {
      shell_surface->SetMaximumSize(*arc_info->maximum_size);
    }
    if (arc_info->minimum_size.has_value()) {
      shell_surface->SetMinimumSize(*arc_info->minimum_size);
    }
  }

  if (restore_data->window_info.app_title.has_value()) {
    shell_surface->SetTitle(*restore_data->window_info.app_title);
  }

  // Set frame buttons.
  constexpr uint32_t kVisibleButtonMask =
      1 << views::CAPTION_BUTTON_ICON_MINIMIZE |
      1 << views::CAPTION_BUTTON_ICON_MAXIMIZE_RESTORE |
      1 << views::CAPTION_BUTTON_ICON_CLOSE |
      1 << views::CAPTION_BUTTON_ICON_BACK;
  shell_surface->SetFrameButtons(kVisibleButtonMask, kVisibleButtonMask);
  shell_surface->OnSetFrameColors(theme_color, theme_color);

  std::optional<gfx::RoundedCornersF> overlay_corners_radii;
  if (ash::WindowState::ShouldWindowStateHaveRoundedCorners(window_state)) {
    CHECK_NE(window_state, chromeos::WindowStateType::kPip);

    const gfx::RoundedCornersF window_radii =
        chromeos::GetWindowRoundedCorners();
    shell_surface->SetWindowCornersRadii(window_radii);

    // Ghost surface shadow radii must match the window radii.
    shell_surface->SetShadowCornersRadii(window_radii);

    // Ghost surface is an overlay widget, so its corners must be rounded. The
    // bottom two corners of the ghost window overlay overlap with the window,
    // so we need to round them.
    overlay_corners_radii = gfx::RoundedCornersF(
        0, 0, window_radii.lower_right(), window_radii.lower_left());
  }

  shell_surface->controller_surface()->Commit();

  shell_surface->InitContentOverlay(app_id, theme_color, type,
                                    std::move(overlay_corners_radii));

  // Relayout overlay.
  shell_surface->GetWidget()->LayoutRootViewIfNecessary();

  // Change the window state at the last operation, since we need create the
  // window entity first.
  if (chromeos::IsMaximizedOrFullscreenWindowStateType(window_state)) {
    shell_surface->SetMaximized();
    shell_surface->controller_surface()->Commit();
  } else if (chromeos::IsMinimizedWindowStateType(window_state)) {
    shell_surface->SetMinimized();
    shell_surface->controller_surface()->Commit();
  } else {
    // Reset the same bounds, to make sure white background can be located in
    // correct place. Without this operation, the white background will not
    // at the same location with window bounds.
    shell_surface->SetBounds(display_id_value, local_bounds);
  }

  return shell_surface;
}

void ArcGhostWindowShellSurface::OverrideInitParams(
    views::Widget::InitParams* params) {
  ClientControlledShellSurface::OverrideInitParams(params);
  SetShellAppId(&params->init_properties_container, app_id_);
}

exo::Surface* ArcGhostWindowShellSurface::controller_surface() {
  return controller_surface_.get();
}

void ArcGhostWindowShellSurface::InitContentOverlay(
    const std::string& app_id,
    uint32_t theme_color,
    arc::GhostWindowType type,
    std::optional<gfx::RoundedCornersF>&& corners_radii) {
  std::string app_name;
  // TODO(sstan): Move this part out of shell surface.
  // In test env, ArcAppListPrefs or App maybe null.
  auto* pref = ArcAppListPrefs::Get(ProfileManager::GetPrimaryUserProfile());
  if (pref) {
    auto app_info = pref->GetApp(app_id);
    if (app_info)
      app_name = app_info->name;
  }
  auto view = std::make_unique<ArcGhostWindowView>(this, app_name);
  view_observer_ = view.get();
  view->LoadIcon(app_id);

  view->SetThemeColor(theme_color);
  view->SetGhostWindowViewType(type);

  exo::ShellSurfaceBase::OverlayParams overlay_params(std::move(view));
  overlay_params.translucent = true;
  overlay_params.overlaps_frame = false;
  overlay_params.corners_radii = std::move(corners_radii);
  AddOverlay(std::move(overlay_params));
}

void ArcGhostWindowShellSurface::SetAppId(
    const std::optional<std::string>& id) {
  app_id_ = id;
  if (GetWidget() && GetWidget()->GetNativeWindow()) {
    SetShellAppId(GetWidget()->GetNativeWindow(), app_id_);
  }
}

void ArcGhostWindowShellSurface::SetShellAppId(
    ui::PropertyHandler* property_handler,
    const std::optional<std::string>& id) {
  if (id)
    property_handler->SetProperty(app_restore::kAppIdKey, *id);
  else
    property_handler->ClearProperty(app_restore::kAppIdKey);
}

void ArcGhostWindowShellSurface::SetWindowType(
    arc::GhostWindowType window_type) {
  DCHECK(view_observer_);
  view_observer_->SetGhostWindowViewType(window_type);
}

}  // namespace ash::full_restore