Skip to content

Commit 78e75dc

Browse files
authored
Fix "Menu Bar Items Not Updating (#1684)" (#1697)
### 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
1 parent 5724c72 commit 78e75dc

File tree

4 files changed

+189
-161
lines changed

4 files changed

+189
-161
lines changed

CodeEdit.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
04BC1CDE2AD9B4B000A83EA5 /* EditorFileTabCloseButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04BC1CDD2AD9B4B000A83EA5 /* EditorFileTabCloseButton.swift */; };
2929
04C3255B2801F86400C8DA2D /* ProjectNavigatorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 285FEC6D27FE4B4A00E57D53 /* ProjectNavigatorViewController.swift */; };
3030
04C3255C2801F86900C8DA2D /* ProjectNavigatorMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 285FEC7127FE4EEF00E57D53 /* ProjectNavigatorMenu.swift */; };
31+
0FD96BCE2BEF42530025A697 /* CodeEditWindowController+Toolbar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0FD96BCD2BEF42530025A697 /* CodeEditWindowController+Toolbar.swift */; };
3132
201169D72837B2E300F92B46 /* SourceControlNavigatorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 201169D62837B2E300F92B46 /* SourceControlNavigatorView.swift */; };
3233
201169DB2837B34000F92B46 /* SourceControlNavigatorChangedFileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 201169DA2837B34000F92B46 /* SourceControlNavigatorChangedFileView.swift */; };
3334
201169DD2837B3AC00F92B46 /* SourceControlNavigatorToolbarBottom.swift in Sources */ = {isa = PBXBuildFile; fileRef = 201169DC2837B3AC00F92B46 /* SourceControlNavigatorToolbarBottom.swift */; };
@@ -588,6 +589,7 @@
588589
04BA7C232AE2E7CD00584E1C /* SourceControlNavigatorSyncView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SourceControlNavigatorSyncView.swift; sourceTree = "<group>"; };
589590
04BA7C262AE2E9F100584E1C /* GitClient+Push.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GitClient+Push.swift"; sourceTree = "<group>"; };
590591
04BC1CDD2AD9B4B000A83EA5 /* EditorFileTabCloseButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorFileTabCloseButton.swift; sourceTree = "<group>"; };
592+
0FD96BCD2BEF42530025A697 /* CodeEditWindowController+Toolbar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CodeEditWindowController+Toolbar.swift"; sourceTree = "<group>"; };
591593
201169D62837B2E300F92B46 /* SourceControlNavigatorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SourceControlNavigatorView.swift; sourceTree = "<group>"; };
592594
201169DA2837B34000F92B46 /* SourceControlNavigatorChangedFileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SourceControlNavigatorChangedFileView.swift; sourceTree = "<group>"; };
593595
201169DC2837B3AC00F92B46 /* SourceControlNavigatorToolbarBottom.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SourceControlNavigatorToolbarBottom.swift; sourceTree = "<group>"; };
@@ -1495,6 +1497,7 @@
14951497
04660F6927E51E5C00477777 /* CodeEditWindowController.swift */,
14961498
B6152B7F2ADAE421004C6012 /* CodeEditWindowControllerExtensions.swift */,
14971499
4E7F066529602E7B00BB3C12 /* CodeEditSplitViewController.swift */,
1500+
0FD96BCD2BEF42530025A697 /* CodeEditWindowController+Toolbar.swift */,
14981501
);
14991502
path = Controllers;
15001503
sourceTree = "<group>";
@@ -3380,6 +3383,7 @@
33803383
587B9E8D29301D8F00AC7927 /* GitHubAccount.swift in Sources */,
33813384
201169E72837B5CA00F92B46 /* SourceControlManager.swift in Sources */,
33823385
58822528292C280D00E83CDE /* StatusBarEncodingSelector.swift in Sources */,
3386+
0FD96BCE2BEF42530025A697 /* CodeEditWindowController+Toolbar.swift in Sources */,
33833387
6C7F37FE2A3EA6FA00217B83 /* View+focusedValue.swift in Sources */,
33843388
B6C4F2A12B3CA37500B2B140 /* SourceControlNavigatorHistoryView.swift in Sources */,
33853389
6CBE1CFB2B71DAA6003AC32E /* Loopable.swift in Sources */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//
2+
// CodeEditWindowController+Toolbar.swift
3+
// CodeEdit
4+
//
5+
// Created by Daniel Zhu on 5/10/24.
6+
//
7+
8+
import AppKit
9+
import SwiftUI
10+
11+
extension CodeEditWindowController {
12+
internal func setupToolbar() {
13+
let toolbar = NSToolbar(identifier: UUID().uuidString)
14+
toolbar.delegate = self
15+
toolbar.displayMode = .labelOnly
16+
toolbar.showsBaselineSeparator = false
17+
self.window?.titleVisibility = toolbarCollapsed ? .visible : .hidden
18+
self.window?.toolbarStyle = .unifiedCompact
19+
if Settings[\.general].tabBarStyle == .native {
20+
// Set titlebar background as transparent by default in order to
21+
// style the toolbar background in native tab bar style.
22+
self.window?.titlebarSeparatorStyle = .none
23+
} else {
24+
// In Xcode tab bar style, we use default toolbar background with
25+
// line separator.
26+
self.window?.titlebarSeparatorStyle = .automatic
27+
}
28+
self.window?.toolbar = toolbar
29+
}
30+
31+
func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
32+
[
33+
.toggleFirstSidebarItem,
34+
.sidebarTrackingSeparator,
35+
.branchPicker,
36+
.flexibleSpace,
37+
.itemListTrackingSeparator,
38+
.flexibleSpace,
39+
.toggleLastSidebarItem
40+
]
41+
}
42+
43+
func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
44+
[
45+
.toggleFirstSidebarItem,
46+
.sidebarTrackingSeparator,
47+
.flexibleSpace,
48+
.itemListTrackingSeparator,
49+
.toggleLastSidebarItem,
50+
.branchPicker
51+
]
52+
}
53+
54+
func toggleToolbar() {
55+
toolbarCollapsed.toggle()
56+
updateToolbarVisibility()
57+
}
58+
59+
private func updateToolbarVisibility() {
60+
if toolbarCollapsed {
61+
window?.titleVisibility = .visible
62+
window?.title = workspace?.workspaceFileManager?.folderUrl.lastPathComponent ?? "Empty"
63+
window?.toolbar = nil
64+
} else {
65+
window?.titleVisibility = .hidden
66+
setupToolbar()
67+
}
68+
}
69+
70+
func toolbar(
71+
_ toolbar: NSToolbar,
72+
itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier,
73+
willBeInsertedIntoToolbar flag: Bool
74+
) -> NSToolbarItem? {
75+
switch itemIdentifier {
76+
case .itemListTrackingSeparator:
77+
guard let splitViewController else { return nil }
78+
79+
return NSTrackingSeparatorToolbarItem(
80+
identifier: .itemListTrackingSeparator,
81+
splitView: splitViewController.splitView,
82+
dividerIndex: 1
83+
)
84+
case .toggleFirstSidebarItem:
85+
let toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier.toggleFirstSidebarItem)
86+
toolbarItem.label = "Navigator Sidebar"
87+
toolbarItem.paletteLabel = " Navigator Sidebar"
88+
toolbarItem.toolTip = "Hide or show the Navigator"
89+
toolbarItem.isBordered = true
90+
toolbarItem.target = self
91+
toolbarItem.action = #selector(self.toggleFirstPanel)
92+
toolbarItem.image = NSImage(
93+
systemSymbolName: "sidebar.leading",
94+
accessibilityDescription: nil
95+
)?.withSymbolConfiguration(.init(scale: .large))
96+
97+
return toolbarItem
98+
case .toggleLastSidebarItem:
99+
let toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier.toggleLastSidebarItem)
100+
toolbarItem.label = "Inspector Sidebar"
101+
toolbarItem.paletteLabel = "Inspector Sidebar"
102+
toolbarItem.toolTip = "Hide or show the Inspectors"
103+
toolbarItem.isBordered = true
104+
toolbarItem.target = self
105+
toolbarItem.action = #selector(self.toggleLastPanel)
106+
toolbarItem.image = NSImage(
107+
systemSymbolName: "sidebar.trailing",
108+
accessibilityDescription: nil
109+
)?.withSymbolConfiguration(.init(scale: .large))
110+
111+
return toolbarItem
112+
case .branchPicker:
113+
let toolbarItem = NSToolbarItem(itemIdentifier: .branchPicker)
114+
let view = NSHostingView(
115+
rootView: ToolbarBranchPicker(
116+
workspaceFileManager: workspace?.workspaceFileManager
117+
)
118+
)
119+
toolbarItem.view = view
120+
121+
return toolbarItem
122+
123+
default:
124+
return NSToolbarItem(itemIdentifier: itemIdentifier)
125+
}
126+
}
127+
}

