Skip to content

Commit

Permalink
fix bugs with theme loading
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseweed committed Apr 24, 2016
1 parent 19f4ac2 commit d99dd00
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 45 deletions.
4 changes: 2 additions & 2 deletions lib/main.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports =
activate: () ->
require( atom.packages.getLoadedPackage('seti-syntax').path + '/lib/settings').init()
activate: (state) ->
require( atom.packages.getLoadedPackage('seti-syntax').path + '/lib/settings').init(state)
122 changes: 81 additions & 41 deletions lib/settings.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,110 @@ Dom = require('./dom')
Utility = require('./utility')

module.exports =
init: () ->
init: (state) ->

self = @

# SET THEME TO DEFAULT IF SETI UI ISN'T LOADED
if (atom.config.get('seti-syntax.themeColor'))
self.setTheme atom.config.get('seti-syntax.themeColor'), false, false
else
self.setTheme 'default', false, false

# WHEN SYNTAX THEME CHANGES
atom.config.onDidChange 'seti-syntax.themeColor', (value) ->
self.setTheme value.newValue, value.oldValue, true

# WHEN AUTO THEME IS UPDATED
atom.config.onDidChange 'seti-syntax.dynamicColor', (value) ->

if (value.newValue)
newColor = atom.config.get('seti-ui.themeColor')
self.setTheme newColor, false, true
else
if (atom.config.get('seti-syntax.themeColor'))
newColor = atom.config.get('seti-syntax.themeColor')
@themeSet = false

# ONCE PACKAGE IS LOADED
if self.isLoaded('seti-syntax')

# WHEN SYNTAX THEME CHANGES
atom.config.onDidChange 'seti-syntax.themeColor', (value) ->
self.setTheme value.newValue, value.oldValue, true

# WHEN DYNAMIC THEME IS ENABLED OR DISABLED
atom.config.onDidChange 'seti-syntax.dynamicColor', (value) ->
# IF DYNIMIC IS ALLWOED
if (value.newValue)
newColor = atom.config.get('seti-ui.themeColor')
self.setTheme newColor, false, true
# IF DYNAMIC IS NOT ALLOWED
else
newColor = 'default'
self.setTheme newColor, false, true

if atom.config.get('seti-syntax.dynamicColor') and self.isLoaded('seti-ui')
color = atom.config.get('seti-syntax.themeColor')

# IF THEME COLOR IS SET
if typeof color == 'string'
self.setTheme color, false, false

# WHEN UI THEME CHANGES
atom.config.onDidChange 'seti-ui.themeColor', (value) ->
self.setTheme value.newValue, value.oldValue, false

# IF SETI IS DEACTIVATED, SET THEME TO DEFAULT
self.onDeactivate 'seti-ui', ->
# IF SYNTAX COLOR HAS BEEN SET
if (atom.config.get('seti-syntax.themeColor'))
newColor = atom.config.get('seti-syntax.themeColor')
# FALLBACK TP DEFAULT COLO IF NONE SET
else
newColor = 'default'
self.setTheme newColor, false, true

# IF SETI UI IS LOADED
if self.isLoaded('seti-ui')

# IF DYNAMIC THEM IS ALLOWED
if atom.config.get('seti-syntax.dynamicColor') and not @themeSet
# SET SYNTAX THEME TO MATCH UI
self.setTheme atom.config.get('seti-ui.themeColor'), false, false

# WHEN UI THEME CHANGES
atom.config.onDidChange 'seti-ui.themeColor', (value) ->
# IF DYNAMIC THEM IS ALLOWED
if atom.config.get('seti-syntax.dynamicColor')
# SET SYNTAX THEME TO MATCH UI
self.setTheme value.newValue, value.oldValue, false

# IF SETI UI IS DEACTIVATED
self.onDeactivate 'seti-ui', ->
# IF DYNAMIC THEM WAS ALLOWED
if atom.config.get('seti-syntax.dynamicColor')
# SET THEME TO DEFAULT
self.setTheme 'default', false, false

# SET USER THEME IS NOT SET DYNAMICALLY
if (atom.config.get('seti-syntax.themeColor')) and not @themeSet
self.setTheme atom.config.get('seti-syntax.themeColor'), false, false

# IF ALL ELSE HAS FAILED, LOAD THE DEFAULT THEME
else if (not @themeSet)
self.setTheme 'default', false, false

# CHECKS IF A PACKAGE IS LOADED
isLoaded: (which) ->
atom.packages.isPackageLoaded()


return atom.packages.isPackageLoaded(which)

# WHEN PACKAGE ACTIVATES
onActivate: (which, cb) ->
atom.packages.onDidActivatePackage (pkg) ->
if pkg.name == which
cb pkg

# WHEN PACKAGE DEACTIVATES
onDeactivate: (which, cb) ->
atom.packages.onDidDeactivatePackage (pkg) ->
if pkg.name == which
cb pkg

# GET INFO ABOUT OUR PACKAGE
package: atom.packages.getLoadedPackage('seti-syntax')

# DETERMINE IF A SPECIFIC PACKAGE HAS BEEN LOADED
packageInfo: (which) ->
return atom.packages.getLoadedPackage which

# RELOAD WHEN SETTINGS CHANGE
refresh: ->
self = @
self.package.deactivate()
setImmediate ->
return self.package.activate()

setTheme: (theme, previous, reload) ->
self = @
fs = require('fs')
pkg = @package
themeData = '@import "themes/' + theme.toLowerCase() + '";'
fs.writeFile pkg.path + '/styles/user-theme.less', themeData

# THIS PREVENTS THEME FROM BEING SET TWICE
@themeSet = true

# CHECK CURRENT THEME FILE
fs.readFile pkg.path + '/styles/user-theme.less', 'utf8', (err, fileData) ->
# IF THEME IS DIFFERENT THAN IS USED TO BE
if fileData != themeData
# SAVE A NEW USER THEME FILE
fs.writeFile pkg.path + '/styles/user-theme.less', themeData, (err) ->
# IF FILE WAS WRITTEN OK
if !err
# RELOAD THE VIEW
self.refresh()
4 changes: 3 additions & 1 deletion sample-files/Less.less
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
.box {
color: saturate(@base, 5%);
border-color: lighten(@base, 30%);
div { .box-shadow(0 0 5px, 30%) }
div {
.box-shadow(0 0 5px, 30%)
}
}
2 changes: 1 addition & 1 deletion styles/colors.less
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@added: @green;

// TEXT
@quotes: @white;
@quotes: lighten(@color1, 20);
@markup: @code-font-color;
@unknown: @code-font-color;

Expand Down

0 comments on commit d99dd00

Please sign in to comment.