Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/accessibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (

// SetAccessibilityFocus moves the screen reader's focus to the element, as after a change the user should hear about first. The web and desktops move the keyboard focus with it, to a field at least, so under a confined owner it does only as Scope.Confine allows.
func (r *Ref) SetAccessibilityFocus() {
if r.n != nil && !r.n.deleted && r.t.mayMoveFocus(r.n.owner) {
if r.placed() && r.t.mayMoveFocus(r.n.owner) {
r.t.command(driver.ItemAccessibilityFocus, r.n)
}
}
Expand Down
22 changes: 14 additions & 8 deletions core/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,12 +570,18 @@ func (s Scroll) mountIn(t *Tree, cx *Scope, parent *node) { t.mountScroll(cx, pa

// Ref refers to a mounted element: an overlay's anchor, focus, its
// bounds. Its zero value is ready to use; pass its address in an
// element's Ref field.
// element's Ref field. Focus, blur and scroll requests are dropped in
// unplaced Holder content; placing it again does not replay them.
type Ref struct {
t *Tree
n *node
}

// placed reports whether a live element carries the ref in the tree.
func (r *Ref) placed() bool {
return r != nil && r.n != nil && !r.n.deleted && !r.t.detached(r.n)
}

// Bounds returns the element's frame in surface points after the last
// commit, and false while it is not mounted.
func (r *Ref) Bounds() (Bounds, bool) {
Expand Down Expand Up @@ -619,9 +625,9 @@ func (r *Ref) Frame() (Bounds, bool) {
return r.n.frame, true
}

// Focus moves keyboard focus to the element, a text field. Under a confined owner it does only as Scope.Confine allows.
// Focus moves keyboard focus to the element, a text field. Under a confined owner it does only as Scope.Confine allows. It does nothing in unplaced Holder content, with no focus deferred until it is placed again.
func (r *Ref) Focus() {
if r.n != nil && !r.n.deleted && r.t.mayMoveFocus(r.n.owner) {
if r.placed() && r.t.mayMoveFocus(r.n.owner) {
r.t.command(driver.ItemFocus, r.n)
}
}
Expand Down Expand Up @@ -668,7 +674,7 @@ func (r *Ref) path() []int {
// ScrollTo scrolls the element, a Scroll or List, so its content's
// offset is at.
func (r *Ref) ScrollTo(at Point, animated bool) {
if r.n == nil || r.n.deleted {
if !r.placed() {
return
}
if ln := r.n.list; ln != nil {
Expand All @@ -684,7 +690,7 @@ func (r *Ref) ScrollTo(at Point, animated bool) {
// content. A list keeps following the end as the rows there are
// measured, until the user scrolls away.
func (r *Ref) ScrollToEnd(animated bool) {
if r.n == nil || r.n.deleted {
if !r.placed() {
return
}
if ln := r.n.list; ln != nil {
Expand All @@ -705,7 +711,7 @@ func (r *Ref) ScrollToEnd(animated bool) {
// its limits), with content point center (unzoomed) in the middle of the
// viewport, or keeping the middle when center is nil.
func (r *Ref) ZoomTo(scale float32, center *Point, animated bool) {
if r.n == nil || r.n.deleted || r.n.scroll == nil {
if !r.placed() || r.n.scroll == nil {
return
}
z := &driver.ZoomRequest{Scale: r.n.scroll.config.ClampZoom(zoomOr(scale, 1))}
Expand All @@ -729,7 +735,7 @@ func (r *Ref) ScrollToIndex(index int, animated bool) {
// list keeps following the row as the rows around it are measured, until
// the user scrolls away or the rows change.
func (r *Ref) ScrollToIndexAt(index int, viewPosition float32, viewOffset Points, animated bool) {
if r.n == nil || r.n.deleted || r.n.list == nil {
if !r.placed() || r.n.list == nil {
return
}
r.t.listCommand(r.n.list, listCommand{pin: &listPin{index: max(index, 0), viewPosition: viewPosition, viewOffset: viewOffset},
Expand All @@ -738,7 +744,7 @@ func (r *Ref) ScrollToIndexAt(index int, viewPosition float32, viewOffset Points

// Blur takes keyboard focus away from the element.
func (r *Ref) Blur() {
if r.n != nil && !r.n.deleted {
if r.placed() {
r.t.command(driver.ItemBlur, r.n)
}
}
Expand Down
15 changes: 12 additions & 3 deletions core/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,10 @@ type listCell struct {
// listCommand is a scroll a Ref asked a list for, resolved with the
// sizes known in the next update.
type listCommand struct {
pin *listPin // ScrollToIndex or ScrollToEnd; nil for to
to Point
animated bool
pin *listPin // ScrollToIndex or ScrollToEnd; nil for to
to Point
animated bool
transient bool // a Ref command, rather than the list's initial position
}

func (t *Tree) mountList(cx *Scope, parent *node, l List) {
Expand Down Expand Up @@ -1085,6 +1086,10 @@ func (t *Tree) updateLists() bool {
continue
}
live = append(live, l)
if t.detached(l) {
l.list.cmds = slices.DeleteFunc(l.list.cmds, func(c listCommand) bool { return c.transient })
continue
}
if l.list.st.parent {
_, l.list.depth = l.list.findOuter()
parents = append(parents, l)
Expand Down Expand Up @@ -1132,6 +1137,9 @@ func (t *Tree) sendSticky() {
}
t.stuckIn = t.stuckIn[:0]
for v := range views {
if t.detached(v) {
continue
}
sc := v.scroll
var cells []StickyCell
if v.list != nil {
Expand Down Expand Up @@ -1223,6 +1231,7 @@ func pinRange(n *node) (*node, StickyCell, bool) {

// listCommand queues a scroll for list ln's next update.
func (t *Tree) listCommand(ln *listNode, c listCommand) {
c.transient = true
ln.cmds = append(ln.cmds, c)
t.relayout(ln.view)
}
Expand Down
61 changes: 46 additions & 15 deletions core/pace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"math"
"slices"
"time"

"github.com/nsgo-dev/nsgo/internal/driver"
)

// Views made over frames, as NSH's (performance.md): a list row bound in
Expand Down Expand Up @@ -186,33 +188,62 @@ func (t *Tree) makeNow(n *node) {
}
}

// makeCommanded makes the views the commit's commands name, which a
// backend must have.
// makeCommanded drops commands whose targets left the tree, then makes
// the views the remaining commands name before a backend receives them.
func (t *Tree) makeCommanded() {
if len(t.waiting) == 0 {
return
}
at := func(id NodeID) {
if int(id) < len(t.nodes) {
if n := t.nodes[id]; n != nil && !n.created {
t.makeNow(n)
}
kept := t.commands[:0]
// Queued scrolls have already contributed to core's predicted offset.
// Cancel them in reverse order so no unsent offset or count survives.
for i := len(t.commands) - 1; i >= 0; i-- {
it := &t.commands[i]
if it.ID == 0 {
continue
}
n := t.nodes[it.ID]
if n != nil && (n.deleted || t.detached(n)) && n.scroll != nil &&
(it.Kind == driver.ItemScrollTo || it.Kind == driver.ItemScrollBy) {
sc := n.scroll
last := len(sc.pending) - 1
sc.offset = sc.pending[last].before
sc.pending = sc.pending[:last]
sc.issued--
}
}
for i := range t.commands {
it := &t.commands[i]
for _, it := range t.commands {
if it.ID != 0 {
at(it.ID)
n := t.nodes[it.ID]
if n == nil || n.deleted || t.detached(n) && (!n.created || commandNeedsPlacement(it.Kind)) {
continue
}
if !n.created {
t.makeNow(n)
}
}
for _, c := range it.Sticky {
at(c.Cell)
if n := t.nodes[c.Cell]; n != nil && !n.deleted && !t.detached(n) && !n.created {
t.makeNow(n)
}
}
kept = append(kept, it)
}
if t.focus != nil && !t.focus.created {
clear(t.commands[len(kept):])
t.commands = kept
if t.focus != nil && !t.focus.created && !t.detached(t.focus) {
t.makeNow(t.focus)
}
}

// Commands on retained web or native views may still run out of the tree;
// moving focus or scrolling instead requires a place showing the content.
func commandNeedsPlacement(kind driver.ItemKind) bool {
switch kind {
case driver.ItemFocus, driver.ItemBlur, driver.ItemAccessibilityFocus,
driver.ItemScrollTo, driver.ItemScrollBy, driver.ItemZoomTo:
return true
}
return false
}

// syncPaced makes the waiting views that show, down to their leaves, then
// as many of the others as the budget allows, nearest first.
func (t *Tree) syncPaced() {
Expand Down
Loading
Loading