/
window_buttons_proxy.mm
223 lines (186 loc) · 5.94 KB
/
window_buttons_proxy.mm
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
// Copyright (c) 2021 Microsoft, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/ui/cocoa/window_buttons_proxy.h"
#include "base/i18n/rtl.h"
#include "base/notreached.h"
@implementation ButtonsAreaHoverView : NSView
- (id)initWithProxy:(WindowButtonsProxy*)proxy {
if ((self = [super init])) {
proxy_ = proxy;
}
return self;
}
// Ignore all mouse events.
- (NSView*)hitTest:(NSPoint)aPoint {
return nil;
}
- (void)updateTrackingAreas {
[proxy_ updateTrackingAreas];
}
@end
@implementation WindowButtonsProxy
- (id)initWithWindow:(NSWindow*)window {
window_ = window;
show_on_hover_ = NO;
mouse_inside_ = NO;
// Remember the default margin.
margin_ = default_margin_ = [self getCurrentMargin];
return self;
}
- (void)dealloc {
if (hover_view_)
[hover_view_ removeFromSuperview];
[super dealloc];
}
- (void)setVisible:(BOOL)visible {
NSView* titleBarContainer = [self titleBarContainer];
if (!titleBarContainer)
return;
[titleBarContainer setHidden:!visible];
}
- (BOOL)isVisible {
NSView* titleBarContainer = [self titleBarContainer];
if (!titleBarContainer)
return YES;
return ![titleBarContainer isHidden];
}
- (void)setShowOnHover:(BOOL)yes {
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]];
[titleBarContainer addSubview:hover_view_.get()];
} else {
[hover_view_ removeFromSuperview];
hover_view_.reset();
}
[self updateButtonsVisibility];
}
- (void)setMargin:(const absl::optional<gfx::Point>&)margin {
if (margin)
margin_ = *margin;
else
margin_ = default_margin_;
[self redraw];
}
- (NSRect)getButtonsContainerBounds {
return NSInsetRect([self getButtonsBounds], -margin_.x(), -margin_.y());
}
- (void)redraw {
NSView* titleBarContainer = [self titleBarContainer];
if (!titleBarContainer)
return;
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 = titleBarContainer.frame;
cbounds.size.height = button_height + 2 * margin_.y();
cbounds.origin.y = NSHeight(window_.frame) - NSHeight(cbounds);
[titleBarContainer setFrame:cbounds];
[left setFrameOrigin:NSMakePoint(start, margin_.y())];
start += button_width + padding;
[middle setFrameOrigin:NSMakePoint(start, margin_.y())];
start += button_width + padding;
[right setFrameOrigin:NSMakePoint(start, margin_.y())];
if (hover_view_)
[hover_view_ setFrame:[self getButtonsBounds]];
}
- (void)updateTrackingAreas {
if (tracking_area_)
[hover_view_ removeTrackingArea:tracking_area_.get()];
tracking_area_.reset([[NSTrackingArea alloc]
initWithRect:NSZeroRect
options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways |
NSTrackingInVisibleRect
owner:self
userInfo:nil]);
[hover_view_ addTrackingArea:tracking_area_.get()];
}
- (void)mouseEntered:(NSEvent*)event {
mouse_inside_ = YES;
[self updateButtonsVisibility];
}
- (void)mouseExited:(NSEvent*)event {
mouse_inside_ = NO;
[self updateButtonsVisibility];
}
- (void)updateButtonsVisibility {
NSArray* buttons = @[
[window_ standardWindowButton:NSWindowCloseButton],
[window_ standardWindowButton:NSWindowMiniaturizeButton],
[window_ standardWindowButton:NSWindowZoomButton],
];
// Show buttons when mouse hovers above them.
BOOL hidden = show_on_hover_ && !mouse_inside_;
// Always show buttons under fullscreen.
if ([window_ styleMask] & NSWindowStyleMaskFullScreen)
hidden = NO;
for (NSView* button in buttons) {
[button setHidden:hidden];
[button setNeedsDisplay:YES];
}
}
// Return the bounds of all 3 buttons.
- (NSRect)getButtonsBounds {
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;
NSView* titleBarContainer = [self titleBarContainer];
if (!titleBarContainer)
return result;
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));
else
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