Skip to content

Commit

Permalink
Select project from previous session when launching Hearth
Browse files Browse the repository at this point in the history
Fixes #94
  • Loading branch information
thomassnielsen committed May 31, 2015
1 parent e1f7f6d commit b470eef
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Ember Hearth/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import Sparkle
import MASPreferences
import MASShortcut

let currentProjectPathKey = "currentProjectPathKey"

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
var projects: [Project]?
var activeProject: Project? {
didSet {
if activeProject != nil {
NSUserDefaults.standardUserDefaults().setObject(activeProject!.path, forKey: currentProjectPathKey)
NSNotificationCenter.defaultCenter().postNotificationName("activeProjectSet", object: activeProject)
} else {
NSNotificationCenter.defaultCenter().postNotificationName("noActiveProject", object: activeProject)
Expand Down
21 changes: 20 additions & 1 deletion Ember Hearth/ProjectListController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ class ProjectListController: NSViewController, NSTableViewDataSource, NSTableVie
refreshList(nil)
}

override func viewDidAppear() {
let delegate = NSApplication.sharedApplication().delegate! as! AppDelegate
if (self.tableView.selectedRow < 0 && delegate.activeProject != nil && self.projects != nil) {
if let index = find(self.projects!, delegate.activeProject!) {
self.tableView.selectRowIndexes(NSIndexSet(index: index), byExtendingSelection: false)
}
}
}

// MARK: Drag-and-drop
func dragEntered(notification: NSNotification?) {
selectedRow = self.tableView.selectedRow
Expand Down Expand Up @@ -212,7 +221,17 @@ class ProjectListController: NSViewController, NSTableViewDataSource, NSTableVie
if indexOfActiveProject != nil {
self.tableView.selectRowIndexes(NSIndexSet(index: indexOfActiveProject!), byExtendingSelection: false)
}
} else {
} else if let currentProjectPath = NSUserDefaults.standardUserDefaults().stringForKey(currentProjectPathKey) {
if let projects = self.projects {
for project in projects {
if project.path == currentProjectPath {
appDelegate.activeProject = project
}
}
}
}

if appDelegate.activeProject == nil {
appDelegate.activeProject = projects?.first
}
}
Expand Down

0 comments on commit b470eef

Please sign in to comment.