summaryrefslogtreecommitdiff
path: root/Sources/BubombUI/Settings/GeneralSettingsView.swift
blob: 4f5948dd687a048014b6e0b0b1d000ee85fe5040 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import SwiftUI
import BubombCore
#if canImport(ServiceManagement)
import ServiceManagement
#endif

/// General application preferences including login item, animations, and safety toggles.
@MainActor
public struct GeneralSettingsView: View {
    @ObservedObject var settingsStore: SettingsStore
    @State private var showResetConfirmation: Bool = false

    public init(settingsStore: SettingsStore = .shared) {
        self.settingsStore = settingsStore
    }

    public var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 18) {
                appIdentitySection
                systemIntegrationSection
                animationPreferenceSection
                safetySection
                dangerZoneSection
            }
            .padding(18)
        }
        .background(BubombTheme.windowBackground)
        .alert("Reset All Settings?", isPresented: $showResetConfirmation) {
            Button("Cancel", role: .cancel) {}
            Button("Reset Everything", role: .destructive) {
                settingsStore.resetToDefaults()
            }
        } message: {
            Text("This will remove your configured payload, execution history, and restore factory defaults.")
        }
    }

    // MARK: - Sections

    private var appIdentitySection: some View {
        HStack(spacing: 16) {
            AnimatedBombView(size: 56)

            VStack(alignment: .leading, spacing: 3) {
                Text("Bubomb")
                    .font(.title2.bold())
                    .foregroundColor(BubombTheme.textPrimary)

                Text("Configure the boom.")
                    .font(.subheadline)
                    .foregroundColor(BubombTheme.accentOrange)

                Text("Version \(AppSettings.currentVersion) (Build \(AppSettings.buildNumber)) • Native macOS")
                    .font(.caption2)
                    .foregroundColor(BubombTheme.textMuted)
            }
            Spacer()
        }
        .bubombCard()
    }

    private var systemIntegrationSection: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("System & Startup")
                .font(.subheadline.bold())
                .foregroundColor(BubombTheme.accentOrange)

            Toggle("Launch Bubomb at Login", isOn: Binding(
                get: { settingsStore.settings.launchAtLogin },
                set: { newValue in
                    var s = settingsStore.settings
                    s.launchAtLogin = newValue
                    settingsStore.update(s)
                    toggleLaunchAtLogin(newValue)
                }
            ))
            .font(.subheadline)
            .foregroundColor(BubombTheme.textPrimary)

            Text("Automatically arms your menu bar bomb when your Mac starts.")
                .font(.caption)
                .foregroundColor(BubombTheme.textSecondary)

            Divider().background(BubombTheme.borderSubtle)

            Toggle("Start Silently in Menu Bar", isOn: Binding(
                get: { settingsStore.settings.startSilentlyInMenuBar },
                set: { newValue in
                    var s = settingsStore.settings
                    s.startSilentlyInMenuBar = newValue
                    settingsStore.update(s)
                }
            ))
            .font(.subheadline)
            .foregroundColor(BubombTheme.textPrimary)

            Text("When enabled, Bubomb starts without opening the Settings window.")
                .font(.caption)
                .foregroundColor(BubombTheme.textSecondary)
        }
        .bubombCard()
    }

    private var animationPreferenceSection: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("Visuals & Feedback")
                .font(.subheadline.bold())
                .foregroundColor(BubombTheme.accentOrange)

            Text("Menu Bar Animation Style:")
                .font(.caption)
                .foregroundColor(BubombTheme.textSecondary)

            Picker("", selection: Binding(
                get: { settingsStore.settings.animationPreference },
                set: { newValue in
                    var s = settingsStore.settings
                    s.animationPreference = newValue
                    settingsStore.update(s)
                }
            )) {
                ForEach(AnimationPreference.allCases, id: \.self) { pref in
                    Text(pref.rawValue).tag(pref)
                }
            }
            .labelsHidden()

            Divider().background(BubombTheme.borderSubtle)

            Toggle("Play subtle completion sound / haptic feedback", isOn: Binding(
                get: { settingsStore.settings.playCompletionFeedback },
                set: { newValue in
                    var s = settingsStore.settings
                    s.playCompletionFeedback = newValue
                    settingsStore.update(s)
                }
            ))
            .font(.subheadline)
            .foregroundColor(BubombTheme.textPrimary)
        }
        .bubombCard()
    }

    private var safetySection: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("Safety & Protection")
                .font(.subheadline.bold())
                .foregroundColor(BubombTheme.accentOrange)

            Toggle("Require explicit confirmation for destructive actions", isOn: Binding(
                get: { settingsStore.settings.confirmDestructiveActions },
                set: { newValue in
                    var s = settingsStore.settings
                    s.confirmDestructiveActions = newValue
                    settingsStore.update(s)
                }
            ))
            .font(.subheadline)
            .foregroundColor(BubombTheme.textPrimary)

            Text("Guards against accidental execution of destructive patterns (e.g. recursive removals, raw disk writes).")
                .font(.caption)
                .foregroundColor(BubombTheme.textSecondary)
        }
        .bubombCard()
    }

    private var dangerZoneSection: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("Reset")
                .font(.subheadline.bold())
                .foregroundColor(BubombTheme.failureRed)

            HStack {
                VStack(alignment: .leading, spacing: 2) {
                    Text("Reset Bubomb Settings")
                        .font(.subheadline)
                        .foregroundColor(BubombTheme.textPrimary)
                    Text("Restores all settings, clears history, and unarms active payload.")
                        .font(.caption)
                        .foregroundColor(BubombTheme.textSecondary)
                }
                Spacer()
                Button("Reset Defaults...") {
                    showResetConfirmation = true
                }
                .buttonStyle(.bordered)
                .tint(BubombTheme.failureRed)
            }
        }
        .bubombCard()
    }

    // MARK: - Helper

    private func toggleLaunchAtLogin(_ enable: Bool) {
        #if canImport(ServiceManagement)
        if #available(macOS 13.0, *) {
            do {
                if enable {
                    if SMAppService.mainApp.status != .enabled {
                        try SMAppService.mainApp.register()
                    }
                } else {
                    if SMAppService.mainApp.status == .enabled {
                        try SMAppService.mainApp.unregister()
                    }
                }
            } catch {
                // If service management registration fails in dev mode, non-fatal
                print("Launch at login registration notice: \(error)")
            }
        }
        #endif
    }
}