msg-desktop/MSG Desktop/ContentView.swift

172 lines
7.6 KiB
Swift
Raw 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
2025-03-29 16:19:53 +00:00
struct ViewHeightKey: PreferenceKey {
static var defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = nextValue()
}
}
2025-03-08 07:20:19 -07:00
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-29 16:19:53 +00:00
@State private var imageHeight: CGFloat = 0
2025-03-09 07:27:37 -06:00
2025-03-08 07:20:19 -07:00
var body: some View {
2025-03-29 16:19:53 +00:00
GeometryReader { geometry in
VStack(spacing: 10) {
// App icon
let appIcon = NSImage(named: NSImage.applicationIconName)
Image(nsImage: appIcon ?? NSImage())
. resizable()
2025-03-29 16:19:53 +00:00
.scaledToFit()
.frame(maxHeight: 150)
.background(
GeometryReader { proxy in
Color.clear
.preference(key: ViewHeightKey.self, value: proxy.size.height)
}
)
.onPreferenceChange(ViewHeightKey.self) { height in
self.imageHeight = height
}
ZStack {
Color.black
.ignoresSafeArea()
// ScrollView fills remaining height
ScrollViewReader { proxy in
ScrollView {
LazyVStack(alignment: .leading, spacing: 0) {
let lines = appState.output.components(separatedBy: .newlines)
ForEach(lines.indices, id: \.self) { index in
Text(lines[index])
.font(.system(.body, design: .monospaced))
.foregroundColor(.gray)
.frame(maxWidth: .infinity, alignment: .leading)
}
Color.clear.frame(height: 1).id("bottom")
}
.padding()
2025-03-29 16:19:53 +00:00
}
.onChange(of: appState.output) { _, _ in
trimLinesIfNeeded()
withAnimation {
proxy.scrollTo("bottom", anchor: .bottom)
}
2025-03-29 16:19:53 +00:00
}
// ScrollView {
// VStack(alignment: .leading) {
// TextEditor(text: $appState.output)
// .font(.system(.body, design: .monospaced))
// .frame(
// width: geometry.size.width - 60,
// height: max(0, geometry.size.height - imageHeight - 120) // - rough height of button row and padding
// )
// .padding()
// .disabled(true)
//
// Text("")
// .id("bottom")
// }
// }
// .onChange(of: appState.output) { _, _ in
// trimLinesIfNeeded()
// withAnimation {
// proxy.scrollTo("bottom", anchor: .bottom)
// }
// }
2025-03-08 13:36:53 -07:00
}
}
2025-03-29 16:19:53 +00:00
// Buttons
HStack {
Button("Init") {
DispatchQueue.main.async { self.showCancelButton = true }
runShellCommandAsync(command: "msg machine init", outputText: $appState.output, showButton: $showCancelButton)
2025-03-08 13:36:53 -07:00
}
2025-03-29 16:19:53 +00:00
Button("Reinit") {
showReinitPopup.toggle()
}
.confirmationDialog("Are you sure?", isPresented: $showReinitPopup, titleVisibility: .visible) {
Button("Confirm", role: .destructive) {
2025-03-09 07:27:37 -06:00
self.showCancelButton = true
runShellCommandAsync(command: "rm -rf ~/.guix && msg machine init", outputText: $appState.output, showButton: $showCancelButton)
}
2025-03-29 16:19:53 +00:00
Button("Cancel", role: .cancel) {}
2025-03-09 07:27:37 -06:00
}
2025-03-29 16:19:53 +00:00
Button("Start") {
runShellCommandAsync(command: "msg machine start", outputText: $appState.output, showButton: nil)
}
if showCancelButton {
Button("Cancel") {
showCancelPopup.toggle()
}
.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
}
2025-03-09 07:27:37 -06:00
}
2025-03-29 16:19:53 +00:00
Button("Cancel", role: .cancel) {}
2025-03-09 07:27:37 -06:00
}
2025-03-08 13:36:53 -07:00
}
2025-03-29 16:19:53 +00:00
if !showCancelButton {
Button("Stop") {
runShellCommandAsync(command: "msg machine stop", outputText: $appState.output, showButton: nil)
}
}
Button("Shell") {
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
}
}
}
2025-03-29 16:19:53 +00:00
.padding()
.frame(maxHeight: .infinity)
.onAppear {
// Silences error due to metallib on resizing of window
let device = MTLCreateSystemDefaultDevice()
_ = device?.makeDefaultLibrary()
}
2025-03-08 07:20:19 -07:00
}
}
2025-03-08 13:36:53 -07:00
private func trimLinesIfNeeded() {
2025-03-29 16:19:53 +00:00
let lines = AppState.shared.output.split(separator: "\n")
if lines.count > maxLines {
let trimmedLines = lines.suffix(maxLines)
AppState.shared.output = trimmedLines.joined(separator: "\n")
2025-03-08 13:36:53 -07:00
}
2025-03-29 16:19:53 +00:00
}
2025-03-08 07:20:19 -07:00
}
#Preview {
ContentView()
}