Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312package ui
import ( "image" "math" "slices" "testing" "time"
"gioui.org/f32" "gioui.org/io/pointer"
"100gex/internal/rex" "100gex/internal/rex/rextest" "100gex/internal/rexsession")
// TestPaneDrag drags panes by their headers: to a pane's edge to split// beside it, to its middle to swap with it, and to another window's tab to// move there. A press on a header that doesn't move is still a click.func TestPaneDrag(t *testing.T) { srv := rextest.Start(t) other := srv.Dial(t, "other-device") sid, b1 := rextest.NewSession(t, other, rextest.RawCat...) var split struct { BlockIDs []string `json:"block_ids"` } rextest.Call(t, other, "session.new_split", map[string]any{ "session_id": sid, "anchor_id": b1, "direction": "horizontal", "side": "after", "layout": rextest.Block(rextest.RawCat...), "focus": false, }, &split) b2 := split.BlockIDs[0] var logs struct { WindowID string `json:"window_id"` } rextest.Call(t, other, "session.new_window", map[string]any{ "session_id": sid, "window_label": "logs", "layout": rextest.Block(rextest.RawCat...), "focus": false, }, &logs)
c := srv.Dial(t, "100gex-test") h := startUI(t, srv, c, sid) f := h.until("two panes", func(f rexsession.Frame) bool { return len(f.Visible) == 2 && f.Ready() }) main := f.View.ActiveWindowID
// layout is the active window's tiling: its split's direction, and its // blocks in order. layout := func(f rexsession.Frame) (string, []string) { w := f.View.Window(main) if w == nil { return "", nil } for _, l := range w.Layers { if l.Kind != "tiled" || l.Layout == nil { continue } var ids []string for _, n := range leaves(l.Layout, nil) { ids = append(ids, n.BlockID) } dir := "" if l.Layout.Split != nil { dir = l.Layout.Split.Direction } return dir, ids } return "", nil } send := func(kind pointer.Kind, p image.Point) { h.router.Queue(pointer.Event{Kind: kind, Source: pointer.Mouse, Position: f32.Pt(float32(p.X), float32(p.Y)), Buttons: pointer.ButtonPrimary}) h.frame() } slot := func(block string) image.Rectangle { for _, s := range h.u.paneSlots { if s.block == block { return s.rect } } t.Fatalf("no pane drawn for %s", block) return image.Rectangle{} } header := func(block string) image.Point { r := slot(block) return image.Pt(r.Min.X+r.Dx()/2, r.Min.Y+12) } // drag presses at from, moves to to in steps, a frame each, and lets // go; along the way it checks what the chip says. drag := func(from, to image.Point, wantLabel string) { t.Helper() send(pointer.Press, from) const steps = 8 for i := 1; i <= steps; i++ { send(pointer.Move, from.Add(to.Sub(from).Mul(i).Div(steps))) } if !h.u.paneDrag.active { t.Fatal("the drag didn't start") } if got := h.u.paneDropAt(h.frame(), to).label(); got != wantLabel { t.Errorf("dropping at %v would %q, want %q", to, got, wantLabel) } send(pointer.Release, to) if h.u.paneDrag.active { t.Error("the drag is still going after the release") } } waitLayout := func(what, dir string, ids ...string) { t.Helper() h.until(what, func(f rexsession.Frame) bool { d, got := layout(f) return d == dir && slices.Equal(got, ids) && f.Ready() }) }
// The left pane, dropped on the bottom of the right one, goes below it. r := slot(b2) drag(header(b1), image.Pt(r.Min.X+r.Dx()/2, r.Max.Y-10), "Split below") waitLayout("b1 below b2", "vertical", b2, b1)
// Dropped on the top pane's middle, it swaps with it. r = slot(b2) drag(header(b1), r.Min.Add(r.Size().Div(2)), "Swap") waitLayout("b1 swapped above b2", "vertical", b1, b2)
// Dropped on the left edge of the other, it goes to its left again. r = slot(b2) drag(header(b1), image.Pt(r.Min.X+10, r.Min.Y+r.Dy()/2), "Split left") waitLayout("b1 left of b2", "horizontal", b1, b2)
// Dropped on its own pane, nothing happens. r = slot(b1) drag(header(b1), r.Min.Add(r.Size().Div(2)), "")
// Dropped on the logs tab, it moves to that window, which becomes // active, for b1 was focused by the press on its header. tab := h.u.tabRects[logs.WindowID] drag(header(b1), tab.Min.Add(tab.Size().Div(2)), "Move to tab") h.until("b1 in the logs window", func(f rexsession.Frame) bool { w := f.View.Window(logs.WindowID) if w == nil { return false } var in []string for _, l := range w.Layers { for _, b := range l.Blocks { in = append(in, b.BlockID) } } _, left := layout(f) return slices.Contains(in, b1) && len(in) == 2 && slices.Equal(left, []string{b2}) })
// The server agrees. var v rex.View rextest.Call(t, other, "session.view", map[string]any{"session_id": sid}, &v) if w := v.Window(main); w == nil || w.Block(b1) != nil { t.Error("the server still has b1 in its first window") }
// A press on a header that doesn't move focuses its pane, and moves // nothing. h.u.session.FocusWindow(main) h.until("the first window", func(f rexsession.Frame) bool { return f.View.ActiveWindowID == main && f.Ready() }) p := header(b2) send(pointer.Press, p) send(pointer.Release, p) if h.u.paneDrag.active || h.u.paneDrag.block != "" { t.Error("a click on a header left a drag behind") }}
// TestFloatingDrag drags floating panes by their headers, which moves their// layers, no further than the window's edges. A press on a layer behind// another brings it to the front.func TestFloatingDrag(t *testing.T) { srv := rextest.Start(t) other := srv.Dial(t, "other-device") sid, _ := rextest.NewSession(t, other, rextest.RawCat...) var back, front struct { LayerID string `json:"layer_id"` BlockIDs []string `json:"block_ids"` } for _, l := range []*struct { LayerID string `json:"layer_id"` BlockIDs []string `json:"block_ids"` }{&back, &front} { rextest.Call(t, other, "session.new_layer", map[string]any{ "session_id": sid, "bounds": []float64{0.1, 0.1, 0.4, 0.4}, "layout": rextest.Block(rextest.RawCat...), "focus": false, }, l) }
c := srv.Dial(t, "100gex-test") h := startUI(t, srv, c, sid) h.until("three panes", func(f rexsession.Frame) bool { return len(f.Visible) == 3 && f.Ready() })
layer := func(f rexsession.Frame, id string) (rex.Layer, int) { for i, l := range f.View.ActiveWindow().Layers { if l.LayerID == id { return l, i } } t.Fatalf("no layer %s", id) return rex.Layer{}, 0 } slot := func(block string) image.Rectangle { var r image.Rectangle for _, s := range h.u.paneSlots { if s.block == block { r = s.rect // the last drawn is on top } } if r.Empty() { t.Fatalf("no pane drawn for %s", block) } return r } send := func(kind pointer.Kind, p image.Point) { h.router.Queue(pointer.Event{Kind: kind, Source: pointer.Mouse, Position: f32.Pt(float32(p.X), float32(p.Y)), Buttons: pointer.ButtonPrimary}) h.frame() } // drag moves a pane by its header's left end, clear of its buttons. drag := func(block string, by image.Point) { t.Helper() from := slot(block).Min.Add(image.Pt(30, 12)) send(pointer.Press, from) for i := 1; i <= 8; i++ { send(pointer.Move, from.Add(by.Mul(i).Div(8))) } if !h.u.paneDrag.active { t.Fatal("the drag didn't start") } send(pointer.Release, from.Add(by)) } area := h.u.panesArea near := func(a, b float64) bool { return math.Abs(a-b) < 0.002 } // at checks that block is drawn within a pixel of want. at := func(what, block string, want image.Point) { t.Helper() if d := slot(block).Min.Sub(want); d.X < -1 || d.X > 1 || d.Y < -1 || d.Y > 1 { t.Errorf("%s: pane drawn at %v, want %v", what, slot(block).Min, want) } }
// The front layer follows the pointer, and stays where it's dropped, // before and after the server has it there. fb := front.BlockIDs[0] start := slot(fb) drag(fb, image.Pt(120, 60)) at("just dropped", fb, start.Min.Add(image.Pt(120, 60))) wantX, wantY := 0.1+120/float64(area.Dx()), 0.1+60/float64(area.Dy()) h.until("the front layer moved", func(f rexsession.Frame) bool { l, _ := layer(f, front.LayerID) return near(l.Bounds[0], wantX) && near(l.Bounds[1], wantY) }) at("moved", fb, start.Min.Add(image.Pt(120, 60)))
// Dragged far past the bottom right, it stops at the edges. drag(fb, image.Pt(5000, 5000)) h.until("the front layer in the corner", func(f rexsession.Frame) bool { l, _ := layer(f, front.LayerID) return near(l.Bounds[0], 0.6) && near(l.Bounds[1], 0.6) })
// The back layer, pressed where nothing covers it now, comes to the // front. bb := back.BlockIDs[0] p := slot(bb).Min.Add(image.Pt(30, 12)) send(pointer.Press, p) send(pointer.Release, p) h.until("the back layer raised", func(f rexsession.Frame) bool { _, b := layer(f, back.LayerID) _, fr := layer(f, front.LayerID) return b > fr && f.View.ActiveWindow().FocusedBlockID == bb })}
// TestLayerBounds checks where a floating layer is drawn: where it was// dropped until the server has it there, or until that's taken too long,// and dragged no further than the window's edges.func TestLayerBounds(t *testing.T) { u := &UI{layerMoves: make(map[string]layerMove), panesArea: image.Rect(0, 0, 1000, 500)} server := rex.Layer{LayerID: "l", Bounds: [4]float64{0.1, 0.1, 0.4, 0.4}} dropped := [4]float64{0.3, 0.2, 0.4, 0.4}
u.layerMoves["l"] = layerMove{bounds: dropped, since: time.Now()} if got := u.layerBounds(server); got != dropped { t.Errorf("before the server moves it, drawn at %v, want %v", got, dropped) } moved := server moved.Bounds = dropped if got := u.layerBounds(moved); got != dropped || len(u.layerMoves) != 0 { t.Errorf("once the server moves it, drawn at %v with %d moves left", got, len(u.layerMoves)) }
u.layerMoves["l"] = layerMove{bounds: dropped, since: time.Now().Add(-layerMoveWait - time.Second)} if got := u.layerBounds(server); got != server.Bounds { t.Errorf("long after a move the server didn't make, drawn at %v, want %v", got, server.Bounds) }
for _, c := range []struct { delta f32.Point want [4]float64 }{ {f32.Pt(100, 50), [4]float64{0.2, 0.2, 0.4, 0.4}}, {f32.Pt(5000, 5000), [4]float64{0.6, 0.6, 0.4, 0.4}}, {f32.Pt(-5000, -5000), [4]float64{0, 0, 0.4, 0.4}}, } { if got := movedBounds(server.Bounds, c.delta, u.panesArea); !sameBounds(got, c.want) { t.Errorf("moved by %v: %v, want %v", c.delta, got, c.want) } }}