From f3f3ddb0845c37541f36f53e6e2c3d07b1f4e7e5 Mon Sep 17 00:00:00 2001 From: Alexei Anoshenko Date: Wed, 10 Aug 2022 15:36:38 +0300 Subject: [PATCH] Updated transition --- CHANGELOG.md | 4 ++- animation.go | 77 +++++++++++++++++++++++++++++++++++------------ customView.go | 19 ++++++++++-- session_test.go | 4 +-- view.go | 2 -- viewStyle.go | 16 +++++++++- viewsContainer.go | 3 +- 7 files changed, 94 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6796ee1..8cd4af3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,9 @@ * The "interface{}" type replaced by "any" * Added "overflow", "arrow", "arrow-align", "arrow-size", "arrow-width", and "arrow-offset" properties * Added "@ruiArrowSize" and "@ruiArrowWidth" constants to the default theme -* Added the GetOverflow function +* Added Transition, Transitions, and SetTransition functions to the ViewStyle interface +* Added GetOverflow, and GetTransitions functions +* Changed GetTransition functions # v0.8.0 diff --git a/animation.go b/animation.go index 29bf505..b08af34 100644 --- a/animation.go +++ b/animation.go @@ -55,16 +55,19 @@ const ( // The animation plays forwards each cycle. In other words, each time the animation cycles, // the animation will reset to the beginning state and start over again. This is the default value. NormalAnimation = 0 + // ReverseAnimation is value of the "animation-direction" property. // The animation plays backwards each cycle. In other words, each time the animation cycles, // the animation will reset to the end state and start over again. Animation steps are performed // backwards, and timing functions are also reversed. // For example, an "ease-in" timing function becomes "ease-out". ReverseAnimation = 1 + // AlternateAnimation is value of the "animation-direction" property. // The animation reverses direction each cycle, with the first iteration being played forwards. // The count to determine if a cycle is even or odd starts at one. AlternateAnimation = 2 + // AlternateReverseAnimation is value of the "animation-direction" property. // The animation reverses direction each cycle, with the first iteration being played backwards. // The count to determine if a cycle is even or odd starts at one. @@ -673,14 +676,38 @@ func (view *viewData) updateTransitionCSS() { updateCSSProperty(view.htmlID(), "transition", view.transitionCSS(view.Session()), view.Session()) } -func (view *viewData) getTransitions() Params { - result := Params{} - for tag, animation := range view.transitions { +func (style *viewStyle) Transition(tag string) Animation { + if style.transitions != nil { + if anim, ok := style.transitions[tag]; ok { + return anim + } + } + return nil +} + +func (style *viewStyle) Transitions() map[string]Animation { + result := map[string]Animation{} + for tag, animation := range style.transitions { result[tag] = animation } return result } +func (style *viewStyle) SetTransition(tag string, animation Animation) { + if animation == nil { + delete(style.transitions, tag) + } else { + style.transitions[tag] = animation + } +} + +func (view *viewData) SetTransition(tag string, animation Animation) { + view.viewStyle.SetTransition(tag, animation) + if view.created { + updateCSSProperty(view.htmlID(), "transition", view.transitionCSS(view.Session()), view.Session()) + } +} + // SetAnimated sets the property with name "tag" of the "rootView" subview with "viewID" id by value. Result: // true - success, // false - error (incompatible type or invalid format of a string value, see AppLog). @@ -697,38 +724,48 @@ func IsAnimationPaused(view View, subviewID string) bool { return boolStyledProperty(view, subviewID, AnimationPaused, false) } -// GetTransition returns the subview transitions. The result is always non-nil. +// GetTransitions returns the subview transitions. The result is always non-nil. // If the second argument (subviewID) is "" then transitions of the first argument (view) is returned -func GetTransition(view View, subviewID string) Params { +func GetTransitions(view View, subviewID string) map[string]Animation { if subviewID != "" { view = ViewByID(view, subviewID) } if view != nil { - return view.getTransitions() + return view.Transitions() } - return Params{} + return map[string]Animation{} +} + +// GetTransition returns the subview property transition. If there is no transition for the given property then nil is returned. +// If the second argument (subviewID) is "" then transitions of the first argument (view) is returned +func GetTransition(view View, subviewID, tag string) Animation { + if subviewID != "" { + view = ViewByID(view, subviewID) + } + + if view != nil { + return view.Transition(tag) + } + + return nil } // AddTransition adds the transition for the subview property. // If the second argument (subviewID) is "" then the transition is added to the first argument (view) func AddTransition(view View, subviewID, tag string, animation Animation) bool { - if tag == "" { - return false - } + if tag != "" { + if subviewID != "" { + view = ViewByID(view, subviewID) + } - if subviewID != "" { - view = ViewByID(view, subviewID) + if view != nil { + view.SetTransition(tag, animation) + return true + } } - - if view == nil { - return false - } - - transitions := view.getTransitions() - transitions[tag] = animation - return view.Set(Transition, transitions) + return false } // GetAnimation returns the subview animations. The result is always non-nil. diff --git a/customView.go b/customView.go index 27b93d5..54e732f 100644 --- a/customView.go +++ b/customView.go @@ -264,9 +264,22 @@ func (customView *CustomViewData) setScroll(x, y, width, height float64) { } } -func (customView *CustomViewData) getTransitions() Params { +func (customView *CustomViewData) Transition(tag string) Animation { if customView.superView != nil { - return customView.superView.getTransitions() + return customView.superView.Transition(tag) + } + return nil +} + +func (customView *CustomViewData) Transitions() map[string]Animation { + if customView.superView != nil { + return customView.superView.Transitions() + } + return map[string]Animation{} +} + +func (customView *CustomViewData) SetTransition(tag string, animation Animation) { + if customView.superView != nil { + customView.superView.SetTransition(tag, animation) } - return Params{} } diff --git a/session_test.go b/session_test.go index d79469c..314ef85 100644 --- a/session_test.go +++ b/session_test.go @@ -4,8 +4,8 @@ import ( "testing" ) -var stopTestLogFlag = false -var testLogDone chan int +// var stopTestLogFlag = false +// var testLogDone chan int var ignoreTestLog = false func createTestLog(t *testing.T, ignore bool) { diff --git a/view.go b/view.go index 813dbe0..cf4380a 100644 --- a/view.go +++ b/view.go @@ -71,8 +71,6 @@ type View interface { cssStyle(self View, builder cssBuilder) addToCSSStyle(addCSS map[string]string) - getTransitions() Params - onResize(self View, x, y, width, height float64) onItemResize(self View, index string, x, y, width, height float64) setNoResizeEvent() diff --git a/viewStyle.go b/viewStyle.go index 38f0c4b..1cd9f43 100644 --- a/viewStyle.go +++ b/viewStyle.go @@ -10,6 +10,16 @@ import ( // ViewStyle interface of the style of view type ViewStyle interface { Properties + + // Transition returns the transition animation of the property. Returns nil is there is no transition animation. + Transition(tag string) Animation + // Transitions returns the map of transition animations. The result is always non-nil. + Transitions() map[string]Animation + // SetTransition sets the transition animation for the property if "animation" argument is not nil, and + // removes the transition animation of the property if "animation" argument is nil. + // The "tag" argument is the property name. + SetTransition(tag string, animation Animation) + cssViewStyle(buffer cssBuilder, session Session) } @@ -741,7 +751,7 @@ func writePropertyValue(buffer *strings.Builder, tag string, value any, indent s if animation := value[tag]; animation != nil { buffer.WriteString(indent2) animation.writeTransitionString(tag, buffer) - buffer.WriteString("\n") + buffer.WriteString(",\n") } } buffer.WriteString(indent) @@ -822,6 +832,10 @@ func writeViewStyle(name string, view ViewStyle, buffer *strings.Builder, indent } } + if transitions := view.Transitions(); len(transitions) > 0 { + writeProperty(Transition, transitions) + } + indent = indent[:len(indent)-1] buffer.WriteString(indent) buffer.WriteString("}") diff --git a/viewsContainer.go b/viewsContainer.go index b92bb3a..e68df72 100644 --- a/viewsContainer.go +++ b/viewsContainer.go @@ -47,8 +47,7 @@ func (container *viewsContainerData) setParentID(parentID string) { func (container *viewsContainerData) Views() []View { if container.views == nil { container.views = []View{} - } - if count := len(container.views); count > 0 { + } else if count := len(container.views); count > 0 { views := make([]View, count) copy(views, container.views) return views