File: capture_mode_session_focus_cycler.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (407 lines) | stat: -rw-r--r-- 17,219 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
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
// 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.

#ifndef ASH_CAPTURE_MODE_CAPTURE_MODE_SESSION_FOCUS_CYCLER_H_
#define ASH_CAPTURE_MODE_CAPTURE_MODE_SESSION_FOCUS_CYCLER_H_

#include <cstddef>
#include <vector>

#include "ash/ash_export.h"
#include "ash/capture_mode/capture_mode_types.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_multi_source_observation.h"
#include "ui/aura/window_observer.h"
#include "ui/views/widget/unique_widget_ptr.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_observer.h"

namespace views {
class AXVirtualView;
class FocusRing;
class HighlightPathGenerator;
class View;
class Widget;
}  // namespace views

namespace ash {

class CaptureModeSession;
class ScopedA11yOverrideWindowSetter;

// CaptureModeSessionFocusCycler handles the special focus transitions which
// happen between the capture session UI items. These include the capture bar
// buttons, the selection region UI and the capture button.
class ASH_EXPORT CaptureModeSessionFocusCycler : public views::WidgetObserver {
 public:
  // The different groups which can receive focus during a capture mode session.
  // A group may have multiple items which can receive focus.
  // TODO(crbug.com/40170806): Investigate removing the groups concept and
  // having one flat list.
  enum class FocusGroup {
    kNone = 0,
    // The buttons to select the capture type and source on the capture bar.
    kTypeSource,
    // The start recording button inside the game capture bar.
    kStartRecordingButton,
    // In region mode, the UI to adjust a partial region.
    kSelection,
    // The button in the middle of a selection region to capture or record.
    kCaptureButton,
    // In window mode, the group to tab through the available MRU windows.
    kCaptureWindow,
    // The buttons to open the settings menu and exit capture mode on the
    // capture bar.
    kSettingsClose,
    // A state where nothing is visibly focused. The next focus will advance to
    // the settings menu. This is to match tab focusing on other menus such as
    // quick settings.
    kPendingSettings,
    // The buttons inside the settings menu.
    kSettingsMenu,
    // The camera preview shown inside the current capture surface.
    kCameraPreview,
    // Similar to `kPendingSettings` above, but for the recording type menu.
    kPendingRecordingType,
    // The menu items inside the recording type menu.
    kRecordingTypeMenu,
    // The action buttons that may be available during region selection.
    kActionButtons,
    // The search results panel that can appear when Sunfish is enabled.
    kSearchResultsPanel,
    // The web contents inside the search results panel.
    kSearchResultsPanelWebContents,
  };

  // If a focusable capture session item is part of a views hierarchy, it needs
  // to override this class, which manages a custom focus ring.
  class HighlightableView {
   public:
    bool has_focus() const { return has_focus_; }

    // Get the view class associated with |this|.
    virtual views::View* GetView() = 0;

    // Subclasses can override this for a custom focus ring shape. Defaults to
    // return nullptr, which means the focus ring will use the
    // HighlightPathGenerator used for clipping ink drops.
    virtual std::unique_ptr<views::HighlightPathGenerator>
    CreatePathGenerator();

    // Sets `needs_highlight_path_` to true, so that a new highlight path
    // generator can be created and installed on the focus ring the next time
    // `PseudoFocus()` is called.
    void InvalidateFocusRingPath();

    // Sets the focus predicate for the view so it will work with both regular
    // focus and `PseudoFocus()`.
    void SetUpFocusPredicate();

    // Shows the focus ring and triggers setting accessibility focus on the
    // associated view.
    virtual void PseudoFocus();

    // Hides the focus ring.
    virtual void PseudoBlur();

    // Attempt to mimic a click on the associated view. Called by
    // CaptureModeSession when it receives a space, or enter key events, as the
    // button is not actuallly focused and will do nothing otherwise. Triggers
    // the button handler if the view is a subclass of Button, and returns true.
    // Does nothing otherwise and returns false.
    virtual bool ClickView();

   protected:
    HighlightableView();
    virtual ~HighlightableView();

