msg-desktop/MSG Desktop/ContentView.swift

140 lines
5.2 KiB
Swift
Raw Permalink Normal View History

2025-03-08 07:20:19 -07:00
//
// ContentView.swift
// MSG Desktop
//
// Created by Chad Nelson on 3/8/25.
//
import SwiftUI
struct ContentView: View {
2025-03-09 07:27:37 -06:00
@ObservedObject var appState = AppState.shared
2025-03-08 13:36:53 -07:00
private let maxLines = 300
2025-03-09 07:27:37 -06:00
@State private var showCancelButton: Bool = false
@State private var showReinitPopup = false
@State private var showCancelPopup = false
2025-03-08 07:20:19 -07:00
var body: some View {
VStack {
2025-03-09 07:27:37 -06:00
// Image(systemName: "globe")
// .imageScale(.large)
// .foregroundStyle(.tint)
let appIcon = NSImage(named: NSImage.applicationIconName)
// Use SwiftUI's Image to display the icon
Image(nsImage: appIcon ?? NSImage())
.resizable() // Optional: if you want to resize the icon
.scaledToFit() // Optional: makes sure the aspect ratio is maintained
2025-03-08 13:36:53 -07:00
ScrollViewReader { proxy in
ScrollView {
VStack(alignment: .leading) {
2025-03-09 07:27:37 -06:00
TextEditor(text: $appState.output)
2025-03-08 13:36:53 -07:00
.font(.system(.body, design: .monospaced))
.frame(minWidth: 750, minHeight: 300)
.padding()
.disabled(true)
Text("")
.id("bottom")
}
}
2025-03-09 07:27:37 -06:00
.onChange(of: appState.output) { _, _ in
2025-03-08 13:36:53 -07:00
trimLinesIfNeeded()
withAnimation {
proxy.scrollTo("bottom", anchor: .bottom)
}
}
}
HStack {
Button(action: {
DispatchQueue.main.async {
self.showCancelButton = true
}
2025-03-09 07:27:37 -06:00
runShellCommandAsync(command: "msg machine init", outputText: $appState.output, showButton: $showCancelButton)
2025-03-08 13:36:53 -07:00
}) {
Text("Init")
}
2025-03-09 07:27:37 -06:00
2025-03-08 13:36:53 -07:00
Button(action: {
2025-03-09 07:27:37 -06:00
showReinitPopup.toggle()
}) {
2025-03-08 13:36:53 -07:00
Text("Reinit")
2025-03-09 07:27:37 -06:00
}.confirmationDialog("Are you sure?", isPresented: $showReinitPopup, titleVisibility: .visible) {
Button("Confirm", role: .destructive) {
DispatchQueue.main.async {
self.showCancelButton = true
runShellCommandAsync(command: "rm -rf ~/.guix && msg machine init", outputText: $appState.output, showButton: $showCancelButton)
}
}
Button("Cancel", role: .cancel) {}
2025-03-08 13:36:53 -07:00
}
2025-03-09 07:27:37 -06:00
2025-03-08 13:36:53 -07:00
Button(action: {
2025-03-09 07:27:37 -06:00
runShellCommandAsync(command: "msg machine start", outputText: $appState.output, showButton: nil) }) {
2025-03-08 13:36:53 -07:00
Text("Start")
}
if showCancelButton {
Button(action: {
2025-03-09 07:27:37 -06:00
showCancelPopup.toggle()
}) {
2025-03-08 13:36:53 -07:00
Text("Cancel")
2025-03-09 07:27:37 -06:00
}.confirmationDialog("Are you sure?", isPresented: $showCancelPopup, titleVisibility: .visible) {
Button("Confirm", role: .destructive) {
runShellCommandAsync(command: "kill $(ps aux | grep '[g]uile' | awk '{print $2}')", outputText: $appState.output, showButton: nil)
runShellCommandAsync(command: "kill $(ps aux | grep '[q]emu' | awk '{print $2}')", outputText: $appState.output, showButton: nil)
DispatchQueue.main.async {
self.showCancelButton = false
}
}
Button("Cancel", role: .cancel) {}
2025-03-08 13:36:53 -07:00
}
}
2025-03-09 07:27:37 -06:00
2025-03-08 13:36:53 -07:00
if !showCancelButton{
Button(action: {
2025-03-09 07:27:37 -06:00
runShellCommandAsync(command: "msg machine stop", outputText: $appState.output, showButton: nil) }) {
2025-03-08 13:36:53 -07:00
Text("Stop")
}
}
2025-03-09 07:27:37 -06:00
2025-03-08 13:36:53 -07:00
Button(action: {
2025-03-09 07:27:37 -06:00
runShellCommandAsync(command: "osascript -e 'tell application \"Terminal\" to do script \"msg shell\"' -e 'tell application \"Terminal\" to activate'", outputText: $appState.output, showButton: nil)
2025-03-08 13:36:53 -07:00
}) {
Text("Shell")
}
}
2025-03-08 07:20:19 -07:00
}
.padding()
2025-03-08 13:36:53 -07:00
.frame(width: 800, height: 400)
2025-03-08 07:20:19 -07:00
}
2025-03-08 13:36:53 -07:00
private func trimLinesIfNeeded() {
2025-03-09 07:27:37 -06:00
let lines = AppState.shared.output.split(separator: "\n")
2025-03-08 13:36:53 -07:00
if lines.count > maxLines {
_ = lines.count - maxLines
2025-03-09 07:27:37 -06:00
let trimmedLines = lines.suffix(maxLines)
AppState.shared.output = trimmedLines.joined(separator: "\n")
2025-03-08 13:36:53 -07:00
}
}
2025-03-08 07:20:19 -07:00
}
#Preview {
ContentView()
}