objects/heading.go

74 lines
2.0 KiB
Go
Raw Normal View History

2023-08-10 15:52:01 -06:00
package objects
import "fmt"
import "git.tebibyte.media/tomo/tomo"
2024-08-24 17:42:25 -06:00
import "git.tebibyte.media/tomo/tomo/text"
import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.Object = new(Heading)
2023-08-10 15:52:01 -06:00
// Heading is a label that denotes the start of some section of content. It can
// have a level from 0 to 2, with 0 being the most prominent and 2 being the
// most subtle.
//
// Tags:
// - [0] The heading has a level of 0 (most prominent).
// - [1] The heading has a level of 1.
// - [2] The heading has a level of 2 (least prominent).
2023-08-10 15:52:01 -06:00
type Heading struct {
2024-08-24 17:42:25 -06:00
box tomo.TextBox
2023-08-10 15:52:01 -06:00
}
// NewHeading creates a new section heading. The level can be from 0 to 2.
func NewHeading (level int, text string) *Heading {
2023-08-10 15:52:01 -06:00
if level < 0 { level = 0 }
if level > 2 { level = 2 }
2024-08-24 17:42:25 -06:00
this := &Heading { box: tomo.NewTextBox() }
this.box.SetRole(tomo.R("objects", "Heading"))
this.box.SetTag(fmt.Sprint(level), true)
2023-08-10 15:52:01 -06:00
this.SetText(text)
2024-08-24 17:42:25 -06:00
this.box.SetSelectable(true)
this.box.SetFocusable(true)
2023-08-10 15:52:01 -06:00
return this
}
2024-08-24 17:42:25 -06:00
// GetBox returns the underlying box.
func (this *Heading) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this heading has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *Heading) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// SetText sets the text content of the heading.
func (this *Heading) SetText (text string) {
this.box.SetText(text)
}
// Select sets the text cursor or selection.
func (this *Heading) Select (dot text.Dot) {
this.box.Select(dot)
}
// Dot returns the text cursor or selection.
func (this *Heading) Dot () text.Dot {
return this.box.Dot()
}
// OnDotChange specifies a function to be called when the text cursor or
// selection changes.
func (this *Heading) OnDotChange (callback func ()) event.Cookie {
return this.box.OnDotChange(callback)
}
2024-08-14 09:45:10 -06:00
// NewMenuHeading creatss a new heading for use in menus.
func NewMenuHeading (text string) *Heading {
heading := NewHeading(0, text)
2024-08-24 17:42:25 -06:00
heading.box.SetTag("menu", true)
2024-08-14 09:45:10 -06:00
return heading
}