Skip to content

Commit

Permalink
fix: titlebar and buttons state under simple fullscreen (#30707)
Browse files Browse the repository at this point in the history
Co-authored-by: Cheng Zhao <zcbenz@gmail.com>
  • Loading branch information
trop[bot] and zcbenz committed Aug 26, 2021
1 parent 832c6c0 commit f2a0450
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 59 deletions.
30 changes: 20 additions & 10 deletions shell/browser/native_window_mac.mm
Expand Up @@ -1028,6 +1028,13 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
window.level = NSPopUpMenuWindowLevel;
}

// Always hide the titlebar in simple fullscreen mode.
//
// Note that we must remove the NSWindowStyleMaskTitled style instead of
// using the [window_ setTitleVisibility:], as the latter would leave the
// window with rounded corners.
SetStyleMask(false, NSWindowStyleMaskTitled);

if (!window_button_visibility_.has_value()) {
// Lets keep previous behaviour - hide window controls in titled
// fullscreen mode when not specified otherwise.
Expand All @@ -1044,16 +1051,6 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
} else if (!simple_fullscreen && is_simple_fullscreen_) {
is_simple_fullscreen_ = false;

// Restore default window controls visibility state.
if (!window_button_visibility_.has_value()) {
bool visibility;
if (has_frame())
visibility = true;
else
visibility = title_bar_style_ != TitleBarStyle::kNormal;
InternalSetWindowButtonVisibility(visibility);
}

[window setFrame:original_frame_ display:YES animate:YES];
window.level = original_level_;

Expand All @@ -1066,6 +1063,19 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
// Restore window manipulation abilities
SetMaximizable(was_maximizable_);
SetMovable(was_movable_);

// Restore default window controls visibility state.
if (!window_button_visibility_.has_value()) {
bool visibility;
if (has_frame())
visibility = true;
else
visibility = title_bar_style_ != TitleBarStyle::kNormal;
InternalSetWindowButtonVisibility(visibility);
}

if (buttons_proxy_)
[buttons_proxy_ redraw];
}
}

