mirror of
https://github.com/atom/atom.git
synced 2026-02-16 09:35:54 -05:00
add pane-resize-handle-view and insert resize view into pane axis element
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
{CompositeDisposable} = require 'event-kit'
|
||||
{callAttachHooks} = require './space-pen-extensions'
|
||||
PaneResizeHandleView = require './pane-resize-handle-view'
|
||||
|
||||
class PaneAxisElement extends HTMLElement
|
||||
createdCallback: ->
|
||||
@@ -22,13 +23,35 @@ class PaneAxisElement extends HTMLElement
|
||||
@classList.add('vertical', 'pane-column')
|
||||
this
|
||||
|
||||
isPaneResizeHandleElement: (element) ->
|
||||
element?.classList.contains('pane-resize-handle')
|
||||
|
||||
childAdded: ({child, index}) ->
|
||||
view = atom.views.getView(child)
|
||||
@insertBefore(view, @children[index])
|
||||
@insertBefore(view, @children[index * 2])
|
||||
|
||||
prevElement = view.previousSibling
|
||||
# if previous element is not pane resize element, then insert new resize element
|
||||
if prevElement? and not @isPaneResizeHandleElement(prevElement)
|
||||
resizeView = new PaneResizeHandleView()
|
||||
resizeView.initialize()
|
||||
@insertBefore(resizeView[0], view)
|
||||
|
||||
nextElement = view.nextSibling
|
||||
# if next element isnot resize element, then insert new resize element
|
||||
if nextElement? and not @isPaneResizeHandleElement(nextElement)
|
||||
resizeView = new PaneResizeHandleView()
|
||||
resizeView.initialize()
|
||||
@insertBefore(resizeView[0], nextElement)
|
||||
|
||||
callAttachHooks(view) # for backward compatibility with SpacePen views
|
||||
|
||||
childRemoved: ({child}) ->
|
||||
view = atom.views.getView(child)
|
||||
siblingView = view.previousSibling
|
||||
# make sure next sibling view is pane resize view
|
||||
if siblingView?.classList.contains('pane-resize-handle')
|
||||
siblingView.remove()
|
||||
view.remove()
|
||||
|
||||
childReplaced: ({index, oldChild, newChild}) ->
|
||||
|
||||
69
src/pane-resize-handle-view.coffee
Normal file
69
src/pane-resize-handle-view.coffee
Normal file
@@ -0,0 +1,69 @@
|
||||
{$, View} = require 'atom-space-pen-views'
|
||||
|
||||
module.exports =
|
||||
class PaneResizeHandleView extends View
|
||||
@content: ->
|
||||
@div class: 'pane-resize-handle'
|
||||
|
||||
initialize: ->
|
||||
@handleEvents()
|
||||
|
||||
attached: ->
|
||||
@isHorizontal = @parent().hasClass("horizontal")
|
||||
@prevPane = @prev()
|
||||
@nextPane = @next()
|
||||
|
||||
detached: ->
|
||||
|
||||
handleEvents: ->
|
||||
@on 'dblclick', =>
|
||||
@resizeToFitContent()
|
||||
@on 'mousedown', (e) =>
|
||||
@resizeStarted(e)
|
||||
|
||||
resizeToFitContent: ->
|
||||
# clear flex-grow css style of both pane
|
||||
@prevPane.css('flexGrow', '')
|
||||
@nextPane.css('flexGrow', '')
|
||||
|
||||
resizeStarted: (e)->
|
||||
e.stopPropagation()
|
||||
$(document).on('mousemove', @resizePane)
|
||||
$(document).on('mouseup', @resizeStopped)
|
||||
|
||||
calcRatio: (ratio1, ratio2, total) ->
|
||||
allRatio = ratio1 + ratio2
|
||||
[total * ratio1 / allRatio, total * ratio2 / allRatio]
|
||||
|
||||
getFlexGrow: (element) ->
|
||||
parseFloat element.css('flexGrow')
|
||||
|
||||
setFlexGrow: (prevSize, nextSize) ->
|
||||
flexGrow = @getFlexGrow(@prevPane) + @getFlexGrow(@nextPane)
|
||||
flexGrows = @calcRatio(prevSize, nextSize, flexGrow)
|
||||
@prevPane.css('flexGrow', flexGrows[0].toString())
|
||||
@nextPane.css('flexGrow', flexGrows[1].toString())
|
||||
|
||||
fixInRange: (val, minValue, maxValue) ->
|
||||
Math.min(Math.max(val, minValue), maxValue)
|
||||
|
||||
resizePane: ({pageX, pageY, which}) =>
|
||||
return @resizeStopped() unless which is 1
|
||||
|
||||
if @isHorizontal
|
||||
totalWidth = @prevPane.outerWidth() + @nextPane.outerWidth()
|
||||
#get the left and right width after move the resize view
|
||||
leftWidth = @fixInRange(pageX - @prevPane.offset().left, 0, totalWidth)
|
||||
rightWidth = totalWidth - leftWidth
|
||||
# set the flex grow by the ratio of left width and right width
|
||||
# to change pane width
|
||||
@setFlexGrow(leftWidth, rightWidth)
|
||||
else
|
||||
totalHeight = @prevPane.outerHeight() + @nextPane.outerHeight()
|
||||
topHeight = @fixInRange(pageY - @prevPane.offset().top, 0, totalHeight)
|
||||
bottomHeight = totalHeight - topHeight
|
||||
@setFlexGrow(topHeight, bottomHeight)
|
||||
|
||||
resizeStopped: =>
|
||||
$(document).off('mousemove', @resizePane)
|
||||
$(document).off('mouseup', @resizeStopped)
|
||||
Reference in New Issue
Block a user