Skip to content

Commit 7c2f2f6

Browse files
committed
UI Project Organization
1 parent c639466 commit 7c2f2f6

File tree

9 files changed

+181
-171
lines changed

9 files changed

+181
-171
lines changed

Sources/hostmgr-helper/HostMgrHelperApp.swift

Lines changed: 0 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -60,172 +60,3 @@ struct HostMgrHelperApp: App {
6060
return options
6161
}()
6262
}
63-
64-
struct EmptyVMListItem: View {
65-
66-
let slot: VirtualMachineSlot
67-
68-
var body: some View {
69-
VStack(alignment: .leading) {
70-
Text(slot.role.displayName).font(.footnote)
71-
72-
Spacer()
73-
HStack {
74-
Spacer()
75-
Text("No VM Running").font(.title2)
76-
Spacer()
77-
}
78-
Spacer()
79-
}
80-
}
81-
}
82-
83-
struct ErrorVMListItem: View {
84-
85-
let slot: VirtualMachineSlot
86-
let error: Error
87-
88-
var body: some View {
89-
VStack(alignment: .leading) {
90-
Text(slot.role.displayName).font(.footnote)
91-
92-
Spacer()
93-
HStack {
94-
Spacer()
95-
Image(systemName: "exclamationmark.triangle.fill")
96-
Text(error.localizedDescription)
97-
Spacer()
98-
}
99-
Spacer()
100-
}
101-
}
102-
}
103-
104-
struct PendingVMListItem: View {
105-
let launchConfiguration: LaunchConfiguration
106-
let slot: VirtualMachineSlot
107-
108-
var body: some View {
109-
VStack(alignment: .leading) {
110-
Text(slot.role.displayName)
111-
.font(.footnote)
112-
113-
Text(launchConfiguration.name)
114-
.font(.title)
115-
.fontWeight(.medium)
116-
117-
ProgressView()
118-
}
119-
}
120-
}
121-
122-
struct RunningVMListItem: View {
123-
let launchConfiguration: LaunchConfiguration
124-
let ipAddress: IPv4Address
125-
126-
@Environment(\.openWindow)
127-
var openWindow: OpenWindowAction
128-
129-
@ObservedObject
130-
var slot: VirtualMachineSlot
131-
132-
func openVNCSession() {
133-
let vncURL = URL(string: "vnc://\(ipAddress.debugDescription)")!
134-
NSWorkspace.shared.open(vncURL)
135-
}
136-
137-
func openSSHSession() {
138-
let sshURL = URL(string: "ssh://\(ipAddress.debugDescription)")!
139-
NSWorkspace.shared.open(sshURL)
140-
}
141-
142-
func openVMWindow() {
143-
self.openWindow(id: "vm-view", value: slot.role)
144-
}
145-
146-
func shutdown() {
147-
Task {
148-
try await slot.stopVirtualMachine()
149-
}
150-
}
151-
152-
var body: some View {
153-
VStack(alignment: .leading) {
154-
Text(slot.role.displayName)
155-
.font(.footnote)
156-
157-
Text(launchConfiguration.name)
158-
.font(.title)
159-
.fontWeight(.medium)
160-
161-
VMListItemDataItem(key: "Handle", value: launchConfiguration.handle)
162-
VMListItemDataItem(key: "IP Address", value: ipAddress.debugDescription)
163-
164-
HStack(alignment: .top) {
165-
Button(action: self.openVNCSession, label: {
166-
Label("VNC", systemImage: "play.display")
167-
})
168-
169-
Button(action: self.openVMWindow, label: {
170-
Label("View", systemImage: "display")
171-
})
172-
173-
Button(action: self.openSSHSession, label: {
174-
Label("SSH", systemImage: "terminal").foregroundStyle(.white)
175-
})
176-
177-
Button(action: self.shutdown, label: {
178-
Label("Stop", systemImage: "stop.circle").foregroundStyle(.white)
179-
})
180-
}.frame(maxWidth: .infinity)
181-
}
182-
183-
}
184-
}
185-
186-
struct VMListItem: View {
187-
188-
@ObservedObject
189-
var slot: VirtualMachineSlot
190-
191-
var body: some View {
192-
switch slot.status {
193-
case .empty: EmptyVMListItem(slot: slot)
194-
case .starting(let launchConfiguration):
195-
PendingVMListItem(
196-
launchConfiguration: launchConfiguration,
197-
slot: slot
198-
)
199-
case .running(let launchConfiguration, let ipAddress):
200-
RunningVMListItem(
201-
launchConfiguration: launchConfiguration,
202-
ipAddress: ipAddress,
203-
slot: slot
204-
)
205-
case .stopping:
206-
EmptyVMListItem(slot: slot)
207-
case .crashed(let error):
208-
ErrorVMListItem(slot: slot, error: error)
209-
}
210-
}
211-
}
212-
213-
struct VMListItemDataItem: View {
214-
215-
let key: String
216-
let value: String?
217-
218-
var body: some View {
219-
VStack(alignment: .leading) {
220-
Text(key).font(.footnote)
221-
222-
if let value {
223-
Text(value)
224-
} else {
225-
ProgressView().controlSize(.small)
226-
}
227-
228-
Text("") // Used as a spacer
229-
}
230-
}
231-
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import SwiftUI
2+
3+
struct VMListItemDataItem: View {
4+
5+
let key: String
6+
let value: String?
7+
8+
var body: some View {
9+
VStack(alignment: .leading) {
10+
Text(key).font(.footnote)
11+
12+
if let value {
13+
Text(value)
14+
} else {
15+
ProgressView().controlSize(.small)
16+
}
17+
18+
Text("") // Used as a spacer
19+
}
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import SwiftUI
2+
3+
struct EmptyVMListItem: View {
4+
5+
let slot: VirtualMachineSlot
6+
7+
var body: some View {
8+
VStack(alignment: .leading) {
9+
Text(slot.role.displayName).font(.footnote)
10+
11+
Spacer()
12+
HStack {
13+
Spacer()
14+
Text("No VM Running").font(.title2)
15+
Spacer()
16+
}
17+
Spacer()
18+
}
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import SwiftUI
2+
3+
struct ErrorVMListItem: View {
4+
5+
let slot: VirtualMachineSlot
6+
let error: Error
7+
8+
var body: some View {
9+
VStack(alignment: .leading) {
10+
Text(slot.role.displayName).font(.footnote)
11+
12+
Spacer()
13+
HStack {
14+
Spacer()
15+
Image(systemName: "exclamationmark.triangle.fill")
16+
Text(error.localizedDescription)
17+
Spacer()
18+
}
19+
Spacer()
20+
}
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import SwiftUI
2+
import libhostmgr
3+
4+
struct PendingVMListItem: View {
5+
let launchConfiguration: LaunchConfiguration
6+
let slot: VirtualMachineSlot
7+
8+
var body: some View {
9+
VStack(alignment: .leading) {
10+
Text(slot.role.displayName)
11+
.font(.footnote)
12+
13+
Text(launchConfiguration.name)
14+
.font(.title)
15+
.fontWeight(.medium)
16+
17+
ProgressView()
18+
}
19+
}
20+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import SwiftUI
2+
import Network
3+
4+
import libhostmgr
5+
6+
struct RunningVMListItem: View {
7+
let launchConfiguration: LaunchConfiguration
8+
let ipAddress: IPv4Address
9+
10+
@Environment(\.openWindow)
11+
var openWindow: OpenWindowAction
12+
13+
@ObservedObject
14+
var slot: VirtualMachineSlot
15+
16+
func openVNCSession() {
17+
let vncURL = URL(string: "vnc://\(ipAddress.debugDescription)")!
18+
NSWorkspace.shared.open(vncURL)
19+
}
20+
21+
func openSSHSession() {
22+
let sshURL = URL(string: "ssh://\(ipAddress.debugDescription)")!
23+
NSWorkspace.shared.open(sshURL)
24+
}
25+
26+
func openVMWindow() {
27+
self.openWindow(id: "vm-view", value: slot.role)
28+
}
29+
30+
func shutdown() {
31+
Task {
32+
try await slot.stopVirtualMachine()
33+
}
34+
}
35+
36+
var body: some View {
37+
VStack(alignment: .leading) {
38+
Text(slot.role.displayName)
39+
.font(.footnote)
40+
41+
Text(launchConfiguration.name)
42+
.font(.title)
43+
.fontWeight(.medium)
44+
45+
VMListItemDataItem(key: "Handle", value: launchConfiguration.handle)
46+
VMListItemDataItem(key: "IP Address", value: ipAddress.debugDescription)
47+
48+
HStack(alignment: .top) {
49+
Button(action: self.openVNCSession, label: {
50+
Label("VNC", systemImage: "play.display")
51+
})
52+
53+
Button(action: self.openVMWindow, label: {
54+
Label("View", systemImage: "display")
55+
})
56+
57+
Button(action: self.openSSHSession, label: {
58+
Label("SSH", systemImage: "terminal").foregroundStyle(.white)
59+
})
60+
61+
Button(action: self.shutdown, label: {
62+
Label("Stop", systemImage: "stop.circle").foregroundStyle(.white)
63+
})
64+
}.frame(maxWidth: .infinity)
65+
}
66+
67+
}
68+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import SwiftUI
2+
3+
struct VMListItem: View {
4+
5+
@ObservedObject
6+
var slot: VirtualMachineSlot
7+
8+
var body: some View {
9+
switch slot.status {
10+
case .empty: EmptyVMListItem(slot: slot)
11+
case .starting(let launchConfiguration):
12+
PendingVMListItem(
13+
launchConfiguration: launchConfiguration,
14+
slot: slot
15+
)
16+
case .running(let launchConfiguration, let ipAddress):
17+
RunningVMListItem(
18+
launchConfiguration: launchConfiguration,
19+
ipAddress: ipAddress,
20+
slot: slot
21+
)
22+
case .stopping:
23+
EmptyVMListItem(slot: slot)
24+
case .crashed(let error):
25+
ErrorVMListItem(slot: slot, error: error)
26+
}
27+
}
28+
}

Sources/hostmgr-helper/VMWindow.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct VMWindowContent: View {
1313
switch vmSlot.status {
1414
case .empty: Text("VM not running")
1515
case .starting: ProgressView()
16-
case .running: VMView(virtualMachine: vmSlot.virtualMachine!)
16+
case .running: VirtualMachineDisplayView(virtualMachine: vmSlot.virtualMachine!)
1717
case .stopping: ProgressView()
1818
case .crashed(let err): Text("VM Error: \(err.localizedDescription)")
1919
}

Sources/hostmgr-helper/VMView.swift renamed to Sources/hostmgr-helper/VirtualMachineDisplayView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import SwiftUI
22
import Virtualization
33

4-
struct VMView: NSViewRepresentable {
4+
struct VirtualMachineDisplayView: NSViewRepresentable {
55
typealias NSViewType = VZVirtualMachineView
66

77
let virtualMachine: VZVirtualMachine

0 commit comments

Comments
 (0)