Expand Down
7 changes: 0 additions & 7 deletions shell/browser/ui/cocoa/window_buttons_proxy.h
Expand Up @@ -25,13 +25,6 @@
@interface WindowButtonsProxy : NSObject {
@private
NSWindow* window_;
// The view that contains the window buttons and title.
NSView* titlebar_container_;

// The window buttons.
NSButton* left_;
NSButton* right_;
NSButton* middle_;

// Current left-top margin of buttons.
gfx::Point margin_;
Expand Down
107 changes: 65 additions & 42 deletions shell/browser/ui/cocoa/window_buttons_proxy.mm
Expand Up @@ -34,25 +34,6 @@ - (id)initWithWindow:(NSWindow*)window {
show_on_hover_ = NO;
mouse_inside_ = NO;

// Save the sequence of the buttons for later computation.
if (base::i18n::IsRTL()) {
left_ = [window_ standardWindowButton:NSWindowZoomButton];
right_ = [window_ standardWindowButton:NSWindowCloseButton];
} else {
left_ = [window_ standardWindowButton:NSWindowCloseButton];
right_ = [window_ standardWindowButton:NSWindowZoomButton];
}
middle_ = [window_ standardWindowButton:NSWindowMiniaturizeButton];

// Safety check just in case Apple changes the view structure in a macOS
// upgrade.
if (!left_.superview || !left_.superview.superview) {
NOTREACHED() << "macOS has changed its window buttons view structure.";
titlebar_container_ = nullptr;
return self;
}
titlebar_container_ = left_.superview.superview;

// Remember the default margin.
margin_ = default_margin_ = [self getCurrentMargin];

Expand All @@ -66,27 +47,30 @@ - (void)dealloc {
}

- (void)setVisible:(BOOL)visible {
if (!titlebar_container_)
NSView* titleBarContainer = [self titleBarContainer];
if (!titleBarContainer)
return;
[titlebar_container_ setHidden:!visible];
[titleBarContainer setHidden:!visible];
}

- (BOOL)isVisible {
if (!titlebar_container_)
NSView* titleBarContainer = [self titleBarContainer];
if (!titleBarContainer)
return YES;
return ![titlebar_container_ isHidden];
return ![titleBarContainer isHidden];
}

- (void)setShowOnHover:(BOOL)yes {
if (!titlebar_container_)
NSView* titleBarContainer = [self titleBarContainer];
if (!titleBarContainer)
return;
show_on_hover_ = yes;
// Put a transparent view above the window buttons so we can track mouse
// events when mouse enter/leave the window buttons.
if (show_on_hover_) {
hover_view_.reset([[ButtonsAreaHoverView alloc] initWithProxy:self]);
[hover_view_ setFrame:[self getButtonsBounds]];
[titlebar_container_ addSubview:hover_view_.get()];
[titleBarContainer addSubview:hover_view_.get()];
} else {
[hover_view_ removeFromSuperview];
hover_view_.reset();
Expand All @@ -107,29 +91,34 @@ - (NSRect)getButtonsContainerBounds {
}

- (void)redraw {
if (!titlebar_container_)
NSView* titleBarContainer = [self titleBarContainer];
if (!titleBarContainer)
return;

float button_width = NSWidth(left_.frame);
float button_height = NSHeight(left_.frame);
float padding = NSMinX(middle_.frame) - NSMaxX(left_.frame);
NSView* left = [self leftButton];
NSView* middle = [self middleButton];
NSView* right = [self rightButton];

float button_width = NSWidth(left.frame);
float button_height = NSHeight(left.frame);
float padding = NSMinX(middle.frame) - NSMaxX(left.frame);
float start;
if (base::i18n::IsRTL())
start =
NSWidth(window_.frame) - 3 * button_width - 2 * padding - margin_.x();
else
start = margin_.x();

NSRect cbounds = titlebar_container_.frame;
NSRect cbounds = titleBarContainer.frame;
cbounds.size.height = button_height + 2 * margin_.y();
cbounds.origin.y = NSHeight(window_.frame) - NSHeight(cbounds);
[titlebar_container_ setFrame:cbounds];
[titleBarContainer setFrame:cbounds];

[left_ setFrameOrigin:NSMakePoint(start, margin_.y())];
[left setFrameOrigin:NSMakePoint(start, margin_.y())];
start += button_width + padding;
[middle_ setFrameOrigin:NSMakePoint(start, margin_.y())];
[middle setFrameOrigin:NSMakePoint(start, margin_.y())];
start += button_width + padding;
[right_ setFrameOrigin:NSMakePoint(start, margin_.y())];
[right setFrameOrigin:NSMakePoint(start, margin_.y())];

if (hover_view_)
[hover_view_ setFrame:[self getButtonsBounds]];
Expand Down Expand Up @@ -176,25 +165,59 @@ - (void)updateButtonsVisibility {

// Return the bounds of all 3 buttons.
- (NSRect)getButtonsBounds {
return NSMakeRect(NSMinX(left_.frame), NSMinY(left_.frame),
NSMaxX(right_.frame) - NSMinX(left_.frame),
NSHeight(left_.frame));
NSView* left = [self leftButton];
NSView* right = [self rightButton];
return NSMakeRect(NSMinX(left.frame), NSMinY(left.frame),
NSMaxX(right.frame) - NSMinX(left.frame),
NSHeight(left.frame));
}

// Compute margin from position of current buttons.
- (gfx::Point)getCurrentMargin {
gfx::Point result;
if (!titlebar_container_)
NSView* titleBarContainer = [self titleBarContainer];
if (!titleBarContainer)
return result;

result.set_y((NSHeight(titlebar_container_.frame) - NSHeight(left_.frame)) /
2);
NSView* left = [self leftButton];
NSView* right = [self rightButton];

result.set_y((NSHeight(titleBarContainer.frame) - NSHeight(left.frame)) / 2);

if (base::i18n::IsRTL())
result.set_x(NSWidth(window_.frame) - NSMaxX(right_.frame));
result.set_x(NSWidth(window_.frame) - NSMaxX(right.frame));
else
result.set_x(NSMinX(left_.frame));
result.set_x(NSMinX(left.frame));
return result;
}

// Receive the titlebar container, which might be nil if the window does not
// have the NSWindowStyleMaskTitled style.
- (NSView*)titleBarContainer {
NSView* left = [self leftButton];
if (!left.superview)
return nil;
return left.superview.superview;
}

// Receive the window buttons, note that the buttons might be removed and
// re-added on the fly so we should not cache them.
- (NSButton*)leftButton {
if (base::i18n::IsRTL())
return [window_ standardWindowButton:NSWindowZoomButton];
else
return [window_ standardWindowButton:NSWindowCloseButton];
}

- (NSButton*)middleButton {
return [window_ standardWindowButton:NSWindowMiniaturizeButton];
}

- (NSButton*)rightButton {
if (base::i18n::IsRTL())
return [window_ standardWindowButton:NSWindowCloseButton];
else
return [window_ standardWindowButton:NSWindowZoomButton];
}

@end

0 comments on commit f2a0450

Please sign in to comment.