forked from MSG/msg-desktop
ENH: Fixed sizing of the main window.
This commit is contained in:
parent
9caec8c99b
commit
a3a858a891
3 changed files with 144 additions and 134 deletions
|
@ -397,7 +397,7 @@
|
||||||
COMBINE_HIDPI_IMAGES = YES;
|
COMBINE_HIDPI_IMAGES = YES;
|
||||||
CURRENT_PROJECT_VERSION = 1;
|
CURRENT_PROJECT_VERSION = 1;
|
||||||
DEVELOPMENT_ASSET_PATHS = "\"MSG Desktop/Preview Content\"";
|
DEVELOPMENT_ASSET_PATHS = "\"MSG Desktop/Preview Content\"";
|
||||||
DEVELOPMENT_TEAM = C8Z9PRF4VH;
|
DEVELOPMENT_TEAM = RN94J52F92;
|
||||||
ENABLE_HARDENED_RUNTIME = YES;
|
ENABLE_HARDENED_RUNTIME = YES;
|
||||||
ENABLE_PREVIEWS = YES;
|
ENABLE_PREVIEWS = YES;
|
||||||
GENERATE_INFOPLIST_FILE = YES;
|
GENERATE_INFOPLIST_FILE = YES;
|
||||||
|
@ -424,7 +424,7 @@
|
||||||
COMBINE_HIDPI_IMAGES = YES;
|
COMBINE_HIDPI_IMAGES = YES;
|
||||||
CURRENT_PROJECT_VERSION = 1;
|
CURRENT_PROJECT_VERSION = 1;
|
||||||
DEVELOPMENT_ASSET_PATHS = "\"MSG Desktop/Preview Content\"";
|
DEVELOPMENT_ASSET_PATHS = "\"MSG Desktop/Preview Content\"";
|
||||||
DEVELOPMENT_TEAM = C8Z9PRF4VH;
|
DEVELOPMENT_TEAM = RN94J52F92;
|
||||||
ENABLE_HARDENED_RUNTIME = YES;
|
ENABLE_HARDENED_RUNTIME = YES;
|
||||||
ENABLE_PREVIEWS = YES;
|
ENABLE_PREVIEWS = YES;
|
||||||
GENERATE_INFOPLIST_FILE = YES;
|
GENERATE_INFOPLIST_FILE = YES;
|
||||||
|
|
|
@ -7,131 +7,131 @@
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
|
struct ViewHeightKey: PreferenceKey {
|
||||||
|
static var defaultValue: CGFloat = 0
|
||||||
|
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
|
||||||
|
value = nextValue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct ContentView: View {
|
struct ContentView: View {
|
||||||
@ObservedObject var appState = AppState.shared
|
@ObservedObject var appState = AppState.shared
|
||||||
private let maxLines = 300
|
private let maxLines = 300
|
||||||
@State private var showCancelButton: Bool = false
|
@State private var showCancelButton: Bool = false
|
||||||
@State private var showReinitPopup = false
|
@State private var showReinitPopup = false
|
||||||
@State private var showCancelPopup = false
|
@State private var showCancelPopup = false
|
||||||
|
@State private var imageHeight: CGFloat = 0
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack {
|
GeometryReader { geometry in
|
||||||
// Image(systemName: "globe")
|
VStack(spacing: 10) {
|
||||||
// .imageScale(.large)
|
// App icon
|
||||||
// .foregroundStyle(.tint)
|
let appIcon = NSImage(named: NSImage.applicationIconName)
|
||||||
let appIcon = NSImage(named: NSImage.applicationIconName)
|
Image(nsImage: appIcon ?? NSImage())
|
||||||
|
.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
|
||||||
|
}
|
||||||
|
|
||||||
// Use SwiftUI's Image to display the icon
|
// ScrollView fills remaining height
|
||||||
Image(nsImage: appIcon ?? NSImage())
|
ScrollViewReader { proxy in
|
||||||
.resizable() // Optional: if you want to resize the icon
|
ScrollView {
|
||||||
.scaledToFit() // Optional: makes sure the aspect ratio is maintained
|
VStack(alignment: .leading) {
|
||||||
|
TextEditor(text: $appState.output)
|
||||||
|
.font(.system(.body, design: .monospaced))
|
||||||
ScrollViewReader { proxy in
|
.frame(
|
||||||
ScrollView {
|
width: geometry.size.width - 60,
|
||||||
VStack(alignment: .leading) {
|
height: max(0, geometry.size.height - imageHeight - 120) // - rough height of button row and padding
|
||||||
TextEditor(text: $appState.output)
|
)
|
||||||
.font(.system(.body, design: .monospaced))
|
.padding()
|
||||||
.frame(minWidth: 750, minHeight: 300)
|
.disabled(true)
|
||||||
.padding()
|
|
||||||
.disabled(true)
|
Text("")
|
||||||
|
.id("bottom")
|
||||||
Text("")
|
}
|
||||||
.id("bottom")
|
}
|
||||||
|
.onChange(of: appState.output) { _, _ in
|
||||||
|
trimLinesIfNeeded()
|
||||||
|
withAnimation {
|
||||||
|
proxy.scrollTo("bottom", anchor: .bottom)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onChange(of: appState.output) { _, _ in
|
|
||||||
trimLinesIfNeeded()
|
// Buttons
|
||||||
withAnimation {
|
HStack {
|
||||||
proxy.scrollTo("bottom", anchor: .bottom)
|
Button("Init") {
|
||||||
|
DispatchQueue.main.async { self.showCancelButton = true }
|
||||||
|
runShellCommandAsync(command: "msg machine init", outputText: $appState.output, showButton: $showCancelButton)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
Button("Reinit") {
|
||||||
HStack {
|
showReinitPopup.toggle()
|
||||||
Button(action: {
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.showCancelButton = true
|
|
||||||
}
|
}
|
||||||
runShellCommandAsync(command: "msg machine init", outputText: $appState.output, showButton: $showCancelButton)
|
.confirmationDialog("Are you sure?", isPresented: $showReinitPopup, titleVisibility: .visible) {
|
||||||
|
Button("Confirm", role: .destructive) {
|
||||||
}) {
|
|
||||||
Text("Init")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Button(action: {
|
|
||||||
showReinitPopup.toggle()
|
|
||||||
}) {
|
|
||||||
Text("Reinit")
|
|
||||||
}.confirmationDialog("Are you sure?", isPresented: $showReinitPopup, titleVisibility: .visible) {
|
|
||||||
Button("Confirm", role: .destructive) {
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.showCancelButton = true
|
self.showCancelButton = true
|
||||||
runShellCommandAsync(command: "rm -rf ~/.guix && msg machine init", outputText: $appState.output, showButton: $showCancelButton)
|
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")
|
|
||||||
}
|
|
||||||
if showCancelButton {
|
|
||||||
Button(action: {
|
|
||||||
showCancelPopup.toggle()
|
|
||||||
}) {
|
|
||||||
Text("Cancel")
|
|
||||||
}.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) {}
|
Button("Cancel", role: .cancel) {}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
Button("Start") {
|
||||||
|
runShellCommandAsync(command: "msg machine start", outputText: $appState.output, showButton: nil)
|
||||||
if !showCancelButton{
|
}
|
||||||
Button(action: {
|
|
||||||
runShellCommandAsync(command: "msg machine stop", outputText: $appState.output, showButton: nil) }) {
|
if showCancelButton {
|
||||||
Text("Stop")
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Button("Cancel", role: .cancel) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
.padding()
|
||||||
|
.frame(maxHeight: .infinity)
|
||||||
|
// Removed fixed frame here to allow dynamic window sizing
|
||||||
}
|
}
|
||||||
.padding()
|
|
||||||
.frame(width: 800, height: 400)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private func trimLinesIfNeeded() {
|
private func trimLinesIfNeeded() {
|
||||||
let lines = AppState.shared.output.split(separator: "\n")
|
let lines = AppState.shared.output.split(separator: "\n")
|
||||||
|
if lines.count > maxLines {
|
||||||
if lines.count > maxLines {
|
let trimmedLines = lines.suffix(maxLines)
|
||||||
_ = lines.count - maxLines
|
AppState.shared.output = trimmedLines.joined(separator: "\n")
|
||||||
let trimmedLines = lines.suffix(maxLines)
|
|
||||||
AppState.shared.output = trimmedLines.joined(separator: "\n")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
|
|
|
@ -59,12 +59,14 @@ func runShellCommandAsync(command: String, outputText: Binding<String>?, showBut
|
||||||
}
|
}
|
||||||
|
|
||||||
class AppDelegate: NSObject, NSApplicationDelegate {
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
||||||
|
var window: NSWindow!
|
||||||
|
|
||||||
func applicationWillTerminate(_ notification: Notification) {
|
func applicationWillTerminate(_ notification: Notification) {
|
||||||
runShellCommandAsync(command: "msg machine stop", outputText: nil, showButton: nil)
|
runShellCommandAsync(command: "msg machine stop", outputText: nil, showButton: nil)
|
||||||
runShellCommandAsync(command: "kill $(ps aux | grep '[g]uile' | awk '{print $2}')", outputText: nil, showButton: nil)
|
runShellCommandAsync(command: "kill $(ps aux | grep '[g]uile' | awk '{print $2}')", outputText: nil, showButton: nil)
|
||||||
runShellCommandAsync(command: "kill $(ps aux | grep '[q]emu' | awk '{print $2}')", outputText: nil, showButton: nil)
|
runShellCommandAsync(command: "kill $(ps aux | grep '[q]emu' | awk '{print $2}')", outputText: nil, showButton: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveAppState() {
|
func saveAppState() {
|
||||||
print("App is terminating. Saving state...")
|
print("App is terminating. Saving state...")
|
||||||
UserDefaults.standard.set(Date(), forKey: "lastClosed")
|
UserDefaults.standard.set(Date(), forKey: "lastClosed")
|
||||||
|
@ -73,44 +75,54 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||||
var statusBarItem: NSStatusItem!
|
var statusBarItem: NSStatusItem!
|
||||||
|
|
||||||
func applicationDidFinishLaunching(_ notification: Notification) {
|
func applicationDidFinishLaunching(_ notification: Notification) {
|
||||||
statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
|
statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
|
||||||
|
|
||||||
if let button = statusBarItem.button {
|
let contentView = ContentView()
|
||||||
button.title = "MSG"
|
window = NSWindow(
|
||||||
button.toolTip = "MSG App"
|
contentRect: NSRect(x: 0, y: 0, width: 800, height: 600),
|
||||||
}
|
styleMask: [.titled, .closable, .resizable, .miniaturizable],
|
||||||
|
backing: .buffered,
|
||||||
let menu = NSMenu()
|
defer: false
|
||||||
|
)
|
||||||
menu.addItem(NSMenuItem(title: "Quit", action: #selector(quitApp), keyEquivalent: "q"))
|
window.center()
|
||||||
|
//window.setFrameAutosaveName("MSG App") // to retain size after quitting
|
||||||
statusBarItem.menu = menu
|
window.contentView = NSHostingView(rootView: contentView)
|
||||||
|
window.makeKeyAndOrderFront(nil)
|
||||||
|
|
||||||
|
if let button = statusBarItem.button {
|
||||||
|
button.title = "MSG"
|
||||||
|
button.toolTip = "MSG App"
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc func quitApp() {
|
let menu = NSMenu()
|
||||||
let alert = NSAlert()
|
|
||||||
alert.messageText = "Are you sure you want to quit?"
|
menu.addItem(NSMenuItem(title: "Quit", action: #selector(quitApp), keyEquivalent: "q"))
|
||||||
alert.informativeText = "Do you want to quit the app and exit?"
|
|
||||||
alert.alertStyle = .warning
|
statusBarItem.menu = menu
|
||||||
|
}
|
||||||
alert.addButton(withTitle: "Quit")
|
|
||||||
alert.addButton(withTitle: "Cancel")
|
@objc func quitApp() {
|
||||||
|
let alert = NSAlert()
|
||||||
let response = alert.runModal()
|
alert.messageText = "Are you sure you want to quit?"
|
||||||
|
alert.informativeText = "Do you want to quit the app and exit?"
|
||||||
if response == .alertFirstButtonReturn {
|
alert.alertStyle = .warning
|
||||||
NSApplication.shared.terminate(nil)
|
|
||||||
}
|
alert.addButton(withTitle: "Quit")
|
||||||
|
alert.addButton(withTitle: "Cancel")
|
||||||
|
|
||||||
|
let response = alert.runModal()
|
||||||
|
|
||||||
|
if response == .alertFirstButtonReturn {
|
||||||
|
NSApplication.shared.terminate(nil)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class AppState: ObservableObject {
|
class AppState: ObservableObject {
|
||||||
@Published var output: String = "Please select an option \n"
|
@Published var output: String = "Please select an option \n"
|
||||||
|
|
||||||
static let shared = AppState()
|
static let shared = AppState()
|
||||||
}
|
}
|
||||||
|
|
||||||
@main
|
@main
|
||||||
|
@ -118,8 +130,6 @@ struct MSG_DesktopApp: App {
|
||||||
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
|
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
|
||||||
|
|
||||||
var body: some Scene {
|
var body: some Scene {
|
||||||
WindowGroup {
|
Settings {} // prevents crash on some macOS version
|
||||||
ContentView()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue