       'flex-tool-bar:edit-config-file': =>
         atom.workspace.open @configFilePath if @configFilePath
 
-  registerEvent: ->
+  registerEvents: ->
+    @subscriptions.add atom.packages.onDidActivateInitialPackages  =>
+      @reloadToolbar()
+
+      @subscriptions.add atom.packages.onDidActivatePackage =>
+        @reloadToolbar()
+
+      @subscriptions.add atom.packages.onDidDeactivatePackage =>
+        @reloadToolbar()
+
     @subscriptions.add atom.workspace.onDidChangeActivePaneItem (item) =>
 
       if @storeProject()

    @storeGrammar()
    @registerTypes()
    @registerCommand()
    @registerEvent()
    @registerWatch()
    @registerProjectWatch()



    return config

  loopThrough: (items, func) ->
    items = [items] if not Array.isArray items
    for item in items
      return true if func(item)

    return false

  condition: (conditions) ->
    return @loopThrough conditions, (condition) =>

      if typeof condition is 'string'
        return true if @grammarCondition(condition)

      else if typeof condition is 'function'
        return true if @functionCondition(condition)

      else

        if condition.function?
          return true if @loopThrough(condition.function, @functionCondition)

        if condition.grammar?
          return true if @loopThrough(condition.grammar, @grammarCondition)

        if condition.pattern?
          return true if @loopThrough(condition.pattern, @patternCondition)

        if condition.package?
          return true if @loopThrough(condition.package, @packageCondition)

  functionCondition: (condition) ->
    condition()

  grammarCondition: (condition) ->
    filePath = atom.workspace.getActivePaneItem()?.getPath?()
    result = false
    reverse = false
    if /^!/.test condition
      condition = condition.replace '!', ''
      reverse = true

    if /^[^\/]+\.(.*?)$/.test condition
      result = true if filePath isnt undefined and filePath.match(condition)?.length > 0
    else
      result = true if @currentGrammar? and @currentGrammar.includes condition.toLowerCase()

    result = !result if reverse

    result

  patternCondition: (condition) ->
    filePath = atom.workspace.getActivePaneItem()?.getPath?()
    result = false

    if filePath isnt undefined
      result = globToRegexp(condition, extended: true).test filePath

    result

  packageCondition: (condition) ->
    result = false
    reverse = false
    if /^!/.test condition
      condition = condition.replace '!', ''
      reverse = true

    result = true if atom.packages.isPackageLoaded(condition) and not atom.packages.isPackageDisabled(condition)
    result = !result if reverse

    result

  storeProject: ->
    editor = atom.workspace.getActivePaneItem()
