rui_orig/textView.go

155 lines
3.5 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"fmt"
"strings"
)
// TextView - text View
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 {
return NewTextView(session, nil)
}
// 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"
}
2022-05-22 12:54:02 +03:00
func (textView *textViewData) String() string {
return getViewString(textView)
}
2022-07-26 18:36:00 +03:00
func (textView *textViewData) Get(tag string) any {
2021-09-07 17:36:50 +03:00
return textView.get(strings.ToLower(tag))
}
func (textView *textViewData) Remove(tag string) {
textView.remove(strings.ToLower(tag))
}
func (textView *textViewData) remove(tag string) {
textView.viewData.remove(tag)
if textView.created {
switch tag {
case Text:
updateInnerHTML(textView.htmlID(), textView.session)
2021-09-07 17:36:50 +03:00
case TextOverflow:
textView.textOverflowUpdated()
}
2021-09-07 17:36:50 +03:00
}
}
2022-07-26 18:36:00 +03:00
func (textView *textViewData) Set(tag string, value any) bool {
2021-09-07 17:36:50 +03:00
return textView.set(strings.ToLower(tag), value)
}
2022-07-26 18:36:00 +03:00
func (textView *textViewData) set(tag string, value any) bool {
2021-09-07 17:36:50 +03:00
switch tag {
case Text:
switch value := value.(type) {
case string:
textView.properties[Text] = value
case fmt.Stringer:
textView.properties[Text] = value.String()
case float32:
textView.properties[Text] = fmt.Sprintf("%g", float64(value))
case float64:
textView.properties[Text] = fmt.Sprintf("%g", value)
case []rune:
textView.properties[Text] = string(value)
case bool:
if value {
textView.properties[Text] = "true"
} else {
textView.properties[Text] = "false"
}
default:
if n, ok := isInt(value); ok {
textView.properties[Text] = fmt.Sprintf("%d", n)
} else {
notCompatibleType(tag, value)
return false
}
}
if textView.created {
updateInnerHTML(textView.htmlID(), textView.session)
}
2021-09-07 17:36:50 +03:00
case TextOverflow:
if !textView.viewData.set(tag, value) {
return false
}
if textView.created {
2021-09-07 17:36:50 +03:00
textView.textOverflowUpdated()
}
2022-05-22 12:54:02 +03:00
case NotTranslate:
if !textView.viewData.set(tag, value) {
return false
}
if textView.created {
updateInnerHTML(textView.htmlID(), textView.Session())
}
default:
return textView.viewData.set(tag, value)
2021-09-07 17:36:50 +03:00
}
textView.propertyChangedEvent(tag)
return true
2021-09-07 17:36:50 +03:00
}
func (textView *textViewData) textOverflowUpdated() {
session := textView.Session()
2022-10-30 17:22:33 +03:00
if n, ok := enumProperty(textView, TextOverflow, session, 0); ok {
2021-09-07 17:36:50 +03:00
values := enumProperties[TextOverflow].cssValues
if n >= 0 && n < len(values) {
2022-10-30 17:22:33 +03:00
session.updateCSSProperty(textView.htmlID(), TextOverflow, values[n])
2021-09-07 17:36:50 +03:00
return
}
}
2022-10-30 17:22:33 +03:00
session.updateCSSProperty(textView.htmlID(), TextOverflow, "")
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
}