From ecf2535be3655a1309f7fcf5fb0040addb7c97fd Mon Sep 17 00:00:00 2001
From: mrmr1993
Date: Mon, 11 May 2015 16:04:50 +0100
Subject: Move the HUD to its own file
---
 content_scripts/hud.coffee             | 88 ++++++++++++++++++++++++++++++++++
 content_scripts/vimium_frontend.coffee | 87 ---------------------------------
 manifest.json                          |  1 +
 tests/dom_tests/dom_tests.html         |  1 +
 4 files changed, 90 insertions(+), 87 deletions(-)
 create mode 100644 content_scripts/hud.coffee
diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee
new file mode 100644
index 00000000..9b23d050
--- /dev/null
+++ b/content_scripts/hud.coffee
@@ -0,0 +1,88 @@
+#
+# A heads-up-display (HUD) for showing Vimium page operations.
+# Note: you cannot interact with the HUD until document.body is available.
+#
+HUD =
+  _tweenId: -1
+  _displayElement: null
+
+  # This HUD is styled to precisely mimick the chrome HUD on Mac. Use the "has_popup_and_link_hud.html"
+  # test harness to tweak these styles to match Chrome's. One limitation of our HUD display is that
+  # it doesn't sit on top of horizontal scrollbars like Chrome's HUD does.
+
+  showForDuration: (text, duration) ->
+    HUD.show(text)
+    HUD._showForDurationTimerId = setTimeout((-> HUD.hide()), duration)
+
+  show: (text) ->
+    return unless HUD.enabled()
+    clearTimeout(HUD._showForDurationTimerId)
+    HUD.displayElement().innerText = text
+    clearInterval(HUD._tweenId)
+    HUD._tweenId = Tween.fade(HUD.displayElement(), 1.0, 150)
+    HUD.displayElement().style.display = ""
+
+  #
+  # Retrieves the HUD HTML element.
+  #
+  displayElement: ->
+    if (!HUD._displayElement)
+      HUD._displayElement = HUD.createHudElement()
+      # Keep this far enough to the right so that it doesn't collide with the "popups blocked" chrome HUD.
+      HUD._displayElement.style.right = "150px"
+    HUD._displayElement
+
+  createHudElement: ->
+    element = document.createElement("div")
+    element.className = "vimiumReset vimiumHUD"
+    document.body.appendChild(element)
+    element
+
+  # Hide the HUD.
+  # If :immediate is falsy, then the HUD is faded out smoothly (otherwise it is hidden immediately).
+  # If :updateIndicator is truthy, then we also refresh the mode indicator.  The only time we don't update the
+  # mode indicator, is when hide() is called for the mode indicator itself.
+  hide: (immediate = false, updateIndicator = true) ->
+    clearInterval(HUD._tweenId)
+    if immediate
+      HUD.displayElement().style.display = "none" unless updateIndicator
+      Mode.setIndicator() if updateIndicator
+    else
+      HUD._tweenId = Tween.fade HUD.displayElement(), 0, 150, -> HUD.hide true, updateIndicator
+
+  isReady: do ->
+    ready = false
+    DomUtils.documentReady -> ready = true
+    -> ready and document.body != null
+
+  # A preference which can be toggled in the Options page. */
+  enabled: -> !settings.get("hideHud")
+
+Tween =
+  #
+  # Fades an element's alpha. Returns a timer ID which can be used to stop the tween via clearInterval.
+  #
+  fade: (element, toAlpha, duration, onComplete) ->
+    state = {}
+    state.duration = duration
+    state.startTime = (new Date()).getTime()
+    state.from = parseInt(element.style.opacity) || 0
+    state.to = toAlpha
+    state.onUpdate = (value) ->
+      element.style.opacity = value
+      if (value == state.to && onComplete)
+        onComplete()
+    state.timerId = setInterval((-> Tween.performTweenStep(state)), 50)
+    state.timerId
+
+  performTweenStep: (state) ->
+    elapsed = (new Date()).getTime() - state.startTime
+    if (elapsed >= state.duration)
+      clearInterval(state.timerId)
+      state.onUpdate(state.to)
+    else
+      value = (elapsed / state.duration)  * (state.to - state.from) + state.from
+      state.onUpdate(value)
+
+root = exports ? window
+root.HUD = HUD
diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee
index d6cc835d..82d24f89 100644
--- a/content_scripts/vimium_frontend.coffee
+++ b/content_scripts/vimium_frontend.coffee
@@ -1141,92 +1141,6 @@ toggleHelpDialog = (html, fid) ->
   else
     showHelpDialog(html, fid)
 
