Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic open/save functionality #331

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Boop/Boop/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Boop/Boop/Boop.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<true/>
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
Expand Down
2 changes: 1 addition & 1 deletion Boop/Boop/BoopRelease.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<true/>
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
Expand Down
57 changes: 54 additions & 3 deletions Boop/Boop/Controllers/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,76 @@ 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 openFile(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 pathUrl = dialog.url { // Pathname of the file
let text=try? String(contentsOf: pathUrl)
setText(text ?? "Failed to load file '\(pathUrl.path)'.")
}
}

}

@IBAction func saveFileAs(sender: AnyObject) {

let dialog = NSSavePanel();

dialog.title = "Save content as…";
dialog.showsResizeIndicator = true;
dialog.showsHiddenFiles = false;
dialog.showsTagField = false;
dialog.canCreateDirectories = true;
dialog.nameFieldStringValue = "Untitled.txt"

if (dialog.runModal() == NSApplication.ModalResponse.OK) {
if let pathUrl = dialog.url { // Pathname of the file
let textView = editorView.contentTextView
let textData=textView.textStorage?.string.data(using: .utf8)
do {
try textData?.write(to: pathUrl)
} catch let error as NSError {
setText("Failed to save content to file '\(pathUrl.path)'. (Reason: \(error.localizedFailureReason!))")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad move ;)

it replaces my "important" text and its deleted :/

// TODO: Show error in alert box
}
}
}

}

}

extension MainViewController: SyntaxTextViewDelegate {
Expand Down
11 changes: 11 additions & 0 deletions Boop/Boop/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,16 @@
</array>
</dict>
</array>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleTypeExtensions</key>
<array>
<string>*</string>
</array>
</dict>
</array>
</dict>
</plist>
22 changes: 12 additions & 10 deletions Boop/UI/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<connections>
<outlet property="closePickerMenuItem" destination="KYm-JI-9g0" id="aZo-Hy-RkG"/>
<outlet property="editor" destination="Ef0-Na-xwu" id="rks-Mp-ffh"/>
<outlet property="mainViewController" destination="unv-pE-gme" id="SJf-P6-FHE"/>
<outlet property="openPickerMenuItem" destination="8ka-cp-srR" id="qZt-zg-2wc"/>
<outlet property="popoverViewController" destination="ugb-uZ-HP1" id="gLb-EK-85g"/>
<outlet property="scriptManager" destination="jdF-I6-2al" id="o2F-Lj-Vou"/>
Expand Down Expand Up @@ -128,9 +129,9 @@
<action selector="clear:" target="unv-pE-gme" id="Mj1-40-053"/>
</connections>
</menuItem>
<menuItem title="Open…" hidden="YES" keyEquivalent="o" id="IAo-SY-fd9">
<menuItem title="Open…" keyEquivalent="o" id="IAo-SY-fd9">
<connections>
<action selector="openDocument:" target="-1" id="bVn-NM-KNZ"/>
<action selector="openFileWithSender:" target="unv-pE-gme" id="l3F-jU-eYg"/>
</connections>
</menuItem>
<menuItem title="Open Recent" hidden="YES" id="tXI-mr-wws">
Expand All @@ -147,19 +148,15 @@
</menu>
</menuItem>
<menuItem isSeparatorItem="YES" id="m54-Is-iLE"/>
<menuItem title="Close" keyEquivalent="w" id="DVo-aG-piG">
<connections>
<action selector="performClose:" target="-1" id="HmO-Ls-i7Q"/>
</connections>
</menuItem>
<menuItem title="Save…" hidden="YES" keyEquivalent="s" id="pxx-59-PXV">
<menuItem title="Save…" hidden="YES" id="pxx-59-PXV">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="saveDocument:" target="-1" id="teZ-XB-qJY"/>
</connections>
</menuItem>
<menuItem title="Save As…" hidden="YES" keyEquivalent="S" id="Bw7-FT-i3A">
<menuItem title="Save As…" keyEquivalent="s" id="Bw7-FT-i3A">
<connections>
<action selector="saveDocumentAs:" target="-1" id="mDf-zr-I0C"/>
<action selector="saveFileAsSender:" target="unv-pE-gme" id="vLF-0s-Nt1"/>
</connections>
</menuItem>
<menuItem title="Revert to Saved" hidden="YES" keyEquivalent="r" id="KaW-ft-85H">
Expand All @@ -179,6 +176,11 @@
<action selector="print:" target="-1" id="qaZ-4w-aoO"/>
</connections>
</menuItem>
<menuItem title="Close" keyEquivalent="w" id="DVo-aG-piG">
<connections>
<action selector="performClose:" target="-1" id="HmO-Ls-i7Q"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
Expand Down