File: on_task_pod_controller_impl.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 (273 lines) | stat: -rw-r--r-- 9,028 bytes parent folder | download | duplicates (5)
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
// Copyright 2025 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/boca/on_task/on_task_pod_controller_impl.h"

#include <memory>
#include <string>

#include "ash/boca/on_task/on_task_pod_utils.h"
#include "ash/boca/on_task/on_task_pod_view.h"
#include "ash/shell.h"
#include "ash/style/icon_button.h"
#include "ash/wm/window_pin_util.h"
#include "ash/wm/window_state.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chromeos/ash/components/boca/boca_metrics_util.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "chromeos/ui/frame/frame_header.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

namespace ash {
namespace {

// Internal name for the OnTask pod widget. Useful for debugging purposes.
constexpr char kOnTaskPodWidgetInternalName[] = "OnTaskPod";

// Creates a child widget for the specified parent window with some common
// characteristics.
std::unique_ptr<views::Widget> CreateChildWidget(
    aura::Window* parent_window,
    const std::string& widget_name,
    std::unique_ptr<views::View> view) {
  views::Widget::InitParams params(
      views::Widget::InitParams::CLIENT_OWNS_WIDGET,
      views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
  params.parent = parent_window;
  params.name = widget_name;
  params.activatable = views::Widget::InitParams::Activatable::kDefault;
  params.opacity = views::Widget::InitParams::WindowOpacity::kTranslucent;
  auto widget = std::make_unique<views::Widget>();
  widget->Init(std::move(params));
  widget->SetContentsView(std::move(view));
  widget->SetVisibilityAnimationTransition(views::Widget::ANIMATE_SHOW);

  return widget;
}
}  // namespace

OnTaskPodControllerImpl::OnTaskPodControllerImpl(Browser* browser)
    : browser_(browser->AsWeakPtr()) {
  aura::Window* const browser_window = browser_->window()->GetNativeWindow();
  auto on_task_pod_view = std::make_unique<OnTaskPodView>(this);
  pod_widget_ = CreateChildWidget(browser_window->GetToplevelWindow(),
                                  kOnTaskPodWidgetInternalName,
                                  std::move(on_task_pod_view));
  // Fetch the header height in unlocked mode, since the value changes when the
  // window enters locked fullscreen.
  frame_header_height_ = boca::GetFrameHeaderHeight(pod_widget_->parent());
  pod_widget_->widget_delegate()->SetAccessibleTitle(
      l10n_util::GetStringUTF16(IDS_ON_TASK_POD_ACCESSIBLE_NAME));
  pod_widget_->SetBounds(CalculateWidgetBounds());
  OnPageNavigationContextChanged();
  pod_widget_->SetFocusTraversableParentView(
      BrowserView::GetBrowserViewForBrowser(browser_.get()));
  pod_widget_->Show();

  browser_window->AddObserver(this);
  WindowState::Get(browser_window)->AddObserver(this);
}

OnTaskPodControllerImpl::~OnTaskPodControllerImpl() {
  if (browser_) {
    aura::Window* const browser_window = browser_->window()->GetNativeWindow();
    WindowState::Get(browser_window)->RemoveObserver(this);
    browser_window->RemoveObserver(this);
  }
}

void OnTaskPodControllerImpl::MaybeNavigateToPreviousPage() {
  if (!browser_) {
    return;
  }
  boca::RecordOnTaskPodNavigateBackClicked();
  chrome::GoBack(browser_.get(), WindowOpenDisposition::CURRENT_TAB);
}

void OnTaskPodControllerImpl::MaybeNavigateToNextPage() {
  if (!browser_) {
    return;
  }
  boca::RecordOnTaskPodNavigateForwardClicked();
  chrome::GoForward(browser_.get(), WindowOpenDisposition::CURRENT_TAB);
}

void OnTaskPodControllerImpl::ReloadCurrentPage() {
  if (!browser_) {
    return;
  }
  boca::RecordOnTaskPodReloadPageClicked();
  chrome::Reload(browser_.get(), WindowOpenDisposition::CURRENT_TAB);
}

void OnTaskPodControllerImpl::ToggleTabStripVisibility(bool show,
                                                       bool user_action) {
  if (user_action) {
    boca::RecordOnTaskPodToggleTabStripVisibilityClicked();
  }

  // Hide tab strip.
  if (!show) {
    tab_strip_reveal_lock_.reset();
    return;
  }

  // Acquire lock to reveal the tab strip.
  auto* const browser_view =
      BrowserView::GetBrowserViewForBrowser(browser_.get());
  tab_strip_reveal_lock_ =
      browser_view->immersive_mode_controller()->GetRevealedLock(
          ImmersiveModeController::ANIMATE_REVEAL_YES);
}

void OnTaskPodControllerImpl::SetSnapLocation(
    OnTaskPodSnapLocation snap_location) {
  boca::RecordOnTaskPodSetSnapLocationClicked(
      snap_location == ash::OnTaskPodSnapLocation::kTopLeft);
  pod_snap_location_ = snap_location;

  // Reposition the widget.
  pod_widget_->SetBounds(CalculateWidgetBounds());
}

void OnTaskPodControllerImpl::OnWindowBoundsChanged(
    aura::Window* window,
    const gfx::Rect& old_bounds,
    const gfx::Rect& new_bounds,
    ui::PropertyChangeReason reason) {
  if (!pod_widget_) {
    return;
  }
  pod_widget_->SetBounds(CalculateWidgetBounds());
}

void OnTaskPodControllerImpl::OnPostWindowStateTypeChange(
    WindowState* window_state,
    chromeos::WindowStateType old_type) {
  // Only update the pod when the old window and new window have different
  // pinned state.
  chromeos::WindowStateType new_type = window_state->GetStateType();
  if (chromeos::IsPinnedWindowStateType(old_type) ==
      chromeos::IsPinnedWindowStateType(new_type)) {
    return;
  }
  views::View* const pod_widget_contents_view = pod_widget_->GetContentsView();
  if (!pod_widget_contents_view) {
    return;
  }
  OnTaskPodView* const on_task_pod_view =
      static_cast<OnTaskPodView*>(pod_widget_contents_view);
  on_task_pod_view->OnLockedModeUpdate();

  // Resize and reset bounds of the widget to fit the contents view.
  pod_widget_->SetSize(on_task_pod_view->GetPreferredSize());
  pod_widget_->SetBounds(CalculateWidgetBounds());
}

void OnTaskPodControllerImpl::OnWindowVisibilityChanged(aura::Window* window,
                                                        bool visible) {
  DCHECK(pod_widget_);
  // We need to check browser window visibility directly; `visible` param is for
  // webcontents visibility, which changes when we switch tabs.
  if (browser_->window()->IsVisible()) {
    pod_widget_->Show();
  } else {
    pod_widget_->Hide();
  }
}

void OnTaskPodControllerImpl::OnPauseModeChanged(bool paused) {
  if (!pod_widget_) {
    return;
  }
  if (paused) {
    pod_widget_->Hide();
  } else {
    pod_widget_->Show();
  }
}

void OnTaskPodControllerImpl::OnPageNavigationContextChanged() {
  if (!pod_widget_) {
    return;
  }
  views::View* const pod_widget_contents_view = pod_widget_->GetContentsView();
  if (!pod_widget_contents_view) {
    return;
  }
  OnTaskPodView* const on_task_pod_view =
      static_cast<OnTaskPodView*>(pod_widget_contents_view);
  on_task_pod_view->OnPageNavigationContextUpdate();
}

bool OnTaskPodControllerImpl::CanNavigateToPreviousPage() {
  if (!browser_) {
    return false;
  }
  return chrome::CanGoBack(browser_.get());
}

bool OnTaskPodControllerImpl::CanNavigateToNextPage() {
  if (!browser_) {
    return false;
  }
  return chrome::CanGoForward(browser_.get());
}

bool OnTaskPodControllerImpl::CanToggleTabStripVisibility() {
  const auto* const browser_view =
      BrowserView::GetBrowserViewForBrowser(browser_.get());
  return browser_ && platform_util::IsBrowserLockedFullscreen(browser_.get()) &&
         browser_view->immersive_mode_controller()->IsEnabled();
}

const gfx::Rect OnTaskPodControllerImpl::CalculateWidgetBounds() {
  const gfx::Rect parent_window_bounds =
      pod_widget_->parent()->GetWindowBoundsInScreen();
  const gfx::Size preferred_size =
      pod_widget_->GetContentsView()->GetPreferredSize();
  gfx::Point origin;
  switch (pod_snap_location_) {
    case ash::OnTaskPodSnapLocation::kTopLeft:
      origin = gfx::Point(parent_window_bounds.x() + kPodVerticalBorder,
                          parent_window_bounds.y() + frame_header_height_ +
                              kPodHorizontalBorder);
      break;
    case ash::OnTaskPodSnapLocation::kTopRight:
      origin = gfx::Point(parent_window_bounds.right() -
                              preferred_size.width() - kPodVerticalBorder,
                          parent_window_bounds.y() + frame_header_height_ +
                              kPodHorizontalBorder);
  }

  return gfx::Rect(origin, preferred_size);
}

views::Widget* OnTaskPodControllerImpl::GetPodWidgetForTesting() {
  if (!pod_widget_) {
    return nullptr;
  }
  return pod_widget_.get();
}

ImmersiveRevealedLock*
OnTaskPodControllerImpl::GetTabStripRevealLockForTesting() {
  if (!tab_strip_reveal_lock_) {
    return nullptr;
  }
  return tab_strip_reveal_lock_.get();
}

OnTaskPodSnapLocation OnTaskPodControllerImpl::GetSnapLocationForTesting()
    const {
  return pod_snap_location_;
}

}  // namespace ash