Skip to content

Commit

Permalink
Allow non animatable properties in states
Browse files Browse the repository at this point in the history
Like scroll, backgroundColor etc. Transition will be instant.
  • Loading branch information
koenbok committed Dec 12, 2014
1 parent 6cc6def commit 3f9411e
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 16 deletions.
22 changes: 10 additions & 12 deletions extras/CactusFramer/static/app.coffee
Original file line number Original file line Diff line number Diff line change
@@ -1,15 +1,13 @@
Framer.Device = new Framer.DeviceView() layer = new Layer
Framer.Device.setupContext() layer.states.add
stateA: {backgroundColor:"red", x:500}


Framer.Device.deviceType = "desktop-browser-1440" # layer.scroll.should.equal false
# layer.x.should.equal 0


f = false layer.states.on Events.StateDidSwitch, ->
l = new Layer print "done", layer.backgroundColor


l.on Events.Click, ->
if f is true
Framer.Device.deviceType = "desktop-browser-1440" layer.states.switch "stateA", {curve:"ease-in-out", time:1}
f = false
else
Framer.Device.deviceType = "fullscreen"
f = true
1 change: 1 addition & 0 deletions framer/Layer.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class exports.Layer extends BaseClass
layer.ignoreEvents = false if value is true layer.ignoreEvents = false if value is true


@define "scroll", @define "scroll",
exportable: true
get: -> @scrollHorizontal is true or @scrollVertical is true get: -> @scrollHorizontal is true or @scrollVertical is true
set: (value) -> @scrollHorizontal = @scrollVertical = value set: (value) -> @scrollHorizontal = @scrollVertical = value


Expand Down
26 changes: 22 additions & 4 deletions framer/LayerStates.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ class exports.LayerStates extends BaseClass
# if stateName is @_currentState # if stateName is @_currentState
# return # return


if not @_states.hasOwnProperty stateName if not @_states.hasOwnProperty(stateName)
throw Error "No such state: '#{stateName}'" throw Error "No such state: '#{stateName}'"


@emit Events.StateWillSwitch, @_currentState, stateName, @ @emit(Events.StateWillSwitch, @_currentState, stateName, @)


@_previousStates.push @_currentState @_previousStates.push(@_currentState)
@_currentState = stateName @_currentState = stateName


properties = {} properties = {}
Expand All @@ -89,6 +89,16 @@ class exports.LayerStates extends BaseClass
# Set the new value # Set the new value
properties[propertyName] = value properties[propertyName] = value


# If we are only transitioning to non-animatable (numeric) properties
# we fallback to an instant switch
animatablePropertyKeys = []

for k, v of properties
animatablePropertyKeys.push(k) if _.isNumber(v)

if animatablePropertyKeys.length == 0
instant = true

if instant is true if instant is true
# We want to switch immediately without animation # We want to switch immediately without animation
@layer.properties = properties @layer.properties = properties
Expand All @@ -102,9 +112,15 @@ class exports.LayerStates extends BaseClass
@_animation?.stop() @_animation?.stop()
@_animation = @layer.animate animationOptions @_animation = @layer.animate animationOptions
@_animation.on "stop", => @_animation.on "stop", =>

# Set all the values for keys that we couldn't animate
for k, v of properties
@layer[k] = v if not _.isNumber(v)

@emit Events.StateDidSwitch, _.last(@_previousStates), stateName, @ @emit Events.StateDidSwitch, _.last(@_previousStates), stateName, @





switchInstant: (stateName) -> switchInstant: (stateName) ->
@switch stateName, null, true @switch stateName, null, true


Expand All @@ -117,11 +133,13 @@ class exports.LayerStates extends BaseClass


animatingKeys: -> animatingKeys: ->


# Get a list of all the propeties controlled by states

keys = [] keys = []


for stateName, state of @_states for stateName, state of @_states
continue if stateName is "default" continue if stateName is "default"
keys = _.union keys, _.keys state keys = _.union(keys, _.keys(state))


keys keys


Expand Down
53 changes: 53 additions & 0 deletions test/tests/LayerStatesTest.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -69,3 +69,56 @@ describe "LayerStates", ->
layer.states.switchInstant "stateB" layer.states.switchInstant "stateB"
layer.states.current.should.equal "stateB" layer.states.current.should.equal "stateB"
layer.y.should.equal 123 layer.y.should.equal 123


describe "Properties", ->

it "should set scroll property", ->

layer = new Layer
layer.states.add
stateA: {scroll:true}
stateB: {scroll:false}

layer.states.switchInstant "stateA"
layer.scroll.should.equal true

layer.states.switchInstant "stateB"
layer.scroll.should.equal false

layer.states.switchInstant "stateA"
layer.scroll.should.equal true

it "should set non numeric properties with animation", (done) ->

layer = new Layer
layer.states.add
stateA: {scroll:true, backgroundColor:"red"}

layer.scroll.should.equal false

layer.states.on Events.StateDidSwitch, ->
layer.scroll.should.equal true
layer.backgroundColor.should.equal "red"
done()

layer.states.switch "stateA"

it "should set non and numeric properties with animation", (done) ->

layer = new Layer
layer.states.add
stateA: {x:200, backgroundColor:"red"}

# layer.scroll.should.equal false
layer.x.should.equal 0

layer.states.on Events.StateDidSwitch, ->
# layer.scroll.should.equal true
layer.x.should.equal 200
layer.backgroundColor.should.equal = "red"
done()

layer.states.switch "stateA", {curve:"linear", time:0.1}


8 changes: 8 additions & 0 deletions test/tests/LayerTest.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ describe "Layer", ->
layer.scroll.should.equal false layer.scroll.should.equal false
layer.style["overflow"].should.equal "hidden" layer.style["overflow"].should.equal "hidden"


it "should set scroll from properties", ->

layer = new Layer
layer.properties = {scroll:false}
layer.scroll.should.equal false
layer.properties = {scroll:true}
layer.scroll.should.equal true

it "should set scrollHorizontal", -> it "should set scrollHorizontal", ->


layer = new Layer layer = new Layer
Expand Down

0 comments on commit 3f9411e

Please sign in to comment.