Optimisation

This commit is contained in:
Alexei Anoshenko 2025-06-23 16:59:24 +03:00
parent 0433f460e4
commit bbbaf28aba
12 changed files with 40 additions and 46 deletions

View File

@ -2,6 +2,7 @@ package rui
import ( import (
"fmt" "fmt"
"maps"
"math" "math"
"strconv" "strconv"
"strings" "strings"
@ -661,7 +662,7 @@ func (animation *animationData) animationCSS(session Session) string {
buffer.WriteString(animation.keyFramesName) buffer.WriteString(animation.keyFramesName)
if duration, ok := floatProperty(animation, Duration, session, 1); ok && duration > 0 { if duration, ok := floatProperty(animation, Duration, session, 1); ok && duration > 0 {
buffer.WriteString(fmt.Sprintf(" %gs ", duration)) fmt.Fprintf(buffer, " %gs ", duration)
} else { } else {
buffer.WriteString(" 1s ") buffer.WriteString(" 1s ")
} }
@ -669,7 +670,7 @@ func (animation *animationData) animationCSS(session Session) string {
buffer.WriteString(timingFunctionCSS(animation, TimingFunction, session)) buffer.WriteString(timingFunctionCSS(animation, TimingFunction, session))
if delay, ok := floatProperty(animation, Delay, session, 0); ok && delay > 0 { if delay, ok := floatProperty(animation, Delay, session, 0); ok && delay > 0 {
buffer.WriteString(fmt.Sprintf(" %gs", delay)) fmt.Fprintf(buffer, " %gs", delay)
} else { } else {
buffer.WriteString(" 0s") buffer.WriteString(" 0s")
} }
@ -678,7 +679,7 @@ func (animation *animationData) animationCSS(session Session) string {
if iterationCount == 0 { if iterationCount == 0 {
iterationCount = 1 iterationCount = 1
} }
buffer.WriteString(fmt.Sprintf(" %d ", iterationCount)) fmt.Fprintf(buffer, " %d ", iterationCount)
} else { } else {
buffer.WriteString(" infinite ") buffer.WriteString(" infinite ")
} }
@ -699,7 +700,7 @@ func (animation *animationData) animationCSS(session Session) string {
func (animation *animationData) transitionCSS(buffer *strings.Builder, session Session) { func (animation *animationData) transitionCSS(buffer *strings.Builder, session Session) {
if duration, ok := floatProperty(animation, Duration, session, 1); ok && duration > 0 { if duration, ok := floatProperty(animation, Duration, session, 1); ok && duration > 0 {
buffer.WriteString(fmt.Sprintf(" %gs ", duration)) fmt.Fprintf(buffer, " %gs ", duration)
} else { } else {
buffer.WriteString(" 1s ") buffer.WriteString(" 1s ")
} }
@ -707,7 +708,7 @@ func (animation *animationData) transitionCSS(buffer *strings.Builder, session S
buffer.WriteString(timingFunctionCSS(animation, TimingFunction, session)) buffer.WriteString(timingFunctionCSS(animation, TimingFunction, session))
if delay, ok := floatProperty(animation, Delay, session, 0); ok && delay > 0 { if delay, ok := floatProperty(animation, Delay, session, 0); ok && delay > 0 {
buffer.WriteString(fmt.Sprintf(" %gs", delay)) fmt.Fprintf(buffer, " %gs", delay)
} }
} }
@ -979,11 +980,10 @@ func (style *viewStyle) Transition(tag PropertyName) AnimationProperty {
} }
func (style *viewStyle) Transitions() map[PropertyName]AnimationProperty { func (style *viewStyle) Transitions() map[PropertyName]AnimationProperty {
result := map[PropertyName]AnimationProperty{} if transitions := getTransitionProperty(style); transitions != nil {
for tag, animation := range getTransitionProperty(style) { return maps.Clone(transitions)
result[tag] = animation
} }
return result return map[PropertyName]AnimationProperty{}
} }
func (style *viewStyle) SetTransition(tag PropertyName, animation AnimationProperty) { func (style *viewStyle) SetTransition(tag PropertyName, animation AnimationProperty) {

View File

@ -575,7 +575,7 @@ func (canvas *canvasData) SetLineDash(dash []float64, offset float64) {
for _, val := range dash { for _, val := range dash {
buffer.WriteRune(lead) buffer.WriteRune(lead)
lead = ',' lead = ','
buffer.WriteString(fmt.Sprintf("%g", val)) fmt.Fprintf(buffer, "%g", val))
} }
buffer.WriteRune(']') buffer.WriteRune(']')

16
data.go
View File

@ -1,6 +1,7 @@
package rui package rui
import ( import (
"slices"
"strings" "strings"
"unicode" "unicode"
) )
@ -321,8 +322,8 @@ func (node *dataNode) ArrayAsParams() []Params {
func ParseDataText(text string) DataObject { func ParseDataText(text string) DataObject {
if strings.ContainsAny(text, "\r") { if strings.ContainsAny(text, "\r") {
text = strings.Replace(text, "\r\n", "\n", -1) text = strings.ReplaceAll(text, "\r\n", "\n")
text = strings.Replace(text, "\r", "\n", -1) text = strings.ReplaceAll(text, "\r", "\n")
} }
data := append([]rune(text), rune(0)) data := append([]rune(text), rune(0))
pos := 0 pos := 0
@ -518,15 +519,8 @@ func ParseDataText(text string) DataObject {
} }
stopSymbol := func(symbol rune) bool { stopSymbol := func(symbol rune) bool {
if unicode.IsSpace(symbol) { return unicode.IsSpace(symbol) ||
return true slices.Contains([]rune{'=', '{', '}', '[', ']', ',', ' ', '\t', '\n', '\'', '"', '`', '/'}, symbol)
}
for _, sym := range []rune{'=', '{', '}', '[', ']', ',', ' ', '\t', '\n', '\'', '"', '`', '/'} {
if sym == symbol {
return true
}
}
return false
} }
for pos < size && !stopSymbol(data[pos]) { for pos < size && !stopSymbol(data[pos]) {

View File

@ -462,13 +462,13 @@ func dragAndDropHtml(view View, buffer *strings.Builder) {
if f := GetDragImageXOffset(view); f != 0 { if f := GetDragImageXOffset(view); f != 0 {
buffer.WriteString(` data-drag-image-x="`) buffer.WriteString(` data-drag-image-x="`)
buffer.WriteString(fmt.Sprintf("%g", f)) fmt.Fprintf(buffer, "%g", f)
buffer.WriteString(`" `) buffer.WriteString(`" `)
} }
if f := GetDragImageYOffset(view); f != 0 { if f := GetDragImageYOffset(view); f != 0 {
buffer.WriteString(` data-drag-image-y="`) buffer.WriteString(` data-drag-image-y="`)
buffer.WriteString(fmt.Sprintf("%g", f)) fmt.Fprintf(buffer, "%g", f)
buffer.WriteString(`" `) buffer.WriteString(`" `)
} }

View File

@ -205,7 +205,7 @@ func imageViewSrcSet(view View, path string) string {
buffer.WriteString(", ") buffer.WriteString(", ")
} }
buffer.WriteString(src.path) buffer.WriteString(src.path)
buffer.WriteString(fmt.Sprintf(" %gx", src.scale)) fmt.Fprintf(buffer, " %gx", src.scale)
} }
return buffer.String() return buffer.String()
} }

View File

@ -244,27 +244,27 @@ func (picker *numberPickerData) htmlProperties(self View, buffer *strings.Builde
min, max := GetNumberPickerMinMax(picker) min, max := GetNumberPickerMinMax(picker)
if min != math.Inf(-1) { if min != math.Inf(-1) {
buffer.WriteString(` min="`) buffer.WriteString(` min="`)
buffer.WriteString(fmt.Sprintf(format, min)) fmt.Fprintf(buffer, format, min)
buffer.WriteByte('"') buffer.WriteByte('"')
} }
if max != math.Inf(1) { if max != math.Inf(1) {
buffer.WriteString(` max="`) buffer.WriteString(` max="`)
buffer.WriteString(fmt.Sprintf(format, max)) fmt.Fprintf(buffer, format, max)
buffer.WriteByte('"') buffer.WriteByte('"')
} }
step := GetNumberPickerStep(picker) step := GetNumberPickerStep(picker)
if step != 0 { if step != 0 {
buffer.WriteString(` step="`) buffer.WriteString(` step="`)
buffer.WriteString(fmt.Sprintf(format, step)) fmt.Fprintf(buffer, format, step)
buffer.WriteByte('"') buffer.WriteByte('"')
} else { } else {
buffer.WriteString(` step="any"`) buffer.WriteString(` step="any"`)
} }
buffer.WriteString(` value="`) buffer.WriteString(` value="`)
buffer.WriteString(fmt.Sprintf(format, GetNumberPickerValue(picker))) fmt.Fprintf(buffer, format, GetNumberPickerValue(picker))
buffer.WriteByte('"') buffer.WriteByte('"')
buffer.WriteString(` oninput="editViewInputEvent(this)"`) buffer.WriteString(` oninput="editViewInputEvent(this)"`)

View File

@ -354,7 +354,7 @@ func (resizable *resizableData) htmlSubviews(self View, buffer *strings.Builder)
} }
writePos := func(x1, x2, y1, y2 int) { writePos := func(x1, x2, y1, y2 int) {
buffer.WriteString(fmt.Sprintf(` grid-column-start: %d; grid-column-end: %d; grid-row-start: %d; grid-row-end: %d;"></div>`, x1, x2, y1, y2)) fmt.Fprintf(buffer, ` grid-column-start: %d; grid-column-end: %d; grid-row-start: %d; grid-row-end: %d;"></div>`, x1, x2, y1, y2)
} }
//htmlID := resizable.htmlID() //htmlID := resizable.htmlID()

View File

@ -208,7 +208,7 @@ func (data *sizeFuncData) writeString(topFunc string, buffer *strings.Builder) {
buffer.WriteString(arg.String()) buffer.WriteString(arg.String())
case float64: case float64:
buffer.WriteString(fmt.Sprintf("%g", arg)) fmt.Fprintf(buffer, "%g", arg)
} }
} }
@ -302,7 +302,7 @@ func (data *sizeFuncData) writeCSS(topFunc string, buffer *strings.Builder, sess
buffer.WriteString(arg.String()) buffer.WriteString(arg.String())
case float64: case float64:
buffer.WriteString(fmt.Sprintf("%g", arg)) fmt.Fprintf(buffer, "%g", arg)
} }
} }

