Skip to content

Commit

Permalink
Fix "Menu Bar Items Not Updating (#1684)" (#1697)
Browse files Browse the repository at this point in the history
### Description

<!--- REQUIRED: Describe what changed in detail -->
The issue is described in detail in #1684.

~I tried to resolve a related issue, #1696, but it was harder than I anticipated since the state of the Utility Area is stored separate to the other panes for some reason. Any ideas to resolve this slight issue would be welcome :)~

**EDIT:** I think I fixed it! 

### Related Issues

<!--- REQUIRED: Tag all related issues (e.g. * #123) -->
<!--- If this PR resolves the issue please specify (e.g. * closes #123) -->
<!--- If this PR addresses multiple issues, these issues must be related to one other -->

* Closes #1684 
* Closes #1696 

### Checklist

<!--- Add things that are not yet implemented above -->

- [x] I read and understood the [contributing guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md) as well as the [code of conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md)
- [x] The issues this PR addresses are related to each other
- [x] My changes generate no new warnings
- [x] My code builds and runs on my machine
- [x] My changes are all related to the related issue above
- [x] I documented my code

### Screenshots

<!--- REQUIRED: if issue is UI related -->

https://github.com/CodeEditApp/CodeEdit/assets/65467530/166d9b35-ede1-47b8-8814-cf1cbb1a61f2
  • Loading branch information
danielzsh committed May 16, 2024
1 parent 5724c72 commit 78e75dc
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 161 deletions.
4 changes: 4 additions & 0 deletions CodeEdit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
04BC1CDE2AD9B4B000A83EA5 /* EditorFileTabCloseButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04BC1CDD2AD9B4B000A83EA5 /* EditorFileTabCloseButton.swift */; };
04C3255B2801F86400C8DA2D /* ProjectNavigatorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 285FEC6D27FE4B4A00E57D53 /* ProjectNavigatorViewController.swift */; };
04C3255C2801F86900C8DA2D /* ProjectNavigatorMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 285FEC7127FE4EEF00E57D53 /* ProjectNavigatorMenu.swift */; };
0FD96BCE2BEF42530025A697 /* CodeEditWindowController+Toolbar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0FD96BCD2BEF42530025A697 /* CodeEditWindowController+Toolbar.swift */; };
201169D72837B2E300F92B46 /* SourceControlNavigatorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 201169D62837B2E300F92B46 /* SourceControlNavigatorView.swift */; };
201169DB2837B34000F92B46 /* SourceControlNavigatorChangedFileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 201169DA2837B34000F92B46 /* SourceControlNavigatorChangedFileView.swift */; };
201169DD2837B3AC00F92B46 /* SourceControlNavigatorToolbarBottom.swift in Sources */ = {isa = PBXBuildFile; fileRef = 201169DC2837B3AC00F92B46 /* SourceControlNavigatorToolbarBottom.swift */; };
Expand Down Expand Up @@ -588,6 +589,7 @@
04BA7C232AE2E7CD00584E1C /* SourceControlNavigatorSyncView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SourceControlNavigatorSyncView.swift; sourceTree = "<group>"; };
04BA7C262AE2E9F100584E1C /* GitClient+Push.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GitClient+Push.swift"; sourceTree = "<group>"; };
04BC1CDD2AD9B4B000A83EA5 /* EditorFileTabCloseButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorFileTabCloseButton.swift; sourceTree = "<group>"; };
0FD96BCD2BEF42530025A697 /* CodeEditWindowController+Toolbar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CodeEditWindowController+Toolbar.swift"; sourceTree = "<group>"; };
201169D62837B2E300F92B46 /* SourceControlNavigatorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SourceControlNavigatorView.swift; sourceTree = "<group>"; };
201169DA2837B34000F92B46 /* SourceControlNavigatorChangedFileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SourceControlNavigatorChangedFileView.swift; sourceTree = "<group>"; };
201169DC2837B3AC00F92B46 /* SourceControlNavigatorToolbarBottom.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SourceControlNavigatorToolbarBottom.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1495,6 +1497,7 @@
04660F6927E51E5C00477777 /* CodeEditWindowController.swift */,
B6152B7F2ADAE421004C6012 /* CodeEditWindowControllerExtensions.swift */,
4E7F066529602E7B00BB3C12 /* CodeEditSplitViewController.swift */,
0FD96BCD2BEF42530025A697 /* CodeEditWindowController+Toolbar.swift */,
);
path = Controllers;
sourceTree = "<group>";
Expand Down Expand Up @@ -3380,6 +3383,7 @@
587B9E8D29301D8F00AC7927 /* GitHubAccount.swift in Sources */,
201169E72837B5CA00F92B46 /* SourceControlManager.swift in Sources */,
58822528292C280D00E83CDE /* StatusBarEncodingSelector.swift in Sources */,
0FD96BCE2BEF42530025A697 /* CodeEditWindowController+Toolbar.swift in Sources */,
6C7F37FE2A3EA6FA00217B83 /* View+focusedValue.swift in Sources */,
B6C4F2A12B3CA37500B2B140 /* SourceControlNavigatorHistoryView.swift in Sources */,
6CBE1CFB2B71DAA6003AC32E /* Loopable.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//
// CodeEditWindowController+Toolbar.swift
// CodeEdit
//
// Created by Daniel Zhu on 5/10/24.
//

import AppKit
import SwiftUI

extension CodeEditWindowController {
internal func setupToolbar() {
let toolbar = NSToolbar(identifier: UUID().uuidString)
toolbar.delegate = self
toolbar.displayMode = .labelOnly
toolbar.showsBaselineSeparator = false
self.window?.titleVisibility = toolbarCollapsed ? .visible : .hidden
self.window?.toolbarStyle = .unifiedCompact
if Settings[\.general].tabBarStyle == .native {
// Set titlebar background as transparent by default in order to
// style the toolbar background in native tab bar style.
self.window?.titlebarSeparatorStyle = .none
} else {
// In Xcode tab bar style, we use default toolbar background with
// line separator.
self.window?.titlebarSeparatorStyle = .automatic
}
self.window?.toolbar = toolbar
}

func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
[
.toggleFirstSidebarItem,
.sidebarTrackingSeparator,
.branchPicker,
.flexibleSpace,
.itemListTrackingSeparator,
.flexibleSpace,
.toggleLastSidebarItem
]
}

func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
[
.toggleFirstSidebarItem,
.sidebarTrackingSeparator,
.flexibleSpace,
.itemListTrackingSeparator,
.toggleLastSidebarItem,
.branchPicker
]
}

func toggleToolbar() {
toolbarCollapsed.toggle()
updateToolbarVisibility()
}

private func updateToolbarVisibility() {
if toolbarCollapsed {
window?.titleVisibility = .visible
window?.title = workspace?.workspaceFileManager?.folderUrl.lastPathComponent ?? "Empty"
window?.toolbar = nil
} else {
window?.titleVisibility = .hidden
setupToolbar()
}
}

func toolbar(
_ toolbar: NSToolbar,
itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier,
willBeInsertedIntoToolbar flag: Bool
) -> NSToolbarItem? {
switch itemIdentifier {
case .itemListTrackingSeparator:
guard let splitViewController else { return nil }

return NSTrackingSeparatorToolbarItem(
identifier: .itemListTrackingSeparator,
splitView: splitViewController.splitView,
dividerIndex: 1
)
case .toggleFirstSidebarItem:
let toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier.toggleFirstSidebarItem)
toolbarItem.label = "Navigator Sidebar"
toolbarItem.paletteLabel = " Navigator Sidebar"
toolbarItem.toolTip = "Hide or show the Navigator"
toolbarItem.isBordered = true
toolbarItem.target = self
toolbarItem.action = #selector(self.toggleFirstPanel)
toolbarItem.image = NSImage(
systemSymbolName: "sidebar.leading",
accessibilityDescription: nil
)?.withSymbolConfiguration(.init(scale: .large))

return toolbarItem
case .toggleLastSidebarItem:
let toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier.toggleLastSidebarItem)
toolbarItem.label = "Inspector Sidebar"
toolbarItem.paletteLabel = "Inspector Sidebar"
toolbarItem.toolTip = "Hide or show the Inspectors"
toolbarItem.isBordered = true
toolbarItem.target = self
toolbarItem.action = #selector(self.toggleLastPanel)
toolbarItem.image = NSImage(
systemSymbolName: "sidebar.trailing",
accessibilityDescription: nil
)?.withSymbolConfiguration(.init(scale: .large))

return toolbarItem
case .branchPicker:
let toolbarItem = NSToolbarItem(itemIdentifier: .branchPicker)
let view = NSHostingView(
rootView: ToolbarBranchPicker(
workspaceFileManager: workspace?.workspaceFileManager
)
)
toolbarItem.view = view

return toolbarItem

default:
return NSToolbarItem(itemIdentifier: itemIdentifier)
}
}
}
123 changes: 3 additions & 120 deletions CodeEdit/Features/Documents/Controllers/CodeEditWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate, Obs

internal var cancellables = [AnyCancellable]()

init(window: NSWindow, workspace: WorkspaceDocument) {
init(window: NSWindow?, workspace: WorkspaceDocument?) {
super.init(window: window)
guard let workspace else { return }
self.workspace = workspace
self.workspaceSettings = CEWorkspaceSettings(workspaceDocument: workspace)
setupSplitView(with: workspace)
Expand All @@ -46,7 +47,7 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate, Obs
self?.navigatorCollapsed = item.isCollapsed
}),
splitViewController.splitViewItems.last!.observe(\.isCollapsed, changeHandler: { [weak self] item, _ in
self?.navigatorCollapsed = item.isCollapsed
self?.inspectorCollapsed = item.isCollapsed
})
]

Expand Down Expand Up @@ -118,124 +119,6 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate, Obs
self.listenToDocumentEdited(workspace: workspace)
}

private func setupToolbar() {
let toolbar = NSToolbar(identifier: UUID().uuidString)
toolbar.delegate = self
toolbar.displayMode = .labelOnly
toolbar.showsBaselineSeparator = false
self.window?.titleVisibility = toolbarCollapsed ? .visible : .hidden
self.window?.toolbarStyle = .unifiedCompact
if Settings[\.general].tabBarStyle == .native {
// Set titlebar background as transparent by default in order to
// style the toolbar background in native tab bar style.
self.window?.titlebarSeparatorStyle = .none
} else {
// In Xcode tab bar style, we use default toolbar background with
// line separator.
self.window?.titlebarSeparatorStyle = .automatic
}
self.window?.toolbar = toolbar
}

// MARK: - Toolbar

func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
[
.toggleFirstSidebarItem,
.sidebarTrackingSeparator,
.branchPicker,
.flexibleSpace,
.itemListTrackingSeparator,
.flexibleSpace,
.toggleLastSidebarItem
]
}

func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
[
.toggleFirstSidebarItem,
.sidebarTrackingSeparator,
.flexibleSpace,
.itemListTrackingSeparator,
.toggleLastSidebarItem,
.branchPicker
]
}

func toggleToolbar() {
toolbarCollapsed.toggle()
updateToolbarVisibility()
}

private func updateToolbarVisibility() {
if toolbarCollapsed {
window?.titleVisibility = .visible
window?.title = workspace?.workspaceFileManager?.folderUrl.lastPathComponent ?? "Empty"
window?.toolbar = nil
} else {
window?.titleVisibility = .hidden
setupToolbar()
}
}

func toolbar(
_ toolbar: NSToolbar,
itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier,
willBeInsertedIntoToolbar flag: Bool
) -> NSToolbarItem? {
switch itemIdentifier {
case .itemListTrackingSeparator:
guard let splitViewController else { return nil }

return NSTrackingSeparatorToolbarItem(
identifier: .itemListTrackingSeparator,
splitView: splitViewController.splitView,
dividerIndex: 1
)
case .toggleFirstSidebarItem:
let toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier.toggleFirstSidebarItem)
toolbarItem.label = "Navigator Sidebar"
toolbarItem.paletteLabel = " Navigator Sidebar"
toolbarItem.toolTip = "Hide or show the Navigator"
toolbarItem.isBordered = true
toolbarItem.target = self
toolbarItem.action = #selector(self.toggleFirstPanel)
toolbarItem.image = NSImage(
systemSymbolName: "sidebar.leading",
accessibilityDescription: nil
)?.withSymbolConfiguration(.init(scale: .large))

return toolbarItem
case .toggleLastSidebarItem:
let toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier.toggleLastSidebarItem)
toolbarItem.label = "Inspector Sidebar"
toolbarItem.paletteLabel = "Inspector Sidebar"
toolbarItem.toolTip = "Hide or show the Inspectors"
toolbarItem.isBordered = true
toolbarItem.target = self
toolbarItem.action = #selector(self.toggleLastPanel)
toolbarItem.image = NSImage(
systemSymbolName: "sidebar.trailing",
accessibilityDescription: nil
)?.withSymbolConfiguration(.init(scale: .large))

return toolbarItem
case .branchPicker:
let toolbarItem = NSToolbarItem(itemIdentifier: .branchPicker)
let view = NSHostingView(
rootView: ToolbarBranchPicker(
workspaceFileManager: workspace?.workspaceFileManager
)
)
toolbarItem.view = view

return toolbarItem

default:
return NSToolbarItem(itemIdentifier: itemIdentifier)
}
}

private func getSelectedCodeFile() -> CodeFileDocument? {
workspace?.editorManager.activeEditor.selectedTab?.file.fileDocument
}
Expand Down

0 comments on commit 78e75dc

Please sign in to comment.