Skip to content

Commit

Permalink
config(macOS): workaround crash when device is removed while focused
Browse files Browse the repository at this point in the history
Fixes #5901
  • Loading branch information
osy committed Mar 31, 2024
1 parent e89346d commit 412878a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Platform/macOS/VMAppleSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ struct VMAppleSettingsView: View {
ToolbarItem(placement: .destructiveAction) {
Button("Remove") {
config.displays.removeAll(where: { $0.id == display.id })
refresh()
}
}
}
Expand All @@ -84,6 +85,7 @@ struct VMAppleSettingsView: View {
}.contextMenu {
DestructiveButton("Remove") {
config.displays.removeAll(where: { $0.id == display.id })
refresh()
}
}
}
Expand All @@ -97,6 +99,7 @@ struct VMAppleSettingsView: View {
ToolbarItem(placement: .destructiveAction) {
Button("Remove") {
config.serials.removeAll(where: { $0.id == serial.id })
refresh()
}
}
}
Expand All @@ -105,6 +108,7 @@ struct VMAppleSettingsView: View {
}.contextMenu {
DestructiveButton("Remove") {
config.serials.removeAll(where: { $0.id == serial.id })
refresh()
}
}
}
Expand All @@ -116,6 +120,7 @@ struct VMAppleSettingsView: View {
ToolbarItem(placement: .destructiveAction) {
Button("Remove") {
config.networks.removeAll(where: { $0.id == network.id })
refresh()
}
}
}
Expand All @@ -124,6 +129,7 @@ struct VMAppleSettingsView: View {
}.contextMenu {
DestructiveButton("Remove") {
config.networks.removeAll(where: { $0.id == network.id })
refresh()
}
}
}
Expand All @@ -133,6 +139,11 @@ struct VMAppleSettingsView: View {
VMDrivesSettingsView(drives: $config.drives, template: UTMAppleConfigurationDrive(newSize: 10240))
}
}

private func refresh() {
// SwiftUI bug: if a TextField is focused while a device is removed, the app will crash
infoActive = true
}
}

struct VMAppleSettingsView_Previews: PreviewProvider {
Expand Down
13 changes: 13 additions & 0 deletions Platform/macOS/VMQEMUSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ struct VMQEMUSettingsView: View {
ToolbarItem(placement: .destructiveAction) {
Button("Remove") {
config.displays.removeAll(where: { $0.id == display.id })
refresh()
}
}
}
Expand All @@ -89,6 +90,7 @@ struct VMQEMUSettingsView: View {
}.contextMenu {
DestructiveButton("Remove") {
config.displays.removeAll(where: { $0.id == display.id })
refresh()
}
}
}
Expand All @@ -100,6 +102,7 @@ struct VMQEMUSettingsView: View {
ToolbarItem(placement: .destructiveAction) {
Button("Remove") {
config.serials.removeAll(where: { $0.id == serial.id })
refresh()
}
}
}
Expand All @@ -108,6 +111,7 @@ struct VMQEMUSettingsView: View {
}.contextMenu {
DestructiveButton("Remove") {
config.serials.removeAll(where: { $0.id == serial.id })
refresh()
}
}
}
Expand All @@ -119,6 +123,7 @@ struct VMQEMUSettingsView: View {
ToolbarItem(placement: .destructiveAction) {
Button("Remove") {
config.networks.removeAll(where: { $0.id == network.id })
refresh()
}
}
}
Expand All @@ -127,6 +132,7 @@ struct VMQEMUSettingsView: View {
}.contextMenu {
DestructiveButton("Remove") {
config.networks.removeAll(where: { $0.id == network.id })
refresh()
}
}
if #available(macOS 12, *), network.mode == .emulated {
Expand All @@ -147,6 +153,7 @@ struct VMQEMUSettingsView: View {
ToolbarItem(placement: .destructiveAction) {
Button("Remove") {
config.sound.removeAll(where: { $0.id == sound.id })
refresh()
}
}
}
Expand All @@ -155,6 +162,7 @@ struct VMQEMUSettingsView: View {
}.contextMenu {
DestructiveButton("Remove") {
config.sound.removeAll(where: { $0.id == sound.id })
refresh()
}
}
}
Expand All @@ -164,6 +172,11 @@ struct VMQEMUSettingsView: View {
VMDrivesSettingsView(drives: $config.drives, template: UTMQemuConfigurationDrive(forArchitecture: config.system.architecture, target: config.system.target))
}
}

private func refresh() {
// SwiftUI bug: if a TextField is focused while a device is removed, the app will crash
infoActive = true
}
}

struct VMQEMUSettingsView_Previews: PreviewProvider {
Expand Down

0 comments on commit 412878a

Please sign in to comment.