rui_orig/textView.go

118 lines
2.8 KiB
Go
Raw Permalink Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"fmt"
"strings"
)
// TextView represents a TextView view
2021-09-07 17:36:50 +03:00
type TextView interface {
View
}
type textViewData struct {
viewData
}
// NewTextView create new TextView object and return it
func NewTextView(session Session, params Params) TextView {
view := new(textViewData)
view.init(session)
2021-09-07 17:36:50 +03:00
setInitParams(view, params)
return view
}
func newTextView(session Session) View {
2024-11-13 12:56:39 +03:00
return new(textViewData)
2021-09-07 17:36:50 +03:00
}
// Init initialize fields of TextView by default values
func (textView *textViewData) init(session Session) {
textView.viewData.init(session)
2021-09-07 17:36:50 +03:00
textView.tag = "TextView"
textView.set = textView.setFunc
textView.changed = textView.propertyChanged
2021-09-07 17:36:50 +03:00
}
func (textView *textViewData) propertyChanged(tag PropertyName) {
2024-11-13 12:56:39 +03:00
switch tag {
case Text:
updateInnerHTML(textView.htmlID(), textView.Session())
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
case TextOverflow:
session := textView.Session()
if n, ok := enumProperty(textView, TextOverflow, session, 0); ok {
2024-11-13 12:56:39 +03:00
values := enumProperties[TextOverflow].cssValues
if n >= 0 && n < len(values) {
session.updateCSSProperty(textView.htmlID(), string(TextOverflow), values[n])
2024-11-13 12:56:39 +03:00
return
}
}
session.updateCSSProperty(textView.htmlID(), string(TextOverflow), "")
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
case NotTranslate:
updateInnerHTML(textView.htmlID(), textView.Session())
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
default:
textView.viewData.propertyChanged(tag)
2021-09-07 17:36:50 +03:00
}
}
func (textView *textViewData) setFunc(tag PropertyName, value any) []PropertyName {
2021-09-07 17:36:50 +03:00
switch tag {
case Text:
switch value := value.(type) {
case string:
textView.setRaw(Text, value)
2021-09-07 17:36:50 +03:00
case fmt.Stringer:
textView.setRaw(Text, value.String())
2021-09-07 17:36:50 +03:00
case float32:
textView.setRaw(Text, fmt.Sprintf("%g", float64(value)))
2021-09-07 17:36:50 +03:00
case float64:
textView.setRaw(Text, fmt.Sprintf("%g", value))
2021-09-07 17:36:50 +03:00
case []rune:
textView.setRaw(Text, string(value))
2021-09-07 17:36:50 +03:00
case bool:
if value {
textView.setRaw(Text, "true")
2021-09-07 17:36:50 +03:00
} else {
textView.setRaw(Text, "false")
2021-09-07 17:36:50 +03:00
}
default:
if n, ok := isInt(value); ok {
textView.setRaw(Text, fmt.Sprintf("%d", n))
2021-09-07 17:36:50 +03:00
} else {
notCompatibleType(tag, value)
2024-11-13 12:56:39 +03:00
return nil
2021-09-07 17:36:50 +03:00
}
}
2024-11-13 12:56:39 +03:00
return []PropertyName{Text}
2021-09-07 17:36:50 +03:00
}
return textView.viewData.setFunc(tag, value)
2021-09-07 17:36:50 +03:00
}
func (textView *textViewData) htmlSubviews(self View, buffer *strings.Builder) {
2022-05-17 10:46:00 +03:00
if value := textView.getRaw(Text); value != nil {
if text, ok := value.(string); ok {
if !GetNotTranslate(textView) {
2022-05-22 12:54:02 +03:00
text, _ = textView.session.GetString(text)
}
2022-10-29 21:08:51 +03:00
buffer.WriteString(text)
2021-09-07 17:36:50 +03:00
}
}
}
// GetTextOverflow returns a value of the "text-overflow" property:
// TextOverflowClip (0) or TextOverflowEllipsis (1).
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetTextOverflow(view View, subviewID ...string) int {
2022-07-28 12:41:50 +03:00
return enumStyledProperty(view, subviewID, TextOverflow, SingleLineText, false)
2021-09-07 17:36:50 +03:00
}