Skip to content

Commit

Permalink
Add basic view update routine
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Dec 14, 2014
1 parent a3f3ac0 commit 312b608
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
29 changes: 27 additions & 2 deletions lib/minimap-element.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class MinimapElement extends HTMLElement

attachedCallback: ->
@measureHeightAndWidth()
@requestUpdate()

detachedCallback: ->

Expand All @@ -21,8 +22,12 @@ class MinimapElement extends HTMLElement
@shadowRoot = @createShadowRoot()

@canvas = document.createElement('canvas')
@context = @canvas.getContext('2d')
@shadowRoot.appendChild(@canvas)

@offscreenCanvas = document.createElement('canvas')
@offscreenContext = @offscreenCanvas.getContext('2d')

@visibleArea = document.createElement('div')
@visibleArea.classList.add('minimap-visible-area')
@shadowRoot.appendChild(@visibleArea)
Expand All @@ -33,8 +38,8 @@ class MinimapElement extends HTMLElement
height = @clientHeight

if width isnt @canvas.width or height isnt @canvas.height
@canvas.width = width
@canvas.height = height
@canvas.width = width * devicePixelRatio
@canvas.height = height * devicePixelRatio

getTextEditorElement: ->
@editorElement ?= atom.views.getView(@minimap.getTextEditor())
Expand All @@ -44,6 +49,26 @@ class MinimapElement extends HTMLElement

editorElement.shadowRoot ? editorElement

requestUpdate: ->
return if @frameRequested

@frameRequested = true
requestAnimationFrame =>
@update()
@frameRequested = false

update: ->
@visibleArea.style.width = @clientWidth + 'px'
@visibleArea.style.height = @minimap.getTextEditorHeight() + 'px'

# ######## ## ######## ## ## ######## ## ## ########
# ## ## ## ### ### ## ### ## ##
# ## ## ## #### #### ## #### ## ##
# ###### ## ###### ## ### ## ###### ## ## ## ##
# ## ## ## ## ## ## ## #### ##
# ## ## ## ## ## ## ## ### ##
# ######## ######## ######## ## ## ######## ## ## ##

module.exports = MinimapElement = document.registerElement 'atom-text-editor-minimap', prototype: MinimapElement.prototype

MinimapElement.registerViewProvider = ->
Expand Down
14 changes: 13 additions & 1 deletion spec/minimap-element-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe 'MinimapElement', ->
expect(minimapElement.shadowRoot.querySelector('.minimap-visible-area')).toExist()

describe 'when attached to the text editor element', ->
[nextAnimationFrame, canvas] = []
[nextAnimationFrame, canvas, visibleArea] = []

beforeEach ->
spyOn(window, "setInterval").andCallFake window.fakeSetInterval
Expand Down Expand Up @@ -84,3 +84,15 @@ describe 'MinimapElement', ->
it 'resizes the canvas to fit the minimap', ->
expect(canvas.offsetHeight).toEqual(minimapElement.offsetHeight)
expect(canvas.offsetWidth).toEqual(minimapElement.offsetWidth)

it 'requests an update', ->
expect(minimapElement.frameRequested).toBeTruthy()

describe 'when the update is performed', ->
beforeEach ->
nextAnimationFrame()
visibleArea = minimapElement.shadowRoot.querySelector('.minimap-visible-area')

it 'sets the visible area width and height', ->
expect(visibleArea.offsetWidth).toEqual(minimapElement.clientWidth)
expect(visibleArea.offsetHeight).toBeCloseTo(minimap.getTextEditorHeight(), 0)
2 changes: 1 addition & 1 deletion stylesheets/minimap.less
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ atom-text-editor::shadow, atom-text-editor {

&::shadow {
canvas {

position: absolute;
top: 0;
left: 0;
}

.minimap-visible-area {
position: absolute;
display: 'block';
}
}
}
Expand Down

0 comments on commit 312b608

Please sign in to comment.