From 349d9b65a54f02179e6e2fc15a09720ebcf948a5 Mon Sep 17 00:00:00 2001 From: Christof Rath Date: Fri, 22 Apr 2022 22:02:18 +0200 Subject: [PATCH 1/2] Add file open and drag on app icon functionality --- Boop/Boop/AppDelegate.swift | 8 +++++ .../Boop/Controllers/MainViewController.swift | 32 +++++++++++++++++-- Boop/Boop/Info.plist | 11 +++++++ Boop/UI/Base.lproj/MainMenu.xib | 5 +-- 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/Boop/Boop/AppDelegate.swift b/Boop/Boop/AppDelegate.swift index bff23474..5786418e 100644 --- a/Boop/Boop/AppDelegate.swift +++ b/Boop/Boop/AppDelegate.swift @@ -17,6 +17,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var closePickerMenuItem: NSMenuItem! @IBOutlet weak var popoverViewController: PopoverViewController! + @IBOutlet weak var mainViewController: MainViewController! @IBOutlet weak var scriptManager: ScriptManager! @IBOutlet weak var editor: SyntaxTextView! @@ -42,6 +43,13 @@ class AppDelegate: NSObject, NSApplicationDelegate { return true } + func application(_ sender: NSApplication, openFile filename: String) -> Bool { + + let text=try? String(contentsOf: URL(fileURLWithPath: filename)) + mainViewController.setText(text ?? "Failed to load: " + filename) + return true + } + @IBAction func showPreferencesWindow(_ sender: NSMenuItem) { let controller = NSStoryboard.init(name: "Preferences", bundle: nil).instantiateInitialController() as? NSWindowController diff --git a/Boop/Boop/Controllers/MainViewController.swift b/Boop/Boop/Controllers/MainViewController.swift index 762b7f49..d3be3816 100644 --- a/Boop/Boop/Controllers/MainViewController.swift +++ b/Boop/Boop/Controllers/MainViewController.swift @@ -48,25 +48,51 @@ class MainViewController: NSViewController { } @IBAction func clear(_ sender: Any) { + setText("") + } + + public func setText(_ text: String) { let textView = editorView.contentTextView textView.textStorage?.beginEditing() let range = NSRange(location: 0, length: textView.textStorage?.length ?? textView.string.count) - guard textView.shouldChangeText(in: range, replacementString: "") else { + guard textView.shouldChangeText(in: range, replacementString: text) else { return } - textView.textStorage?.replaceCharacters(in: range, with: "") + textView.textStorage?.replaceCharacters(in: range, with: text) textView.textStorage?.endEditing() textView.didChangeText() } - + @IBAction func checkForUpdates(_ sender: Any) { updateBuddy.check() } + + @IBAction func browseFile(sender: AnyObject) { + + let dialog = NSOpenPanel(); + + dialog.title = "Choose a file"; + dialog.showsResizeIndicator = true; + dialog.showsHiddenFiles = false; + dialog.canChooseDirectories = false; + dialog.canCreateDirectories = false; + dialog.allowsMultipleSelection = false; + + if (dialog.runModal() == NSApplication.ModalResponse.OK) { + if let result = dialog.url { // Pathname of the file + let path = result.path + let text=try? String(contentsOf: URL(fileURLWithPath: path)) + setText(text ?? "Failed to load: " + path) + } + } + + } + } extension MainViewController: SyntaxTextViewDelegate { diff --git a/Boop/Boop/Info.plist b/Boop/Boop/Info.plist index 5753c31a..72579964 100644 --- a/Boop/Boop/Info.plist +++ b/Boop/Boop/Info.plist @@ -53,5 +53,16 @@ + CFBundleDocumentTypes + + + CFBundleTypeRole + Viewer + CFBundleTypeExtensions + + * + + + diff --git a/Boop/UI/Base.lproj/MainMenu.xib b/Boop/UI/Base.lproj/MainMenu.xib index f132c070..be04d0d4 100644 --- a/Boop/UI/Base.lproj/MainMenu.xib +++ b/Boop/UI/Base.lproj/MainMenu.xib @@ -55,6 +55,7 @@ + @@ -128,9 +129,9 @@ -