ENH: Fixed sizing of the main window.

This commit is contained in:
Etienne Roesch 2025-03-29 16:19:53 +00:00
parent 9caec8c99b
commit a3a858a891
3 changed files with 144 additions and 134 deletions

View file

@ -397,7 +397,7 @@
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"MSG Desktop/Preview Content\"";
DEVELOPMENT_TEAM = C8Z9PRF4VH;
DEVELOPMENT_TEAM = RN94J52F92;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
@ -424,7 +424,7 @@
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"MSG Desktop/Preview Content\"";
DEVELOPMENT_TEAM = C8Z9PRF4VH;
DEVELOPMENT_TEAM = RN94J52F92;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;

View file

@ -7,32 +7,50 @@
import SwiftUI
struct ViewHeightKey: PreferenceKey {
static var defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = nextValue()
}
}
struct ContentView: View {
@ObservedObject var appState = AppState.shared
private let maxLines = 300
@State private var showCancelButton: Bool = false
@State private var showReinitPopup = false
@State private var showCancelPopup = false
@State private var imageHeight: CGFloat = 0
var body: some View {
VStack {
// Image(systemName: "globe")
// .imageScale(.large)
// .foregroundStyle(.tint)
GeometryReader { geometry in
VStack(spacing: 10) {
// App icon
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
.resizable()
.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
}
// ScrollView fills remaining height
ScrollViewReader { proxy in
ScrollView {
VStack(alignment: .leading) {
TextEditor(text: $appState.output)
.font(.system(.body, design: .monospaced))
.frame(minWidth: 750, minHeight: 300)
.frame(
width: geometry.size.width - 60,
height: max(0, geometry.size.height - imageHeight - 120) // - rough height of button row and padding
)
.padding()
.disabled(true)
@ -47,51 +65,38 @@ struct ContentView: View {
}
}
}
// Buttons
HStack {
Button(action: {
DispatchQueue.main.async {
self.showCancelButton = true
}
Button("Init") {
DispatchQueue.main.async { self.showCancelButton = true }
runShellCommandAsync(command: "msg machine init", outputText: $appState.output, showButton: $showCancelButton)
}) {
Text("Init")
}
Button(action: {
Button("Reinit") {
showReinitPopup.toggle()
}) {
Text("Reinit")
}.confirmationDialog("Are you sure?", isPresented: $showReinitPopup, titleVisibility: .visible) {
}
.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) {}
}
Button(action: {
runShellCommandAsync(command: "msg machine start", outputText: $appState.output, showButton: nil) }) {
Text("Start")
Button("Start") {
runShellCommandAsync(command: "msg machine start", outputText: $appState.output, showButton: nil)
}
if showCancelButton {
Button(action: {
showCancelPopup.toggle()
}) {
Text("Cancel")
}.confirmationDialog("Are you sure?", isPresented: $showCancelPopup, titleVisibility: .visible) {
Button("Confirm", role: .destructive) {
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
}
}
@ -99,35 +104,30 @@ struct ContentView: View {
}
}
if !showCancelButton {
Button(action: {
runShellCommandAsync(command: "msg machine stop", outputText: $appState.output, showButton: nil) }) {
Text("Stop")
Button("Stop") {
runShellCommandAsync(command: "msg machine stop", outputText: $appState.output, showButton: nil)
}
}
Button(action: {
runShellCommandAsync(command: "osascript -e 'tell application \"Terminal\" to do script \"msg shell\"' -e 'tell application \"Terminal\" to activate'", outputText: $appState.output, showButton: nil)
}) {
Text("Shell")
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
)
}
}
}
.padding()
.frame(width: 800, height: 400)
.frame(maxHeight: .infinity)
// Removed fixed frame here to allow dynamic window sizing
}
}
private func trimLinesIfNeeded() {
let lines = AppState.shared.output.split(separator: "\n")
if lines.count > maxLines {
_ = lines.count - maxLines
let trimmedLines = lines.suffix(maxLines)
AppState.shared.output = trimmedLines.joined(separator: "\n")
}

View file

@ -59,6 +59,8 @@ func runShellCommandAsync(command: String, outputText: Binding<String>?, showBut
}
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow!
func applicationWillTerminate(_ notification: Notification) {
runShellCommandAsync(command: "msg machine stop", outputText: nil, showButton: nil)
runShellCommandAsync(command: "kill $(ps aux | grep '[g]uile' | awk '{print $2}')", outputText: nil, showButton: nil)
@ -75,6 +77,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
let contentView = ContentView()
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 800, height: 600),
styleMask: [.titled, .closable, .resizable, .miniaturizable],
backing: .buffered,
defer: false
)
window.center()
//window.setFrameAutosaveName("MSG App") // to retain size after quitting
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
if let button = statusBarItem.button {
button.title = "MSG"
button.toolTip = "MSG App"
@ -105,8 +119,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
class AppState: ObservableObject {
@Published var output: String = "Please select an option \n"
@ -118,8 +130,6 @@ struct MSG_DesktopApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
Settings {} // prevents crash on some macOS version
}
}