2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// 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)
|
2022-09-01 11:04:50 +03:00
|
|
|
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
|
2022-09-01 11:04:50 +03:00
|
|
|
func (textView *textViewData) init(session Session) {
|
|
|
|
textView.viewData.init(session)
|
2021-09-07 17:36:50 +03:00
|
|
|
textView.tag = "TextView"
|
2024-11-18 17:20:25 +03:00
|
|
|
textView.set = textView.setFunc
|
|
|
|
textView.changed = textView.propertyChanged
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
2024-11-18 17:20:25 +03:00
|
|
|
func (textView *textViewData) propertyChanged(tag PropertyName) {
|
2024-11-13 12:56:39 +03:00
|
|
|
switch tag {
|
|
|
|
case Text:
|
2024-11-18 17:20:25 +03:00
|
|
|
updateInnerHTML(textView.htmlID(), textView.Session())
|
2021-09-07 17:36:50 +03:00
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
case TextOverflow:
|
2024-11-18 17:20:25 +03:00
|
|
|
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) {
|
2024-11-18 17:20:25 +03:00
|
|
|
session.updateCSSProperty(textView.htmlID(), string(TextOverflow), values[n])
|
2024-11-13 12:56:39 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-11-18 17:20:25 +03:00
|
|
|
session.updateCSSProperty(textView.htmlID(), string(TextOverflow), "")
|
2021-09-07 17:36:50 +03:00
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
case NotTranslate:
|
2024-11-18 17:20:25 +03:00
|
|
|
updateInnerHTML(textView.htmlID(), textView.Session())
|
2021-09-07 17:36:50 +03:00
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
default:
|
2024-11-18 17:20:25 +03:00
|
|
|
textView.viewData.propertyChanged(tag)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-18 17:20:25 +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:
|
2024-11-18 17:20:25 +03:00
|
|
|
textView.setRaw(Text, value)
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case fmt.Stringer:
|
2024-11-18 17:20:25 +03:00
|
|
|
textView.setRaw(Text, value.String())
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case float32:
|
2024-11-18 17:20:25 +03:00
|
|
|
textView.setRaw(Text, fmt.Sprintf("%g", float64(value)))
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case float64:
|
2024-11-18 17:20:25 +03:00
|
|
|
textView.setRaw(Text, fmt.Sprintf("%g", value))
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case []rune:
|
2024-11-18 17:20:25 +03:00
|
|
|
textView.setRaw(Text, string(value))
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case bool:
|
|
|
|
if value {
|
2024-11-18 17:20:25 +03:00
|
|
|
textView.setRaw(Text, "true")
|
2021-09-07 17:36:50 +03:00
|
|
|
} else {
|
2024-11-18 17:20:25 +03:00
|
|
|
textView.setRaw(Text, "false")
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
if n, ok := isInt(value); ok {
|
2024-11-18 17:20:25 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-11-18 17:20:25 +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 {
|
2022-08-31 22:17:46 +03:00
|
|
|
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).
|
2022-08-31 22:17:46 +03:00
|
|
|
// 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
|
|
|
}
|