Skip to content
This repository was archived by the owner on Nov 7, 2026. It is now read-only.

Commit 054d6bf

Browse files
committed
Move simulation state and controls into SimulationController
The engine, play/pause task, input text and the simulation mode flag move from ContentView @State into an @observable SimulationController. The step delay and highlight mode are read from UserDefaults at each step, as the @AppStorage values were before. No behavior change intended. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Cp4EQgFQyGbM8MVLiWbJKQ
1 parent efabe1b commit 054d6bf

2 files changed

Lines changed: 164 additions & 127 deletions

File tree

‎VisuStruct-iOS/VisuStructXcodeOpen/VisuStruct/ContentView.swift‎

Lines changed: 22 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ struct ContentView: View {
3939
@State private var showInfo = false
4040
@State private var showCodeGeneratorPaywall = false
4141
@State private var showCodeGeneratorPlaceholder = false
42-
@State private var isSimulationMode = false
43-
@State private var simulationEngine: VisuStructSimulationEngine
44-
@State private var simulationPlayTask: Task<Void, Never>? = nil
45-
@State private var isSimulationPlaying = false
46-
@State private var simulationWasPlayingBeforeInput = false
47-
@State private var simulationInputText = ""
42+
@State private var simulation: SimulationController
4843
@ObservedObject private var recentFilesStore: RecentFilesStore
4944
/// Erhöhen bei jedem Tipp auf „Code erzeugen“, damit die Anzeige neu aufgebaut wird (auch wenn der Dialog schon offen ist).
5045
@State private var codeGeneratorRegenerationToken: Int = 0
@@ -74,7 +69,6 @@ struct ContentView: View {
7469
@AppStorage(VisuStructUIScaleStep.appStorageKeyCanvas) private var canvasScaleRaw: String = VisuStructUIScaleStep.small.rawValue
7570
@AppStorage(VisuStructUIScaleStep.appStorageKeyInspector) private var inspectorScaleRaw: String = VisuStructUIScaleStep.small.rawValue
7671
@State private var editorFocusToken = UUID()
77-
@AppStorage(SimulationStepDelayOption.appStorageKey) private var simulationStepDelayRaw: String = SimulationStepDelayOption.defaultRawValue
7872
@AppStorage(SimulationHighlightMode.appStorageKey) private var simulationHighlightModeRaw: String = SimulationHighlightMode.defaultRawValue
7973

8074
private var canvasScaleStep: VisuStructUIScaleStep {
@@ -89,22 +83,14 @@ struct ContentView: View {
8983
ExportFilenameStyle.fromStored(exportFilenameStyleRaw)
9084
}
9185

92-
private var simulationStepDelay: SimulationStepDelayOption {
93-
SimulationStepDelayOption.fromStored(simulationStepDelayRaw)
94-
}
95-
96-
private var simulationHighlightMode: SimulationHighlightMode {
97-
SimulationHighlightMode.fromStored(simulationHighlightModeRaw)
98-
}
99-
10086
init() {
10187
let startupLang = AppLanguage.currentFromDefaults()
10288
let model = DiagramSheetTabsModel.makeInitial(language: startupLang)
10389
let recentStore = RecentFilesStore.shared
10490
_tabsModel = State(initialValue: model)
10591
_recentFilesStore = ObservedObject(wrappedValue: recentStore)
10692
_documentServices = State(initialValue: DiagramEditorDocumentServices(tabsModel: model, recentFilesStore: recentStore))
107-
_simulationEngine = State(initialValue: VisuStructSimulationEngine(document: model.tabs[0].document))
93+
_simulation = State(initialValue: SimulationController(tabsModel: model))
10894
}
10995

11096
// MARK: - Tab-Titel & Dokument öffnen
@@ -770,15 +756,15 @@ struct ContentView: View {
770756
simulationCanvasBlock
771757
.layoutPriority(1)
772758
SimulationStatePanel(
773-
engine: simulationEngine,
774-
isPlaying: isSimulationPlaying,
775-
inputText: $simulationInputText,
759+
engine: simulation.engine,
760+
isPlaying: simulation.isPlaying,
761+
inputText: $simulation.inputText,
776762
highlightModeRaw: $simulationHighlightModeRaw,
777-
onPlay: playSimulation,
778-
onPause: pauseSimulation,
779-
onStep: stepSimulation,
780-
onSubmitInput: submitSimulationInput,
781-
onStop: resetSimulation
763+
onPlay: { simulation.play() },
764+
onPause: { simulation.pause() },
765+
onStep: { simulation.step() },
766+
onSubmitInput: { simulation.submitInput() },
767+
onStop: { simulation.reset() }
782768
)
783769
.frame(width: panelWidth)
784770
.frame(maxHeight: .infinity, alignment: .top)
@@ -789,15 +775,15 @@ struct ContentView: View {
789775
simulationCanvasBlock
790776
.frame(height: max(340, geo.size.height * 0.55))
791777
SimulationStatePanel(
792-
engine: simulationEngine,
793-
isPlaying: isSimulationPlaying,
794-
inputText: $simulationInputText,
778+
engine: simulation.engine,
779+
isPlaying: simulation.isPlaying,
780+
inputText: $simulation.inputText,
795781
highlightModeRaw: $simulationHighlightModeRaw,
796-
onPlay: playSimulation,
797-
onPause: pauseSimulation,
798-
onStep: stepSimulation,
799-
onSubmitInput: submitSimulationInput,
800-
onStop: resetSimulation
782+
onPlay: { simulation.play() },
783+
onPause: { simulation.pause() },
784+
onStep: { simulation.step() },
785+
onSubmitInput: { simulation.submitInput() },
786+
onStop: { simulation.reset() }
801787
)
802788
}
803789
.frame(maxWidth: .infinity, alignment: .leading)
@@ -926,104 +912,13 @@ struct ContentView: View {
926912
)
927913
}
928914

929-
// MARK: - Simulation (Modus & Steuerung)
930-
931-
private func beginSimulationMode() {
932-
pauseSimulation()
933-
var engine = VisuStructSimulationEngine(document: document)
934-
engine.reset()
935-
simulationEngine = engine
936-
simulationInputText = ""
937-
replaceSelectedPathOnCurrentTab(engine.currentStepPath)
938-
withAnimation(.easeInOut(duration: 0.18)) {
939-
isSimulationMode = true
940-
}
941-
}
942-
943-
private func endSimulationMode() {
944-
pauseSimulation()
945-
withAnimation(.easeInOut(duration: 0.18)) {
946-
isSimulationMode = false
947-
}
948-
if tabsModel.currentTab.selectedPath == nil, !document.elements.isEmpty {
949-
replaceSelectedPathOnCurrentTab([0])
950-
}
951-
}
952-
953-
private func stepSimulation() {
954-
guard simulationEngine.canStep else {
955-
pauseSimulation()
956-
return
957-
}
958-
let wasPlaying = isSimulationPlaying
959-
let lastExecutedPath = simulationEngine.currentStepPath
960-
var engine = simulationEngine
961-
engine.step()
962-
simulationEngine = engine
963-
if simulationHighlightMode == .nextToExecute, engine.canStep {
964-
replaceSelectedPathOnCurrentTab(engine.currentStepPath)
965-
} else {
966-
// lastExecuted (oder Ende/Input): letztes ausgeführtes Element bleibt markiert.
967-
replaceSelectedPathOnCurrentTab(lastExecutedPath ?? engine.currentStepPath)
968-
}
969-
// Wenn ein Input als Ergebnis des Steps angefordert wurde, Play pausieren,
970-
// aber merken, dass wir nach OK automatisch weiterlaufen sollen.
971-
if engine.state.inputRequest != nil, wasPlaying {
972-
simulationWasPlayingBeforeInput = true
973-
}
974-
if !engine.canStep {
975-
pauseSimulation()
976-
}
977-
}
978-
979-
private func resetSimulation() {
980-
pauseSimulation()
981-
var engine = VisuStructSimulationEngine(document: document)
982-
engine.reset()
983-
simulationEngine = engine
984-
simulationInputText = ""
985-
replaceSelectedPathOnCurrentTab(engine.currentStepPath)
986-
}
915+
// MARK: - Simulation (Modus & Steuerung) → `SimulationController`
987916

988-
private func submitSimulationInput() {
989-
let shouldResume = isSimulationPlaying || simulationWasPlayingBeforeInput
990-
pauseSimulation()
991-
var engine = simulationEngine
992-
engine.provideInput(simulationInputText)
993-
simulationEngine = engine
994-
if engine.state.inputRequest == nil {
995-
simulationInputText = ""
996-
}
997-
replaceSelectedPathOnCurrentTab(engine.currentStepPath)
917+
private var isSimulationMode: Bool { simulation.isActive }
998918

999-
if shouldResume, isSimulationMode, engine.canStep {
1000-
simulationWasPlayingBeforeInput = false
1001-
playSimulation()
1002-
}
1003-
}
919+
private func beginSimulationMode() { simulation.begin() }
1004920

1005-
private func playSimulation() {
1006-
guard !isSimulationPlaying, simulationEngine.canStep else { return }
1007-
isSimulationPlaying = true
1008-
simulationPlayTask?.cancel()
1009-
simulationPlayTask = Task { @MainActor in
1010-
while !Task.isCancelled && isSimulationMode && simulationEngine.canStep {
1011-
stepSimulation()
1012-
guard !Task.isCancelled, isSimulationMode, simulationEngine.canStep else { break }
1013-
try? await Task.sleep(nanoseconds: simulationStepDelay.nanoseconds)
1014-
}
1015-
if !Task.isCancelled {
1016-
isSimulationPlaying = false
1017-
simulationPlayTask = nil
1018-
}
1019-
}
1020-
}
1021-
1022-
private func pauseSimulation() {
1023-
simulationPlayTask?.cancel()
1024-
simulationPlayTask = nil
1025-
isSimulationPlaying = false
1026-
}
921+
private func endSimulationMode() { simulation.end() }
1027922

1028923
// MARK: - Tab-Titel (Platzhalter)
1029924

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import SwiftUI
2+
import Observation
3+
import VisuStructCore
4+
5+
/// Simulationsmodus: Engine, Abspielen/Pause, Eingabe. Die Markierung des aktuellen Schritts ist die Auswahl im aktuellen Reiter.
6+
@MainActor
7+
@Observable
8+
final class SimulationController {
9+
private(set) var isActive = false
10+
private(set) var engine: VisuStructSimulationEngine
11+
private(set) var isPlaying = false
12+
var inputText = ""
13+
14+
@ObservationIgnored private var playTask: Task<Void, Never>? = nil
15+
@ObservationIgnored private var wasPlayingBeforeInput = false
16+
@ObservationIgnored private let tabsModel: DiagramSheetTabsModel
17+
18+
init(tabsModel: DiagramSheetTabsModel) {
19+
self.tabsModel = tabsModel
20+
self.engine = VisuStructSimulationEngine(document: tabsModel.currentTab.document)
21+
}
22+
23+
/// Wie `@AppStorage` in den Einstellungen; bei jedem Schritt neu gelesen, damit Änderungen während des Abspielens greifen.
24+
private var stepDelay: SimulationStepDelayOption {
25+
SimulationStepDelayOption.fromStored(
26+
UserDefaults.standard.string(forKey: SimulationStepDelayOption.appStorageKey) ?? SimulationStepDelayOption.defaultRawValue
27+
)
28+
}
29+
30+
private var highlightMode: SimulationHighlightMode {
31+
SimulationHighlightMode.fromStored(
32+
UserDefaults.standard.string(forKey: SimulationHighlightMode.appStorageKey) ?? SimulationHighlightMode.defaultRawValue
33+
)
34+
}
35+
36+
private func select(_ path: [Int]?) {
37+
tabsModel.updateCurrentTab { $0.selectedPath = path }
38+
}
39+
40+
// MARK: - Modus
41+
42+
func begin() {
43+
pause()
44+
restartEngine()
45+
withAnimation(.easeInOut(duration: 0.18)) {
46+
isActive = true
47+
}
48+
}
49+
50+
func end() {
51+
pause()
52+
withAnimation(.easeInOut(duration: 0.18)) {
53+
isActive = false
54+
}
55+
let tab = tabsModel.currentTab
56+
if tab.selectedPath == nil, !tab.document.elements.isEmpty {
57+
select([0])
58+
}
59+
}
60+
61+
// MARK: - Steuerung
62+
63+
func step() {
64+
guard engine.canStep else {
65+
pause()
66+
return
67+
}
68+
let wasPlaying = isPlaying
69+
let lastExecutedPath = engine.currentStepPath
70+
var next = engine
71+
next.step()
72+
engine = next
73+
if highlightMode == .nextToExecute, next.canStep {
74+
select(next.currentStepPath)
75+
} else {
76+
// lastExecuted (oder Ende/Input): letztes ausgeführtes Element bleibt markiert.
77+
select(lastExecutedPath ?? next.currentStepPath)
78+
}
79+
// Wenn ein Input als Ergebnis des Steps angefordert wurde, Play pausieren,
80+
// aber merken, dass wir nach OK automatisch weiterlaufen sollen.
81+
if next.state.inputRequest != nil, wasPlaying {
82+
wasPlayingBeforeInput = true
83+
}
84+
if !next.canStep {
85+
pause()
86+
}
87+
}
88+
89+
func reset() {
90+
pause()
91+
restartEngine()
92+
}
93+
94+
func submitInput() {
95+
let shouldResume = isPlaying || wasPlayingBeforeInput
96+
pause()
97+
var next = engine
98+
next.provideInput(inputText)
99+
engine = next
100+
if next.state.inputRequest == nil {
101+
inputText = ""
102+
}
103+
select(next.currentStepPath)
104+
105+
if shouldResume, isActive, next.canStep {
106+
wasPlayingBeforeInput = false
107+
play()
108+
}
109+
}
110+
111+
func play() {
112+
guard !isPlaying, engine.canStep else { return }
113+
isPlaying = true
114+
playTask?.cancel()
115+
playTask = Task { @MainActor in
116+
while !Task.isCancelled && isActive && engine.canStep {
117+
step()
118+
guard !Task.isCancelled, isActive, engine.canStep else { break }
119+
try? await Task.sleep(nanoseconds: stepDelay.nanoseconds)
120+
}
121+
if !Task.isCancelled {
122+
isPlaying = false
123+
playTask = nil
124+
}
125+
}
126+
}
127+
128+
func pause() {
129+
playTask?.cancel()
130+
playTask = nil
131+
isPlaying = false
132+
}
133+
134+
/// Neue Engine für das aktuelle Dokument, Markierung auf den ersten Schritt.
135+
private func restartEngine() {
136+
var fresh = VisuStructSimulationEngine(document: tabsModel.currentTab.document)
137+
fresh.reset()
138+
engine = fresh
139+
inputText = ""
140+
select(fresh.currentStepPath)
141+
}
142+
}

0 commit comments

Comments
 (0)