    // TODO(crbug.com/40170806): This can result in multiple of these objects
    // thinking they have focus if CaptureModeSessionFocusCycler does not call
    // PseudoFocus or PseudoBlur properly. Investigate if there is a better
    // approach.
    bool has_focus_ = false;

   private:
    // A convenience pointer to the focus ring, which is owned by the views
    // hierarchy.
    raw_ptr<views::FocusRing, DanglingUntriaged> focus_ring_ = nullptr;

    // True until a highlight path generator has been installed on the focus
    // ring. The path generator can be refreshed (e.g. to change the shape of
    // the focus ring) via calling `InvalidateFocusRingPath()`, which will set
    // this to back to `true`.
    bool needs_highlight_path_ = true;

    base::WeakPtrFactory<HighlightableView> weak_ptr_factory_{this};
  };

  // An aura window that can be focused in capture session.
  class HighlightableWindow : public HighlightableView,
                              public aura::WindowObserver {
   public:
    HighlightableWindow(aura::Window* window, CaptureModeSession* session);
    HighlightableWindow(const HighlightableWindow&) = delete;
    HighlightableWindow& operator=(const HighlightableWindow&) = delete;
    ~HighlightableWindow() override;

    // HighlightableView:
    views::View* GetView() override;
    void PseudoFocus() override;
    void PseudoBlur() override;
    bool ClickView() override;

    // aura::WindowObserver:
    void OnWindowDestroying(aura::Window* window) override;

   private:
    const raw_ptr<aura::Window> window_;
    const raw_ptr<CaptureModeSession> session_;
  };

  // Defines a type for a callback that can be called to construct a highlight
  // path generator which will be used for a custom focus ring shape.
  using HighlightPathGeneratorFactory =
      base::RepeatingCallback<std::unique_ptr<views::HighlightPathGenerator>()>;

  // A helper class that creates a highlightable object for a given view. The
  // helper is mainly used for the views that need to be created by other
  // classes, such as the `IconButton` created by `IconSwitch`.
  class HighlightHelper
      : public CaptureModeSessionFocusCycler::HighlightableView {
   public:
    explicit HighlightHelper(views::View* view);
    HighlightHelper(views::View* view, HighlightPathGeneratorFactory callback);
    HighlightHelper(const HighlightHelper&) = delete;
    HighlightHelper& operator=(const HighlightHelper&) = delete;
    ~HighlightHelper() override;

    static void Install(views::View* view);
    static void Install(views::View* view,
                        HighlightPathGeneratorFactory callback);
    static HighlightHelper* Get(views::View* view);

    // CaptureModeSessionFocusCycler::HighlightableView:
    views::View* GetView() override;
    std::unique_ptr<views::HighlightPathGenerator> CreatePathGenerator()
        override;

   private:
    const raw_ptr<views::View> view_;
    HighlightPathGeneratorFactory highlight_path_generator_factory_;
  };

  explicit CaptureModeSessionFocusCycler(CaptureModeSession* session);
  CaptureModeSessionFocusCycler(const CaptureModeSessionFocusCycler&) = delete;
  CaptureModeSessionFocusCycler& operator=(
      const CaptureModeSessionFocusCycler&) = delete;
  ~CaptureModeSessionFocusCycler() override;

  // Advances the focus by simulating focus on a view, or updating the
  // CaptureModeSession to draw focus on elements which are not associated with
  // a view. The order is as follows:
  //   1) Capture mode type and source: (Screenshot/recording) and
  //      (fullscreen/region/window) on the capture bar.
  //   2) Region selection area: If visible.
  //   3) Capture/record button: If visible.
  //   4) Recording type menu: If visible.
  //   5) Settings menu: If visible.
  //   6) Settings and close button: On the capture bar.
  // This should be called by CaptureModeSession when it receives a VKEY_TAB.
  void AdvanceFocus(bool reverse);

  // Removes focus. Called by CaptureModeSession when it receives a VKEY_ESC, or
  // when the state has changed such that a currently focus item is invalid
  // (i.e. switching from region mode to windowed mode makes a focused selection
  // or capture button invalid).
  void ClearFocus();

