diff --git a/virtualization.go b/virtualization.go index 350fe6e..7c6ee71 100644 --- a/virtualization.go +++ b/virtualization.go @@ -361,7 +361,8 @@ func (v *VirtualMachine) Stop() error { } type startGraphicApplicationOptions struct { - title string + title string + enableController bool } // StartGraphicApplicationOption is an option for display graphics start. @@ -375,6 +376,14 @@ func WithWindowTitle(title string) StartGraphicApplicationOption { } } +// WithController is an option to set virtual machine controller on graphics window toolbar. +func WithController(enable bool) StartGraphicApplicationOption { + return func(sgao *startGraphicApplicationOptions) error { + sgao.enableController = enable + return nil + } +} + // StartGraphicApplication starts an application to display graphics of the VM. // // You must to call runtime.LockOSThread before calling this method. @@ -390,7 +399,14 @@ func (v *VirtualMachine) StartGraphicApplication(width, height float64, opts ... } windowTitle := charWithGoString(defaultOpts.title) defer windowTitle.Free() - C.startVirtualMachineWindow(objc.Ptr(v), v.dispatchQueue, C.double(width), C.double(height), windowTitle.CString()) + C.startVirtualMachineWindow( + objc.Ptr(v), + v.dispatchQueue, + C.double(width), + C.double(height), + windowTitle.CString(), + C.bool(defaultOpts.enableController), + ) return nil } diff --git a/virtualization_12.h b/virtualization_12.h index f3080ad..dd679a7 100644 --- a/virtualization_12.h +++ b/virtualization_12.h @@ -45,4 +45,4 @@ void setKeyboardsVZVirtualMachineConfiguration(void *config, void setAudioDevicesVZVirtualMachineConfiguration(void *config, void *audioDevices); -void startVirtualMachineWindow(void *machine, void *queue, double width, double height, const char *title); \ No newline at end of file +void startVirtualMachineWindow(void *machine, void *queue, double width, double height, const char *title, bool enableController); \ No newline at end of file diff --git a/virtualization_12.m b/virtualization_12.m index 2f3f948..1ab6b52 100644 --- a/virtualization_12.m +++ b/virtualization_12.m @@ -343,7 +343,7 @@ void setVZVirtioFileSystemDeviceConfigurationShare(void *config, void *share) RAISE_UNSUPPORTED_MACOS_EXCEPTION(); } -void startVirtualMachineWindow(void *machine, void *queue, double width, double height, const char *title) +void startVirtualMachineWindow(void *machine, void *queue, double width, double height, const char *title, bool enableController) { // Create a shared app instance. // This will initialize the global variable @@ -357,7 +357,8 @@ void startVirtualMachineWindow(void *machine, void *queue, double width, double queue:(dispatch_queue_t)queue windowWidth:(CGFloat)width windowHeight:(CGFloat)height - windowTitle:windowTitle] autorelease]; + windowTitle:windowTitle + enableController:enableController] autorelease]; NSApp.delegate = appDelegate; [NSApp run]; diff --git a/virtualization_view.h b/virtualization_view.h index 61bded5..09766a7 100644 --- a/virtualization_view.h +++ b/virtualization_view.h @@ -29,5 +29,6 @@ API_AVAILABLE(macos(12.0)) queue:(dispatch_queue_t)queue windowWidth:(CGFloat)windowWidth windowHeight:(CGFloat)windowHeight - windowTitle:(NSString *)windowTitle; + windowTitle:(NSString *)windowTitle + enableController:(BOOL)enableController; @end \ No newline at end of file diff --git a/virtualization_view.m b/virtualization_view.m index e8c366c..c9f8824 100644 --- a/virtualization_view.m +++ b/virtualization_view.m @@ -171,6 +171,7 @@ @implementation AppDelegate { VZVirtualMachineView *_virtualMachineView; NSWindow *_window; NSToolbar *_toolbar; + BOOL _enableController; // Overlay for pause mode. NSVisualEffectView *_pauseOverlayView; // Zoom function properties. @@ -185,6 +186,7 @@ - (instancetype)initWithVirtualMachine:(VZVirtualMachine *)virtualMachine windowWidth:(CGFloat)windowWidth windowHeight:(CGFloat)windowHeight windowTitle:(NSString *)windowTitle + enableController:(BOOL)enableController { self = [super init]; _virtualMachine = virtualMachine; @@ -206,6 +208,7 @@ - (instancetype)initWithVirtualMachine:(VZVirtualMachine *)virtualMachine // Setup some window configs _window = [self createMainWindowWithTitle:windowTitle width:windowWidth height:windowHeight]; _toolbar = [self createCustomToolbar]; + _enableController = enableController; [_virtualMachine addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew @@ -321,23 +324,31 @@ - (void)hideOverlay static NSString *const PowerToolbarIdentifier = @"Power"; static NSString *const SpaceToolbarIdentifier = @"Space"; -- (void)updateToolbarItems +- (NSArray *)setupToolbarItemIdentifiers { NSMutableArray *toolbarItems = [NSMutableArray array]; - if ([self canPauseVirtualMachine]) { - [toolbarItems addObject:PauseToolbarIdentifier]; - } - if ([self canResumeVirtualMachine]) { - [toolbarItems addObject:SpaceToolbarIdentifier]; - [toolbarItems addObject:PlayToolbarIdentifier]; - } - if ([self canStopVirtualMachine] || [self canStartVirtualMachine]) { - [toolbarItems addObject:SpaceToolbarIdentifier]; - [toolbarItems addObject:PowerToolbarIdentifier]; + if (_enableController) { + if ([self canPauseVirtualMachine]) { + [toolbarItems addObject:PauseToolbarIdentifier]; + } + if ([self canResumeVirtualMachine]) { + [toolbarItems addObject:SpaceToolbarIdentifier]; + [toolbarItems addObject:PlayToolbarIdentifier]; + } + if ([self canStopVirtualMachine] || [self canStartVirtualMachine]) { + [toolbarItems addObject:SpaceToolbarIdentifier]; + [toolbarItems addObject:PowerToolbarIdentifier]; + } } [toolbarItems addObject:NSToolbarSpaceItemIdentifier]; [toolbarItems addObject:ZoomToolbarIdentifier]; [toolbarItems addObject:NSToolbarFlexibleSpaceItemIdentifier]; + return [toolbarItems copy]; +} + +- (void)updateToolbarItems +{ + NSArray *toolbarItems = [self setupToolbarItemIdentifiers]; [self setToolBarItems:toolbarItems]; } @@ -467,14 +478,7 @@ - (NSWindow *)createMainWindowWithTitle:(NSString *)title - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar { - return @[ - PauseToolbarIdentifier, - SpaceToolbarIdentifier, - PowerToolbarIdentifier, - NSToolbarSpaceItemIdentifier, - ZoomToolbarIdentifier, - NSToolbarFlexibleSpaceItemIdentifier - ]; + return [self setupToolbarItemIdentifiers]; } - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar @@ -526,10 +530,10 @@ - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSToolbar [item setLabel:@"Zoom"]; [item setToolTip:@"Toggle Zoom"]; } else if ([itemIdentifier isEqualToString:SpaceToolbarIdentifier]) { - NSView *spaceView = [[[NSView alloc] initWithFrame:NSMakeRect(0, 0, 5, 10)] autorelease]; + NSView *spaceView = [[[NSView alloc] initWithFrame:NSMakeRect(0, 0, 2, 10)] autorelease]; item.view = spaceView; - item.minSize = NSMakeSize(2.5, 10); - item.maxSize = NSMakeSize(2.5, 10); + item.minSize = NSMakeSize(1, 10); + item.maxSize = NSMakeSize(1, 10); } return item;