Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse empty views when opening a new document #171

Merged
merged 2 commits into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion XiEditor/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
var dispatcher: Dispatcher?
var styleMap: StyleMap = StyleMap()

/// used internally to force open to an existing empty document when present.
var _documentForNextOpenCall: Document?

func applicationWillFinishLaunching(_ aNotification: Notification) {

guard let corePath = Bundle.main.path(forResource: "xi-core", ofType: "")
Expand All @@ -46,7 +49,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
return nil
}

func handleCoreCmd(_ json: Any) {
guard let obj = json as? [String : Any],
let method = obj["method"] as? String,
Expand Down
44 changes: 34 additions & 10 deletions XiEditor/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class Document: NSDocument {
var pendingNotifications: [PendingNotification] = [];
var editViewController: EditViewController?

/// used to keep track of whether we're in the process of reusing an empty window
fileprivate var _skipShowingWindow = false

override init() {
dispatcher = (NSApplication.shared().delegate as? AppDelegate)?.dispatcher
super.init()
Expand All @@ -44,25 +47,46 @@ class Document: NSDocument {
}

override func makeWindowControllers() {
// Returns the Storyboard that contains your Document window.
let storyboard = NSStoryboard(name: "Main", bundle: nil)
let windowController = storyboard.instantiateController(withIdentifier: "Document Window Controller") as! NSWindowController
var windowController: NSWindowController!
// check to see if we should reuse another document's window
if let delegate = (NSApplication.shared().delegate as? AppDelegate), let existing = delegate._documentForNextOpenCall {
assert(existing.windowControllers.count == 1, "each document should only and always own a single windowController")
windowController = existing.windowControllers[0]
delegate._documentForNextOpenCall = nil
// if we're reusing an existing window, we want to noop on the `showWindows()` call we receive from the DocumentController
_skipShowingWindow = true
} else {
// if we aren't reusing, create a new window as normal:
let storyboard = NSStoryboard(name: "Main", bundle: nil)
windowController = storyboard.instantiateController(withIdentifier: "Document Window Controller") as! NSWindowController

//FIXME: some saner way of positioning new windows. maybe based on current window size, with some checks to not completely obscure an existing window?
// also awareness of multiple screens (prefer to open on currently active screen)
let screenHeight = windowController.window?.screen?.frame.height ?? 800
let windowHeight: CGFloat = 800
windowController.window?.setFrame(NSRect(x: 200, y: screenHeight - windowHeight - 200, width: 700, height: 800), display: true)
}

self.editViewController = windowController.contentViewController as? EditViewController
editViewController?.document = self
windowController.window?.delegate = editViewController

self.addWindowController(windowController)

Events.NewTab().dispatchWithCallback(dispatcher!) { (tabName) in
DispatchQueue.main.async {
self.tabName = tabName
}
}
//FIXME: some saner way of positioning new windows. maybe based on current window size, with some checks to not completely obscure an existing window?
// also awareness of multiple screens (prefer to open on currently active screen)
let screenHeight = windowController.window?.screen?.frame.height ?? 800
let windowHeight: CGFloat = 800
windowController.window?.setFrame(NSRect(x: 200, y: screenHeight - windowHeight - 200, width: 700, height: 800), display: true)
}

self.addWindowController(windowController)
override func showWindows() {
// part of our code to reuse existing windows when opening documents
assert(windowControllers.count == 1, "documents should have a single window controller")
if !(_skipShowingWindow) {
super.showWindows()
} else {
_skipShowingWindow = false
}
}

override func read(from url: URL, ofType typeName: String) throws {
Expand Down
8 changes: 8 additions & 0 deletions XiEditor/EditViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ class EditViewController: NSViewController {
document?.sendRpcAsync("insert", params: insertedStringToJson(insertString as! NSString))
}

// we override this to see if our view is empty, and should be reused for this open call
func openDocument(_ sender: Any?) {
if editView?.lines.isEmpty ?? false {
(NSApplication.shared().delegate as? AppDelegate)?._documentForNextOpenCall = self.document
}
NSDocumentController.shared().openDocument(sender)
}

// MARK: - Menu Items
fileprivate func cutCopy(_ method: String) {
let text = document?.sendRpc(method, params: [])
Expand Down
7 changes: 7 additions & 0 deletions XiEditor/LineCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ class LineCache {
return nInvalidBefore + lines.count + nInvalidAfter
}
}

/// A boolean value indicating whether or not the linecache contains any text.
/// - Note: An empty line cache will still contain a single empty line, this
/// is sent as an update from the core after a new document is created.
var isEmpty: Bool {
return lines.count == 1 && lines[0]?.text == ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A case can be made this should return true if lines.count == 0 as well. Unlikely to happen much in practice, but maybe could happen when startup / file opening is scripted.

}

func get(_ ix: Int) -> Line? {
if ix < nInvalidBefore { return nil }
Expand Down