mirror of
https://github.com/atom/atom.git
synced 2026-01-29 08:48:17 -05:00
149 lines
4.5 KiB
CoffeeScript
149 lines
4.5 KiB
CoffeeScript
fs = require 'fs'
|
|
_ = require 'underscore'
|
|
Package = require 'package'
|
|
TextMatePackage = require 'text-mate-package'
|
|
Theme = require 'theme'
|
|
LoadTextMatePackagesTask = require 'load-text-mate-packages-task'
|
|
|
|
messageIdCounter = 1
|
|
originalSendMessageToBrowserProcess = atom.sendMessageToBrowserProcess
|
|
|
|
_.extend atom,
|
|
exitWhenDone: window.location.params.exitWhenDone
|
|
loadedThemes: []
|
|
pendingBrowserProcessCallbacks: {}
|
|
loadedPackages: []
|
|
|
|
loadPackage: (name, options) ->
|
|
packagePath = _.find @getPackagePaths(), (packagePath) -> fs.base(packagePath) == name
|
|
pack = Package.build(packagePath)
|
|
pack?.load(options)
|
|
|
|
loadPackages: ->
|
|
textMatePackages = []
|
|
for path in @getPackagePaths()
|
|
pack = Package.build(path)
|
|
@loadedPackages.push(pack)
|
|
if pack instanceof TextMatePackage and fs.base(pack.path) isnt 'text.tmbundle'
|
|
textMatePackages.push(pack)
|
|
else
|
|
pack.load()
|
|
|
|
new LoadTextMatePackagesTask(textMatePackages).start() if textMatePackages.length > 0
|
|
|
|
getLoadedPackages: ->
|
|
_.clone(@loadedPackages)
|
|
|
|
getPackagePaths: ->
|
|
disabledPackages = config.get("core.disabledPackages") ? []
|
|
packagePaths = []
|
|
for packageDirPath in config.packageDirPaths
|
|
for packagePath in fs.list(packageDirPath)
|
|
continue if not fs.isDirectory(packagePath)
|
|
continue if fs.base(packagePath) in disabledPackages
|
|
continue if packagePath in packagePaths
|
|
packagePaths.push(packagePath)
|
|
|
|
packagePaths
|
|
|
|
loadThemes: ->
|
|
themeNames = config.get("core.themes") ? ['atom-dark-ui', 'atom-dark-syntax']
|
|
themeNames = [themeNames] unless _.isArray(themeNames)
|
|
@loadTheme(themeName) for themeName in themeNames
|
|
@loadUserStylesheet()
|
|
|
|
loadTheme: (name) ->
|
|
@loadedThemes.push Theme.load(name)
|
|
|
|
loadUserStylesheet: ->
|
|
userStylesheetPath = fs.join(config.configDirPath, 'user.css')
|
|
if fs.isFile(userStylesheetPath)
|
|
applyStylesheet(userStylesheetPath, fs.read(userStylesheetPath), 'userTheme')
|
|
|
|
getAtomThemeStylesheets: ->
|
|
themeNames = config.get("core.themes") ? ['atom-dark-ui', 'atom-dark-syntax']
|
|
themeNames = [themeNames] unless _.isArray(themeNames)
|
|
|
|
open: (args...) ->
|
|
@sendMessageToBrowserProcess('open', args)
|
|
|
|
openUnstable: (args...) ->
|
|
@sendMessageToBrowserProcess('openUnstable', args)
|
|
|
|
newWindow: (args...) ->
|
|
@sendMessageToBrowserProcess('newWindow', args)
|
|
|
|
confirm: (message, detailedMessage, buttonLabelsAndCallbacks...) ->
|
|
args = [message, detailedMessage]
|
|
callbacks = []
|
|
while buttonLabelsAndCallbacks.length
|
|
args.push(buttonLabelsAndCallbacks.shift())
|
|
callbacks.push(buttonLabelsAndCallbacks.shift())
|
|
@sendMessageToBrowserProcess('confirm', args, callbacks)
|
|
|
|
showSaveDialog: (callback) ->
|
|
@sendMessageToBrowserProcess('showSaveDialog', [], callback)
|
|
|
|
toggleDevTools: ->
|
|
@sendMessageToBrowserProcess('toggleDevTools')
|
|
|
|
showDevTools: ->
|
|
@sendMessageToBrowserProcess('showDevTools')
|
|
|
|
focus: ->
|
|
@sendMessageToBrowserProcess('focus')
|
|
|
|
show: ->
|
|
@sendMessageToBrowserProcess('show')
|
|
|
|
exit: (status) ->
|
|
@sendMessageToBrowserProcess('exit', [status])
|
|
|
|
log: (message) ->
|
|
@sendMessageToBrowserProcess('log', [message])
|
|
|
|
beginTracing: ->
|
|
@sendMessageToBrowserProcess('beginTracing')
|
|
|
|
endTracing: ->
|
|
@sendMessageToBrowserProcess('endTracing')
|
|
|
|
toggleFullScreen: ->
|
|
@sendMessageToBrowserProcess('toggleFullScreen')
|
|
|
|
getRootViewStateForPath: (path) ->
|
|
if json = localStorage[path]
|
|
JSON.parse(json)
|
|
|
|
setRootViewStateForPath: (path, state) ->
|
|
return unless path
|
|
if state?
|
|
localStorage[path] = JSON.stringify(state)
|
|
else
|
|
delete localStorage[path]
|
|
|
|
sendMessageToBrowserProcess: (name, data=[], callbacks) ->
|
|
messageId = messageIdCounter++
|
|
data.unshift(messageId)
|
|
callbacks = [callbacks] if typeof callbacks is 'function'
|
|
@pendingBrowserProcessCallbacks[messageId] = callbacks
|
|
originalSendMessageToBrowserProcess(name, data)
|
|
|
|
receiveMessageFromBrowserProcess: (name, data) ->
|
|
if name is 'reply'
|
|
[messageId, callbackIndex] = data.shift()
|
|
@pendingBrowserProcessCallbacks[messageId]?[callbackIndex]?(data...)
|
|
|
|
setWindowState: (keyPath, value) ->
|
|
windowState = @getWindowState()
|
|
_.setValueForKeyPath(windowState, keyPath, value)
|
|
$native.setWindowState(JSON.stringify(windowState))
|
|
windowState
|
|
|
|
getWindowState: (keyPath) ->
|
|
windowState = JSON.parse($native.getWindowState())
|
|
if keyPath
|
|
_.valueForKeyPath(windowState, keyPath)
|
|
else
|
|
windowState
|