From 64ebc372040bbfa3329473b1bc20f7de16d03ec9 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 17 Jan 2014 11:08:43 -0800 Subject: [PATCH] Reload user stylesheet when changed --- spec/theme-manager-spec.coffee | 28 ++++++++++++++++++++++++++++ src/theme-manager.coffee | 25 ++++++++++++++++++------- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/spec/theme-manager-spec.coffee b/spec/theme-manager-spec.coffee index cc49a19f1..5991aebd6 100644 --- a/spec/theme-manager-spec.coffee +++ b/spec/theme-manager-spec.coffee @@ -1,5 +1,7 @@ path = require 'path' + {$, $$, fs, WorkspaceView} = require 'atom' +temp = require 'temp' ThemeManager = require '../src/theme-manager' AtomPackage = require '../src/atom-package' @@ -155,3 +157,29 @@ describe "ThemeManager", -> expect($(".editor").css("padding-top")).toBe "150px" expect($(".editor").css("padding-right")).toBe "150px" expect($(".editor").css("padding-bottom")).toBe "150px" + + describe "when the user stylesheet changes", -> + it "reloads it", -> + userStylesheetPath = path.join(temp.mkdirSync("atom"), 'user.css') + fs.writeFileSync(userStylesheetPath, 'body {border-style: dotted !important;}') + + spyOn(themeManager, 'getUserStylesheetPath').andReturn userStylesheetPath + themeManager.activateThemes() + + expect($(document.body).css('border-style')).toBe 'dotted' + spyOn(themeManager, 'loadUserStylesheet').andCallThrough() + + fs.writeFileSync(userStylesheetPath, 'body {border-style: dashed}') + + waitsFor -> + themeManager.loadUserStylesheet.callCount is 1 + + runs -> + expect($(document.body).css('border-style')).toBe 'dashed' + fs.removeSync(userStylesheetPath) + + waitsFor -> + themeManager.loadUserStylesheet.callCount is 2 + + runs -> + expect($(document.body).css('border-style')).toBe 'none' diff --git a/src/theme-manager.coffee b/src/theme-manager.coffee index 6b1470e60..b6a0e9237 100644 --- a/src/theme-manager.coffee +++ b/src/theme-manager.coffee @@ -4,6 +4,7 @@ _ = require 'underscore-plus' {Emitter} = require 'emissary' fs = require 'fs-plus' +File = require './file' Package = require './package' AtomPackage = require './atom-package' {$} = require './space-pen-extensions' @@ -71,7 +72,7 @@ class ThemeManager # Internal-only: deactivateThemes: -> - @removeStylesheet(@userStylesheetPath) if @userStylesheetPath? + @unwatchUserStylesheet() @packageManager.deactivatePackage(pack.name) for pack in @getActiveThemes() null @@ -106,12 +107,23 @@ class ThemeManager else null + #Private: + unwatchUserStylesheet: -> + @userStylesheetFile?.off() + @removeStylesheet(@userStylesheetPath) if @userStylesheetPath? + # Private: loadUserStylesheet: -> - if userStylesheetPath = @getUserStylesheetPath() - @userStylesheetPath = userStylesheetPath - userStylesheetContents = @loadStylesheet(userStylesheetPath) - @applyStylesheet(userStylesheetPath, userStylesheetContents, 'userTheme') + @unwatchUserStylesheet() + userStylesheetPath = @getUserStylesheetPath() + return unless fs.isFileSync(userStylesheetPath) + + @userStylesheetPath = userStylesheetPath + @userStylesheetFile = new File(userStylesheetPath) + @userStylesheetFile.on 'contents-changed moved removed', => + @loadUserStylesheet() + userStylesheetContents = @loadStylesheet(userStylesheetPath) + @applyStylesheet(userStylesheetPath, userStylesheetContents, 'userTheme') # Internal-only: loadBaseStylesheets: -> @@ -178,8 +190,7 @@ class ThemeManager # Internal-only: removeStylesheet: (stylesheetPath) -> - unless fullPath = @resolveStylesheet(stylesheetPath) - throw new Error("Could not find a file at path '#{stylesheetPath}'") + fullPath = @resolveStylesheet(stylesheetPath) ? stylesheetPath @stylesheetElementForId(@stringToId(fullPath)).remove() # Internal-only: