File: capture_window_observer.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 (203 lines) | stat: -rw-r--r-- 6,790 bytes parent folder | download | duplicates (6)
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
// Copyright 2020 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/capture_mode/capture_window_observer.h"

#include "ash/app_list/app_list_controller_impl.h"
#include "ash/capture_mode/capture_mode_camera_controller.h"
#include "ash/capture_mode/capture_mode_controller.h"
#include "ash/capture_mode/capture_mode_session.h"
#include "ash/capture_mode/capture_mode_util.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/shell.h"
#include "ash/wm/desks/desks_util.h"
#include "ui/compositor/layer.h"
#include "ui/wm/public/activation_client.h"

namespace ash {

CaptureWindowObserver::CaptureWindowObserver(
    CaptureModeSession* capture_mode_session)
    : capture_mode_session_(capture_mode_session) {
  Shell::Get()->activation_client()->AddObserver(this);
  DesksController::Get()->AddObserver(this);
}

CaptureWindowObserver::~CaptureWindowObserver() {
  DesksController::Get()->RemoveObserver(this);
  Shell::Get()->activation_client()->RemoveObserver(this);
  StopObserving();
}

void CaptureWindowObserver::UpdateSelectedWindowAtPosition(
    const gfx::Point& location_in_screen) {
  if (capture_mode_session_->IsInCountDownAnimation()) {
    return;
  }

  location_in_screen_ = location_in_screen;

  SetSelectedWindow(
      capture_mode_util::GetTopMostCapturableWindowAtPoint(location_in_screen),
      /*a11y_alert_again=*/false,
      /*bar_anchored_to_window=*/false);
  capture_mode_session_->UpdateCursor(location_in_screen, /*is_touch=*/false);
}

void CaptureWindowObserver::SetSelectedWindow(aura::Window* window,
                                              bool a11y_alert_again,
                                              bool bar_anchored_to_window) {
  if (window_ == window) {
    if (a11y_alert_again && window_) {
      capture_mode_session_->A11yAlertCaptureSource(/*trigger_now=*/true);
    }
    return;
  }

  if (window_ && bar_anchored_to_window_) {
    return;
  }

  bar_anchored_to_window_ = bar_anchored_to_window;

  // Don't capture wallpaper window.
  if (window && window->parent() &&
      window->parent()->GetId() == kShellWindowId_WallpaperContainer) {
    window = nullptr;
  }

  // Don't capture the shelf.
  if (window && window->parent() &&
      window->parent()->GetId() == kShellWindowId_ShelfContainer) {
    window = nullptr;
  }

  // Don't capture home screen window.
  if (window &&
      window == Shell::Get()->app_list_controller()->GetHomeScreenWindow()) {
    window = nullptr;
  }

  // Stop observing the current selected window if there is one.
  StopObserving();
  if (window) {
    StartObserving(window);
    capture_mode_session_->A11yAlertCaptureSource(/*trigger_now=*/true);
  }
  RepaintCaptureRegion();

  auto* controller = CaptureModeController::Get();
  if (!controller->is_recording_in_progress())
    controller->camera_controller()->MaybeReparentPreviewWidget();
  capture_mode_session_->MaybeUpdateCaptureUisOpacity(
      display::Screen::GetScreen()->GetCursorScreenPoint());
}

void CaptureWindowObserver::OnWindowBoundsChanged(
    aura::Window* window,
    const gfx::Rect& old_bounds,
    const gfx::Rect& new_bounds,
    ui::PropertyChangeReason reason) {
  DCHECK_EQ(window, window_);
  RepaintCaptureRegion();

  // The bounds of camera preview should be updated accordingly if the bounds of
  // the selected window has been updated.
  auto* controller = CaptureModeController::Get();
  if (!controller->is_recording_in_progress())
    controller->camera_controller()->MaybeUpdatePreviewWidget();

  // The bounds of the capture bar should be updated accordingly if the bounds
  // of the selected window has been updated.
  if (bar_anchored_to_window_ &&
      capture_mode_session_->GetCaptureModeBarWidget()) {
    capture_mode_session_->RefreshBarWidgetBounds();
  }
}

void CaptureWindowObserver::OnWindowParentChanged(aura::Window* window,
                                                  aura::Window* parent) {
  if (!parent || !bar_anchored_to_window_) {
    return;
  }
  CHECK_EQ(window, window_);
  if (!desks_util::BelongsToActiveDesk(window)) {
    // Window has been sent to another desk, so we should stop recording.
    CaptureModeController::Get()->Stop();
    return;
  }
  // Move the capture mode widgets to the new root and repaint the capture
  // region when the window parent changes. E.g, `window_` is moved to another
  // display.
  capture_mode_session_->MaybeChangeRoot(window_->GetRootWindow(),
                                         /*root_window_will_shutdown=*/false);
  RepaintCaptureRegion();
}

void CaptureWindowObserver::OnWindowVisibilityChanging(aura::Window* window,
                                                       bool visible) {
  CHECK_EQ(window, window_);
  CHECK(!visible);
  StopObserving();

  if (bar_anchored_to_window_ ||
      capture_mode_session_->IsInCountDownAnimation()) {
    CaptureModeController::Get()->Stop();
    return;
  }

  UpdateSelectedWindowAtPosition(location_in_screen_);
}

void CaptureWindowObserver::OnWindowDestroying(aura::Window* window) {
  CHECK_EQ(window, window_);
  StopObserving();

  if (bar_anchored_to_window_ ||
      capture_mode_session_->IsInCountDownAnimation()) {
    CaptureModeController::Get()->Stop();
    return;
  }

  UpdateSelectedWindowAtPosition(location_in_screen_);
}

void CaptureWindowObserver::OnWindowActivated(ActivationReason reason,
                                              aura::Window* gained_active,
                                              aura::Window* lost_active) {
  // If another window is activated on top of the current selected window, we
  // may change the selected window to the activated window if it's under the
  // current event location. If there is no selected window at the moment, we
  // also want to check if new activated window should be focused.
  UpdateSelectedWindowAtPosition(location_in_screen_);
}

void CaptureWindowObserver::OnDeskActivationChanged(const Desk* activated,
                                                    const Desk* deactivated) {
  // When the desk switches and the window and bar are no longer visible, we
  // should stop the session.
  if (window_ && bar_anchored_to_window_ &&
      !desks_util::BelongsToActiveDesk(window_)) {
    CaptureModeController::Get()->Stop();
  }
}

void CaptureWindowObserver::StartObserving(aura::Window* window) {
  window_ = window;
  window_->AddObserver(this);
}

void CaptureWindowObserver::StopObserving() {
  if (window_) {
    window_->RemoveObserver(this);
    window_ = nullptr;
  }
}

void CaptureWindowObserver::RepaintCaptureRegion() {
  ui::Layer* layer = capture_mode_session_->layer();
  layer->SchedulePaint(layer->bounds());
}

}  // namespace ash