forked from Code-Hex/vz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtualization_view.m
374 lines (319 loc) · 13.4 KB
/
virtualization_view.m
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
//
// virtualization_view.m
//
// Created by codehex.
//
#import "virtualization_view.h"
@implementation VZApplication
- (void)run
{
@autoreleasepool {
[self finishLaunching];
shouldKeepRunning = YES;
do {
NSEvent *event = [self
nextEventMatchingMask:NSEventMaskAny
untilDate:[NSDate distantFuture]
inMode:NSDefaultRunLoopMode
dequeue:YES];
// NSLog(@"event: %@", event);
[self sendEvent:event];
[self updateWindows];
} while (shouldKeepRunning);
}
}
- (void)terminate:(id)sender
{
shouldKeepRunning = NO;
// We should call this method if we want to use `applicationWillTerminate` method.
//
// [[NSNotificationCenter defaultCenter]
// postNotificationName:NSApplicationWillTerminateNotification
// object:NSApp];
// This method is used to end up the event loop.
// If no events are coming, the event loop will always be in a waiting state.
[self postEvent:self.currentEvent atStart:NO];
}
@end
@implementation AboutViewController
- (instancetype)init
{
self = [super initWithNibName:nil bundle:nil];
return self;
}
- (void)loadView
{
self.view = [NSView new];
NSImageView *imageView = [NSImageView imageViewWithImage:[NSApp applicationIconImage]];
NSTextField *appLabel = [self makeLabel:[[NSProcessInfo processInfo] processName]];
[appLabel setFont:[NSFont boldSystemFontOfSize:16]];
NSTextField *subLabel = [self makePoweredByLabel];
NSStackView *stackView = [NSStackView stackViewWithViews:@[
imageView,
appLabel,
subLabel,
]];
[stackView setOrientation:NSUserInterfaceLayoutOrientationVertical];
[stackView setDistribution:NSStackViewDistributionFillProportionally];
[stackView setSpacing:10];
[stackView setAlignment:NSLayoutAttributeCenterX];
[stackView setContentCompressionResistancePriority:NSLayoutPriorityRequired forOrientation:NSLayoutConstraintOrientationHorizontal];
[stackView setContentCompressionResistancePriority:NSLayoutPriorityRequired forOrientation:NSLayoutConstraintOrientationVertical];
[self.view addSubview:stackView];
[NSLayoutConstraint activateConstraints:@[
[imageView.widthAnchor constraintEqualToConstant:80], // image size
[imageView.heightAnchor constraintEqualToConstant:80], // image size
[stackView.topAnchor constraintEqualToAnchor:self.view.topAnchor
constant:4],
[stackView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor
constant:-16],
[stackView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor
constant:32],
[stackView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor
constant:-32],
[stackView.widthAnchor constraintEqualToConstant:300]
]];
}
- (NSTextField *)makePoweredByLabel
{
NSMutableAttributedString *poweredByAttr = [[[NSMutableAttributedString alloc]
initWithString:@"Powered by "
attributes:@{
NSForegroundColorAttributeName : [NSColor labelColor]
}] autorelease];
NSURL *repositoryURL = [NSURL URLWithString:@"https://github.com/Code-Hex/vz"];
NSMutableAttributedString *repository = [self makeHyperLink:@"github.com/Code-Hex/vz" withURL:repositoryURL];
[poweredByAttr appendAttributedString:repository];
[poweredByAttr addAttribute:NSFontAttributeName
value:[NSFont systemFontOfSize:12]
range:NSMakeRange(0, [poweredByAttr length])];
NSTextField *label = [self makeLabel:@""];
[label setSelectable:YES];
[label setAllowsEditingTextAttributes:YES];
[label setAttributedStringValue:poweredByAttr];
return label;
}
- (NSTextField *)makeLabel:(NSString *)label
{
NSTextField *appLabel = [NSTextField labelWithString:label];
[appLabel setTextColor:[NSColor labelColor]];
[appLabel setEditable:NO];
[appLabel setSelectable:NO];
[appLabel setBezeled:NO];
[appLabel setBordered:NO];
[appLabel setBackgroundColor:[NSColor clearColor]];
[appLabel setAlignment:NSTextAlignmentCenter];
[appLabel setLineBreakMode:NSLineBreakByWordWrapping];
[appLabel setUsesSingleLineMode:NO];
[appLabel setMaximumNumberOfLines:20];
return appLabel;
}
// https://developer.apple.com/library/archive/qa/qa1487/_index.html
- (NSMutableAttributedString *)makeHyperLink:(NSString *)inString withURL:(NSURL *)aURL
{
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:inString];
NSRange range = NSMakeRange(0, [attrString length]);
[attrString beginEditing];
[attrString addAttribute:NSLinkAttributeName value:[aURL absoluteString] range:range];
// make the text appear in blue
[attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:range];
// next make the text appear with an underline
[attrString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:NSUnderlineStyleSingle]
range:range];
[attrString endEditing];
return [attrString autorelease];
}
@end
@implementation AboutPanel
- (instancetype)init
{
self = [super initWithContentRect:NSZeroRect styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable backing:NSBackingStoreBuffered defer:NO];
AboutViewController *viewController = [[[AboutViewController alloc] init] autorelease];
[self setContentViewController:viewController];
[self setTitleVisibility:NSWindowTitleHidden];
[self setTitlebarAppearsTransparent:YES];
[self setBecomesKeyOnlyIfNeeded:NO];
[self center];
return self;
}
@end
@implementation AppDelegate {
VZVirtualMachine *_virtualMachine;
VZVirtualMachineView *_virtualMachineView;
CGFloat _windowWidth;
CGFloat _windowHeight;
}
- (instancetype)initWithVirtualMachine:(VZVirtualMachine *)virtualMachine
windowWidth:(CGFloat)windowWidth
windowHeight:(CGFloat)windowHeight
{
self = [super init];
_virtualMachine = virtualMachine;
_virtualMachine.delegate = self;
// Setup virtual machine view configs
VZVirtualMachineView *view = [[[VZVirtualMachineView alloc] init] autorelease];
view.capturesSystemKeys = YES;
view.virtualMachine = _virtualMachine;
_virtualMachineView = view;
// Setup some window configs
_windowWidth = windowWidth;
_windowHeight = windowHeight;
return self;
}
/* IMPORTANT: delegate methods are called from VM's queue */
- (void)guestDidStopVirtualMachine:(VZVirtualMachine *)virtualMachine
{
[NSApp performSelectorOnMainThread:@selector(terminate:) withObject:self waitUntilDone:NO];
}
- (void)virtualMachine:(VZVirtualMachine *)virtualMachine didStopWithError:(NSError *)error
{
NSLog(@"VM %@ didStopWithError: %@", virtualMachine, error);
[NSApp performSelectorOnMainThread:@selector(terminate:) withObject:self waitUntilDone:NO];
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
[self setupMenuBar];
[self setupGraphicWindow];
// These methods are required to call here. Because the menubar will be not active even if
// application is running.
// See: https://stackoverflow.com/questions/62739862/why-doesnt-activateignoringotherapps-enable-the-menu-bar
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp activateIgnoringOtherApps:YES];
}
- (void)windowWillClose:(NSNotification *)notification
{
[NSApp performSelectorOnMainThread:@selector(terminate:) withObject:self waitUntilDone:NO];
}
- (void)setupGraphicWindow
{
NSRect rect = NSMakeRect(0, 0, _windowWidth, _windowHeight);
NSWindow *window = [[[NSWindow alloc] initWithContentRect:rect
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable //|NSTexturedBackgroundWindowMask
backing:NSBackingStoreBuffered
defer:NO] autorelease];
[window setOpaque:NO];
[window setContentView:_virtualMachineView];
[window setTitleVisibility:NSWindowTitleHidden];
[window center];
[window setDelegate:self];
[window makeKeyAndOrderFront:nil];
// This code to prevent crash when called applicationShouldTerminateAfterLastWindowClosed.
// https://stackoverflow.com/a/13470694
[window setReleasedWhenClosed:NO];
}
- (void)setupMenuBar
{
NSMenu *menuBar = [[[NSMenu alloc] init] autorelease];
NSMenuItem *menuBarItem = [[[NSMenuItem alloc] init] autorelease];
[menuBar addItem:menuBarItem];
[NSApp setMainMenu:menuBar];
// App menu
NSMenu *appMenu = [self setupApplicationMenu];
[menuBarItem setSubmenu:appMenu];
// Window menu
NSMenu *windowMenu = [self setupWindowMenu];
NSMenuItem *windowMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
[menuBar addItem:windowMenuItem];
[windowMenuItem setSubmenu:windowMenu];
// Help menu
NSMenu *helpMenu = [self setupHelpMenu];
NSMenuItem *helpMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Help" action:nil keyEquivalent:@""] autorelease];
[menuBar addItem:helpMenuItem];
[helpMenuItem setSubmenu:helpMenu];
}
- (NSMenu *)setupApplicationMenu
{
NSMenu *appMenu = [[[NSMenu alloc] init] autorelease];
NSString *applicationName = [[NSProcessInfo processInfo] processName];
NSMenuItem *aboutMenuItem = [[[NSMenuItem alloc]
initWithTitle:[NSString stringWithFormat:@"About %@", applicationName]
action:@selector(openAboutWindow:)
keyEquivalent:@""] autorelease];
// CapturesSystemKeys toggle
NSMenuItem *capturesSystemKeysItem = [[[NSMenuItem alloc]
initWithTitle:@"Enable to send system hot keys to virtual machine"
action:@selector(toggleCapturesSystemKeys:)
keyEquivalent:@""] autorelease];
[capturesSystemKeysItem setState:[self capturesSystemKeysState]];
// Service menu
NSMenuItem *servicesMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Services" action:nil keyEquivalent:@""] autorelease];
NSMenu *servicesMenu = [[[NSMenu alloc] initWithTitle:@"Services"] autorelease];
[servicesMenuItem setSubmenu:servicesMenu];
[NSApp setServicesMenu:servicesMenu];
NSMenuItem *hideOthersItem = [[[NSMenuItem alloc]
initWithTitle:@"Hide Others"
action:@selector(hideOtherApplications:)
keyEquivalent:@"h"] autorelease];
[hideOthersItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption | NSEventModifierFlagCommand)];
NSArray *menuItems = @[
aboutMenuItem,
[NSMenuItem separatorItem],
capturesSystemKeysItem,
[NSMenuItem separatorItem],
servicesMenuItem,
[NSMenuItem separatorItem],
[[[NSMenuItem alloc]
initWithTitle:[@"Hide " stringByAppendingString:applicationName]
action:@selector(hide:)
keyEquivalent:@"h"] autorelease],
hideOthersItem,
[NSMenuItem separatorItem],
[[[NSMenuItem alloc]
initWithTitle:[@"Quit " stringByAppendingString:applicationName]
action:@selector(terminate:)
keyEquivalent:@"q"] autorelease],
];
for (NSMenuItem *menuItem in menuItems) {
[appMenu addItem:menuItem];
}
return appMenu;
}
- (NSMenu *)setupWindowMenu
{
NSMenu *windowMenu = [[[NSMenu alloc] initWithTitle:@"Window"] autorelease];
NSArray *menuItems = @[
[[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease],
[[[NSMenuItem alloc] initWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""] autorelease],
[NSMenuItem separatorItem],
[[[NSMenuItem alloc] initWithTitle:@"Bring All to Front" action:@selector(arrangeInFront:) keyEquivalent:@""] autorelease],
];
for (NSMenuItem *menuItem in menuItems) {
[windowMenu addItem:menuItem];
}
[NSApp setWindowsMenu:windowMenu];
return windowMenu;
}
- (NSMenu *)setupHelpMenu
{
NSMenu *helpMenu = [[[NSMenu alloc] initWithTitle:@"Help"] autorelease];
NSArray *menuItems = @[
[[[NSMenuItem alloc] initWithTitle:@"Report issue" action:@selector(reportIssue:) keyEquivalent:@""] autorelease],
];
for (NSMenuItem *menuItem in menuItems) {
[helpMenu addItem:menuItem];
}
[NSApp setHelpMenu:helpMenu];
return helpMenu;
}
- (void)toggleCapturesSystemKeys:(id)sender
{
NSMenuItem *item = (NSMenuItem *)sender;
_virtualMachineView.capturesSystemKeys = !_virtualMachineView.capturesSystemKeys;
[item setState:[self capturesSystemKeysState]];
}
- (NSControlStateValue)capturesSystemKeysState
{
return _virtualMachineView.capturesSystemKeys ? NSControlStateValueOn : NSControlStateValueOff;
}
- (void)reportIssue:(id)sender
{
NSString *url = @"https://github.com/Code-Hex/vz/issues/new";
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
}
- (void)openAboutWindow:(id)sender
{
AboutPanel *aboutPanel = [[[AboutPanel alloc] init] autorelease];
[aboutPanel makeKeyAndOrderFront:nil];
}
@end