  // Returns true if anything has focus.
  bool HasFocus() const;

  // Activates the currently focused view (if any) (e.g. by pressing a button if
  // the focused view is a button). If the given `ignore_view` is the currently
  // focused view, it does nothing and returns false. Returns true if the
  // focused view should take the event; when this happens the
  // CaptureModeSession should not handle the event.
  bool MaybeActivateFocusedView(views::View* ignore_view);

  // Returns true if the current focus group is associated with the UI used for
  // displaying a region.
  bool RegionGroupFocused() const;

  // Returns true if the current focus group is associated with capture bar,
  // otherwise returns false.
  bool CaptureBarFocused() const;

  // Returns true if the current focus is on capture label, otherwise returns
  // false.
  bool CaptureLabelFocused() const;

  // Gets the current fine tune position for drawing the focus rects/rings on
  // the session's layer. Returns FineTunePosition::kNone if focus is on another
  // group.
  FineTunePosition GetFocusedFineTunePosition() const;

  // Called when the fine tune position is updated, to update the focus ring of
  // the focused fine tune position if needed. (Note that this will not create a
  // focus ring if one doesn't exist.) If `notify_selection_event` is true, this
  // will also trigger an a11y announcement that the fine tune position has been
  // selected.
  void OnFineTunePositionUpdated(bool notify_selection_event);

  // Called when the capture label widget is made visible or hidden, or changes
  // states. If the label button is visible, it should be on the a11y annotation
  // cycle, otherwise it should be removed from the a11y annotation cycle.
  void OnCaptureLabelWidgetUpdated();

  // Called when either the settings or the recording type menus `widget`'s are
  // opened to set up the focus state. The given `focus_group` will be set as
  // the `current_focus_group_`. If `by_key_event` is true, it means the menu
  // was opened via keyboard navigation, and therefore future calls to
  // `AdvanceFocus()` will navigate to items within the menu, rather than
  // closing the menu.
  void OnMenuOpened(views::Widget* widget,
                    FocusGroup focus_group,
                    bool by_key_event);

  // Called when the search results panel is created. Observes the
  // `panel_widget` so focus can be changed to a different element if the panel
  // is destroyed while it has focus.
  void OnSearchResultsPanelCreated(views::Widget* panel_widget);

  // Called when Scanner actions are fetched, to move focus onto the first
  // suggested action if needed after the smart actions button is removed.
  void OnScannerActionsFetched();

  // Called when the disclaimer widget is opened, to move focus onto the
  // disclaimer if needed.
  void OnDisclaimerWidgetOpened(views::Widget* disclaimer_widget);

  // Called when the disclaimer widget is closed, to pass focus back to the
  // focus cycler if needed.
  void OnDisclaimerWidgetClosed();

  // Advances focus to the next item, assuming the last focusable element inside
  // the search results panel web contents has been reached.
  void AdvanceFocusAfterSearchResultsPanel(bool reverse);

  // Uses the `scoped_a11y_overrider_` to set the A11y override window to the
  // search results panel's native window.
  void SetA11yOverrideWindowToSearchResultsPanel();

  // views::WidgetObserver:
  void OnWidgetClosing(views::Widget* widget) override;
  void OnWidgetDestroying(views::Widget* widget) override;

 private:
  friend class CaptureModeSessionTestApi;

  // Removes the focus ring from the current focused item if possible. Does not
  // alter |current_focus_group_| or |focus_index_|.
  void ClearCurrentVisibleFocus();

  // Get the next group in the focus order.
  FocusGroup GetNextGroup(bool reverse) const;

  // Returns the current focus group list. It will be one of
  // `groups_for_fullscreen_`, `groups_for_region_` and `groups_for_window_`,
  // depending on the current capture source.
  const std::vector<FocusGroup>& GetCurrentGroupList() const;

  // Returns true if the given `group` is available inside the focus group list
  // returned from GetCurrentGroupList().
  bool IsGroupAvailable(FocusGroup group) const;

