Skip to content

Commit

Permalink
Make EditViewController show/hide status bar
Browse files Browse the repository at this point in the history
  • Loading branch information
nangtrongvuon committed Dec 17, 2019
1 parent 03130e5 commit c424915
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 39 deletions.
4 changes: 2 additions & 2 deletions Sources/XiEditor/ClientImplementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class ClientImplementation: XiClient, DocumentsProviding, ConfigCacheProviding,
let document = documentForViewIdentifier(viewIdentifier: viewIdentifier)
DispatchQueue.main.async {
let newStatusItem = StatusItem(source, key, value, alignment)
document?.editViewController?.statusBar.addStatusItem(newStatusItem)
document?.editViewController?.addStatusItem(newStatusItem)
}
}

Expand All @@ -164,7 +164,7 @@ class ClientImplementation: XiClient, DocumentsProviding, ConfigCacheProviding,
func removeStatusItem(viewIdentifier: String, key: String) {
let document = documentForViewIdentifier(viewIdentifier: viewIdentifier)
DispatchQueue.main.async {
document?.editViewController?.statusBar.removeStatusItem(key)
document?.editViewController?.removeStatusItem(key)
}
}

Expand Down
2 changes: 0 additions & 2 deletions Sources/XiEditor/EditViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ class EditViewController: NSViewController, EditViewDataSource, FindDelegate, Sc

override func viewDidLoad() {
super.viewDidLoad()
statusBar.delegate = self
shadowView.wantsLayer = true
editView.dataSource = self
editContainerView.contextMenu = contextMenu
Expand Down Expand Up @@ -273,7 +272,6 @@ class EditViewController: NSViewController, EditViewDataSource, FindDelegate, Sc

func setupStatusBar() {
statusBar.hasUnifiedTitlebar = unifiedTitlebar
hideStatusBar()
}

func updateGutterWidth() {
Expand Down
63 changes: 28 additions & 35 deletions Sources/XiEditor/StatusBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
import Foundation
import Cocoa

protocol StatusBarDelegate: class {
func showStatusBar()
func hideStatusBar()
}

enum StatusItemAlignment: String {
case left = "left"
case right = "right"
Expand Down Expand Up @@ -57,9 +52,6 @@ class StatusItem: NSTextField {
}

class StatusBar: NSView {

weak var delegate: StatusBarDelegate?

var currentItems = [String: StatusItem]()
var hiddenItems: [StatusItem] {
return currentItems.values
Expand Down Expand Up @@ -122,7 +114,6 @@ class StatusBar: NSView {
currentItems[item.key] = item
self.needsUpdateConstraints = true
checkItemsFitFor(windowWidth: self.bounds.width)
updateStatusBarVisibility()
}

// Update a status bar item with a new value.
Expand All @@ -143,7 +134,6 @@ class StatusBar: NSView {
currentItems.removeValue(forKey: key)
self.needsUpdateConstraints = true
checkItemsFitFor(windowWidth: self.bounds.width)
updateStatusBarVisibility()
} else {
print("tried to remove item with \(key) that doesn't exist")
return
Expand Down Expand Up @@ -210,27 +200,6 @@ class StatusBar: NSView {
self.needsDisplay = true
}

// Shows/hides the statusbar based on whether it has any contents.
func updateStatusBarVisibility() {
if !currentItems.isEmpty {
self.hideTimer?.invalidate()
delegate?.showStatusBar()
self.isHidden = false
} else if !self.isHidden {
// Wait a moment before hiding the bar to avoid blinking in the case
// that a single item is added and removed repeatedly in rapid succession.
if #available(OSX 10.12, *) {
hideTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { _ in
self.delegate?.hideStatusBar()
self.isHidden = true
}
} else {
delegate?.hideStatusBar()
self.isHidden = true
}
}
}

// When items are added, expanded or removed, the status bar checks if
// any items need to be hidden or unhidden to make use of available space.
func checkItemsFitFor(windowWidth: CGFloat) {
Expand Down Expand Up @@ -287,12 +256,36 @@ class StatusBar: NSView {
}
}

extension EditViewController: StatusBarDelegate {
func showStatusBar() {
extension EditViewController {
func addStatusItem(_ item: StatusItem) {
self.statusBar.addStatusItem(item)
self.updateStatusBarVisibility()
}

func removeStatusItem(_ key: String) {
self.statusBar.removeStatusItem(key)
self.updateStatusBarVisibility()
}

// Shows/hides the statusbar based on whether it has any contents.
func updateStatusBarVisibility() {
if !statusBar.currentItems.isEmpty {
self.showStatusBar()
} else if statusBarHeight.constant > 0 {
// If status bar is currently showing, hide it after a delay.
Timer.scheduledTimer(timeInterval: 1.0,
target: self,
selector: #selector(hideStatusBar),
userInfo: nil,
repeats: false)
}
}

private func showStatusBar() {
statusBarHeight.constant = StatusBar.statusBarHeight
}
func hideStatusBar() {

@objc private func hideStatusBar() {
statusBarHeight.constant = 0
}
}

0 comments on commit c424915

Please sign in to comment.