Skip to content

Commit b8eb57e

Browse files
committed
Basic open/save functionality IvanMathy#331
Port nobecutan's changes to the latest version of Boop and then make the errors show in the status view.
1 parent 5cf6914 commit b8eb57e

File tree

7 files changed

+100
-19
lines changed

7 files changed

+100
-19
lines changed

Boop/Boop/AppDelegate.swift

+14
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
1717
@IBOutlet weak var closePickerMenuItem: NSMenuItem!
1818

1919
@IBOutlet weak var popoverViewController: PopoverViewController!
20+
@IBOutlet weak var mainViewController: MainViewController!
2021
@IBOutlet weak var scriptManager: ScriptManager!
2122
@IBOutlet weak var editor: SyntaxTextView!
23+
@IBOutlet weak var statusView: StatusView!
2224

2325
// Frame auto save name for app window frame restoration.
2426
private static let appWindowName = "boop.app.window"
@@ -42,6 +44,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {
4244
return true
4345
}
4446

47+
func application(_ sender: NSApplication, openFile filename: String) -> Bool {
48+
49+
let text=try? String(contentsOf: URL(fileURLWithPath: filename))
50+
51+
if text == nil {
52+
self.statusView.setStatus(.error("Failed to load file: '\(filename)'."))
53+
} else {
54+
editor.text = text!
55+
}
56+
return true
57+
}
58+
4559
@IBAction func showPreferencesWindow(_ sender: NSMenuItem) {
4660
let controller = NSStoryboard.init(name: "Preferences", bundle: nil).instantiateInitialController() as? NSWindowController
4761

Boop/Boop/Boop.entitlements

+2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@
1010
<true/>
1111
<key>com.apple.security.network.client</key>
1212
<true/>
13+
<key>com.apple.security.files.user-selected.read-write</key>
14+
<true/>
1315
</dict>
1416
</plist>

Boop/Boop/BoopRelease.entitlements

+2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@
1010
<true/>
1111
<key>com.apple.security.network.client</key>
1212
<true/>
13+
<key>com.apple.security.files.user-selected.read-write</key>
14+
<true/>
1315
</dict>
1416
</plist>

Boop/Boop/Controllers/MainViewController.swift

+50-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import Cocoa
1010
import SavannaKit
1111

1212
class MainViewController: NSViewController {
13-
13+
14+
@IBOutlet weak var statusView: StatusView!
1415
@IBOutlet weak var editorView: SyntaxTextView!
1516
@IBOutlet weak var updateBuddy: UpdateBuddy!
1617
@IBOutlet weak var checkUpdateMenuItem: NSMenuItem!
@@ -67,6 +68,54 @@ class MainViewController: NSViewController {
6768
@IBAction func checkForUpdates(_ sender: Any) {
6869
updateBuddy.check()
6970
}
71+
72+
@IBAction func openFile(sender: AnyObject) {
73+
74+
let dialog = NSOpenPanel();
75+
76+
dialog.title = "Choose a file";
77+
dialog.showsResizeIndicator = true;
78+
dialog.showsHiddenFiles = false;
79+
dialog.canChooseDirectories = false;
80+
dialog.canCreateDirectories = false;
81+
dialog.allowsMultipleSelection = false;
82+
83+
if (dialog.runModal() == NSApplication.ModalResponse.OK) {
84+
if let pathUrl = dialog.url { // Pathname of the file
85+
let text=try? String(contentsOf: pathUrl)
86+
87+
if text == nil {
88+
self.statusView.setStatus(.error("Failed to load file: '\(pathUrl.path)'."))
89+
} else {
90+
editorView.text = text!
91+
}
92+
}
93+
}
94+
}
95+
96+
@IBAction func saveFileAs(sender: AnyObject) {
97+
98+
let dialog = NSSavePanel();
99+
100+
dialog.title = "Save content as…";
101+
dialog.showsResizeIndicator = true;
102+
dialog.showsHiddenFiles = false;
103+
dialog.showsTagField = false;
104+
dialog.canCreateDirectories = true;
105+
dialog.nameFieldStringValue = "Untitled.txt"
106+
107+
if (dialog.runModal() == NSApplication.ModalResponse.OK) {
108+
if let pathUrl = dialog.url { // Pathname of the file
109+
let textView = editorView.contentTextView
110+
let textData=textView.textStorage?.string.data(using: .utf8)
111+
do {
112+
try textData?.write(to: pathUrl)
113+
} catch let error as NSError {
114+
self.statusView.setStatus(.error("Failed to save content to file '\(pathUrl.path)'. (Reason: \(error.localizedFailureReason!))"))
115+
}
116+
}
117+
}
118+
}
70119
}
71120

72121
extension MainViewController: SyntaxTextViewDelegate {

Boop/Boop/Info.plist

+11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@
2828
<string>MainMenu</string>
2929
<key>NSPrincipalClass</key>
3030
<string>NSApplication</string>
31+
<key>CFBundleDocumentTypes</key>
32+
<array>
33+
<dict>
34+
<key>CFBundleTypeRole</key>
35+
<string>Viewer</string>
36+
<key>CFBundleTypeExtensions</key>
37+
<array>
38+
<string>*</string>
39+
</array>
40+
</dict>
41+
</array>
3142
<key>NSServices</key>
3243
<array>
3344
<dict>

Boop/Boop/System/Models/Script.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ extension Script: Fuseable {
9797
}
9898
}
9999

100-
protocol ScriptDelegate: class {
100+
protocol ScriptDelegate: AnyObject {
101101
func onScriptError(message: String)
102102
func onScriptInfo(message: String)
103103
}

Boop/UI/Base.lproj/MainMenu.xib

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="18122" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21701" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
33
<dependencies>
44
<deployment identifier="macosx"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="18122"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21701"/>
66
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
77
</dependencies>
88
<objects>
@@ -27,6 +27,7 @@
2727
<connections>
2828
<outlet property="checkUpdateMenuItem" destination="mdD-pu-Aba" id="Ffr-2n-BVJ"/>
2929
<outlet property="editorView" destination="Ef0-Na-xwu" id="Bgy-ZL-Z2D"/>
30+
<outlet property="statusView" destination="zMi-QK-dkF" id="9vO-9b-1jg"/>
3031
<outlet property="updateBuddy" destination="twH-mb-iPe" id="Sqq-Ci-vlD"/>
3132
<outlet property="view" destination="Ef0-Na-xwu" id="wVq-8C-Rni"/>
3233
</connections>
@@ -58,6 +59,7 @@
5859
<outlet property="openPickerMenuItem" destination="8ka-cp-srR" id="qZt-zg-2wc"/>
5960
<outlet property="popoverViewController" destination="ugb-uZ-HP1" id="gLb-EK-85g"/>
6061
<outlet property="scriptManager" destination="jdF-I6-2al" id="o2F-Lj-Vou"/>
62+
<outlet property="statusView" destination="zMi-QK-dkF" id="Q7F-n7-RCv"/>
6163
<outlet property="window" destination="QvC-M9-y7g" id="gIp-Ho-8D9"/>
6264
</connections>
6365
</customObject>
@@ -123,14 +125,9 @@
123125
<modifierMask key="keyEquivalentModifierMask"/>
124126
<menu key="submenu" title="File" id="bib-Uj-vzu">
125127
<items>
126-
<menuItem title="Clear" keyEquivalent="n" id="Was-JA-tGl">
127-
<connections>
128-
<action selector="clear:" target="unv-pE-gme" id="Mj1-40-053"/>
129-
</connections>
130-
</menuItem>
131-
<menuItem title="Open…" hidden="YES" keyEquivalent="o" id="IAo-SY-fd9">
128+
<menuItem title="Open…" keyEquivalent="o" id="IAo-SY-fd9">
132129
<connections>
133-
<action selector="openDocument:" target="-1" id="bVn-NM-KNZ"/>
130+
<action selector="openFileWithSender:" target="unv-pE-gme" id="xcF-n8-bT8"/>
134131
</connections>
135132
</menuItem>
136133
<menuItem title="Open Recent" hidden="YES" id="tXI-mr-wws">
@@ -147,19 +144,14 @@
147144
</menu>
148145
</menuItem>
149146
<menuItem isSeparatorItem="YES" id="m54-Is-iLE"/>
150-
<menuItem title="Close" keyEquivalent="w" id="DVo-aG-piG">
151-
<connections>
152-
<action selector="performClose:" target="-1" id="HmO-Ls-i7Q"/>
153-
</connections>
154-
</menuItem>
155147
<menuItem title="Save…" hidden="YES" keyEquivalent="s" id="pxx-59-PXV">
156148
<connections>
157149
<action selector="saveDocument:" target="-1" id="teZ-XB-qJY"/>
158150
</connections>
159151
</menuItem>
160-
<menuItem title="Save As…" hidden="YES" keyEquivalent="S" id="Bw7-FT-i3A">
152+
<menuItem title="Save As…" keyEquivalent="S" id="Bw7-FT-i3A">
161153
<connections>
162-
<action selector="saveDocumentAs:" target="-1" id="mDf-zr-I0C"/>
154+
<action selector="saveFileAsSender:" target="unv-pE-gme" id="lbR-ze-VhW"/>
163155
</connections>
164156
</menuItem>
165157
<menuItem title="Revert to Saved" hidden="YES" keyEquivalent="r" id="KaW-ft-85H">
@@ -179,6 +171,17 @@
179171
<action selector="print:" target="-1" id="qaZ-4w-aoO"/>
180172
</connections>
181173
</menuItem>
174+
<menuItem isSeparatorItem="YES" id="ggp-Jn-CEd"/>
175+
<menuItem title="Clear" keyEquivalent="n" id="Was-JA-tGl">
176+
<connections>
177+
<action selector="clear:" target="unv-pE-gme" id="Mj1-40-053"/>
178+
</connections>
179+
</menuItem>
180+
<menuItem title="Close" keyEquivalent="w" id="DVo-aG-piG">
181+
<connections>
182+
<action selector="performClose:" target="-1" id="HmO-Ls-i7Q"/>
183+
</connections>
184+
</menuItem>
182185
</items>
183186
</menu>
184187
</menuItem>
@@ -509,7 +512,7 @@ Gw
509512
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
510513
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
511514
<rect key="contentRect" x="335" y="390" width="480" height="360"/>
512-
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="900"/>
515+
<rect key="screenRect" x="0.0" y="0.0" width="1920" height="1055"/>
513516
<value key="minSize" type="size" width="480" height="360"/>
514517
<view key="contentView" wantsLayer="YES" id="EiT-Mj-1SZ">
515518
<rect key="frame" x="0.0" y="0.0" width="480" height="360"/>

0 commit comments

Comments
 (0)