Skip to content

Commit

Permalink
Merge quick settings as custom element branch
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Feb 16, 2015
2 parents 4e30323 + c276d4f commit d114120
Show file tree
Hide file tree
Showing 11 changed files with 463 additions and 351 deletions.
4 changes: 2 additions & 2 deletions keymaps/minimap.cson
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
# 'ctrl-k ctrl-m': 'minimap:toggle'
# 'ctrl-k ctrl-s': 'minimap:open-quick-settings'

'.minimap-quick-settings':
'space': 'core:validate'
'minimap-quick-settings':
'space': 'core:confirm'
6 changes: 6 additions & 0 deletions lib/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class Main
type: 'boolean'
default: false
description: "If this option is enabled then when you click the minimap it will scroll to the destination with animation"
devicePixelRatio:
type: 'number'
minimum: 1
maximum: 3
default: window.devicePixelRatio
description: 'The device pixel ratio used to draw the canvas on high-DPI device.'

# Internal: The activation state of the minimap package.
active: false
Expand Down
62 changes: 38 additions & 24 deletions lib/minimap-element.coffee
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
{debounce} = require 'underscore-plus'
{CompositeDisposable, Disposable} = require 'event-kit'
{EventsDelegation} = require 'atom-utils'
DOMStylesReader = require './mixins/dom-styles-reader'
CanvasDrawer = require './mixins/canvas-drawer'

MinimapQuickSettingsView = null
MinimapQuickSettingsElement = null

# Public:
class MinimapElement extends HTMLElement
DOMStylesReader.includeInto(this)
CanvasDrawer.includeInto(this)
EventsDelegation.includeInto(this)

### Public ###

domPollingInterval: 100
domPollingIntervalId: null
domPollingPaused: false
displayMinimapOnLeft: false
devicePixelRatio: 1

# ## ## ####### ####### ## ## ######
# ## ## ## ## ## ## ## ## ## ##
Expand Down Expand Up @@ -68,6 +71,9 @@ class MinimapElement extends HTMLElement
'minimap.useHardwareAcceleration': (@useHardwareAcceleration) =>
@requestUpdate() if @attached

'minimap.devicePixelRatio': (@devicePixelRatio) =>
@requestUpdate() if @attached

attachedCallback: ->
@domPollingIntervalId = setInterval((=> @pollDOM()), @domPollingInterval)
@measureHeightAndWidth()
Expand Down Expand Up @@ -163,31 +169,39 @@ class MinimapElement extends HTMLElement
@scrollIndicator = undefined

initializeOpenQuickSettings: ->
return if @openQuickSettings?

@openQuickSettings = document.createElement('div')
@openQuickSettings.classList.add 'open-minimap-quick-settings'
@controls.appendChild(@openQuickSettings)
@openQuickSettings.addEventListener 'mousedown', (e) =>
e.preventDefault()
e.stopPropagation()

if @quickSettingsView?
@quickSettingsView.destroy()
@quickSettingsSubscription.dispose()
else
MinimapQuickSettingsView ?= require './minimap-quick-settings-view'
@quickSettingsView = new MinimapQuickSettingsView(this)
@quickSettingsSubscription = @quickSettingsView.onDidDestroy =>
@quickSettingsView = null

@quickSettingsView.attach()
{top, left} = @getBoundingClientRect()
@quickSettingsView.css({
top: top + 'px'
left: (left - @quickSettingsView.width()) + 'px'
})
@openQuickSettingSubscription = @subscribeTo @openQuickSettings,
'mousedown': (e) =>
e.preventDefault()
e.stopPropagation()

if @quickSettingsElement?
@quickSettingsElement.destroy()
@quickSettingsSubscription.dispose()
else
MinimapQuickSettingsElement ?= require './minimap-quick-settings-element'
@quickSettingsElement = new MinimapQuickSettingsElement
@quickSettingsElement.setModel(this)
@quickSettingsSubscription = @quickSettingsElement.onDidDestroy =>
@quickSettingsElement = null