  // Returns the number of elements in a particular group.
  size_t GetGroupSize(FocusGroup group) const;

  // Returns the items of a particular |group|. Returns an empty array for the
  // kSelection group as the items in that group do not have associated views.
  std::vector<HighlightableView*> GetGroupItems(FocusGroup group) const;

  // Updates the capture mode widgets so that accessibility features can
  // traverse between our widgets.
  void UpdateA11yAnnotation();

  views::Widget* GetSettingsMenuWidget() const;
  views::Widget* GetRecordingTypeMenuWidget() const;

  // Returns the window which is supposed to be set as the a11y override window
  // for accessibility controller according to the `current_focus_group_`.
  aura::Window* GetA11yOverrideWindow() const;

  // Returns true if there's a focused view in the given `views`, otherwise
  // returns false. In the meanwhile, updates `focus_index_` according to the
  // index of the current focused view to ensure it is up to date.
  bool FindFocusedViewAndUpdateFocusIndex(
      std::vector<HighlightableView*> views);

  // Highlights the corresponding HighlightableWindow first before moving the
  // focus on the items inside. This happens when current focus group is
  // `kCaptureWindow`, we need to focus the window first to update it as the
  // current selected window. Thus the camera preview can be shown inside the
  // updated selected window.
  void MaybeFocusHighlightableWindow(
      const std::vector<HighlightableView*>& current_views);

  // Callback for when `ax_widget_` is closing. Clears `ax_virtual_views_` which
  // has pointers to objects owned by `ax_widget_`.
  void OnAXWidgetClosing();

  // The current focus group and focus index.
  FocusGroup current_focus_group_ = FocusGroup::kNone;
  size_t focus_index_ = 0u;

  // Focusable groups for each capture source.
  const std::vector<FocusGroup> groups_for_fullscreen_;
  const std::vector<FocusGroup> groups_for_region_;
  const std::vector<FocusGroup> groups_for_window_;

  // Focusable groups for the game capture session that always has `kWindow`
  // capture source selected and the selected window is not changeable.
  const std::vector<FocusGroup> groups_for_game_capture_;

  // Focusable groups for the sunfish session that always has `kRegion` capture
  // source selected.
  const std::vector<FocusGroup> groups_for_sunfish_;

  // Highlightable windows of the focus group `kCaptureWindow`. Windows opened
  // after the session starts will not be included.
  std::map<aura::Window*, std::unique_ptr<HighlightableWindow>>
      highlightable_windows_;

  // Virtual a11y views for the affordance circles, since this UI is drawn
  // directly on the layer and has no associated view. These are raw pointers
  // that are owned by the views hierarchy, and are lazily created. The virtual
  // views are attached to a fullscreen widget backed by a not drawn layer which
  // is also lazily created.
  views::UniqueWidgetPtr ax_widget_;
  std::map<FineTunePosition, raw_ptr<views::AXVirtualView>> ax_virtual_views_;

  // The session that owns |this|. Guaranteed to be non null for the lifetime of
  // |this|.
  raw_ptr<CaptureModeSession> session_;

  // Accessibility features will focus on whatever window is returned by
  // GetA11yOverrideWindow(). Once `this` goes out of scope, the a11y override
  // window is set to null.
  std::unique_ptr<ScopedA11yOverrideWindowSetter> scoped_a11y_overrider_;

  base::ScopedMultiSourceObservation<views::Widget, views::WidgetObserver>
      session_widget_observeration_{this};

  // True if the current open menu (either settings or recording type) was open
  // by a key event (e.g. spacebar press) on their entry point button. In that
  // case, `AdvanceFocus()` will navigate to items within that menu. Otherwise,
  // `AdvanceFocus()` will close the menu.
  bool menu_opened_with_keyboard_nav_ = false;

  base::WeakPtrFactory<CaptureModeSessionFocusCycler> weak_ptr_factory_{this};
};

}  // namespace ash

#endif  // ASH_CAPTURE_MODE_CAPTURE_MODE_SESSION_FOCUS_CYCLER_H_