Skip to content

Commit

Permalink
Remove toolbar, add overlay with Run and Settings buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
franklefebvre committed Jun 3, 2022
1 parent e283f9a commit c008222
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 23 deletions.
4 changes: 4 additions & 0 deletions MacVM.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
01B37758281D772700BB9111 /* VMControlOverlay.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01B37757281D772700BB9111 /* VMControlOverlay.swift */; };
01B3775A281F06F900BB9111 /* VMSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01B37759281F06F900BB9111 /* VMSettingsView.swift */; };
CC4E3630268AD4E400AA5EA1 /* MacVMApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC4E362F268AD4E400AA5EA1 /* MacVMApp.swift */; };
CC4E3634268AD4E500AA5EA1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = CC4E3633268AD4E500AA5EA1 /* Assets.xcassets */; };
Expand All @@ -20,6 +21,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
01B37757281D772700BB9111 /* VMControlOverlay.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VMControlOverlay.swift; sourceTree = "<group>"; };
01B37759281F06F900BB9111 /* VMSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VMSettingsView.swift; sourceTree = "<group>"; };
CC4E362C268AD4E400AA5EA1 /* MacVM.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MacVM.app; sourceTree = BUILT_PRODUCTS_DIR; };
CC4E362F268AD4E400AA5EA1 /* MacVMApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacVMApp.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -70,6 +72,7 @@
CC4E362F268AD4E400AA5EA1 /* MacVMApp.swift */,
CC4E3641268AD85400AA5EA1 /* VMView.swift */,
CCD1A9B4268C34AD001E87AC /* VMUIView.swift */,
01B37757281D772700BB9111 /* VMControlOverlay.swift */,
CC4E3647268AE5A800AA5EA1 /* VMInstallView.swift */,
01B37759281F06F900BB9111 /* VMSettingsView.swift */,
CC4E363D268AD76000AA5EA1 /* VMDocument.swift */,
Expand Down Expand Up @@ -167,6 +170,7 @@
CC4E363E268AD76000AA5EA1 /* VMDocument.swift in Sources */,
01B3775A281F06F900BB9111 /* VMSettingsView.swift in Sources */,
CC4E3646268ADC6800AA5EA1 /* VMInstance.swift in Sources */,
01B37758281D772700BB9111 /* VMControlOverlay.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
74 changes: 74 additions & 0 deletions MacVM/VMControlOverlay.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// VMControlOverlay.swift
// MacVM
//
// Created by Frank Lefebvre on 30/04/2022.
//

import SwiftUI

struct VMControlOverlay: View {
@ObservedObject var document: VMDocument
var fileURL: URL
@State private var presentSettings = false
@State private var documentContent = VMContent.empty
@Environment(\.undoManager) private var undoManager

var body: some View {
HStack(spacing: 40) {
Button(action: {
document.createVMInstance(with: fileURL)
document.vmInstance?.start()
}) {
Image(systemName: "play.circle")
.font(.system(size: 96, weight: .regular, design: .rounded))
}
.buttonStyle(.borderless)
Button(action: {
documentContent = document.content
presentSettings = true
}) {
Image(systemName: "gear.circle")
.font(.system(size: 96, weight: .regular, design: .rounded))
}
.buttonStyle(.borderless)
}
.padding(30)
.background {
Color.gray
.cornerRadius(30)
}
.sheet(isPresented: $presentSettings) {
VStack {
VMSettingsView(content: $document.content)
Spacer()
HStack {
Spacer()
Button(action: {
document.content = documentContent // restore previous settings
presentSettings = false // or use @Environment(\.presentationMode)
}) {
Text("Cancel")
}
.buttonStyle(.bordered)
.keyboardShortcut(.cancelAction)
Button(action: {
save()
presentSettings = false
}) {
Text("OK")
}
.buttonStyle(.bordered)
.keyboardShortcut(.defaultAction)
}
.padding(.top)
}
.padding()
.fixedSize()
}
}

func save() {
undoManager?.registerUndo(withTarget: document, handler: { _ in })
}
}
2 changes: 1 addition & 1 deletion MacVM/VMInstallView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UniformTypeIdentifiers
struct VMInstallView: View {

var fileURL: URL?
var document: VMDocument
@ObservedObject var document: VMDocument

@Environment(\.undoManager) var undoManager
@ObservedObject var state: VMInstallationState
Expand Down
2 changes: 1 addition & 1 deletion MacVM/VMInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class VMInstance: NSObject, VZVirtualMachineDelegate {
let networkDevice = VZVirtioNetworkDeviceConfiguration()
networkDevice.attachment = VZNATNetworkDeviceAttachment()

let heightOfToolbar = 98.0
let heightOfToolbar = 0.0
let graphics = VZMacGraphicsDeviceConfiguration()
graphics.displays = NSScreen.screens.count > 0 ? NSScreen.screens.map {
VZMacGraphicsDisplayConfiguration(
Expand Down
26 changes: 5 additions & 21 deletions MacVM/VMView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ struct VMView: View {
if let fileURL = fileURL {
if document.content.installed {
VMUIView(virtualMachine: document.vmInstance?.virtualMachine)
.overlay {
if !document.isRunning {
VMControlOverlay(document: document, fileURL: fileURL)
}
}
} else {
VMInstallView(
fileURL: fileURL,
Expand All @@ -34,26 +39,5 @@ struct VMView: View {
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
.toolbar {
ToolbarItem {
Button(action: {
guard let fileURL = fileURL else {
return
}

if document.isRunning {
document.vmInstance?.stop()
} else {
document.createVMInstance(with: fileURL)
document.vmInstance?.start()
}
}) {
Image(systemName: document.isRunning ? "stop.circle" : "play.circle")
.font(.system(size: 24, weight: .regular, design: .rounded))
}
.buttonStyle(BorderlessButtonStyle())
.disabled(!document.content.installed)
}
}
}
}

0 comments on commit c008222

Please sign in to comment.