Add workspace center

This commit is contained in:
Matthew Dapena-Tretter
2017-03-10 16:42:11 -08:00
parent 62926e6b5b
commit 7f5ad9a359
2 changed files with 290 additions and 0 deletions

279
src/workspace-center.js Normal file
View File

@@ -0,0 +1,279 @@
'use strict'
const TextEditor = require('./text-editor')
module.exports = class WorkspaceCenter {
constructor (paneContainer) {
this.paneContainer = paneContainer
}
/*
Section: Event Subscription
*/
// Essential: Invoke the given callback with all current and future text
// editors in the workspace.
//
// * `callback` {Function} to be called with current and future text editors.
// * `editor` An {TextEditor} that is present in {::getTextEditors} at the time
// of subscription or that is added at some later time.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observeTextEditors (callback) {
for (let textEditor of this.getTextEditors()) { callback(textEditor) }
return this.onDidAddTextEditor(({textEditor}) => callback(textEditor))
}
// Essential: Invoke the given callback with all current and future panes items
// in the workspace.
//
// * `callback` {Function} to be called with current and future pane items.
// * `item` An item that is present in {::getPaneItems} at the time of
// subscription or that is added at some later time.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observePaneItems (callback) { return this.paneContainer.observePaneItems(callback) }
// Essential: Invoke the given callback when the active pane item changes.
//
// Because observers are invoked synchronously, it's important not to perform
// any expensive operations via this method. Consider
// {::onDidStopChangingActivePaneItem} to delay operations until after changes
// stop occurring.
//
// * `callback` {Function} to be called when the active pane item changes.
// * `item` The active pane item.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidChangeActivePaneItem (callback) {
return this.paneContainer.onDidChangeActivePaneItem(callback)
}
// Essential: Invoke the given callback when the active pane item stops
// changing.
//
// Observers are called asynchronously 100ms after the last active pane item
// change. Handling changes here rather than in the synchronous
// {::onDidChangeActivePaneItem} prevents unneeded work if the user is quickly
// changing or closing tabs and ensures critical UI feedback, like changing the
// highlighted tab, gets priority over work that can be done asynchronously.
//
// * `callback` {Function} to be called when the active pane item stopts
// changing.
// * `item` The active pane item.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidStopChangingActivePaneItem (callback) {
return this.paneContainer.onDidStopChangingActivePaneItem(callback)
}
// Essential: Invoke the given callback with the current active pane item and
// with all future active pane items in the workspace.
//
// * `callback` {Function} to be called when the active pane item changes.
// * `item` The current active pane item.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observeActivePaneItem (callback) {
return this.paneContainer.observeActivePaneItem(callback)
}
// Extended: Invoke the given callback when a pane is added to the workspace.
//
// * `callback` {Function} to be called panes are added.
// * `event` {Object} with the following keys:
// * `pane` The added pane.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidAddPane (callback) {
return this.paneContainer.onDidAddPane(callback)
}
// Extended: Invoke the given callback before a pane is destroyed in the
// workspace.
//
// * `callback` {Function} to be called before panes are destroyed.
// * `event` {Object} with the following keys:
// * `pane` The pane to be destroyed.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onWillDestroyPane (callback) {
return this.paneContainer.onWillDestroyPane(callback)
}
// Extended: Invoke the given callback when a pane is destroyed in the
// workspace.
//
// * `callback` {Function} to be called panes are destroyed.
// * `event` {Object} with the following keys:
// * `pane` The destroyed pane.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidDestroyPane (callback) {
return this.paneContainer.onDidDestroyPane(callback)
}
// Extended: Invoke the given callback with all current and future panes in the
// workspace.
//
// * `callback` {Function} to be called with current and future panes.
// * `pane` A {Pane} that is present in {::getPanes} at the time of
// subscription or that is added at some later time.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observePanes (callback) {
return this.paneContainer.observePanes(callback)
}
// Extended: Invoke the given callback when the active pane changes.
//
// * `callback` {Function} to be called when the active pane changes.
// * `pane` A {Pane} that is the current return value of {::getActivePane}.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidChangeActivePane (callback) {
return this.paneContainer.onDidChangeActivePane(callback)
}
// Extended: Invoke the given callback with the current active pane and when
// the active pane changes.
//
// * `callback` {Function} to be called with the current and future active#
// panes.
// * `pane` A {Pane} that is the current return value of {::getActivePane}.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observeActivePane (callback) {
return this.paneContainer.observeActivePane(callback)
}
// Extended: Invoke the given callback when a pane item is added to the
// workspace.
//
// * `callback` {Function} to be called when pane items are added.
// * `event` {Object} with the following keys:
// * `item` The added pane item.
// * `pane` {Pane} containing the added item.
// * `index` {Number} indicating the index of the added item in its pane.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidAddPaneItem (callback) {
return this.paneContainer.onDidAddPaneItem(callback)
}
// Extended: Invoke the given callback when a pane item is about to be
// destroyed, before the user is prompted to save it.
//
// * `callback` {Function} to be called before pane items are destroyed.
// * `event` {Object} with the following keys:
// * `item` The item to be destroyed.
// * `pane` {Pane} containing the item to be destroyed.
// * `index` {Number} indicating the index of the item to be destroyed in
// its pane.
//
// Returns a {Disposable} on which `.dispose` can be called to unsubscribe.
onWillDestroyPaneItem (callback) {
return this.paneContainer.onWillDestroyPaneItem(callback)
}
// Extended: Invoke the given callback when a pane item is destroyed.
//
// * `callback` {Function} to be called when pane items are destroyed.
// * `event` {Object} with the following keys:
// * `item` The destroyed item.
// * `pane` {Pane} containing the destroyed item.
// * `index` {Number} indicating the index of the destroyed item in its
// pane.
//
// Returns a {Disposable} on which `.dispose` can be called to unsubscribe.
onDidDestroyPaneItem (callback) {
return this.paneContainer.onDidDestroyPaneItem(callback)
}
/*
Section: Pane Items
*/
// Essential: Get all pane items in the workspace.
//
// Returns an {Array} of items.
getPaneItems () {
return this.paneContainer.getPaneItems()
}
// Essential: Get the active {Pane}'s active item.
//
// Returns an pane item {Object}.
getActivePaneItem () {
return this.paneContainer.getActivePaneItem()
}
// Essential: Get all text editors in the workspace.
//
// Returns an {Array} of {TextEditor}s.
getTextEditors () {
return this.getPaneItems().filter(item => item instanceof TextEditor)
}
// Essential: Get the active item if it is an {TextEditor}.
//
// Returns an {TextEditor} or `undefined` if the current active item is not an
// {TextEditor}.
getActiveTextEditor () {
const activeItem = this.getActivePaneItem()
if (activeItem instanceof TextEditor) { return activeItem }
}
// Save all pane items.
saveAll () {
this.paneContainer.saveAll()
}
confirmClose (options) {
return this.paneContainer.confirmClose(options)
}
/*
Section: Panes
*/
// Extended: Get all panes in the workspace.
//
// Returns an {Array} of {Pane}s.
getPanes () {
return this.paneContainer.getPanes()
}
// Extended: Get the active {Pane}.
//
// Returns a {Pane}.
getActivePane () {
return this.paneContainer.getActivePane()
}
// Extended: Make the next pane active.
activateNextPane () {
return this.paneContainer.activateNextPane()
}
// Extended: Make the previous pane active.
activatePreviousPane () {
return this.paneContainer.activatePreviousPane()
}
paneForURI (uri) {
return this.paneContainer.paneForURI(uri)
}
paneForItem (item) {
return this.paneContainer.paneForItem(item)
}
// Destroy (close) the active pane.
destroyActivePane () {
const activePane = this.getActivePane()
if (activePane != null) {
activePane.destroy()
}
}
}

View File

@@ -13,6 +13,7 @@ const PaneContainer = require('./pane-container')
const Panel = require('./panel')
const PanelContainer = require('./panel-container')
const Task = require('./task')
const WorkspaceCenter = require('./workspace-center')
// Essential: Represents the state of the user interface for the entire window.
// An instance of this class is available via the `atom.workspace` global.
@@ -57,6 +58,8 @@ module.exports = class Workspace extends Model {
this.defaultDirectorySearcher = new DefaultDirectorySearcher()
this.consumeServices(this.packageManager)
this.center = new WorkspaceCenter(this.paneContainer)
this.panelContainers = {
top: new PanelContainer({location: 'top'}),
left: new PanelContainer({location: 'left'}),
@@ -1008,6 +1011,14 @@ module.exports = class Workspace extends Model {
}
}
/*
Section: Pane Locations
*/
getCenter () {
return this.center
}
/*
Section: Panels