View File

@ -1270,10 +1270,10 @@ func (table *tableViewData) htmlSubviews(self View, buffer *strings.Builder) {
buffer.WriteString(string(value)) buffer.WriteString(string(value))
case float32: case float32:
buffer.WriteString(fmt.Sprintf("%g", float64(value))) fmt.Fprintf(buffer, "%g", float64(value))
case float64: case float64:
buffer.WriteString(fmt.Sprintf("%g", value)) fmt.Fprintf(buffer, "%g", value)
case bool: case bool:
if value { if value {
@ -1284,7 +1284,7 @@ func (table *tableViewData) htmlSubviews(self View, buffer *strings.Builder) {
default: default:
if n, ok := isInt(value); ok { if n, ok := isInt(value); ok {
buffer.WriteString(fmt.Sprintf("%d", n)) fmt.Fprintf(buffer, "%d", n)
} else { } else {
buffer.WriteString("<Unsupported value>") buffer.WriteString("<Unsupported value>")
} }
@ -1537,10 +1537,10 @@ func (table *tableViewData) writeCellHtml(adapter TableAdapter, row, column int,
buffer.WriteString(string(value)) buffer.WriteString(string(value))
case float32: case float32:
buffer.WriteString(fmt.Sprintf("%g", float64(value))) fmt.Fprintf(buffer, "%g", float64(value))
case float64: case float64:
buffer.WriteString(fmt.Sprintf("%g", value)) fmt.Fprintf(buffer, "%g", value)
case bool: case bool:
accentColor := Color(0) accentColor := Color(0)
@ -1555,7 +1555,7 @@ func (table *tableViewData) writeCellHtml(adapter TableAdapter, row, column int,
default: default:
if n, ok := isInt(value); ok { if n, ok := isInt(value); ok {
buffer.WriteString(fmt.Sprintf("%d", n)) fmt.Fprintf(buffer, "%d", n)
} else { } else {
buffer.WriteString("<Unsupported value>") buffer.WriteString("<Unsupported value>")
} }

View File

@ -975,10 +975,10 @@ func (theme *theme) String() string {
buffer.WriteString(":landscape") buffer.WriteString(":landscape")
} }
if maxWidth > 0 { if maxWidth > 0 {
buffer.WriteString(fmt.Sprintf(":width%d", maxWidth)) fmt.Fprintf(buffer, ":width%d", maxWidth)
} }
if maxHeight > 0 { if maxHeight > 0 {
buffer.WriteString(fmt.Sprintf(":height%d", maxHeight)) fmt.Fprintf(buffer, ":height%d", maxHeight)
} }
buffer.WriteString(" = [\n") buffer.WriteString(" = [\n")
@ -1014,21 +1014,21 @@ func (theme *theme) String() string {
} }
if media.MinWidth > 0 { if media.MinWidth > 0 {
buffer.WriteString(fmt.Sprintf(":width%d-", media.MinWidth)) fmt.Fprintf(buffer, ":width%d-", media.MinWidth)
if media.MaxWidth > 0 { if media.MaxWidth > 0 {
buffer.WriteString(strconv.Itoa(media.MaxWidth)) buffer.WriteString(strconv.Itoa(media.MaxWidth))
} }
} else if media.MaxWidth > 0 { } else if media.MaxWidth > 0 {
buffer.WriteString(fmt.Sprintf(":width%d", media.MaxWidth)) fmt.Fprintf(buffer, ":width%d", media.MaxWidth)
} }
if media.MinHeight > 0 { if media.MinHeight > 0 {
buffer.WriteString(fmt.Sprintf(":height%d-", media.MinHeight)) fmt.Fprintf(buffer, ":height%d-", media.MinHeight)
if media.MaxHeight > 0 { if media.MaxHeight > 0 {
buffer.WriteString(strconv.Itoa(media.MaxHeight)) buffer.WriteString(strconv.Itoa(media.MaxHeight))
} }
} else if media.MaxHeight > 0 { } else if media.MaxHeight > 0 {
buffer.WriteString(fmt.Sprintf(":height%d", media.MaxHeight)) fmt.Fprintf(buffer, ":height%d", media.MaxHeight)
} }
buffer.WriteString(" = [\n") buffer.WriteString(" = [\n")

View File

@ -1066,8 +1066,8 @@ func (view *viewData) htmlProperties(self View, buffer *strings.Builder) {
} }
if view.frame.Left != 0 || view.frame.Top != 0 || view.frame.Width != 0 || view.frame.Height != 0 { if view.frame.Left != 0 || view.frame.Top != 0 || view.frame.Width != 0 || view.frame.Height != 0 {
buffer.WriteString(fmt.Sprintf(` data-left="%g" data-top="%g" data-width="%g" data-height="%g"`, fmt.Fprintf(buffer, ` data-left="%g" data-top="%g" data-width="%g" data-height="%g"`,
view.frame.Left, view.frame.Top, view.frame.Width, view.frame.Height)) view.frame.Left, view.frame.Top, view.frame.Width, view.frame.Height)
} }
} }

View File

@ -190,7 +190,7 @@ func (bridge *webBridge) argToString(arg any) (string, bool) {
for _, val := range arg { for _, val := range arg {
buffer.WriteRune(lead) buffer.WriteRune(lead)
lead = ',' lead = ','
buffer.WriteString(fmt.Sprintf("%g", val)) fmt.Fprintf(buffer, "%g", val)
} }
buffer.WriteRune(']') buffer.WriteRune(']')
return buffer.String(), true return buffer.String(), true