CodeEdit/Features/Documents/Controllers/CodeEditWindowController.swift

+3-120
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate, Obs
2929

3030
internal var cancellables = [AnyCancellable]()
3131

32-
init(window: NSWindow, workspace: WorkspaceDocument) {
32+
init(window: NSWindow?, workspace: WorkspaceDocument?) {
3333
super.init(window: window)
34+
guard let workspace else { return }
3435
self.workspace = workspace
3536
self.workspaceSettings = CEWorkspaceSettings(workspaceDocument: workspace)
3637
setupSplitView(with: workspace)
@@ -46,7 +47,7 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate, Obs
4647
self?.navigatorCollapsed = item.isCollapsed
4748
}),
4849
splitViewController.splitViewItems.last!.observe(\.isCollapsed, changeHandler: { [weak self] item, _ in
49-
self?.navigatorCollapsed = item.isCollapsed
50+
self?.inspectorCollapsed = item.isCollapsed
5051
})
5152
]
5253

@@ -118,124 +119,6 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate, Obs
118119
self.listenToDocumentEdited(workspace: workspace)
119120
}
120121

121-
private func setupToolbar() {
122-
let toolbar = NSToolbar(identifier: UUID().uuidString)
123-
toolbar.delegate = self
124-
toolbar.displayMode = .labelOnly
125-
toolbar.showsBaselineSeparator = false
126-
self.window?.titleVisibility = toolbarCollapsed ? .visible : .hidden
127-
self.window?.toolbarStyle = .unifiedCompact
128-
if Settings[\.general].tabBarStyle == .native {
129-
// Set titlebar background as transparent by default in order to
130-
// style the toolbar background in native tab bar style.
131-
self.window?.titlebarSeparatorStyle = .none
132-
} else {
133-
// In Xcode tab bar style, we use default toolbar background with
134-
// line separator.
135-
self.window?.titlebarSeparatorStyle = .automatic
136-
}
137-
self.window?.toolbar = toolbar
138-
}
139-
140-
// MARK: - Toolbar
141-
142-
func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
143-
[
144-
.toggleFirstSidebarItem,
145-
.sidebarTrackingSeparator,
146-
.branchPicker,
147-
.flexibleSpace,
148-
.itemListTrackingSeparator,
149-
.flexibleSpace,
150-
.toggleLastSidebarItem
151-
]
152-
}
153-
154-
func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
155-
[
156-
.toggleFirstSidebarItem,
157-
.sidebarTrackingSeparator,
158-
.flexibleSpace,
159-
.itemListTrackingSeparator,
160-
.toggleLastSidebarItem,
161-
.branchPicker
162-
]
163-
}
164-
165-
func toggleToolbar() {
166-
toolbarCollapsed.toggle()
167-
updateToolbarVisibility()
168-
}
169-
170-
private func updateToolbarVisibility() {
171-
if toolbarCollapsed {
172-
window?.titleVisibility = .visible
173-
window?.title = workspace?.workspaceFileManager?.folderUrl.lastPathComponent ?? "Empty"
174-
window?.toolbar = nil
175-
} else {
176-
window?.titleVisibility = .hidden
177-
setupToolbar()
178-
}
179-
}
180-
181-
func toolbar(
182-
_ toolbar: NSToolbar,
183-
itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier,
184-
willBeInsertedIntoToolbar flag: Bool
185-
) -> NSToolbarItem? {
186-
switch itemIdentifier {
187-
case .itemListTrackingSeparator:
188-
guard let splitViewController else { return nil }
189-
190-
return NSTrackingSeparatorToolbarItem(
191-
identifier: .itemListTrackingSeparator,
192-
splitView: splitViewController.splitView,
193-
dividerIndex: 1
194-
)
195-
case .toggleFirstSidebarItem:
196-
let toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier.toggleFirstSidebarItem)
197-
toolbarItem.label = "Navigator Sidebar"
198-
toolbarItem.paletteLabel = " Navigator Sidebar"
199-
toolbarItem.toolTip = "Hide or show the Navigator"
200-
toolbarItem.isBordered = true
201-
toolbarItem.target = self
202-
toolbarItem.action = #selector(self.toggleFirstPanel)
203-
toolbarItem.image = NSImage(
204-
systemSymbolName: "sidebar.leading",
205-
accessibilityDescription: nil
206-
)?.withSymbolConfiguration(.init(scale: .large))
207-
208-
return toolbarItem
209-
case .toggleLastSidebarItem:
210-
let toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier.toggleLastSidebarItem)
211-
toolbarItem.label = "Inspector Sidebar"
212-
toolbarItem.paletteLabel = "Inspector Sidebar"
213-
toolbarItem.toolTip = "Hide or show the Inspectors"
214-
toolbarItem.isBordered = true
215-
toolbarItem.target = self
216-
toolbarItem.action = #selector(self.toggleLastPanel)
217-
toolbarItem.image = NSImage(
218-
systemSymbolName: "sidebar.trailing",
219-
accessibilityDescription: nil
220-
)?.withSymbolConfiguration(.init(scale: .large))
221-
222-
return toolbarItem
223-
case .branchPicker:
224-
let toolbarItem = NSToolbarItem(itemIdentifier: .branchPicker)
225-
let view = NSHostingView(
226-
rootView: ToolbarBranchPicker(
227-
workspaceFileManager: workspace?.workspaceFileManager
228-
)
229-
)
230-
toolbarItem.view = view
231-
232-
return toolbarItem
233-
234-
default:
235-
return NSToolbarItem(itemIdentifier: itemIdentifier)
236-
}
237-
}
238-
239122
private func getSelectedCodeFile() -> CodeFileDocument? {
240123
workspace?.editorManager.activeEditor.selectedTab?.file.fileDocument
241124
}

0 commit comments

Comments
 (0)