Skip to content

Commit

Permalink
adding ability to hide toolbar (#1683)
Browse files Browse the repository at this point in the history
* hide toolbar button and command

* removed debug funcs

* fix swiftlint violations
  • Loading branch information
knotbin authored Apr 25, 2024
1 parent 70cb34a commit ea2ec6f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate, Obs

@Published var navigatorCollapsed = false
@Published var inspectorCollapsed = false
@Published var toolbarCollapsed = false

var observers: [NSKeyValueObservation] = []

Expand All @@ -31,9 +32,7 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate, Obs
init(window: NSWindow, workspace: WorkspaceDocument) {
super.init(window: window)
self.workspace = workspace
self.workspaceSettings = CEWorkspaceSettings(
workspaceDocument: workspace
)
self.workspaceSettings = CEWorkspaceSettings(workspaceDocument: workspace)
setupSplitView(with: workspace)

let view = CodeEditSplitView(controller: splitViewController).ignoresSafeArea()
Expand All @@ -55,9 +54,7 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate, Obs
registerCommands()
}

deinit {
cancellables.forEach({ $0.cancel() })
}
deinit { cancellables.forEach({ $0.cancel() }) }

@available(*, unavailable)
required init?(coder: NSCoder) {
Expand Down Expand Up @@ -126,7 +123,7 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate, Obs
toolbar.delegate = self
toolbar.displayMode = .labelOnly
toolbar.showsBaselineSeparator = false
self.window?.titleVisibility = .hidden
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
Expand Down Expand Up @@ -165,16 +162,30 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate, Obs
]
}

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
}
guard let splitViewController else { return nil }

return NSTrackingSeparatorToolbarItem(
identifier: .itemListTrackingSeparator,
Expand Down Expand Up @@ -219,6 +230,7 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate, Obs
toolbarItem.view = view

return toolbarItem

default:
return NSToolbarItem(itemIdentifier: itemIdentifier)
}
Expand Down
11 changes: 11 additions & 0 deletions CodeEdit/Features/WindowCommands/ViewCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ struct ViewCommands: Commands {
windowController?.navigatorCollapsed ?? false
}

var toolbarCollapsed: Bool {
windowController?.toolbarCollapsed ?? false
}

var body: some Commands {

CommandGroup(after: .toolbar) {
Button("Show Command Palette") {
NSApp.sendAction(#selector(CodeEditWindowController.openCommandPalette(_:)), to: nil, from: nil)
Expand Down Expand Up @@ -102,6 +107,12 @@ struct ViewCommands: Commands {
.disabled(windowController == nil)
.keyboardShortcut("y", modifiers: [.shift, .command])

Button("\(navigatorCollapsed ? "Show" : "Hide") Toolbar") {
windowController?.toggleToolbar()
}
.disabled(windowController == nil)
.keyboardShortcut("t", modifiers: [.option, .command])

Divider()

Button("\(showEditorPathBar ? "Hide" : "Show") Path Bar") {
Expand Down

0 comments on commit ea2ec6f

Please sign in to comment.