objects/textview.go

88 lines
2.6 KiB
Go
Raw Normal View History

2023-09-15 14:11:59 -06:00
package objects
import "image"
import "git.tebibyte.media/tomo/tomo"
2024-08-24 23:40:40 -06:00
import "git.tebibyte.media/tomo/tomo/text"
import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.Object = new(TextView)
2023-09-15 14:11:59 -06:00
// TextView is an area for displaying a large amount of multi-line text.
type TextView struct {
2024-08-24 23:40:40 -06:00
box tomo.TextBox
2023-09-15 14:11:59 -06:00
}
// NewTextView creates a new text view.
func NewTextView (text string) *TextView {
2024-08-24 23:40:40 -06:00
textView := &TextView { box: tomo.NewTextBox() }
textView.box.SetRole(tomo.R("objects", "TextView"))
textView.box.SetFocusable(true)
textView.box.SetSelectable(true)
textView.SetText(text)
textView.box.SetAttr(tomo.AOverflow(false, true))
textView.box.SetAttr(tomo.AWrap(true))
textView.box.OnScroll(textView.handleScroll)
return textView
}
// GetBox returns the underlying box.
func (this *TextView) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this text view has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *TextView) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// Select sets the text cursor or selection.
func (this *TextView) Select (dot text.Dot) {
this.box.Select(dot)
}
// Dot returns the text cursor or selection.
func (this *TextView) Dot () text.Dot {
return this.box.Dot()
}
// OnDotChange specifies a function to be called when the text cursor or
// selection changes.
func (this *TextView) OnDotChange (callback func ()) event.Cookie {
return this.box.OnDotChange(callback)
}
2024-08-24 23:40:40 -06:00
// SetAlign sets the X and Y alignment of the text view.
func (this *TextView) SetAlign (x, y tomo.Align) {
this.box.SetAttr(tomo.AAlign(x, y))
}
// ContentBounds returns the bounds of the inner content of the text view
// relative to the text view's InnerBounds.
func (this *TextView) ContentBounds () image.Rectangle {
return this.box.ContentBounds()
}
// ScrollTo shifts the origin of the text view's content to the origin of the
// text view's InnerBounds, offset by the given point.
func (this *TextView) ScrollTo (position image.Point) {
this.box.ScrollTo(position)
}
// OnContentBoundsChange specifies a function to be called when the text view's
// ContentBounds or InnerBounds changes.
func (this *TextView) OnContentBoundsChange (callback func ()) event.Cookie {
return this.box.OnContentBoundsChange(callback)
}
// SetText sets the text content of the view.
func (this *TextView) SetText (text string) {
this.box.SetText(text)
2023-09-15 14:11:59 -06:00
}
2024-07-25 10:58:38 -06:00
func (this *TextView) handleScroll (x, y float64) bool {
2024-07-26 22:19:53 -06:00
this.ScrollTo(this.ContentBounds().Min.Sub(image.Pt(int(x), int(y))))
2024-07-25 10:58:38 -06:00
return true
2023-09-15 14:11:59 -06:00
}