/
window.mm
251 lines (213 loc) · 6.33 KB
/
window.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
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
// LAF OS Library
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2012-2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "os/osx/window.h"
#include "base/debug.h"
#include "os/event.h"
#include "os/osx/event_queue.h"
#include "os/osx/view.h"
#include "os/osx/window_delegate.h"
#include "os/surface.h"
using namespace os;
@implementation OSXWindow
- (OSXWindow*)initWithImpl:(OSXWindowImpl*)impl
width:(int)width
height:(int)height
scale:(int)scale
{
m_impl = impl;
m_scale = scale;
NSRect rect = NSMakeRect(0, 0, width, height);
self = [self initWithContentRect:rect
styleMask:(NSWindowStyleMaskTitled |
NSWindowStyleMaskClosable |
NSWindowStyleMaskMiniaturizable |
NSWindowStyleMaskResizable)
backing:NSBackingStoreBuffered
defer:NO];
if (!self)
return nil;
m_delegate = [[OSXWindowDelegate alloc] initWithWindowImpl:impl];
// The NSView width and height will be a multiple of 4. In this way
// all scaled pixels should be exactly the same
// for Screen Scaling > 1 and <= 4)
self.contentResizeIncrements = NSMakeSize(4, 4);
OSXView* view = [[OSXView alloc] initWithFrame:rect];
[view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
// Redraw the entire window content when we resize it.
// TODO add support to avoid redrawing the entire window
self.preservesContentDuringLiveResize = false;
[self setDelegate:m_delegate];
[self setContentView:view];
[self center];
[self makeKeyAndOrderFront:self];
// Hide the "View > Show Tab Bar" menu item
if ([self respondsToSelector:@selector(setTabbingMode:)])
[self setTabbingMode:NSWindowTabbingModeDisallowed];
return self;
}
- (OSXWindowImpl*)impl
{
return m_impl;
}
- (int)scale
{
return m_scale;
}
- (void)setScale:(int)scale
{
// If the scale is the same, we don't generate a resize event.
if (m_scale == scale)
return;
m_scale = scale;
if (m_impl) {
NSRect bounds = [[self contentView] bounds];
m_impl->onResize(gfx::Size(bounds.size.width,
bounds.size.height));
}
}
- (gfx::Size)clientSize
{
return gfx::Size([[self contentView] frame].size.width,
[[self contentView] frame].size.height);
}
- (gfx::Size)restoredSize
{
return [self clientSize];
}
- (void)setMousePosition:(const gfx::Point&)position
{
NSView* view = self.contentView;
NSPoint pt = NSMakePoint(
position.x*m_scale,
view.frame.size.height - position.y*m_scale);
pt = [view convertPoint:pt toView:view];
pt = [view convertPoint:pt toView:nil];
pt = [self convertBaseToScreen:pt];
pt.y = [[self screen] frame].size.height - pt.y;
CGPoint pos = CGPointMake(pt.x, pt.y);
CGEventRef event = CGEventCreateMouseEvent(
NULL, kCGEventMouseMoved, pos, kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);
}
- (BOOL)setNativeMouseCursor:(NativeCursor)cursor
{
NSCursor* nsCursor = nullptr;
switch (cursor) {
case kArrowCursor:
case kWaitCursor:
case kHelpCursor:
case kSizeNECursor:
case kSizeNWCursor:
case kSizeSECursor:
case kSizeSWCursor:
nsCursor = [NSCursor arrowCursor];
break;
case kCrosshairCursor:
nsCursor = [NSCursor crosshairCursor];
break;
case kIBeamCursor:
nsCursor = [NSCursor IBeamCursor];
break;
case kLinkCursor:
nsCursor = [NSCursor pointingHandCursor];
break;
case kForbiddenCursor:
nsCursor = [NSCursor operationNotAllowedCursor];
break;
case kMoveCursor:
nsCursor = [NSCursor openHandCursor];
break;
case kSizeNSCursor:
nsCursor = [NSCursor resizeUpDownCursor];
break;
case kSizeWECursor:
nsCursor = [NSCursor resizeLeftRightCursor];
break;
case kSizeNCursor:
nsCursor = [NSCursor resizeUpCursor];
break;
case kSizeECursor:
nsCursor = [NSCursor resizeRightCursor];
break;
case kSizeSCursor:
nsCursor = [NSCursor resizeDownCursor];
break;
case kSizeWCursor:
nsCursor = [NSCursor resizeLeftCursor];
break;
}
[self.contentView setCursor:nsCursor];
return (nsCursor ? YES: NO);
}
- (BOOL)setNativeMouseCursor:(const os::Surface*)surface
focus:(const gfx::Point&)focus
scale:(const int)scale
{
ASSERT(surface);
SurfaceFormatData format;
surface->getFormat(&format);
if (format.bitsPerPixel != 32)
return NO;
const int w = scale*surface->width();
const int h = scale*surface->height();
if (4*w*h == 0)
return NO;
@autoreleasepool {
NSBitmapImageRep* bmp =
[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:nil
pixelsWide:w
pixelsHigh:h
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
bytesPerRow:w*4
bitsPerPixel:32];
if (!bmp)
return NO;
uint32_t* dst = (uint32_t*)[bmp bitmapData];
for (int y=0; y<h; ++y) {
const uint32_t* src = (const uint32_t*)surface->getData(0, y/scale);
for (int x=0, u=0; x<w; ++x, ++dst) {
*dst = *src;
if (++u == scale) {
u = 0;
++src;
}
}
}
NSImage* img = [[NSImage alloc] initWithSize:NSMakeSize(w, h)];
if (!img)
return NO;
[img addRepresentation:bmp];
NSCursor* nsCursor =
[[NSCursor alloc] initWithImage:img
hotSpot:NSMakePoint(scale*focus.x + scale/2,
scale*focus.y + scale/2)];
if (!nsCursor)
return NO;
[self.contentView setCursor:nsCursor];
return YES;
}
}
- (void)noResponderFor:(SEL)eventSelector
{
if (eventSelector == @selector(keyDown:)) {
// Do nothing (avoid beep)
}
else {
[super noResponderFor:eventSelector];
}
}
@end