@quickSettingsElement.attach()
{top, left, right} = @canvas.getBoundingClientRect()
@quickSettingsElement.style.top = top + 'px'

if @displayMinimapOnLeft
@quickSettingsElement.style.left = (right) + 'px'
else
@quickSettingsElement.style.left = (left - @quickSettingsElement.clientWidth) + 'px'

disposeOpenQuickSettings: ->
return unless @openQuickSettings?
@controls.removeChild(@openQuickSettings)
@openQuickSettingSubscription.dispose()
@openQuickSettings = undefined

getTextEditor: -> @minimap.getTextEditor()
Expand Down Expand Up @@ -266,12 +280,12 @@ class MinimapElement extends HTMLElement
transform: @makeTranslate(visibleAreaLeft, visibleAreaTop)

@applyStyles @controls,
width: Math.min(@canvas.width, @width) + 'px'
width: Math.min(@canvas.width / devicePixelRatio, @width) + 'px'

canvasTop = @minimap.getFirstVisibleScreenRow() * @minimap.getLineHeight() - @minimap.getScrollTop()

canvasTransform = @makeTranslate(0, canvasTop)
canvasTransform += " " + @makeScale(1/devicePixelRatio) if devicePixelRatio isnt 1
canvasTransform += " " + @makeScale(1 / devicePixelRatio) if devicePixelRatio isnt 1
@applyStyles @canvas, transform: canvasTransform

if @minimapScrollIndicator and @minimap.canScroll() and not @scrollIndicator
Expand Down Expand Up @@ -332,8 +346,8 @@ class MinimapElement extends HTMLElement
delete @marginRight

if canvasWidth isnt @canvas.width or @height isnt @canvas.height
@canvas.width = canvasWidth * devicePixelRatio
@canvas.height = (@height + @minimap.getLineHeight()) * devicePixelRatio
@canvas.width = canvasWidth * @devicePixelRatio
@canvas.height = (@height + @minimap.getLineHeight()) * @devicePixelRatio

# ######## ## ## ######## ## ## ######## ######
# ## ## ## ## ### ## ## ## ##
Expand Down
32 changes: 0 additions & 32 deletions lib/minimap-open-quick-settings-view.coffee

This file was deleted.

144 changes: 144 additions & 0 deletions lib/minimap-quick-settings-element.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
{EventsDelegation, SpacePenDSL} = require 'atom-utils'
{CompositeDisposable, Emitter} = require 'event-kit'

Main = require './main'

module.exports =
class MinimapQuickSettingsElement extends HTMLElement
SpacePenDSL.includeInto(this)
EventsDelegation.includeInto(this)

@content: ->
@div class: 'select-list popover-list minimap-quick-settings', =>
@input type: 'text', class: 'hidden-input', outlet: 'hiddenInput'
@ol class: 'list-group mark-active', outlet: 'list', =>
@li class: 'separator', outlet: 'separator'
@li class: '', outlet: 'codeHighlights', 'code-highlights'
@div class: 'btn-group', =>
@button class: 'btn btn-default', outlet: 'onLeftButton', 'On Left'
@button class: 'btn btn-default', outlet: 'onRightButton', 'On Right'

selectedItem: null

setModel: (@minimap) ->
@emitter = new Emitter
@subscriptions = new CompositeDisposable
@plugins = {}
@itemsActions = new WeakMap

@subscriptions.add Main.onDidAddPlugin ({name, plugin}) =>
@addItemFor(name, plugin)
@subscriptions.add Main.onDidRemovePlugin ({name, plugin}) =>
@removeItemFor(name, plugin)
@subscriptions.add Main.onDidActivatePlugin ({name, plugin}) =>
@activateItem(name, plugin)
@subscriptions.add Main.onDidDeactivatePlugin ({name, plugin}) =>
@deactivateItem(name, plugin)

