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

Added NSWindow environment key #962

Merged
merged 2 commits into from
Jan 14, 2023
Merged
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
20 changes: 20 additions & 0 deletions CodeEdit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@
6C05A8AF284D0CA3007F4EAA /* WorkspaceDocument+Listeners.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C05A8AE284D0CA3007F4EAA /* WorkspaceDocument+Listeners.swift */; };
6C14CEB028777D3C001468FE /* FindNavigatorListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C14CEAF28777D3C001468FE /* FindNavigatorListViewController.swift */; };
6C14CEB32877A68F001468FE /* FindNavigatorMatchListCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C14CEB22877A68F001468FE /* FindNavigatorMatchListCell.swift */; };
6C48D8F22972DAFC00D6D205 /* IsFullscreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48D8F12972DAFC00D6D205 /* IsFullscreen.swift */; };
6C48D8F42972DB1A00D6D205 /* Window.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48D8F32972DB1A00D6D205 /* Window.swift */; };
6C48D8F72972E5F300D6D205 /* WindowObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48D8F62972E5F300D6D205 /* WindowObserver.swift */; };
6CDA84AB284C0E4A00C1CC3A /* TabBarItemButtonStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CDA84AA284C0E4A00C1CC3A /* TabBarItemButtonStyle.swift */; };
6CDA84AD284C1BA000C1CC3A /* TabBarContextMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CDA84AC284C1BA000C1CC3A /* TabBarContextMenu.swift */; };
B62617282964924E00E866AB /* CodeEditKit in Embed Frameworks */ = {isa = PBXBuildFile; productRef = 2801BB89290D5A8E00EBF552 /* CodeEditKit */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
Expand Down Expand Up @@ -669,6 +672,9 @@
6C05A8AE284D0CA3007F4EAA /* WorkspaceDocument+Listeners.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WorkspaceDocument+Listeners.swift"; sourceTree = "<group>"; };
6C14CEAF28777D3C001468FE /* FindNavigatorListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindNavigatorListViewController.swift; sourceTree = "<group>"; };
6C14CEB22877A68F001468FE /* FindNavigatorMatchListCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindNavigatorMatchListCell.swift; sourceTree = "<group>"; };
6C48D8F12972DAFC00D6D205 /* IsFullscreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IsFullscreen.swift; sourceTree = "<group>"; };
6C48D8F32972DB1A00D6D205 /* Window.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Window.swift; sourceTree = "<group>"; };
6C48D8F62972E5F300D6D205 /* WindowObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowObserver.swift; sourceTree = "<group>"; };
6CDA84AA284C0E4A00C1CC3A /* TabBarItemButtonStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabBarItemButtonStyle.swift; sourceTree = "<group>"; };
6CDA84AC284C1BA000C1CC3A /* TabBarContextMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabBarContextMenu.swift; sourceTree = "<group>"; };
B658FB2C27DA9E0F00EA4DBD /* CodeEdit.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CodeEdit.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -1777,6 +1783,7 @@
58D01C85293167DC00C5B6B4 /* Utils */ = {
isa = PBXGroup;
children = (
6C48D8EF2972DAC300D6D205 /* Environment */,
58D01C87293167DC00C5B6B4 /* Extensions */,
58D01C8F293167DC00C5B6B4 /* KeyChain */,
5831E3C92933E83400D5A6D2 /* Protocols */,
Expand Down Expand Up @@ -2112,6 +2119,15 @@
path = FindNavigatorResultList;
sourceTree = "<group>";
};
6C48D8EF2972DAC300D6D205 /* Environment */ = {
isa = PBXGroup;
children = (
6C48D8F12972DAFC00D6D205 /* IsFullscreen.swift */,
6C48D8F32972DB1A00D6D205 /* Window.swift */,
);
path = Environment;
sourceTree = "<group>";
};
B658FB2327DA9E0F00EA4DBD = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2151,6 +2167,7 @@
04660F6027E3A68A00477777 /* Info.plist */,
043C321927E32295006AE443 /* MainMenu.xib */,
B658FB3127DA9E0F00EA4DBD /* WorkspaceView.swift */,
6C48D8F62972E5F300D6D205 /* WindowObserver.swift */,
28069DA527F5BD320016BC47 /* DefaultThemes */,
);
path = CodeEdit;
Expand Down Expand Up @@ -2532,6 +2549,7 @@
D7012EE827E757850001E1EF /* FindNavigatorView.swift in Sources */,
58A5DF8029325B5A00D1BD5D /* GitClient.swift in Sources */,
D7E201AE27E8B3C000CB86D0 /* String+Ranges.swift in Sources */,
6C48D8F72972E5F300D6D205 /* WindowObserver.swift in Sources */,
587B9E6B29301D8F00AC7927 /* GitLabAvatarURL.swift in Sources */,
58798262292EC4080085B254 /* PluginRelease.swift in Sources */,
58F2EB10292FB2B0004A9BDE /* PreferencesColorPicker.swift in Sources */,
Expand Down Expand Up @@ -2591,6 +2609,7 @@
5882252D292C280D00E83CDE /* StatusBarSplitTerminalButton.swift in Sources */,
58798238292E30B90085B254 /* FeedbackWindowController.swift in Sources */,
587B9E6C29301D8F00AC7927 /* GitLabNamespace.swift in Sources */,
6C48D8F22972DAFC00D6D205 /* IsFullscreen.swift in Sources */,
587B9E8729301D8F00AC7927 /* GitHubRepositories.swift in Sources */,
587B9DA329300ABD00AC7927 /* SettingsTextEditor.swift in Sources */,
2072FA1A280D872600C7F8D4 /* LineEndings.swift in Sources */,
Expand Down Expand Up @@ -2684,6 +2703,7 @@
58F2EB09292FB2B0004A9BDE /* TerminalPreferences.swift in Sources */,
587D9B742933BF5700BF7490 /* FileItem+Array.swift in Sources */,
587D9B772933BF5700BF7490 /* Live.swift in Sources */,
6C48D8F42972DB1A00D6D205 /* Window.swift in Sources */,
2072FA18280D871200C7F8D4 /* TextEncoding.swift in Sources */,
58F2EAF3292FB2B0004A9BDE /* ThemePreviewIcon.swift in Sources */,
58F2EB07292FB2B0004A9BDE /* GeneralPreferences.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate {
let feedbackPerformer = NSHapticFeedbackManager.defaultPerformer
let splitVC = CodeEditSplitViewController(feedbackPerformer: feedbackPerformer)

let navigatorView = NavigatorSidebarView(workspace: workspace, windowController: self)
let navigatorView = NavigatorSidebarView(workspace: workspace)
let navigator = NSSplitViewItem(
sidebarWithViewController: NSHostingController(rootView: navigatorView)
)
Expand All @@ -77,14 +77,17 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate {
navigator.collapseBehavior = .useConstraints
splitVC.addSplitViewItem(navigator)

let workspaceView = WorkspaceView(windowController: self, workspace: workspace)
let workspaceView = WindowObserver(window: window!) {
WorkspaceView(workspace: workspace)
}

let mainContent = NSSplitViewItem(
viewController: NSHostingController(rootView: workspaceView)
)
mainContent.titlebarSeparatorStyle = .line
splitVC.addSplitViewItem(mainContent)

let inspectorView = InspectorSidebarView(workspace: workspace, windowController: self)
let inspectorView = InspectorSidebarView(workspace: workspace)
let inspector = NSSplitViewItem(
viewController: NSHostingController(rootView: inspectorView)
)
Expand Down
2 changes: 0 additions & 2 deletions CodeEdit/Features/Documents/Views/WorkspaceCodeFileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import SwiftUI
import UniformTypeIdentifiers

struct WorkspaceCodeFileView: View {
var windowController: NSWindowController

@ObservedObject
var workspace: WorkspaceDocument

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ struct InspectorSidebarView: View {
@ObservedObject
private var workspace: WorkspaceDocument

private let windowController: NSWindowController

@State
private var selection: Int = 0

init(workspace: WorkspaceDocument, windowController: NSWindowController) {
init(workspace: WorkspaceDocument) {
self.workspace = workspace
self.windowController = windowController
}

var body: some View {
Expand Down
7 changes: 2 additions & 5 deletions CodeEdit/Features/NavigatorSidebar/NavigatorSidebarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,20 @@ struct NavigatorSidebarView: View {
@ObservedObject
private var workspace: WorkspaceDocument

private let windowController: NSWindowController

@State
private var selection: Int = 0

private let toolbarPadding: Double = -8.0

init(workspace: WorkspaceDocument, windowController: NSWindowController) {
init(workspace: WorkspaceDocument) {
self.workspace = workspace
self.windowController = windowController
}

var body: some View {
VStack {
switch selection {
case 0:
ProjectNavigatorView(workspace: workspace, windowController: windowController)
ProjectNavigatorView(workspace: workspace)
case 1:
SourceControlNavigatorView(workspace: workspace)
case 2:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import SwiftUI
///
struct ProjectNavigatorView: View {
@ObservedObject var workspace: WorkspaceDocument
var windowController: NSWindowController

var body: some View {
OutlineView(workspace: workspace)
Expand Down
5 changes: 0 additions & 5 deletions CodeEdit/Features/TabBar/Views/TabBarItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ struct TabBarItemView: View {
/// You can get tab-related information from here, like `label`, `icon`, etc.
private var item: TabBarItemRepresentable

/// AppKit window controller.
private var windowController: NSWindowController

private var isTemporary: Bool

/// Is the current tab the active tab.
Expand Down Expand Up @@ -121,14 +118,12 @@ struct TabBarItemView: View {
init(
expectedWidth: Binding<CGFloat>,
item: TabBarItemRepresentable,
windowController: NSWindowController,
draggingTabId: Binding<TabBarItemID?>,
onDragTabId: Binding<TabBarItemID?>,
workspace: WorkspaceDocument
) {
self._expectedWidth = expectedWidth
self.item = item
self.windowController = windowController
self._draggingTabId = draggingTabId
self._onDragTabId = onDragTabId
self.workspace = workspace
Expand Down
9 changes: 2 additions & 7 deletions CodeEdit/Features/TabBar/Views/TabBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ struct TabBarView: View {
@StateObject
private var prefs: AppPreferencesModel = .shared

/// The controller of current NSWindow.
private let windowController: NSWindowController

/// The tab id of current dragging tab.
///
/// It will be `nil` when there is no tab dragged currently.
Expand Down Expand Up @@ -117,9 +114,8 @@ struct TabBarView: View {
@State
private var onDragLastLocation: CGPoint?

// TabBar(windowController: windowController, workspace: workspace)
init(windowController: NSWindowController, workspace: WorkspaceDocument) {
self.windowController = windowController
// TabBar(workspace: workspace)
init(workspace: WorkspaceDocument) {
self.workspace = workspace
}

Expand Down Expand Up @@ -302,7 +298,6 @@ struct TabBarView: View {
TabBarItemView(
expectedWidth: $expectedTabWidth,
item: item,
windowController: windowController,
draggingTabId: $draggingTabId,
onDragTabId: $onDragTabId,
workspace: workspace
Expand Down
19 changes: 19 additions & 0 deletions CodeEdit/Utils/Environment/IsFullscreen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// IsFullscreen.swift
// CodeEdit
//
// Created by Wouter Hennen on 14/01/2023.
//

import SwiftUI

private struct WorkspaceFullscreenStateEnvironmentKey: EnvironmentKey {
static let defaultValue: Bool = false
}

extension EnvironmentValues {
var isFullscreen: Bool {
get { self[WorkspaceFullscreenStateEnvironmentKey.self] }
set { self[WorkspaceFullscreenStateEnvironmentKey.self] = newValue }
}
}
19 changes: 19 additions & 0 deletions CodeEdit/Utils/Environment/Window.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Window.swift
// CodeEdit
//
// Created by Wouter Hennen on 14/01/2023.
//

import SwiftUI

struct NSWindowEnvironmentKey: EnvironmentKey {
static var defaultValue = NSWindow()
}

extension EnvironmentValues {
var window: NSWindowEnvironmentKey.Value {
get { self[NSWindowEnvironmentKey.self] }
set { self[NSWindowEnvironmentKey.self] = newValue }
}
}
48 changes: 48 additions & 0 deletions CodeEdit/WindowObserver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// WindowObserver.swift
// CodeEdit
//
// Created by Wouter Hennen on 14/01/2023.
//

import SwiftUI

struct WindowObserver<Content: View>: View {

var window: NSWindow

@ViewBuilder
var content: Content

/// The fullscreen state of the NSWindow.
/// This will be passed into all child views as an environment variable.
@State
private var isFullscreen = false

@StateObject
private var prefs: AppPreferencesModel = .shared

var body: some View {
content
.environment(\.window, window)
.environment(\.isFullscreen, isFullscreen)
.onReceive(NotificationCenter.default.publisher(for: NSWindow.didEnterFullScreenNotification)) { _ in
self.isFullscreen = true
}
.onReceive(NotificationCenter.default.publisher(for: NSWindow.willExitFullScreenNotification)) { _ in
self.isFullscreen = false
}
// When tab bar style is changed, update NSWindow configuration as follows.
.onChange(of: prefs.preferences.general.tabBarStyle) { newStyle in
DispatchQueue.main.async {
if newStyle == .native {
window.titlebarAppearsTransparent = true
window.titlebarSeparatorStyle = .none
} else {
window.titlebarAppearsTransparent = false
window.titlebarSeparatorStyle = .automatic
}
}
}
}
}
Loading