mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Refactor pkg and themeManager -- all themes stored in the pkg manager
This commit is contained in:
@@ -8,10 +8,25 @@ describe "ThemeManager", ->
|
||||
themeManager = null
|
||||
|
||||
beforeEach ->
|
||||
themeManager = new ThemeManager()
|
||||
themeManager = new ThemeManager(atom.packages)
|
||||
|
||||
afterEach ->
|
||||
themeManager.unload()
|
||||
themeManager.deactivateThemes()
|
||||
|
||||
describe "theme getters and setters", ->
|
||||
beforeEach ->
|
||||
atom.packages.loadPackages()
|
||||
|
||||
it 'getLoadedThemes get all the loaded themes', ->
|
||||
themes = themeManager.getLoadedThemes()
|
||||
expect(themes).toHaveLength(13)
|
||||
|
||||
it 'getActiveThemes get all the active themes', ->
|
||||
themeManager.activateThemes()
|
||||
names = atom.config.get('core.themes')
|
||||
expect(names.length).toBeGreaterThan(0)
|
||||
themes = themeManager.getActiveThemes()
|
||||
expect(themes).toHaveLength(names.length)
|
||||
|
||||
describe "getImportPaths()", ->
|
||||
it "returns the theme directories before the themes are loaded", ->
|
||||
@@ -32,7 +47,7 @@ describe "ThemeManager", ->
|
||||
it "add/removes stylesheets to reflect the new config value", ->
|
||||
themeManager.on 'reloaded', reloadHandler = jasmine.createSpy()
|
||||
spyOn(themeManager, 'getUserStylesheetPath').andCallFake -> null
|
||||
themeManager.load()
|
||||
themeManager.activateThemes()
|
||||
|
||||
config.set('core.themes', [])
|
||||
expect($('style.theme').length).toBe 0
|
||||
@@ -59,21 +74,7 @@ describe "ThemeManager", ->
|
||||
describe "when a theme fails to load", ->
|
||||
it "logs a warning", ->
|
||||
spyOn(console, 'warn')
|
||||
themeManager.activateTheme('a-theme-that-will-not-be-found')
|
||||
expect(console.warn).toHaveBeenCalled()
|
||||
|
||||
describe "theme-loaded event", ->
|
||||
beforeEach ->
|
||||
spyOn(themeManager, 'getUserStylesheetPath').andCallFake -> null
|
||||
themeManager.load()
|
||||
|
||||
it "fires when a new theme has been added", ->
|
||||
themeManager.on 'theme-activated', loadHandler = jasmine.createSpy()
|
||||
|
||||
config.set('core.themes', ['atom-dark-syntax'])
|
||||
|
||||
expect(loadHandler).toHaveBeenCalled()
|
||||
expect(loadHandler.mostRecentCall.args[0]).toBeInstanceOf AtomPackage
|
||||
expect(-> themeManager.activateTheme('a-theme-that-will-not-be-found')).toThrow()
|
||||
|
||||
describe "requireStylesheet(path)", ->
|
||||
it "synchronously loads css at the given path and installs a style tag for it in the head", ->
|
||||
@@ -140,7 +141,7 @@ describe "ThemeManager", ->
|
||||
window.rootView = new RootView
|
||||
rootView.append $$ -> @div class: 'editor'
|
||||
rootView.attachToDom()
|
||||
themeManager.load()
|
||||
themeManager.activateThemes()
|
||||
|
||||
it "loads the correct values from the theme's ui-variables file", ->
|
||||
config.set('core.themes', ['theme-with-ui-variables'])
|
||||
|
||||
@@ -25,20 +25,18 @@ class AtomPackage extends Package
|
||||
resolvedMainModulePath: false
|
||||
mainModule: null
|
||||
|
||||
constructor: (path, {@metadata}) ->
|
||||
super(path)
|
||||
@reset()
|
||||
|
||||
getType: -> 'atom'
|
||||
|
||||
load: ->
|
||||
@metadata = {}
|
||||
@stylesheets = []
|
||||
@keymaps = []
|
||||
@menus = []
|
||||
@grammars = []
|
||||
@scopedProperties = []
|
||||
getStylesheetType: -> 'bundled'
|
||||
|
||||
load: ->
|
||||
@measure 'loadTime', =>
|
||||
try
|
||||
@metadata = Package.loadMetadata(@path)
|
||||
return if @isTheme()
|
||||
@metadata = Package.loadMetadata(@path) unless @metadata
|
||||
|
||||
@loadKeymaps()
|
||||
@loadMenus()
|
||||
@@ -55,9 +53,22 @@ class AtomPackage extends Package
|
||||
console.warn "Failed to load package named '#{@name}'", e.stack ? e
|
||||
this
|
||||
|
||||
enable: ->
|
||||
atom.config.removeAtKeyPath('core.disabledPackages', @metadata.name)
|
||||
|
||||
disable: ->
|
||||
atom.config.pushAtKeyPath('core.disabledPackages', @metadata.name)
|
||||
|
||||
reset: ->
|
||||
@stylesheets = []
|
||||
@keymaps = []
|
||||
@menus = []
|
||||
@grammars = []
|
||||
@scopedProperties = []
|
||||
|
||||
activate: ({immediate}={}) ->
|
||||
@measure 'activateTime', =>
|
||||
@loadStylesheets() if @isTheme()
|
||||
@loadStylesheets()
|
||||
@activateResources()
|
||||
if @metadata.activationEvents? and not immediate
|
||||
@subscribeToActivationEvents()
|
||||
@@ -69,7 +80,7 @@ class AtomPackage extends Package
|
||||
@activateConfig()
|
||||
@activateStylesheets()
|
||||
if @requireMainModule()
|
||||
@mainModule.activate(atom.getPackageState(@name) ? {})
|
||||
@mainModule.activate(atom.packages.getPackageState(@name) ? {})
|
||||
@mainActivated = true
|
||||
catch e
|
||||
console.warn "Failed to activate package named '#{@name}'", e.stack
|
||||
@@ -86,7 +97,7 @@ class AtomPackage extends Package
|
||||
activateStylesheets: ->
|
||||
return if @stylesheetsActivated
|
||||
|
||||
type = if @metadata.theme then 'theme' else 'bundled'
|
||||
type = @getStylesheetType()
|
||||
for [stylesheetPath, content] in @stylesheets
|
||||
atom.themes.applyStylesheet(stylesheetPath, content, type)
|
||||
@stylesheetsActivated = true
|
||||
@@ -183,8 +194,7 @@ class AtomPackage extends Package
|
||||
@reloadStylesheet(stylesheetPath, content) for [stylesheetPath, content] in @stylesheets
|
||||
|
||||
reloadStylesheet: (stylesheetPath, content) ->
|
||||
type = if @metadata.theme then 'theme' else 'bundled'
|
||||
atom.themes.applyStylesheet(stylesheetPath, content, type)
|
||||
atom.themes.applyStylesheet(stylesheetPath, content, @getStylesheetType())
|
||||
|
||||
requireMainModule: ->
|
||||
return @mainModule if @mainModule?
|
||||
|
||||
@@ -55,7 +55,7 @@ class Atom
|
||||
@__defineSetter__ 'packageStates', (packageStates) => @packages.packageStates = packageStates
|
||||
|
||||
@subscribe @packages, 'loaded', => @watchThemes()
|
||||
@themes = new ThemeManager()
|
||||
@themes = new ThemeManager(@packages)
|
||||
@contextMenu = new ContextMenuManager(devMode)
|
||||
@menu = new MenuManager()
|
||||
@pasteboard = new Pasteboard()
|
||||
|
||||
@@ -4,6 +4,21 @@ _ = require 'underscore-plus'
|
||||
Package = require './package'
|
||||
path = require 'path'
|
||||
|
||||
###
|
||||
Packages have a lifecycle
|
||||
|
||||
* The paths to all non-disabled packages and themes are found on disk (these are available packages)
|
||||
* Every package (except those in core.disabledPackages) is 'loaded', meaning
|
||||
`Package` objects are created, and their metadata loaded. This includes themes,
|
||||
as themes are packages
|
||||
* Each non-theme package is 'activated', meaning its resources are loaded into the
|
||||
* Packages and themes can be enabled and disabled, and
|
||||
|
||||
TODO:
|
||||
* test that it doesnt activate all the theme packages
|
||||
* originally disabled packages can be enabled, and loaded without reloading
|
||||
* config.observe the core.disabledPackages
|
||||
###
|
||||
module.exports =
|
||||
class PackageManager
|
||||
Emitter.includeInto(this)
|
||||
@@ -23,17 +38,25 @@ class PackageManager
|
||||
setPackageState: (name, state) ->
|
||||
@packageStates[name] = state
|
||||
|
||||
enablePackage: (name) ->
|
||||
pack = @loadPackage(name)
|
||||
pack?.enable()
|
||||
|
||||
disablePackage: (name) ->
|
||||
pack = @loadPackage(name)
|
||||
pack?.disable()
|
||||
|
||||
activatePackages: ->
|
||||
@activatePackage(pack.name) for pack in @getLoadedPackages()
|
||||
# ThemeManager handles themes. Only activate non theme packages
|
||||
# This is the only part I dislike
|
||||
@activatePackage(pack.name) for pack in @getLoadedPackages() when not pack.isTheme()
|
||||
|
||||
activatePackage: (name, options) ->
|
||||
return if @getActivePackage(name)
|
||||
if pack = @loadPackage(name, options)
|
||||
if pack.isTheme()
|
||||
atom.themes.enableTheme(pack.name)
|
||||
else
|
||||
@activePackages[pack.name] = pack
|
||||
pack.activate(options)
|
||||
pack
|
||||
@activePackages[pack.name] = pack
|
||||
pack.activate(options)
|
||||
pack
|
||||
|
||||
deactivatePackages: ->
|
||||
@deactivatePackage(pack.name) for pack in @getActivePackages()
|
||||
@@ -46,15 +69,15 @@ class PackageManager
|
||||
else
|
||||
throw new Error("No active package for name '#{name}'")
|
||||
|
||||
getActivePackages: ->
|
||||
_.values(@activePackages)
|
||||
|
||||
getActivePackage: (name) ->
|
||||
@activePackages[name]
|
||||
|
||||
isPackageActive: (name) ->
|
||||
@getActivePackage(name)?
|
||||
|
||||
getActivePackages: ->
|
||||
_.values(@activePackages)
|
||||
|
||||
loadPackages: ->
|
||||
# Ensure atom exports is already in the require cache so the load time
|
||||
# of the first package isn't skewed by being the first to require atom
|
||||
@@ -67,14 +90,14 @@ class PackageManager
|
||||
if @isPackageDisabled(name)
|
||||
return console.warn("Tried to load disabled package '#{name}'")
|
||||
|
||||
pack = @getLoadedPackage(name)
|
||||
return pack if pack
|
||||
|
||||
if packagePath = @resolvePackagePath(name)
|
||||
return pack if pack = @getLoadedPackage(name)
|
||||
|
||||
pack = Package.load(packagePath, options)
|
||||
if pack.metadata?.theme
|
||||
atom.themes.register(pack)
|
||||
else
|
||||
@loadedPackages[pack.name] = pack
|
||||
@loadedPackages[pack.name] = pack
|
||||
pack
|
||||
else
|
||||
throw new Error("Could not resolve '#{name}' to a package path")
|
||||
@@ -88,19 +111,6 @@ class PackageManager
|
||||
else
|
||||
throw new Error("No loaded package for name '#{name}'")
|
||||
|
||||
resolvePackagePath: (name) ->
|
||||
return name if fsUtils.isDirectorySync(name)
|
||||
|
||||
packagePath = fsUtils.resolve(@packageDirPaths..., name)
|
||||
return packagePath if fsUtils.isDirectorySync(packagePath)
|
||||
|
||||
packagePath = path.join(@resourcePath, 'node_modules', name)
|
||||
return packagePath if @isInternalPackage(packagePath)
|
||||
|
||||
isInternalPackage: (packagePath) ->
|
||||
{engines} = Package.loadMetadata(packagePath, true)
|
||||
engines?.atom?
|
||||
|
||||
getLoadedPackage: (name) ->
|
||||
@loadedPackages[name]
|
||||
|
||||
@@ -110,9 +120,22 @@ class PackageManager
|
||||
getLoadedPackages: ->
|
||||
_.values(@loadedPackages)
|
||||
|
||||
resolvePackagePath: (name) ->
|
||||
return name if fsUtils.isDirectorySync(name)
|
||||
|
||||
packagePath = fsUtils.resolve(@packageDirPaths..., name)
|
||||
return packagePath if fsUtils.isDirectorySync(packagePath)
|
||||
|
||||
packagePath = path.join(@resourcePath, 'node_modules', name)
|
||||
return packagePath if @isInternalPackage(packagePath)
|
||||
|
||||
isPackageDisabled: (name) ->
|
||||
_.include(config.get('core.disabledPackages') ? [], name)
|
||||
|
||||
isInternalPackage: (packagePath) ->
|
||||
{engines} = Package.loadMetadata(packagePath, true)
|
||||
engines?.atom?
|
||||
|
||||
getAvailablePackagePaths: ->
|
||||
packagePaths = []
|
||||
|
||||
|
||||
@@ -7,11 +7,16 @@ class Package
|
||||
@build: (path) ->
|
||||
TextMatePackage = require './text-mate-package'
|
||||
AtomPackage = require './atom-package'
|
||||
ThemePackage = require './theme-package'
|
||||
|
||||
if TextMatePackage.testName(path)
|
||||
new TextMatePackage(path)
|
||||
else
|
||||
new AtomPackage(path)
|
||||
metadata = @loadMetadata(path)
|
||||
if metadata.theme
|
||||
new ThemePackage(path, {metadata})
|
||||
else
|
||||
new AtomPackage(path, {metadata})
|
||||
|
||||
@load: (path, options) ->
|
||||
pack = @build(path)
|
||||
|
||||
@@ -8,31 +8,93 @@ _ = require 'underscore-plus'
|
||||
fsUtils = require './fs-utils'
|
||||
|
||||
# Private: Handles discovering and loading available themes.
|
||||
###
|
||||
Themes are a subset of packages
|
||||
###
|
||||
module.exports =
|
||||
class ThemeManager
|
||||
Emitter.includeInto(this)
|
||||
|
||||
constructor: ->
|
||||
@loadedThemes = []
|
||||
@activeThemes = []
|
||||
constructor: (@packageManager) ->
|
||||
@lessCache = null
|
||||
|
||||
# Internal-only:
|
||||
register: (theme) ->
|
||||
@loadedThemes.push(theme)
|
||||
theme
|
||||
getAvailableNames: ->
|
||||
# TODO: Maybe should change to list all the available themes out there?
|
||||
@getLoadedNames()
|
||||
|
||||
getLoadedNames: ->
|
||||
name for theme.name in @getLoadedThemes()
|
||||
|
||||
# Internal-only:
|
||||
getAvailableNames: ->
|
||||
_.map @loadedThemes, (theme) -> theme.metadata.name
|
||||
getActiveNames: ->
|
||||
name for theme.name in @getActiveThemes()
|
||||
|
||||
# Internal-only:
|
||||
getActiveThemes: ->
|
||||
_.clone(@activeThemes)
|
||||
pack for pack in @packageManager.getActivePackages() when pack.isTheme()
|
||||
|
||||
# Internal-only:
|
||||
getLoadedThemes: ->
|
||||
_.clone(@loadedThemes)
|
||||
pack for pack in @packageManager.getLoadedPackages() when pack.isTheme()
|
||||
|
||||
# Internal-only:
|
||||
activateThemes: ->
|
||||
# atom.config.observe runs the callback once, then on subsequent changes.
|
||||
atom.config.observe 'core.themes', (themeNames) =>
|
||||
@deactivateThemes()
|
||||
themeNames = [themeNames] unless _.isArray(themeNames)
|
||||
|
||||
# Reverse so the first (top) theme is loaded after the others. We want
|
||||
# the first/top theme to override later themes in the stack.
|
||||
themeNames = _.clone(themeNames).reverse()
|
||||
|
||||
@activateTheme(themeName) for themeName in themeNames
|
||||
@loadUserStylesheet()
|
||||
@reloadBaseStylesheets()
|
||||
@emit('reloaded')
|
||||
|
||||
# Internal-only:
|
||||
activateTheme: (themeName) ->
|
||||
@packageManager.activatePackage(themeName)
|
||||
|
||||
# Internal-only:
|
||||
deactivateThemes: ->
|
||||
@removeStylesheet(@userStylesheetPath) if @userStylesheetPath?
|
||||
@deactivateTheme(pack.name) for pack in @getActiveThemes()
|
||||
null
|
||||
|
||||
# Internal-only:
|
||||
deactivateTheme: (themeName) ->
|
||||
@packageManager.deactivatePackage(themeName)
|
||||
|
||||
# Public:
|
||||
getImportPaths: ->
|
||||
activeThemes = @getActiveThemes()
|
||||
if activeThemes.length > 0
|
||||
themePaths = (theme.getStylesheetsPath() for theme in activeThemes when theme)
|
||||
else
|
||||
themePaths = []
|
||||
for themeName in atom.config.get('core.themes') ? []
|
||||
if themePath = @packageManager.resolvePackagePath(themeName)
|
||||
themePaths.push(path.join(themePath, AtomPackage.stylesheetsDir))
|
||||
|
||||
themePath for themePath in themePaths when fsUtils.isDirectorySync(themePath)
|
||||
|
||||
# Public:
|
||||
getUserStylesheetPath: ->
|
||||
stylesheetPath = fsUtils.resolve(path.join(atom.config.configDirPath, 'user'), ['css', 'less'])
|
||||
if fsUtils.isFileSync(stylesheetPath)
|
||||
stylesheetPath
|
||||
else
|
||||
null
|
||||
|
||||
# Private:
|
||||
loadUserStylesheet: ->
|
||||
if userStylesheetPath = @getUserStylesheetPath()
|
||||
@userStylesheetPath = userStylesheetPath
|
||||
userStylesheetContents = @loadStylesheet(userStylesheetPath)
|
||||
@applyStylesheet(userStylesheetPath, userStylesheetContents, 'userTheme')
|
||||
|
||||
# Internal-only:
|
||||
loadBaseStylesheets: ->
|
||||
@@ -109,96 +171,3 @@ class ThemeManager
|
||||
$("head style.#{ttype}:last").after "<style class='#{ttype}' id='#{id}'>#{text}</style>"
|
||||
else
|
||||
$("head").append "<style class='#{ttype}' id='#{id}'>#{text}</style>"
|
||||
|
||||
# Internal-only:
|
||||
unload: ->
|
||||
@removeStylesheet(@userStylesheetPath) if @userStylesheetPath?
|
||||
theme.deactivate() while theme = @activeThemes.pop()
|
||||
|
||||
# Internal-only:
|
||||
load: ->
|
||||
config.observe 'core.themes', (themeNames) =>
|
||||
@unload()
|
||||
themeNames = [themeNames] unless _.isArray(themeNames)
|
||||
|
||||
# Reverse so the first (top) theme is loaded after the others. We want
|
||||
# the first/top theme to override later themes in the stack.
|
||||
themeNames = _.clone(themeNames).reverse()
|
||||
|
||||
@activateTheme(themeName) for themeName in themeNames
|
||||
@loadUserStylesheet()
|
||||
@reloadBaseStylesheets()
|
||||
@emit('reloaded')
|
||||
|
||||
# Private:
|
||||
loadTheme: (name, options) ->
|
||||
if themePath = @resolveThemePath(name)
|
||||
return theme if theme = @getLoadedTheme(name)
|
||||
pack = Package.load(themePath, options)
|
||||
if pack.isTheme()
|
||||
@register(pack)
|
||||
else
|
||||
throw new Error("Attempted to load a non-theme package '#{name}' as a theme")
|
||||
else
|
||||
throw new Error("Could not resolve '#{name}' to a theme path")
|
||||
|
||||
# Private:
|
||||
getLoadedTheme: (name) ->
|
||||
_.find @loadedThemes, (theme) -> theme.metadata.name is name
|
||||
|
||||
# Private:
|
||||
resolveThemePath: (name) ->
|
||||
return name if fsUtils.isDirectorySync(name)
|
||||
|
||||
packagePath = fsUtils.resolve(config.packageDirPaths..., name)
|
||||
return packagePath if fsUtils.isDirectorySync(packagePath)
|
||||
|
||||
packagePath = path.join(window.resourcePath, 'node_modules', name)
|
||||
return packagePath if @isThemePath(packagePath)
|
||||
|
||||
# Private:
|
||||
isThemePath: (packagePath) ->
|
||||
{engines, theme} = Package.loadMetadata(packagePath, true)
|
||||
engines?.atom? and theme
|
||||
|
||||
# Private:
|
||||
activateTheme: (name) ->
|
||||
try
|
||||
theme = @loadTheme(name)
|
||||
theme.activate()
|
||||
@activeThemes.push(theme)
|
||||
@emit('theme-activated', theme)
|
||||
catch error
|
||||
console.warn("Failed to load theme #{name}", error.stack ? error)
|
||||
|
||||
# Public:
|
||||
enableTheme: (name) ->
|
||||
themes = config.get('core.themes')
|
||||
config.set('core.themes', _.uniq(themes.concat([name])))
|
||||
|
||||
# Public:
|
||||
getUserStylesheetPath: ->
|
||||
stylesheetPath = fsUtils.resolve(path.join(config.configDirPath, 'user'), ['css', 'less'])
|
||||
if fsUtils.isFileSync(stylesheetPath)
|
||||
stylesheetPath
|
||||
else
|
||||
null
|
||||
|
||||
# Public:
|
||||
getImportPaths: ->
|
||||
if @activeThemes.length > 0
|
||||
themePaths = (theme.getStylesheetsPath() for theme in @activeThemes when theme)
|
||||
else
|
||||
themePaths = []
|
||||
for themeName in config.get('core.themes') ? []
|
||||
if themePath = @resolveThemePath(themeName)
|
||||
themePaths.push(path.join(themePath, AtomPackage.stylesheetsDir))
|
||||
|
||||
themePath for themePath in themePaths when fsUtils.isDirectorySync(themePath)
|
||||
|
||||
# Private:
|
||||
loadUserStylesheet: ->
|
||||
if userStylesheetPath = @getUserStylesheetPath()
|
||||
@userStylesheetPath = userStylesheetPath
|
||||
userStylesheetContents = @loadStylesheet(userStylesheetPath)
|
||||
@applyStylesheet(userStylesheetPath, userStylesheetContents, 'userTheme')
|
||||
|
||||
25
src/theme-package.coffee
Normal file
25
src/theme-package.coffee
Normal file
@@ -0,0 +1,25 @@
|
||||
AtomPackage = require './atom-package'
|
||||
Package = require './package'
|
||||
|
||||
### Internal: Loads and resolves packages. ###
|
||||
|
||||
module.exports =
|
||||
class ThemePackage extends AtomPackage
|
||||
|
||||
getType: -> 'theme'
|
||||
|
||||
getStylesheetType: -> 'theme'
|
||||
|
||||
enable: ->
|
||||
atom.config.pushAtKeyPath('core.themes', @metadata.name)
|
||||
|
||||
disable: ->
|
||||
atom.config.removeAtKeyPath('core.themes', @metadata.name)
|
||||
|
||||
load: ->
|
||||
@measure 'loadTime', =>
|
||||
try
|
||||
@metadata = Package.loadMetadata(@path) unless @metadata
|
||||
catch e
|
||||
console.warn "Failed to load theme named '#{@name}'", e.stack ? e
|
||||
this
|
||||
Reference in New Issue
Block a user