-#
-# A heads-up-display (HUD) for showing Vimium page operations.
-# Note: you cannot interact with the HUD until document.body is available.
-#
-HUD =
-  _tweenId: -1
-  _displayElement: null
-
-  # This HUD is styled to precisely mimick the chrome HUD on Mac. Use the "has_popup_and_link_hud.html"
-  # test harness to tweak these styles to match Chrome's. One limitation of our HUD display is that
-  # it doesn't sit on top of horizontal scrollbars like Chrome's HUD does.
-
-  showForDuration: (text, duration) ->
-    HUD.show(text)
-    HUD._showForDurationTimerId = setTimeout((-> HUD.hide()), duration)
-
-  show: (text) ->
-    return unless HUD.enabled()
-    clearTimeout(HUD._showForDurationTimerId)
-    HUD.displayElement().innerText = text
-    clearInterval(HUD._tweenId)
-    HUD._tweenId = Tween.fade(HUD.displayElement(), 1.0, 150)
-    HUD.displayElement().style.display = ""
-
-  #
-  # Retrieves the HUD HTML element.
-  #
-  displayElement: ->
-    if (!HUD._displayElement)
-      HUD._displayElement = HUD.createHudElement()
-      # Keep this far enough to the right so that it doesn't collide with the "popups blocked" chrome HUD.
-      HUD._displayElement.style.right = "150px"
-    HUD._displayElement
-
-  createHudElement: ->
-    element = document.createElement("div")
-    element.className = "vimiumReset vimiumHUD"
-    document.body.appendChild(element)
-    element
-
-  # Hide the HUD.
-  # If :immediate is falsy, then the HUD is faded out smoothly (otherwise it is hidden immediately).
-  # If :updateIndicator is truthy, then we also refresh the mode indicator.  The only time we don't update the
-  # mode indicator, is when hide() is called for the mode indicator itself.
-  hide: (immediate = false, updateIndicator = true) ->
-    clearInterval(HUD._tweenId)
-    if immediate
-      HUD.displayElement().style.display = "none" unless updateIndicator
-      Mode.setIndicator() if updateIndicator
-    else
-      HUD._tweenId = Tween.fade HUD.displayElement(), 0, 150, -> HUD.hide true, updateIndicator
-
-  isReady: do ->
-    ready = false
-    DomUtils.documentReady -> ready = true
-    -> ready and document.body != null
-
-  # A preference which can be toggled in the Options page. */
-  enabled: -> !settings.get("hideHud")
-
-Tween =
-  #
-  # Fades an element's alpha. Returns a timer ID which can be used to stop the tween via clearInterval.
-  #
-  fade: (element, toAlpha, duration, onComplete) ->
-    state = {}
-    state.duration = duration
-    state.startTime = (new Date()).getTime()
-    state.from = parseInt(element.style.opacity) || 0
-    state.to = toAlpha
-    state.onUpdate = (value) ->
-      element.style.opacity = value
-      if (value == state.to && onComplete)
-        onComplete()
-    state.timerId = setInterval((-> Tween.performTweenStep(state)), 50)
-    state.timerId
-
-  performTweenStep: (state) ->
-    elapsed = (new Date()).getTime() - state.startTime
-    if (elapsed >= state.duration)
-      clearInterval(state.timerId)
-      state.onUpdate(state.to)
-    else
-      value = (elapsed / state.duration)  * (state.to - state.from) + state.from
-      state.onUpdate(value)
-
 CursorHider =
   #
   # Hide the cursor when the browser scrolls, and prevent mouse from hovering while invisible.
@@ -1274,7 +1188,6 @@ window.onbeforeunload = ->
 
 root = exports ? window
 root.settings = settings
-root.HUD = HUD
 root.handlerStack = handlerStack
 root.frameId = frameId
 root.windowIsFocused = windowIsFocused
diff --git a/manifest.json b/manifest.json
index 6445548a..59e210eb 100644
--- a/manifest.json
+++ b/manifest.json
@@ -52,6 +52,7 @@
              "content_scripts/mode_passkeys.js",
              "content_scripts/mode_find.js",
              "content_scripts/mode_visual_edit.js",
+             "content_scripts/hud.js",
              "content_scripts/vimium_frontend.js"
             ],
       "css": ["content_scripts/vimium.css"],
diff --git a/tests/dom_tests/dom_tests.html b/tests/dom_tests/dom_tests.html
index cbd91bca..5ccd39e7 100644
--- a/tests/dom_tests/dom_tests.html
+++ b/tests/dom_tests/dom_tests.html
@@ -44,6 +44,7 @@
     
     
     
+    
     
 
     
-- 
cgit v1.2.3