Skip to content

Commit e9ffd6a

Browse files
committed
Added WindowObserver View
Signed-off-by: Wouter01 <wouterhennen@gmail.com>
1 parent 0ee36ee commit e9ffd6a

File tree

4 files changed

+56
-53
lines changed

4 files changed

+56
-53
lines changed

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@
307307
6C14CEB32877A68F001468FE /* FindNavigatorMatchListCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C14CEB22877A68F001468FE /* FindNavigatorMatchListCell.swift */; };
308308
6C48D8F22972DAFC00D6D205 /* IsFullscreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48D8F12972DAFC00D6D205 /* IsFullscreen.swift */; };
309309
6C48D8F42972DB1A00D6D205 /* Window.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48D8F32972DB1A00D6D205 /* Window.swift */; };
310+
6C48D8F72972E5F300D6D205 /* WindowObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48D8F62972E5F300D6D205 /* WindowObserver.swift */; };
310311
6CDA84AB284C0E4A00C1CC3A /* TabBarItemButtonStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CDA84AA284C0E4A00C1CC3A /* TabBarItemButtonStyle.swift */; };
311312
6CDA84AD284C1BA000C1CC3A /* TabBarContextMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CDA84AC284C1BA000C1CC3A /* TabBarContextMenu.swift */; };
312313
B62617282964924E00E866AB /* CodeEditKit in Embed Frameworks */ = {isa = PBXBuildFile; productRef = 2801BB89290D5A8E00EBF552 /* CodeEditKit */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
@@ -673,6 +674,7 @@
673674
6C14CEB22877A68F001468FE /* FindNavigatorMatchListCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindNavigatorMatchListCell.swift; sourceTree = "<group>"; };
674675
6C48D8F12972DAFC00D6D205 /* IsFullscreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IsFullscreen.swift; sourceTree = "<group>"; };
675676
6C48D8F32972DB1A00D6D205 /* Window.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Window.swift; sourceTree = "<group>"; };
677+
6C48D8F62972E5F300D6D205 /* WindowObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowObserver.swift; sourceTree = "<group>"; };
676678
6CDA84AA284C0E4A00C1CC3A /* TabBarItemButtonStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabBarItemButtonStyle.swift; sourceTree = "<group>"; };
677679
6CDA84AC284C1BA000C1CC3A /* TabBarContextMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabBarContextMenu.swift; sourceTree = "<group>"; };
678680
B658FB2C27DA9E0F00EA4DBD /* CodeEdit.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CodeEdit.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -2165,6 +2167,7 @@
21652167
04660F6027E3A68A00477777 /* Info.plist */,
21662168
043C321927E32295006AE443 /* MainMenu.xib */,
21672169
B658FB3127DA9E0F00EA4DBD /* WorkspaceView.swift */,
2170+
6C48D8F62972E5F300D6D205 /* WindowObserver.swift */,
21682171
28069DA527F5BD320016BC47 /* DefaultThemes */,
21692172
);
21702173
path = CodeEdit;
@@ -2546,6 +2549,7 @@
25462549
D7012EE827E757850001E1EF /* FindNavigatorView.swift in Sources */,
25472550
58A5DF8029325B5A00D1BD5D /* GitClient.swift in Sources */,
25482551
D7E201AE27E8B3C000CB86D0 /* String+Ranges.swift in Sources */,
2552+
6C48D8F72972E5F300D6D205 /* WindowObserver.swift in Sources */,
25492553
587B9E6B29301D8F00AC7927 /* GitLabAvatarURL.swift in Sources */,
25502554
58798262292EC4080085B254 /* PluginRelease.swift in Sources */,
25512555
58F2EB10292FB2B0004A9BDE /* PreferencesColorPicker.swift in Sources */,