@subscriptions.add atom.commands.add 'minimap-quick-settings',
'core:move-up': => @selectPreviousItem()
'core:move-down': => @selectNextItem()
'core:move-left': => atom.config.set('minimap.displayMinimapOnLeft', true)
'core:move-right': => atom.config.set('minimap.displayMinimapOnLeft', false)
'core:cancel': => @destroy()
'core:confirm': => @toggleSelectedItem()

@codeHighlights.classList.toggle('active', @minimap.displayCodeHighlights)
@subscriptions.add @subscribeTo @codeHighlights,
'mousedown': (e) =>
e.preventDefault()
atom.config.set('minimap.displayCodeHighlights', !@minimap.displayCodeHighlights)

@itemsActions.set @codeHighlights, =>
atom.config.set('minimap.displayCodeHighlights', !@minimap.displayCodeHighlights)

@subscriptions.add @subscribeTo @hiddenInput,
'focusout': (e) =>
@destroy()

@subscriptions.add @subscribeTo @onLeftButton,
'mousedown': (e) ->
e.preventDefault()
atom.config.set('minimap.displayMinimapOnLeft', true)

@subscriptions.add @subscribeTo @onRightButton,
'mousedown': (e) ->
e.preventDefault()
atom.config.set('minimap.displayMinimapOnLeft', false)

@subscriptions.add atom.config.observe 'minimap.displayCodeHighlights', (bool) =>
@codeHighlights.classList.toggle('active', bool)

@subscriptions.add atom.config.observe 'minimap.displayMinimapOnLeft', (bool) =>
@onLeftButton.classList.toggle('selected', bool)
@onRightButton.classList.toggle('selected', not bool)

@initList()

onDidDestroy: (callback) ->
@emitter.on 'did-destroy', callback

attach: ->
workspaceElement = atom.views.getView(atom.workspace)
workspaceElement.appendChild this
@hiddenInput.focus()

destroy: ->
@emitter.emit('did-destroy')
@subscriptions.dispose()
@parentNode.removeChild(this)

initList: ->
@itemsDisposables = new WeakMap
@addItemFor(name, plugin) for name, plugin of Main.plugins

toggleSelectedItem: => @itemsActions.get(@selectedItem)?()

selectNextItem: ->
@selectedItem.classList.remove('selected')
if @selectedItem.nextSibling?
@selectedItem = @selectedItem.nextSibling
@selectedItem = @selectedItem.nextSibling if @selectedItem.matches('.separator')
else
@selectedItem = @list.firstChild
@selectedItem.classList.add('selected')

selectPreviousItem: ->
@selectedItem.classList.remove('selected')
if @selectedItem.previousSibling?
@selectedItem = @selectedItem.previousSibling
@selectedItem = @selectedItem.previousSibling if @selectedItem.matches('.separator')
else
@selectedItem = @list.lastChild
@selectedItem.classList.add('selected')

addItemFor: (name, plugin) ->
item = document.createElement('li')
item.classList.add('active') if plugin.isActive()
item.textContent = name

action = => Main.togglePluginActivation(name)

@itemsActions.set(item, action)
@itemsDisposables.set item, @addDisposableEventListener item, 'mousedown', (e) =>
e.preventDefault()
action()

@plugins[name] = item
@list.insertBefore item, @separator

unless @selectedItem?
@selectedItem = item
@selectedItem.classList.add('selected')

removeItemFor: (name, plugin) ->
try @list.removeChild(@plugins[name])
delete @plugins[name]

activateItem: (name, plugin) ->
@plugins[name].classList.add('active')

deactivateItem: (name, plugin) ->
@plugins[name].classList.remove('active')

module.exports = MinimapQuickSettingsElement = document.registerElement 'minimap-quick-settings', prototype: MinimapQuickSettingsElement.prototype
Loading

0 comments on commit d114120

Please sign in to comment.