Skip to content

Commit

Permalink
Fix sidebar item not getting selected
Browse files Browse the repository at this point in the history
  • Loading branch information
lukakerr committed Mar 10, 2019
1 parent ffc1985 commit 37d8d10
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
44 changes: 31 additions & 13 deletions Pine/Controllers/SidebarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class SidebarViewController: NSViewController {
// The structure is only set once on first load
var structureHasSynced: Bool = false

// Whether to ignore the next row selection
var ignoreNextSelection: Bool = false

/// The split view item holding this sidebar view controller
private var sidebarSplitViewItem: NSSplitViewItem? {
return (parent as? NSSplitViewController)?.splitViewItems.first
Expand Down Expand Up @@ -49,8 +52,6 @@ class SidebarViewController: NSViewController {
}

public func sync() {
let selectedRows = sidebar.selectedRowIndexes

items = openDocuments.getDocuments()
sidebar.reloadData()

Expand All @@ -59,7 +60,7 @@ class SidebarViewController: NSViewController {
structureHasSynced = true
}

sidebar.selectRowIndexes(selectedRows, byExtendingSelection: false)
syncSelectedRow()
}

@IBAction func toggleSidebar(_ sender: NSButton) {
Expand Down Expand Up @@ -108,6 +109,25 @@ class SidebarViewController: NSViewController {
}
}

private func syncSelectedRow() {
let doc = windowController?.document as? Document

var selectedRow: Int?

for row in IndexSet(integersIn: 0..<sidebar.numberOfRows) {
let rowItem = sidebar.item(atRow: row) as? FileSystemItem

if rowItem?.fullPath == doc?.fileURL?.relativePath {
selectedRow = row
}
}

if let selected = selectedRow {
ignoreNextSelection = true
sidebar.selectRowIndexes(IndexSet(integer: selected), byExtendingSelection: false)
}
}

/// Set a contextual menu (called on right click) for the sidebar
private func setupContextualMenu() {
let menu = NSMenu()
Expand Down Expand Up @@ -151,14 +171,10 @@ class SidebarViewController: NSViewController {

var backgroundColor: NSColor = .clear

if preferences[Preference.useThemeColorForSidebar] {
if preferences[Preference.useThemeColorForSidebar] && !preferences[Preference.useSystemAppearance] {
backgroundColor = theme.background
}

if preferences[Preference.useSystemAppearance] {
backgroundColor = .clear
}

sidebar.backgroundColor = backgroundColor
sidebarActionsView.setBackgroundColor(backgroundColor)
}
Expand Down Expand Up @@ -215,14 +231,16 @@ extension SidebarViewController: NSOutlineViewDataSource {

// When a row is selected
func outlineViewSelectionDidChange(_ notification: Notification) {
guard
let doc = sidebar.item(atRow: sidebar.selectedRow) as? FileSystemItem,
let window = view.window?.windowController as? PineWindowController
else { return }
if ignoreNextSelection {
ignoreNextSelection = false
return
}

guard let doc = sidebar.item(atRow: sidebar.selectedRow) as? FileSystemItem else { return }

if doc.isDirectory { return }

window.changeDocument(file: doc.fileURL)
windowController?.changeDocument(file: doc.fileURL)
}

/// The NSTableRowView instance to be used
Expand Down
16 changes: 4 additions & 12 deletions Pine/Views/MarkdownView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ class MarkdownView: NSView {
}

private func updateUI() {
var appAppearance = window?.appearance?.name

if #available(OSX 10.14, *) {
appAppearance = NSApp.effectiveAppearance.name
}

self.window?.titlebarAppearsTransparent = preferences[Preference.modernTitlebar]

// Not using system appearance, so stick with theme
Expand All @@ -32,12 +26,10 @@ class MarkdownView: NSView {
)
}
} else {
if let appearance = appAppearance {
setThemeAndWindowAppearance(
isDark: appearance == .dark,
color: NSColor.textBackgroundColor
)
}
setThemeAndWindowAppearance(
isDark: NSAppearance.Name.current == .dark,
color: NSColor.textBackgroundColor
)

window?.appearance = nil
}
Expand Down

0 comments on commit 37d8d10

Please sign in to comment.