Skip to content

Commit

Permalink
added WithController option
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Hex committed Nov 17, 2024
1 parent 592f054 commit a9d5633
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 28 deletions.
20 changes: 18 additions & 2 deletions virtualization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion virtualization_12.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
void startVirtualMachineWindow(void *machine, void *queue, double width, double height, const char *title, bool enableController);
5 changes: 3 additions & 2 deletions virtualization_12.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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];
Expand Down
3 changes: 2 additions & 1 deletion virtualization_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 26 additions & 22 deletions virtualization_view.m
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ @implementation AppDelegate {
VZVirtualMachineView *_virtualMachineView;
NSWindow *_window;
NSToolbar *_toolbar;
BOOL _enableController;
// Overlay for pause mode.
NSVisualEffectView *_pauseOverlayView;
// Zoom function properties.
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -321,23 +324,31 @@ - (void)hideOverlay
static NSString *const PowerToolbarIdentifier = @"Power";
static NSString *const SpaceToolbarIdentifier = @"Space";

- (void)updateToolbarItems
- (NSArray<NSToolbarItemIdentifier> *)setupToolbarItemIdentifiers
{
NSMutableArray<NSToolbarItemIdentifier> *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<NSToolbarItemIdentifier> *toolbarItems = [self setupToolbarItemIdentifiers];
[self setToolBarItems:toolbarItems];
}

Expand Down Expand Up @@ -467,14 +478,7 @@ - (NSWindow *)createMainWindowWithTitle:(NSString *)title

- (NSArray<NSToolbarItemIdentifier> *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
{
return @[
PauseToolbarIdentifier,
SpaceToolbarIdentifier,
PowerToolbarIdentifier,
NSToolbarSpaceItemIdentifier,
ZoomToolbarIdentifier,
NSToolbarFlexibleSpaceItemIdentifier
];
return [self setupToolbarItemIdentifiers];
}

- (NSArray<NSToolbarItemIdentifier> *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a9d5633

Please sign in to comment.