File: immersive_mode_tabbed_controller_cocoa.mm

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (290 lines) | stat: -rw-r--r-- 11,086 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
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/remote_cocoa/app_shim/immersive_mode_tabbed_controller_cocoa.h"

#include "base/apple/foundation_util.h"
#include "base/debug/dump_without_crashing.h"
#include "base/feature_list.h"
#include "base/functional/callback_forward.h"
#import "components/remote_cocoa/app_shim/NSToolbar+Private.h"
#import "components/remote_cocoa/app_shim/bridged_content_view.h"
#import "components/remote_cocoa/app_shim/browser_native_widget_window_mac.h"
#include "components/remote_cocoa/app_shim/features.h"

namespace {
void SetAlwaysShowTrafficLights(NSWindow* browser_window, bool always_show) {
  [base::apple::ObjCCast<BrowserNativeWidgetWindow>(browser_window)
      setAlwaysShowTrafficLights:YES];
}
}  // namespace

namespace remote_cocoa {

ImmersiveModeTabbedControllerCocoa::ImmersiveModeTabbedControllerCocoa(
    BrowserNativeWidgetWindow* browser_window,
    NativeWidgetMacOverlayNSWindow* overlay_window,
    NativeWidgetMacOverlayNSWindow* tab_window)
    : ImmersiveModeControllerCocoa(browser_window, overlay_window) {
  tab_window_ = tab_window;
#ifndef NDEBUG
  tab_window_.title = @"tab overlay";
#endif  // NDEBUG

  browser_window.titleVisibility = NSWindowTitleHidden;

  tab_titlebar_view_controller_ =
      [[NSTitlebarAccessoryViewController alloc] init];
  tab_titlebar_view_controller_.view = [[NSView alloc] init];

  // The view is pinned to the opposite side of the traffic lights. A view long
  // enough is able to paint underneath the traffic lights. This also works with
  // RTL setups.
  tab_titlebar_view_controller_.layoutAttribute = NSLayoutAttributeTrailing;

  // During fullscreen restore or split screen restore tab window can be left
  // without a parent, leading to the window being hidden which causes
  // compositing to stop.
  if (!tab_window.parentWindow) {
    [overlay_window addChildWindow:tab_window ordered:NSWindowAbove];
  }
}

ImmersiveModeTabbedControllerCocoa::~ImmersiveModeTabbedControllerCocoa() {
  SetAlwaysShowTrafficLights(browser_window(), false);
  StopObservingChildWindows(tab_window_);
  browser_window().toolbar = nil;
  BridgedContentView* tab_content_view = tab_content_view_;
  [tab_content_view removeFromSuperview];
  tab_window_.contentView = tab_content_view;
  [tab_titlebar_view_controller_ removeFromParentViewController];
  tab_titlebar_view_controller_ = nil;
}

void ImmersiveModeTabbedControllerCocoa::Init() {
  SetAlwaysShowTrafficLights(browser_window(), true);

  ImmersiveModeControllerCocoa::Init();
  BridgedContentView* tab_content_view =
      base::apple::ObjCCastStrict<BridgedContentView>(tab_window_.contentView);
  [tab_content_view removeFromSuperview];
  tab_content_view_ = tab_content_view;

  // Use a placeholder view since the content has been moved to the
  // NSTitlebarAccessoryViewController.
  tab_window_.contentView = [[OpaqueView alloc] init];
  if (base::FeatureList::IsEnabled(
          remote_cocoa::features::kImmersiveFullscreenOverlayWindowDebug)) {
    [tab_window_ debugWithColor:NSColor.redColor];
  }

  // This will allow the NSToolbarFullScreenWindow to become key when
  // interacting with the tab strip.
  // The `overlay_window_` is handled the same way in ImmersiveModeController.
  // See the comment there for more details.
  tab_window_.ignoresMouseEvents = YES;

  [tab_titlebar_view_controller_.view addSubview:tab_content_view];
  [tab_titlebar_view_controller_.view setFrameSize:tab_window_.frame.size];
  tab_titlebar_view_controller_.fullScreenMinHeight =
      tab_window_.frame.size.height;

  // Keep the tab content view's size in sync with its parent view.
  tab_content_view_.translatesAutoresizingMaskIntoConstraints = NO;
  [tab_content_view_.heightAnchor
      constraintEqualToAnchor:tab_content_view_.superview.heightAnchor]
      .active = YES;
  [tab_content_view_.widthAnchor
      constraintEqualToAnchor:tab_content_view_.superview.widthAnchor]
      .active = YES;
  [tab_content_view_.centerXAnchor
      constraintEqualToAnchor:tab_content_view_.superview.centerXAnchor]
      .active = YES;
  [tab_content_view_.centerYAnchor
      constraintEqualToAnchor:tab_content_view_.superview.centerYAnchor]
      .active = YES;

  ObserveChildWindows(tab_window_);

  // The presence of a visible NSToolbar causes the titlebar to be revealed.
  browser_window().toolbar = [[NSToolbar alloc] init];

  // `UpdateToolbarVisibility()` will make the toolbar visible as necessary.
  UpdateToolbarVisibility(last_used_style());
}

void ImmersiveModeTabbedControllerCocoa::UpdateToolbarVisibility(
    std::optional<mojom::ToolbarVisibilityStyle> style) {
  if (!style.has_value()) {
    return;
  }

  // Don't make changes when a reveal lock is active. Do update the
  // `last_used_style` so the style will be updated once all outstanding reveal
  // locks are released.
  if (reveal_lock_count() > 0) {
    set_last_used_style(style);
    return;
  }

  // TODO(crbug.com/40261565): A NSTitlebarAccessoryViewController hosted
  // in the titlebar, as opposed to above or below it, does not hide/show when
  // using the `hidden` property. Instead we must entirely remove the view
  // controller to make the view hide. Switch to using the `hidden` property
  // once Apple resolves this bug.
  switch (style.value()) {
    case mojom::ToolbarVisibilityStyle::kAlways:
      AddController();
      TitlebarReveal();
      break;
    case mojom::ToolbarVisibilityStyle::kAutohide:
      AddController();
      TitlebarHide();
      break;
    case mojom::ToolbarVisibilityStyle::kNone:
      RemoveController();
      TitlebarHide();
      break;
  }
  ImmersiveModeControllerCocoa::UpdateToolbarVisibility(style);

  // Ensures that tab window is in the correct z-order.
  OrderTabWindowZOrderOnTop();

  // macOS 10.15 does not call `OnTitlebarFrameDidChange` as often as newer
  // versions of macOS. Add a layout call here and in `RevealLock` and
  // `RevealUnlock` to pickup the slack. There is no harm in extra layout calls
  // on newer versions of macOS, -setFrameOrigin: is essentially a NOP when the
  // frame size doesn't change.
  LayoutWindowWithAnchorView(tab_window_, tab_content_view_);
}

void ImmersiveModeTabbedControllerCocoa::AddController() {
  NSWindow* window = browser_window();
  if (![window.titlebarAccessoryViewControllers
          containsObject:tab_titlebar_view_controller_]) {
    [window addTitlebarAccessoryViewController:tab_titlebar_view_controller_];
  }
}

void ImmersiveModeTabbedControllerCocoa::RemoveController() {
  [tab_titlebar_view_controller_ removeFromParentViewController];
}

void ImmersiveModeTabbedControllerCocoa::OnTopViewBoundsChanged(
    const gfx::Rect& bounds) {
  ImmersiveModeControllerCocoa::OnTopViewBoundsChanged(bounds);
  NSRect frame = NSRectFromCGRect(bounds.ToCGRect());
  [tab_titlebar_view_controller_.view
      setFrameSize:NSMakeSize(
                       frame.size.width,
                       tab_titlebar_view_controller_.view.frame.size.height)];
}

void ImmersiveModeTabbedControllerCocoa::RevealLocked() {
  AddController();
  TitlebarReveal();
  // Call after TitlebarReveal() for a proper layout.
  ImmersiveModeControllerCocoa::RevealLocked();
  LayoutWindowWithAnchorView(tab_window_, tab_content_view_);
}

void ImmersiveModeTabbedControllerCocoa::RevealUnlocked() {
  if (last_used_style() == mojom::ToolbarVisibilityStyle::kAutohide) {
    TitlebarHide();
  }

  // Call after TitlebarReveal() for a proper layout.
  ImmersiveModeControllerCocoa::RevealUnlocked();
  LayoutWindowWithAnchorView(tab_window_, tab_content_view_);
}

void ImmersiveModeTabbedControllerCocoa::TitlebarReveal() {
  NSToolbar* toolbar = browser_window().toolbar;
  if (toolbar.visible) {
    return;
  }
  toolbar.visible = YES;

  // The tab controller and toolbar views are siblings. When the toolbar view
  // is removed then re-added it becomes z-order on top of the tab controller
  // view. This becomes an issue when the window is not active but we want to
  // handle the first click. The toolbar view returns NO for
  // -acceptsFirstMouse:. Prefer to send the toolbar view to the back of the
  // siblings list. If we are unable to get a handle on the toolbar view remove
  // and re-add the tab controller so its view is z-order above the toolbar
  // view. See http://crbug/40283902 for details.
  // TODO(http://crbug.com/40261565): Remove when FB12010731 is fixed in AppKit.
  if (NSView* toolbar_view = toolbar.privateToolbarView) {
    [toolbar_view.superview addSubview:toolbar_view
                            positioned:NSWindowBelow
                            relativeTo:nil];
  } else {
    // We want to know if the toolbar no longer responds to _toolbarView but
    // since we have a backup workaround DumpWithoutCrashing();
    base::debug::DumpWithoutCrashing();
    RemoveController();
    AddController();
  }
}

void ImmersiveModeTabbedControllerCocoa::TitlebarHide() {
  browser_window().toolbar.visible = NO;
}

void ImmersiveModeTabbedControllerCocoa::Reanchor() {
  ImmersiveModeControllerCocoa::Reanchor();
  LayoutWindowWithAnchorView(tab_window_, tab_content_view_);
}

void ImmersiveModeTabbedControllerCocoa::OnChildWindowAdded(NSWindow* child) {
  // The `tab_window_` is a child of the `overlay_window_`. Ignore
  // the `tab_window_`.
  if (child == tab_window_) {
    return;
  }

  OrderTabWindowZOrderOnTop();
  ImmersiveModeControllerCocoa::OnChildWindowAdded(child);
}

void ImmersiveModeTabbedControllerCocoa::OnChildWindowRemoved(NSWindow* child) {
  // The `tab_window_` is a child of the `overlay_window_`. Ignore
  // the `tab_window_`.
  if (child == tab_window_) {
    return;
  }
  ImmersiveModeControllerCocoa::OnChildWindowRemoved(child);
}

bool ImmersiveModeTabbedControllerCocoa::ShouldObserveChildWindow(
    NSWindow* child) {
  // Filter out the `tab_window_`.
  if (child == tab_window_) {
    return false;
  }
  return ImmersiveModeControllerCocoa::ShouldObserveChildWindow(child);
}

bool ImmersiveModeTabbedControllerCocoa::IsTabbed() {
  return true;
}

void ImmersiveModeTabbedControllerCocoa::OrderTabWindowZOrderOnTop() {
  // Keep the tab window on top of its siblings. This will allow children of tab
  // window to always be z-order on top of overlay window children.
  // Practically this allows for the tab preview hover card to be z-order on top
  // of omnibox results popup.
  // If the tab window does not have a parent or the parent is not the overlay
  // window, do not perform the shuffle. Otherwise we could throw off the child
  // window counts in NativeWidgetNSWindowBridge::NotifyVisibilityChangeDown
  // during immersive fullscreen exit.
  if (tab_window_.parentWindow == overlay_window() &&
      overlay_window().childWindows.lastObject != tab_window_) {
    [overlay_window() removeChildWindow:tab_window_];
    [overlay_window() addChildWindow:tab_window_ ordered:NSWindowAbove];
  }
}

}  // namespace remote_cocoa