mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 01:45:36 +00:00
ui/text: add submit events to Editor
Add Editor.Text() and fix a reset while here. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+29
-4
@@ -23,6 +23,7 @@ type Editor struct {
|
|||||||
Face Face
|
Face Face
|
||||||
Alignment Alignment
|
Alignment Alignment
|
||||||
SingleLine bool
|
SingleLine bool
|
||||||
|
Submit bool
|
||||||
|
|
||||||
cfg *ui.Config
|
cfg *ui.Config
|
||||||
blinkStart time.Time
|
blinkStart time.Time
|
||||||
@@ -49,12 +50,22 @@ type Editor struct {
|
|||||||
clicker gesture.Click
|
clicker gesture.Click
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type EditorEvent interface {
|
||||||
|
isEditorEvent()
|
||||||
|
}
|
||||||
|
|
||||||
|
type Submission struct {
|
||||||
|
Text string
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
blinksPerSecond = 1
|
blinksPerSecond = 1
|
||||||
maxBlinkDuration = 10 * time.Second
|
maxBlinkDuration = 10 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
func (e *Editor) Update(c *ui.Config, q input.Events) {
|
func (s Submission) isEditorEvent() {}
|
||||||
|
|
||||||
|
func (e *Editor) Update(c *ui.Config, q input.Events) []EditorEvent {
|
||||||
if e.cfg == nil || c.PxPerDp != e.cfg.PxPerDp || c.PxPerSp != e.cfg.PxPerSp {
|
if e.cfg == nil || c.PxPerDp != e.cfg.PxPerDp || c.PxPerSp != e.cfg.PxPerSp {
|
||||||
e.invalidate()
|
e.invalidate()
|
||||||
}
|
}
|
||||||
@@ -93,12 +104,23 @@ func (e *Editor) Update(c *ui.Config, q input.Events) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
stop := (sdist > 0 && soff >= smax) || (sdist < 0 && soff <= smin)
|
stop := (sdist > 0 && soff >= smax) || (sdist < 0 && soff <= smin)
|
||||||
|
var events []EditorEvent
|
||||||
for _, ke := range q.For(e) {
|
for _, ke := range q.For(e) {
|
||||||
e.blinkStart = c.Now
|
e.blinkStart = c.Now
|
||||||
switch ke := ke.(type) {
|
switch ke := ke.(type) {
|
||||||
case key.Focus:
|
case key.Focus:
|
||||||
e.focused = ke.Focus
|
e.focused = ke.Focus
|
||||||
case key.Chord:
|
case key.Chord:
|
||||||
|
if !e.focused {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if e.Submit && (ke.Name == key.NameReturn || ke.Name == key.NameEnter) {
|
||||||
|
if !ke.Modifiers.Contain(key.ModShift) {
|
||||||
|
events = append(events, Submission{e.Text()})
|
||||||
|
e.SetText("")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
if e.command(ke) {
|
if e.command(ke) {
|
||||||
stop = true
|
stop = true
|
||||||
scrollTo = true
|
scrollTo = true
|
||||||
@@ -115,6 +137,7 @@ func (e *Editor) Update(c *ui.Config, q input.Events) {
|
|||||||
if stop {
|
if stop {
|
||||||
e.scroller.Stop()
|
e.scroller.Stop()
|
||||||
}
|
}
|
||||||
|
return events
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) caretWidth() fixed.Int26_6 {
|
func (e *Editor) caretWidth() fixed.Int26_6 {
|
||||||
@@ -341,8 +364,13 @@ func (e *Editor) deleteRuneForward() {
|
|||||||
e.invalidate()
|
e.invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Editor) Text() string {
|
||||||
|
return e.rr.String()
|
||||||
|
}
|
||||||
|
|
||||||
func (e *Editor) SetText(s string) {
|
func (e *Editor) SetText(s string) {
|
||||||
e.rr = editBuffer{}
|
e.rr = editBuffer{}
|
||||||
|
e.carXOff = 0
|
||||||
e.prepend(s)
|
e.prepend(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -496,9 +524,6 @@ func (e *Editor) scrollToCaret() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) command(k key.Chord) bool {
|
func (e *Editor) command(k key.Chord) bool {
|
||||||
if !e.focused {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
switch k.Name {
|
switch k.Name {
|
||||||
case key.NameReturn, key.NameEnter:
|
case key.NameReturn, key.NameEnter:
|
||||||
if !e.SingleLine {
|
if !e.SingleLine {
|
||||||
|
|||||||
Reference in New Issue
Block a user