rui_orig/textView.go

118 lines
2.7 KiB
Go
Raw 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"
2024-11-13 12:56:39 +03:00
textView.set = textViewSet
textView.changed = textViewPropertyChanged
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
func textViewPropertyChanged(view View, tag PropertyName) {
switch tag {
case Text:
updateInnerHTML(view.htmlID(), view.Session())
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
case TextOverflow:
session := view.Session()
if n, ok := enumProperty(view, TextOverflow, session, 0); ok {
values := enumProperties[TextOverflow].cssValues
if n >= 0 && n < len(values) {
session.updateCSSProperty(view.htmlID(), string(TextOverflow), values[n])
return
}
}
session.updateCSSProperty(view.htmlID(), string(TextOverflow), "")
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
case NotTranslate:
updateInnerHTML(view.htmlID(), view.Session())
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
default:
viewPropertyChanged(view, tag)
2021-09-07 17:36:50 +03:00
}
}
2024-11-13 12:56:39 +03:00
func textViewSet(view View, tag PropertyName, value any) []PropertyName {
2021-09-07 17:36:50 +03:00
switch tag {
case Text:
switch value := value.(type) {
case string:
2024-11-13 12:56:39 +03:00
view.setRaw(Text, value)
2021-09-07 17:36:50 +03:00
case fmt.Stringer:
2024-11-13 12:56:39 +03:00
view.setRaw(Text, value.String())
2021-09-07 17:36:50 +03:00
case float32:
2024-11-13 12:56:39 +03:00
view.setRaw(Text, fmt.Sprintf("%g", float64(value)))
2021-09-07 17:36:50 +03:00
case float64:
2024-11-13 12:56:39 +03:00
view.setRaw(Text, fmt.Sprintf("%g", value))
2021-09-07 17:36:50 +03:00
case []rune:
2024-11-13 12:56:39 +03:00
view.setRaw(Text, string(value))
2021-09-07 17:36:50 +03:00
case bool:
if value {
2024-11-13 12:56:39 +03:00
view.setRaw(Text, "true")
2021-09-07 17:36:50 +03:00
} else {
2024-11-13 12:56:39 +03:00
view.setRaw(Text, "false")
2021-09-07 17:36:50 +03:00
}
default:
if n, ok := isInt(value); ok {
2024-11-13 12:56:39 +03:00
view.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
}
2024-11-13 12:56:39 +03:00
return viewSet(view, 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
}