Skip to content

Commit 179f6bb

Browse files
authored
Merge pull request #173 from Code-Hex/add/graphic-display-rich
make more rich graphical window
2 parents e20c903 + 8dcce62 commit 179f6bb

File tree

7 files changed

+610
-25
lines changed

7 files changed

+610
-25
lines changed

example/gui-linux/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func run(ctx context.Context) error {
9494
}
9595

9696
runtime.LockOSThread()
97-
vm.StartGraphicApplication(960, 600)
97+
vm.StartGraphicApplication(960, 600, vz.WithWindowTitle("Linux"), vz.WithController(true))
9898
runtime.UnlockOSThread()
9999

100100
cleanup()

example/macOS/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func runVM(ctx context.Context) error {
117117
log.Println("finished cleanup")
118118
}
119119

120-
vm.StartGraphicApplication(960, 600)
120+
vm.StartGraphicApplication(960, 600, vz.WithWindowTitle("macOS"), vz.WithController(true))
121121

122122
cleanup()
123123

virtualization.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,16 +360,55 @@ func (v *VirtualMachine) Stop() error {
360360
return <-errCh
361361
}
362362

363+
type startGraphicApplicationOptions struct {
364+
title string
365+
enableController bool
366+
}
367+
368+
// StartGraphicApplicationOption is an option for display graphics start.
369+
type StartGraphicApplicationOption func(*startGraphicApplicationOptions) error
370+
371+
// WithWindowTitle is an option to set window title of display graphics window.
372+
func WithWindowTitle(title string) StartGraphicApplicationOption {
373+
return func(sgao *startGraphicApplicationOptions) error {
374+
sgao.title = title
375+
return nil
376+
}
377+
}
378+
379+
// WithController is an option to set virtual machine controller on graphics window toolbar.
380+
func WithController(enable bool) StartGraphicApplicationOption {
381+
return func(sgao *startGraphicApplicationOptions) error {
382+
sgao.enableController = enable
383+
return nil
384+
}
385+
}
386+
363387
// StartGraphicApplication starts an application to display graphics of the VM.
364388
//
365389
// You must to call runtime.LockOSThread before calling this method.
366390
//
367391
// This is only supported on macOS 12 and newer, error will be returned on older versions.
368-
func (v *VirtualMachine) StartGraphicApplication(width, height float64) error {
392+
func (v *VirtualMachine) StartGraphicApplication(width, height float64, opts ...StartGraphicApplicationOption) error {
369393
if err := macOSAvailable(12); err != nil {
370394
return err
371395
}
372-
C.startVirtualMachineWindow(objc.Ptr(v), C.double(width), C.double(height))
396+
defaultOpts := &startGraphicApplicationOptions{}
397+
for _, opt := range opts {
398+
if err := opt(defaultOpts); err != nil {
399+
return err
400+
}
401+
}
402+
windowTitle := charWithGoString(defaultOpts.title)
403+
defer windowTitle.Free()
404+
C.startVirtualMachineWindow(
405+
objc.Ptr(v),
406+
v.dispatchQueue,
407+
C.double(width),
408+
C.double(height),
409+
windowTitle.CString(),
410+
C.bool(defaultOpts.enableController),
411+
)
373412
return nil
374413
}
375414

virtualization_12.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ void setKeyboardsVZVirtualMachineConfiguration(void *config,
4545
void setAudioDevicesVZVirtualMachineConfiguration(void *config,
4646
void *audioDevices);
4747

48-
void startVirtualMachineWindow(void *machine, double width, double height);
48+
void startVirtualMachineWindow(void *machine, void *queue, double width, double height, const char *title, bool enableController);

virtualization_12.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,18 +343,22 @@ void setVZVirtioFileSystemDeviceConfigurationShare(void *config, void *share)
343343
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
344344
}
345345

346-
void startVirtualMachineWindow(void *machine, double width, double height)
346+
void startVirtualMachineWindow(void *machine, void *queue, double width, double height, const char *title, bool enableController)
347347
{
348348
// Create a shared app instance.
349349
// This will initialize the global variable
350350
// 'NSApp' with the application instance.
351351
[VZApplication sharedApplication];
352352
if (@available(macOS 12, *)) {
353353
@autoreleasepool {
354+
NSString *windowTitle = [NSString stringWithUTF8String:title];
354355
AppDelegate *appDelegate = [[[AppDelegate alloc]
355356
initWithVirtualMachine:(VZVirtualMachine *)machine
357+
queue:(dispatch_queue_t)queue
356358
windowWidth:(CGFloat)width
357-
windowHeight:(CGFloat)height] autorelease];
359+
windowHeight:(CGFloat)height
360+
windowTitle:windowTitle
361+
enableController:enableController] autorelease];
358362

359363
NSApp.delegate = appDelegate;
360364
[NSApp run];

virtualization_view.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
@end
2525

2626
API_AVAILABLE(macos(12.0))
27-
@interface AppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate, VZVirtualMachineDelegate>
27+
@interface AppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate, VZVirtualMachineDelegate, NSToolbarDelegate>
2828
- (instancetype)initWithVirtualMachine:(VZVirtualMachine *)virtualMachine
29+
queue:(dispatch_queue_t)queue
2930
windowWidth:(CGFloat)windowWidth
30-
windowHeight:(CGFloat)windowHeight;
31+
windowHeight:(CGFloat)windowHeight
32+
windowTitle:(NSString *)windowTitle
33+
enableController:(BOOL)enableController;
3134
@end

0 commit comments

Comments
 (0)