📝 Mark ThemeManager class public

This commit is contained in:
Kevin Sawicki 2014-01-22 16:25:55 -08:00
parent f3f6ec424f
commit a4754b2bd5

View File

@ -8,9 +8,9 @@ fs = require 'fs-plus'
AtomPackage = require './atom-package' AtomPackage = require './atom-package'
File = require './file' File = require './file'
# Private: Handles discovering and loading available themes. # Public: Handles loading and activating available themes.
# #
# Themes are a subset of packages # A ThemeManager instance is always available under the `atom.themes` global.
module.exports = module.exports =
class ThemeManager class ThemeManager
Emitter.includeInto(this) Emitter.includeInto(this)
@ -19,7 +19,6 @@ class ThemeManager
@lessCache = null @lessCache = null
@packageManager.registerPackageActivator(this, ['theme']) @packageManager.registerPackageActivator(this, ['theme'])
# Internal-only:
getAvailableNames: -> getAvailableNames: ->
# TODO: Maybe should change to list all the available themes out there? # TODO: Maybe should change to list all the available themes out there?
@getLoadedNames() @getLoadedNames()
@ -27,19 +26,15 @@ class ThemeManager
getLoadedNames: -> getLoadedNames: ->
theme.name for theme in @getLoadedThemes() theme.name for theme in @getLoadedThemes()
# Internal-only:
getActiveNames: -> getActiveNames: ->
theme.name for theme in @getActiveThemes() theme.name for theme in @getActiveThemes()
# Internal-only:
getActiveThemes: -> getActiveThemes: ->
pack for pack in @packageManager.getActivePackages() when pack.isTheme() pack for pack in @packageManager.getActivePackages() when pack.isTheme()
# Internal-only:
getLoadedThemes: -> getLoadedThemes: ->
pack for pack in @packageManager.getLoadedPackages() when pack.isTheme() pack for pack in @packageManager.getLoadedPackages() when pack.isTheme()
# Internal-only: adhere to the PackageActivator interface
activatePackages: (themePackages) -> @activateThemes() activatePackages: (themePackages) -> @activateThemes()
# Private: Get the enabled theme names from the config. # Private: Get the enabled theme names from the config.
@ -53,7 +48,6 @@ class ThemeManager
# the first/top theme to override later themes in the stack. # the first/top theme to override later themes in the stack.
themeNames.reverse() themeNames.reverse()
# Internal-only:
activateThemes: -> activateThemes: ->
# atom.config.observe runs the callback once, then on subsequent changes. # atom.config.observe runs the callback once, then on subsequent changes.
atom.config.observe 'core.themes', => atom.config.observe 'core.themes', =>
@ -69,13 +63,11 @@ class ThemeManager
@emit('reloaded') @emit('reloaded')
# Internal-only:
deactivateThemes: -> deactivateThemes: ->
@unwatchUserStylesheet() @unwatchUserStylesheet()
@packageManager.deactivatePackage(pack.name) for pack in @getActiveThemes() @packageManager.deactivatePackage(pack.name) for pack in @getActiveThemes()
null null
# Internal-only:
refreshLessCache: -> refreshLessCache: ->
@lessCache?.setImportPaths(@getImportPaths()) @lessCache?.setImportPaths(@getImportPaths())
@ -85,7 +77,6 @@ class ThemeManager
setEnabledThemes: (enabledThemeNames) -> setEnabledThemes: (enabledThemeNames) ->
atom.config.set('core.themes', enabledThemeNames) atom.config.set('core.themes', enabledThemeNames)
# Public:
getImportPaths: -> getImportPaths: ->
activeThemes = @getActiveThemes() activeThemes = @getActiveThemes()
if activeThemes.length > 0 if activeThemes.length > 0
@ -98,7 +89,7 @@ class ThemeManager
themePath for themePath in themePaths when fs.isDirectorySync(themePath) themePath for themePath in themePaths when fs.isDirectorySync(themePath)
# Public: # Public: Returns the {String} path to the user's stylesheet under ~/.atom
getUserStylesheetPath: -> getUserStylesheetPath: ->
stylesheetPath = fs.resolve(path.join(@configDirPath, 'user'), ['css', 'less']) stylesheetPath = fs.resolve(path.join(@configDirPath, 'user'), ['css', 'less'])
if fs.isFileSync(stylesheetPath) if fs.isFileSync(stylesheetPath)
@ -106,13 +97,11 @@ class ThemeManager
else else
path.join(@configDirPath, 'user.less') path.join(@configDirPath, 'user.less')
#Private:
unwatchUserStylesheet: -> unwatchUserStylesheet: ->
@userStylesheetFile?.off() @userStylesheetFile?.off()
@userStylesheetFile = null @userStylesheetFile = null
@removeStylesheet(@userStylesheetPath) if @userStylesheetPath? @removeStylesheet(@userStylesheetPath) if @userStylesheetPath?
# Private:
loadUserStylesheet: -> loadUserStylesheet: ->
@unwatchUserStylesheet() @unwatchUserStylesheet()
userStylesheetPath = @getUserStylesheetPath() userStylesheetPath = @getUserStylesheetPath()
@ -125,34 +114,32 @@ class ThemeManager
userStylesheetContents = @loadStylesheet(userStylesheetPath) userStylesheetContents = @loadStylesheet(userStylesheetPath)
@applyStylesheet(userStylesheetPath, userStylesheetContents, 'userTheme') @applyStylesheet(userStylesheetPath, userStylesheetContents, 'userTheme')
# Internal-only:
loadBaseStylesheets: -> loadBaseStylesheets: ->
@requireStylesheet('bootstrap/less/bootstrap') @requireStylesheet('bootstrap/less/bootstrap')
@reloadBaseStylesheets() @reloadBaseStylesheets()
# Internal-only:
reloadBaseStylesheets: -> reloadBaseStylesheets: ->
@requireStylesheet('../static/atom') @requireStylesheet('../static/atom')
if nativeStylesheetPath = fs.resolveOnLoadPath(process.platform, ['css', 'less']) if nativeStylesheetPath = fs.resolveOnLoadPath(process.platform, ['css', 'less'])
@requireStylesheet(nativeStylesheetPath) @requireStylesheet(nativeStylesheetPath)
# Internal-only:
stylesheetElementForId: (id, htmlElement=$('html')) -> stylesheetElementForId: (id, htmlElement=$('html')) ->
htmlElement.find("""head style[id="#{id}"]""") htmlElement.find("""head style[id="#{id}"]""")
# Internal-only:
resolveStylesheet: (stylesheetPath) -> resolveStylesheet: (stylesheetPath) ->
if path.extname(stylesheetPath).length > 0 if path.extname(stylesheetPath).length > 0
fs.resolveOnLoadPath(stylesheetPath) fs.resolveOnLoadPath(stylesheetPath)
else else
fs.resolveOnLoadPath(stylesheetPath, ['css', 'less']) fs.resolveOnLoadPath(stylesheetPath, ['css', 'less'])
# Public: resolves and applies the stylesheet specified by the path. # Public: Resolve and apply the stylesheet specified by the path.
# #
# * stylesheetPath: String. Can be an absolute path or the name of a CSS or # This supports both CSS and LESS stylsheets.
# LESS file in the stylesheets path.
# #
# Returns the absolute path to the stylesheet # * stylesheetPath: A {String} path to the stylesheet that can be an absolute
# path or a relative path that will be resolved against the load path.
#
# Returns the absolute path to the required stylesheet.
requireStylesheet: (stylesheetPath, ttype = 'bundled', htmlElement) -> requireStylesheet: (stylesheetPath, ttype = 'bundled', htmlElement) ->
if fullPath = @resolveStylesheet(stylesheetPath) if fullPath = @resolveStylesheet(stylesheetPath)
content = @loadStylesheet(fullPath) content = @loadStylesheet(fullPath)
@ -162,14 +149,12 @@ class ThemeManager
fullPath fullPath
# Internal-only:
loadStylesheet: (stylesheetPath) -> loadStylesheet: (stylesheetPath) ->
if path.extname(stylesheetPath) is '.less' if path.extname(stylesheetPath) is '.less'
@loadLessStylesheet(stylesheetPath) @loadLessStylesheet(stylesheetPath)
else else
fs.readFileSync(stylesheetPath, 'utf8') fs.readFileSync(stylesheetPath, 'utf8')
# Internal-only:
loadLessStylesheet: (lessStylesheetPath) -> loadLessStylesheet: (lessStylesheetPath) ->
unless @lessCache? unless @lessCache?
LessCompileCache = require './less-compile-cache' LessCompileCache = require './less-compile-cache'
@ -184,16 +169,13 @@ class ThemeManager
#{e.message} #{e.message}
""" """
# Internal-only:
stringToId: (string) -> stringToId: (string) ->
string.replace(/\\/g, '/') string.replace(/\\/g, '/')
# Internal-only:
removeStylesheet: (stylesheetPath) -> removeStylesheet: (stylesheetPath) ->
fullPath = @resolveStylesheet(stylesheetPath) ? stylesheetPath fullPath = @resolveStylesheet(stylesheetPath) ? stylesheetPath
@stylesheetElementForId(@stringToId(fullPath)).remove() @stylesheetElementForId(@stringToId(fullPath)).remove()
# Internal-only:
applyStylesheet: (path, text, ttype = 'bundled', htmlElement=$('html')) -> applyStylesheet: (path, text, ttype = 'bundled', htmlElement=$('html')) ->
styleElement = @stylesheetElementForId(@stringToId(path), htmlElement) styleElement = @stylesheetElementForId(@stringToId(path), htmlElement)
if styleElement.length if styleElement.length