Added PopupDefault, PopupDefaultsSeq, and SetPopupDefaults methods to Session interface

This commit is contained in:
Alexei Anoshenko 2026-07-01 13:37:23 +03:00
parent 4c3bcf4427
commit 84c8c1e3d7
3 changed files with 67 additions and 64 deletions

View File

@ -3,7 +3,7 @@
* Removed "style-disabled" property and GetDisabledStyle function
* Added GoogleFonts field to AppParams
* Added functions: GetWhiteSpace, GetWordBreak, ScrollIntoViewIfNeeded
* Added Popups, PopupShowAnimation, and SetPopupShowAnimation methods to Session interface
* Added Popups, PopupDefault, PopupDefaultsSeq, and SetPopupDefaults methods to Session interface
* Added DismissWithoutAnimation add SetHotKey methods to Popup interface
* Added ViewSeq add ViewCount methods to ParentView interface
* Added ToBoundsProperty method to Bounds struct

View File

@ -798,14 +798,34 @@ func (popup *popupData) propertyChanged(tag PropertyName) {
}
}
func (popup *popupData) animationProperty() AnimationProperty {
_, _, defaultDuration, defaultTiming := popup.session.PopupShowAnimation()
duration, _ := floatProperty(popup, ShowDuration, popup.session, defaultDuration)
timing, ok := stringProperty(popup, ShowTiming, popup.session)
if !ok {
timing = defaultTiming
func (popup *popupData) defaultPopupFloat(tag PropertyName, defaultValue float64) float64 {
if value := popup.session.PopupDefault(tag); value != nil {
if f, ok := valueToFloat(value, popup.session, defaultValue); ok {
return f
} else {
DebugLogF(`"%v" is an invalid float64 value`, value)
}
}
return defaultValue
}
func (popup *popupData) animationProperty() AnimationProperty {
timing, ok := stringProperty(popup, ShowTiming, popup.session)
if ok && !isTimingFunctionValid(timing) {
DebugLog(`"` + timing + `" is an invalid timing function`)
ok = false
}
if !ok {
timing = EaseTiming
if value := popup.session.PopupDefault(ShowTiming); value != nil {
if text, ok := value.(string); ok && text != "" && isTimingFunctionValid(text) {
timing = text
}
}
}
duration, _ := floatProperty(popup, ShowDuration, popup.session, popup.defaultPopupFloat(ShowDuration, 1))
return NewAnimationProperty(Params{
Duration: duration,
TimingFunction: timing,
@ -967,12 +987,15 @@ func (popup *popupData) Show() {
}
func (popup *popupData) showTransformAndOpacity() (TransformProperty, float64) {
defaultTransform, defaultOpacity, _, _ := popup.session.PopupShowAnimation()
opacity, _ := floatProperty(popup, ShowOpacity, popup.session, defaultOpacity)
opacity, _ := floatProperty(popup, ShowOpacity, popup.session, popup.defaultPopupFloat(ShowOpacity, 1))
transform := getTransformProperty(popup, ShowTransform)
if transform == nil {
transform = defaultTransform
if value := popup.session.PopupDefault(ShowTransform); value != nil {
if t, ok := value.(TransformProperty); ok {
transform = t
}
}
}
return transform, opacity
@ -1387,34 +1410,9 @@ func NewPopup(view View, param Params) Popup {
popup.properties = map[PropertyName]any{}
popup.hotkeys = map[string]func(Popup){}
defaultTransform, defaultOpacity, duration, timing := popup.session.PopupShowAnimation()
if value, ok := param[ShowTransform]; ok {
if transform := valueToTransformProperty(value); transform != nil {
defaultTransform = transform
} else {
param[ShowTransform] = defaultTransform
}
} else if defaultTransform != nil {
param[ShowTransform] = defaultTransform
}
if value, ok := param[ShowOpacity]; ok {
if opacity, _ := valueToFloat(value, popup.session, 1); opacity >= 0 && opacity < 1 {
defaultOpacity = opacity
} else {
param[ShowOpacity] = defaultOpacity
}
} else if defaultOpacity != 1 {
param[ShowOpacity] = defaultOpacity
}
if defaultTransform != nil || defaultOpacity != 1 {
if _, ok := param[ShowDuration]; !ok {
param[ShowDuration] = duration
}
if _, ok := param[ShowTiming]; !ok {
param[ShowTiming] = timing
for tag, value := range popup.session.PopupDefaultsSeq() {
if _, ok := param[tag]; !ok && value != nil {
param[tag] = value
}
}

View File

@ -2,6 +2,8 @@ package rui
import (
"fmt"
"iter"
"maps"
"net/url"
"slices"
"strconv"
@ -112,16 +114,17 @@ type Session interface {
// a description of the error is written to the log
Set(viewID string, tag PropertyName, value any) bool
// PopupShowAnimation returns default popup animation parameters.
// Returns the default value for the properties: "show-transform", "show-opacity", "show-duration" and "show-timing".
PopupShowAnimation() (transform TransformProperty, opacity, duration float64, timing string)
// PopupShowAnimation returns a list of displayed popups or nil if there are no displayed popups.
Popups() []Popup
// SetPopupShowAnimation sets default popup animation parameters.
// Sets the default value for the properties: "show-transform", "show-opacity", "show-duration" and "show-timing".
SetPopupShowAnimation(transform TransformProperty, opacity, duration float64, timing string)
// PopupDefault returns default values for the Popup property
PopupDefault(tag PropertyName) any
// PopupDefaultsSeq returns an iterator over all default values for Popup properties
PopupDefaultsSeq() iter.Seq2[PropertyName, any]
// SetPopupDefaults sets default values for Popup properties
SetPopupDefaults(params Params)
// DownloadFile downloads (saves) on the client side the file located at the specified path on the server.
DownloadFile(path string)
@ -250,10 +253,7 @@ type sessionData struct {
timers map[int]func(Session)
nextTimerID int
pauseTime int64
popupTransform TransformProperty
popupOpacity float64
popupDuration float64
popupTiming string
popupDefaults Params
}
func newSession(app Application, id int, customTheme string, params DataObject) Session {
@ -274,9 +274,6 @@ func newSession(app Application, id int, customTheme string, params DataObject)
session.hotkeys = map[string]func(Session){}
session.timers = map[int]func(Session){}
session.nextTimerID = 1
session.popupOpacity = 1
session.popupDuration = 1
session.popupTiming = EaseTiming
if customTheme != "" {
if theme, ok := CreateThemeFromText(customTheme); ok {
@ -977,19 +974,27 @@ func (session *sessionData) StopTimer(timerID int) {
}
}
func (session *sessionData) SetPopupShowAnimation(transform TransformProperty, opacity, duration float64, timing string) {
session.popupTransform = transform
if opacity >= 0 && opacity <= 1 {
session.popupOpacity = opacity
func (session *sessionData) PopupDefault(tag PropertyName) any {
if value, ok := session.popupDefaults[tag]; ok {
return value
}
if duration > 0 {
session.popupDuration = duration
}
if isTimingFunctionValid(timing) {
session.popupTiming = timing
return nil
}
func (session *sessionData) PopupDefaultsSeq() iter.Seq2[PropertyName, any] {
return func(yield func(PropertyName, any) bool) {
for tag, value := range session.popupDefaults {
if !yield(tag, value) {
return
}
}
}
}
func (session *sessionData) PopupShowAnimation() (transform TransformProperty, opacity, duration float64, timing string) {
return session.popupTransform, session.popupOpacity, session.popupDuration, session.popupTiming
func (session *sessionData) SetPopupDefaults(params Params) {
if len(params) > 0 {
session.popupDefaults = maps.Clone(params)
} else {
session.popupDefaults = nil
}
}