Skip to content

Commit 495b8dd

Browse files
luah5pythonista5
andauthored
Add setting to determine what happens after closing the last open workspace (#1194)
* adds setting to determine what happens after closing the last open window, closing #1193 * clean up code and add comment * minor rephrase * refactor reopenWindowAfterWorkspaceClose to reopenWindowAfterClose and add switch resolves @lukepistrol review * finalize refactor * respond to @lukepistrol's constructive comment * Fix error * revert a change --------- Co-authored-by: luah5 <russo.173.328@gmail.com>
1 parent 0c3f75e commit 495b8dd

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

CodeEdit/Features/AppPreferences/Model/General/GeneralPreferences.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ extension AppPreferences {
3939
/// The reopen behavior of the app
4040
var reopenBehavior: ReopenBehavior = .welcome
4141

42+
/// Decides what the app does after a workspace is closed
43+
var reopenWindowAfterClose: ReopenWindowBehavior = .doNothing
44+
45+
/// The size of the project navigator
4246
var projectNavigatorSize: ProjectNavigatorSize = .medium
4347

4448
/// The Find Navigator Detail line limit
@@ -96,6 +100,10 @@ extension AppPreferences {
96100
ReopenBehavior.self,
97101
forKey: .reopenBehavior
98102
) ?? .welcome
103+
self.reopenWindowAfterClose = try container.decodeIfPresent(
104+
ReopenWindowBehavior.self,
105+
forKey: .reopenWindowAfterClose
106+
) ?? .doNothing
99107
self.projectNavigatorSize = try container.decodeIfPresent(
100108
ProjectNavigatorSize.self,
101109
forKey: .projectNavigatorSize
@@ -212,6 +220,12 @@ extension AppPreferences {
212220
case newDocument
213221
}
214222

223+
enum ReopenWindowBehavior: String, Codable {
224+
case showWelcomeWindow
225+
case doNothing
226+
case quit
227+
}
228+
215229
enum ProjectNavigatorSize: String, Codable {
216230
case small
217231
case medium

CodeEdit/Features/AppPreferences/Sections/GeneralPreferences/GeneralPreferencesView.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ struct GeneralPreferencesView: View {
4040
fileExtensionsSection
4141
fileIconStyleSection
4242
tabBarStyleSection
43-
reopenBehaviorSection
43+
Group {
44+
reopenBehaviorSection
45+
reopenAfterWindowCloseBehaviourSection
46+
}
4447
projectNavigatorSizeSection
4548
findNavigatorDetailSection
4649
Group {
@@ -162,6 +165,24 @@ private extension GeneralPreferencesView {
162165
}
163166
}
164167

168+
var reopenAfterWindowCloseBehaviourSection: some View {
169+
PreferencesSection("After last window closed") {
170+
Picker(
171+
"After last window closed:",
172+
selection: $prefs.preferences.general.reopenWindowAfterClose
173+
) {
174+
Text("Do nothing")
175+
.tag(AppPreferences.ReopenWindowBehavior.doNothing)
176+
Divider()
177+
Text("Show Welcome Window")
178+
.tag(AppPreferences.ReopenWindowBehavior.showWelcomeWindow)
179+
Text("Quit")
180+
.tag(AppPreferences.ReopenWindowBehavior.quit)
181+
}
182+
.frame(width: inputWidth)
183+
}
184+
}
185+
165186
var projectNavigatorSizeSection: some View {
166187
PreferencesSection("Project Navigator Size") {
167188
Picker("Project Navigator Size", selection: $prefs.preferences.general.projectNavigatorSize) {

CodeEdit/Features/Documents/Controllers/CodeEditDocumentController.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ final class CodeEditDocumentController: NSDocumentController {
1212
FileManager.default
1313
}()
1414

15+
private var prefs: AppPreferencesModel = .shared
16+
1517
override func newDocument(_ sender: Any?) {
1618
guard let newDocumentUrl = self.newDocumentUrl else { return }
1719

@@ -76,6 +78,18 @@ final class CodeEditDocumentController: NSDocumentController {
7678

7779
override func removeDocument(_ document: NSDocument) {
7880
super.removeDocument(document)
81+
82+
if CodeEditDocumentController.shared.documents.isEmpty {
83+
switch prefs.preferences.general.reopenWindowAfterClose {
84+
case .showWelcomeWindow:
85+
// Opens the welcome window
86+
NSApp.openWindow(.welcome)
87+
case .quit:
88+
// Quits CodeEdit
89+
NSApplication.shared.terminate(nil)
90+
case .doNothing: break
91+
}
92+
}
7993
}
8094

8195
override func clearRecentDocuments(_ sender: Any?) {

0 commit comments

Comments
 (0)