61 lines
3.2 KiB
Swift
61 lines
3.2 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import Combine
|
|
import SwiftUI
|
|
|
|
// SwiftUI rebuilds native menu groups when an observed Commands object changes.
|
|
// Keep frame, audio and media-status polling out of that observation: rebuilding
|
|
// the system View menu while it is tracking can dismiss it before selection.
|
|
@MainActor
|
|
final class MachineMenuState: ObservableObject {
|
|
struct Values: Equatable {
|
|
var busy: Bool
|
|
var diskSaving: Bool
|
|
var state: VCCMachineState
|
|
var videoOutput: DisplayOutput
|
|
var fastForward: Bool
|
|
var audioEnabled: Bool
|
|
var diskConnected: Bool
|
|
var hardDiskConnected: Bool
|
|
var pasting: Bool = false
|
|
var canPaste: Bool { !busy && state == .running && !pasting }
|
|
var canQuickLoad: Bool { !busy && !diskSaving && (state == .running || state == .paused) }
|
|
}
|
|
@Published private(set) var values: Values
|
|
private var observation: AnyCancellable?
|
|
|
|
init(machine: MachineModel) {
|
|
values = Values(busy: machine.busy || machine.diskEditorLocked, diskSaving: machine.diskSaving, state: machine.state,
|
|
videoOutput: machine.videoOutput, fastForward: machine.settings.fastForward, audioEnabled: machine.audioEnabled,
|
|
diskConnected: machine.diskConnected, hardDiskConnected: machine.hardDiskConnected, pasting: machine.pasteRemaining > 0)
|
|
let effectiveBusy = Publishers.CombineLatest(machine.$busy, machine.$diskEditorLocked).map { $0 || $1 }.removeDuplicates()
|
|
observation = Publishers.CombineLatest4(effectiveBusy, machine.$diskSaving, machine.$state, machine.$videoOutput)
|
|
.combineLatest(machine.$settings.map(\.fastForward), machine.$diskConnected, machine.$hardDiskConnected)
|
|
.combineLatest(machine.$audioEnabled, machine.$pasteRemaining.map { $0 > 0 }.removeDuplicates())
|
|
.map { controlsAndDevices, audioEnabled, pasting in
|
|
let (controls, fastForward, diskConnected, hardDiskConnected) = controlsAndDevices
|
|
return Values(busy: controls.0, diskSaving: controls.1, state: controls.2, videoOutput: controls.3,
|
|
fastForward: fastForward, audioEnabled: audioEnabled, diskConnected: diskConnected, hardDiskConnected: hardDiskConnected, pasting: pasting)
|
|
}
|
|
.removeDuplicates()
|
|
.sink { [weak self] value in
|
|
// The model and its published setters belong to the main actor.
|
|
guard let self, self.values != value else { return }
|
|
self.values = value
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Keep the emulator command available in its menu throughout the app. Only
|
|
/// the active emulator window owns its keyboard equivalent.
|
|
struct FastForwardMenuItem: View {
|
|
let machine: MachineModel
|
|
let values: MachineMenuState.Values
|
|
let emulatorFocused: Bool
|
|
|
|
var body: some View {
|
|
Toggle("Fast-forward", isOn: Binding(get: { values.fastForward }, set: machine.setFastForward))
|
|
.keyboardShortcut(emulatorFocused ? KeyboardShortcut("f", modifiers: [.command, .shift]) : nil)
|
|
.disabled(values.busy || values.diskSaving || values.state == .closed)
|
|
}
|
|
}
|