CodeEdit/Features/Documents/Controllers/CodeEditWindowController.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate {
7777
navigator.collapseBehavior = .useConstraints
7878
splitVC.addSplitViewItem(navigator)
7979

80-
let workspaceView = WorkspaceView(workspace: workspace)
81-
.environment(\.window, window!)
80+
let workspaceView = WindowObserver(window: window!) {
81+
WorkspaceView(workspace: workspace)
82+
}
83+
8284
let mainContent = NSSplitViewItem(
8385
viewController: NSHostingController(rootView: workspaceView)
8486
)

CodeEdit/WindowObserver.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// WindowObserver.swift
3+
// CodeEdit
4+
//
5+
// Created by Wouter Hennen on 14/01/2023.
6+
//
7+
8+
import SwiftUI
9+
10+
struct WindowObserver<Content: View>: View {
11+
12+
var window: NSWindow
13+
14+
@ViewBuilder
15+
var content: Content
16+
17+
/// The fullscreen state of the NSWindow.
18+
/// This will be passed into all child views as an environment variable.
19+
@State
20+
private var isFullscreen = false
21+
22+
@StateObject
23+
private var prefs: AppPreferencesModel = .shared
24+
25+
var body: some View {
26+
content
27+
.environment(\.window, window)
28+
.environment(\.isFullscreen, isFullscreen)
29+
.onReceive(NotificationCenter.default.publisher(for: NSWindow.didEnterFullScreenNotification)) { _ in
30+
self.isFullscreen = true
31+
}
32+
.onReceive(NotificationCenter.default.publisher(for: NSWindow.willExitFullScreenNotification)) { _ in
33+
self.isFullscreen = false
34+
}
35+
// When tab bar style is changed, update NSWindow configuration as follows.
36+
.onChange(of: prefs.preferences.general.tabBarStyle) { newStyle in
37+
DispatchQueue.main.async {
38+
if newStyle == .native {
39+
window.titlebarAppearsTransparent = true
40+
window.titlebarSeparatorStyle = .none
41+
} else {
42+
window.titlebarAppearsTransparent = false
43+
window.titlebarSeparatorStyle = .automatic
44+
}
45+
}
46+
}
47+
}
48+
}

CodeEdit/WorkspaceView.swift

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,6 @@ struct WorkspaceView: View {
3939
@State
4040
var showInspector = true
4141

42-
/// The fullscreen state of the NSWindow.
43-
/// This will be passed into all child views as an environment variable.
44-
@State
45-
var isFullscreen = false
46-
47-
@State
48-
private var enterFullscreenObserver: Any?
49-
50-
@State
51-
private var leaveFullscreenObserver: Any?
52-
5342
@Environment(\.colorScheme) var colorScheme
5443

5544
var noEditor: some View {
@@ -109,45 +98,5 @@ struct WorkspaceView: View {
10998
window.subtitle = ""
11099
}
111100
}
112-
.onAppear {
113-
// There may be other methods to monitor the full-screen state.
114-
// But I cannot find a better one for now because I need to pass this into the SwiftUI.
115-
// And it should always be updated.
116-
enterFullscreenObserver = NotificationCenter.default.addObserver(
117-
forName: NSWindow.didEnterFullScreenNotification,
118-
object: nil,
119-
queue: .current,
120-
using: { _ in self.isFullscreen = true }
121-
)
122-
leaveFullscreenObserver = NotificationCenter.default.addObserver(
123-
forName: NSWindow.willExitFullScreenNotification,
124-
object: nil,
125-
queue: .current,
126-
using: { _ in self.isFullscreen = false }
127-
)
128-
}
129-
.onDisappear {
130-
// Unregister the observer when the view is going to disappear.
131-
if enterFullscreenObserver != nil {
132-
NotificationCenter.default.removeObserver(enterFullscreenObserver!)
133-
}
134-
if leaveFullscreenObserver != nil {
135-
NotificationCenter.default.removeObserver(leaveFullscreenObserver!)
136-
}
137-
}
138-
// Send the environment to all subviews.
139-
.environment(\.isFullscreen, self.isFullscreen)
140-
// When tab bar style is changed, update NSWindow configuration as follows.
141-
.onChange(of: prefs.preferences.general.tabBarStyle) { newStyle in
142-
DispatchQueue.main.async {
143-
if newStyle == .native {
144-
window.titlebarAppearsTransparent = true
145-
window.titlebarSeparatorStyle = .none
146-
} else {
147-
window.titlebarAppearsTransparent = false
148-
window.titlebarSeparatorStyle = .automatic
149-
}
150-
}
151-
}
152101
}
153102
}

0 commit comments

Comments
 (0)