Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402package ui
import ( "image" "image/color" "math" "time"
"gioui.org/f32" "gioui.org/font" "gioui.org/io/pointer" "gioui.org/layout" "gioui.org/op/clip" "gioui.org/unit"
"100gex/internal/rex" "100gex/internal/rexsession" "100gex/internal/scene" "100gex/internal/theme")
// paneDrag follows a press on a pane's header, and the drag it may become.// Positions are in the window's coordinates.type paneDrag struct { pid pointer.ID block string // the block whose header was pressed, or "" for none start f32.Point // where the press was at f32.Point // where the pointer is now active bool // past dragSlop: the pane is being moved // layer is the floating layer that the pane is in, which moves with // it, or "" for a pane in the layout. bounds is where the layer was // when pressed. layer string bounds [4]float64 // dropped is set by the release, for layoutPaneDrag to drop the pane // once every pane and tab has been drawn where it is this frame. dropped bool}
// paneSlot is where a pane was drawn, in the window's coordinates.type paneSlot struct { block string rect image.Rectangle}
// dropZone is the part of a pane a dragged one is over.type dropZone int
const ( zoneNone dropZone = iota zoneLeft zoneRight zoneTop zoneBottom zoneCentre)
// edgeShare is how far into a pane, as a fraction of its width or height,// its edge zones reach. Inside them all is the centre.const edgeShare = 0.25
// paneDrop is what dropping a dragged pane would do.type paneDrop struct { block string // the pane it would go beside or swap with, or "" zone dropZone window string // the window whose tab it would move to, or "" anchor string // that window's block it would go beside rect image.Rectangle}
// floatLayer is the floating layer a pane is drawn in.type floatLayer struct { id string bounds [4]float64 // where it's drawn, as fractions of the window front bool // in front of the window's other floating layers // rect is where its cards are drawn, shadows and all, in the window, // and area what it's cut to: the workspace. rect, area image.Rectangle}
// layerMove is a floating layer dropped at bounds, which the server hasn't// yet said it put there.type layerMove struct { bounds [4]float64 since time.Time}
// layerMoveWait is how long a dropped layer stays where it was dropped// while the server hasn't moved it there, as when the move failed.const layerMoveWait = 2 * time.Second
// layerBounds is where floating layer l is drawn: where it's being dragged// to, or where it was dropped until the server has it there, else where// the server has it.func (u *UI) layerBounds(l rex.Layer) [4]float64 { if d := &u.paneDrag; d.active && d.layer == l.LayerID { return movedBounds(d.bounds, d.at.Sub(d.start), u.panesArea) } if b, ok := u.resizedLayer(l.LayerID); ok { return b } m, ok := u.layerMoves[l.LayerID] if !ok { return l.Bounds } if sameBounds(m.bounds, l.Bounds) || time.Since(m.since) > layerMoveWait { delete(u.layerMoves, l.LayerID) return l.Bounds } return m.bounds}
// movedBounds is b, as fractions of area, moved by delta pixels, but no// further than the edges of area.func movedBounds(b [4]float64, delta f32.Point, area image.Rectangle) [4]float64 { if area.Dx() <= 0 || area.Dy() <= 0 { return b } b[0] = min(max(b[0]+float64(delta.X)/float64(area.Dx()), 0), 1-b[2]) b[1] = min(max(b[1]+float64(delta.Y)/float64(area.Dy()), 0), 1-b[3]) return b}
func sameBounds(a, b [4]float64) bool { for i := range a { if math.Abs(a[i]-b[i]) > 1e-6 { return false } } return true}
// paneHeaderEvent acts on a pointer event on a pane's header: a press// starts following it, a drag past dragSlop takes the pointer, and a// release drops the pane where it is. tag is the header's handler, and fl// the floating layer the pane is in, or nil.func (u *UI) paneHeaderEvent(gtx layout.Context, tag any, blockID string, fl *floatLayer, e pointer.Event) { d := &u.paneDrag switch e.Kind { case pointer.Press: if d.block == "" && (e.Source != pointer.Mouse || e.Buttons == pointer.ButtonPrimary) { *d = paneDrag{pid: e.PointerID, block: blockID, start: e.Position, at: e.Position} if fl != nil { d.layer, d.bounds = fl.id, fl.bounds } } case pointer.Drag: if d.block != blockID || e.PointerID != d.pid { return } d.at = e.Position if !d.active { diff := e.Position.Sub(d.start) slop := float32(gtx.Dp(dragSlop)) if diff.X*diff.X+diff.Y*diff.Y > slop*slop { gtx.Execute(pointer.GrabCmd{Tag: tag, ID: e.PointerID}) d.active = true } } case pointer.Release: if d.block == blockID && e.PointerID == d.pid && d.active { d.at = e.Position d.dropped = true } else if d.block == blockID { *d = paneDrag{} } case pointer.Cancel: if d.block == blockID { *d = paneDrag{} } }}
// paneDropAt is what dropping the dragged pane at p would do: move it to// another window, if p is on that window's tab; else split beside the pane// under p, or swap with it, by which part of it p is in.func (u *UI) paneDropAt(f rexsession.Frame, p image.Point) paneDrop { dragged := u.paneDrag.block for i := range f.View.Windows { w := &f.View.Windows[i] r, ok := u.tabRects[w.WindowID] if !ok || !p.In(r) { continue } if w.WindowID == f.View.ActiveWindowID { return paneDrop{} // where it is already } anchor := w.FocusedBlockID if w.ZoomedBlockID != "" { anchor = w.ZoomedBlockID } if anchor == "" { for _, l := range w.Layers { if l.Kind == "tiled" { if leaves := leaves(l.Layout, nil); len(leaves) > 0 { anchor = leaves[0].BlockID } } } } if anchor == "" { return paneDrop{} } return paneDrop{window: w.WindowID, anchor: anchor, rect: r} }
// The panes drawn last are on top. for i := len(u.paneSlots) - 1; i >= 0; i-- { s := u.paneSlots[i] if !p.In(s.rect) { continue } if s.block == dragged { return paneDrop{} } zone, rect := dropZoneAt(s.rect, p) return paneDrop{block: s.block, zone: zone, rect: rect} } return paneDrop{}}
// dropZoneAt is the part of r that p is in, and the part of r that a pane// dropped there would take.func dropZoneAt(r image.Rectangle, p image.Point) (dropZone, image.Rectangle) { fx := float64(p.X-r.Min.X) / float64(r.Dx()) fy := float64(p.Y-r.Min.Y) / float64(r.Dy()) zone, nearest := zoneCentre, edgeShare for _, z := range []struct { zone dropZone dist float64 }{{zoneLeft, fx}, {zoneRight, 1 - fx}, {zoneTop, fy}, {zoneBottom, 1 - fy}} { if z.dist < nearest { zone, nearest = z.zone, z.dist } } mid := r.Min.Add(r.Size().Div(2)) switch zone { case zoneLeft: r.Max.X = mid.X case zoneRight: r.Min.X = mid.X case zoneTop: r.Max.Y = mid.Y case zoneBottom: r.Min.Y = mid.Y } return zone, r}
// dropPane does what dropping the dragged pane where the pointer is does.func (u *UI) dropPane(f rexsession.Frame) { if u.session == nil { return } block := u.paneDrag.block drop := u.paneDropAt(f, u.paneDrag.at.Round()) switch { case drop.window != "": u.session.MoveBlock(block, drop.anchor, "horizontal", "after") case drop.zone == zoneCentre: u.session.SwapBlocks(block, drop.block) case drop.zone == zoneLeft: u.session.MoveBlock(block, drop.block, "horizontal", "before") case drop.zone == zoneRight: u.session.MoveBlock(block, drop.block, "horizontal", "after") case drop.zone == zoneTop: u.session.MoveBlock(block, drop.block, "vertical", "before") case drop.zone == zoneBottom: u.session.MoveBlock(block, drop.block, "vertical", "after") }}
// dropLayer moves the dragged pane's floating layer where it was dropped.func (u *UI) dropLayer() { d := &u.paneDrag b := movedBounds(d.bounds, d.at.Sub(d.start), u.panesArea) if u.session == nil || sameBounds(b, d.bounds) { return } u.layerMoves[d.layer] = layerMove{bounds: b, since: time.Now()} u.session.MoveLayer(d.layer, b)}
// dropLabel says what a drop does, for the chip that follows the pointer.func (d paneDrop) label() string { switch { case d.window != "": return "Move to tab" case d.zone == zoneCentre: return "Swap" case d.zone == zoneLeft: return "Split left" case d.zone == zoneRight: return "Split right" case d.zone == zoneTop: return "Split above" case d.zone == zoneBottom: return "Split below" } return ""}
func withAlpha(c color.NRGBA, a uint8) color.NRGBA { c.A = a return c}
// layoutPaneDrag draws a pane drag over everything else: the pane being// moved washed out, where it would land, and a chip with its title under// the pointer. A floating pane's drag draws none of that, for the layer// itself follows the pointer.func (u *UI) layoutPaneDrag(gtx layout.Context, f rexsession.Frame) { d := &u.paneDrag if !d.active { return } if d.dropped { if d.layer != "" { u.dropLayer() } else { u.dropPane(f) } *d = paneDrag{} return } found := false for _, s := range u.paneSlots { if s.block == d.block { found = true if d.layer == "" { w := u.layer(gtx, layerDragWash, "", scene.Spec{Bounds: s.rect}) theme.Fill(w, withAlpha(theme.Cream, 0xB0), image.Rectangle{Max: s.rect.Size()}) } } } if !found { // Its block closed or left the window: there's nothing to drop. *d = paneDrag{} return }
// The pointer is the pane's now: it shows a closed hand everywhere. whole := image.Rectangle{Max: gtx.Constraints.Max} in := u.layer(gtx, layerDragInput, "", scene.Spec{Bounds: whole, Absolute: true, InputOnly: true}) area := clip.Rect(whole).Push(in.Ops) pointer.CursorGrabbing.Add(in.Ops) area.Pop() if d.layer != "" { return // the layer is drawn where it's going }
p := d.at.Round() drop := u.paneDropAt(f, p) border := gtx.Dp(theme.Border) switch { case drop.window != "": // A ring round the tab, which leaves its label to be read. ring := drop.rect.Inset(-2 * border) gtx := u.layer(gtx, layerDragDrop, "", scene.Spec{Bounds: ring, Absolute: true}) theme.Outline(gtx, theme.Yellow, ring.Inset(border), border) theme.Outline(gtx, theme.Ink, ring, border) case !drop.rect.Empty(): gtx := u.layer(gtx, layerDragDrop, "", scene.Spec{Bounds: drop.rect, Absolute: true}) theme.Bordered(gtx, withAlpha(theme.Lavender, 0xC0), theme.Ink, drop.rect, border) }
// The chip: the pane's title, and what letting go does. text := truncate(paneTitle(f, d.block), 40) if l := drop.label(); l != "" { text += " → " + l } chip := theme.Box{Fill: theme.Purple, Shadow: theme.Shadow, Padding: layout.Inset{Top: 4, Bottom: 4, Left: 10, Right: 10}} u.layoutChip(gtx, layerDragChip, theme.Shadow, func(gtx layout.Context) layout.Dimensions { return chip.Layout(gtx, u.theme.Label(13, text, theme.White, font.Bold).Layout) }, func(size, window image.Point) image.Point { // Below and right of the pointer, kept inside the window. pos := p.Add(image.Pt(gtx.Dp(14), gtx.Dp(14))) pos.X = max(0, min(pos.X, window.X-size.X)) pos.Y = max(0, min(pos.Y, window.Y-size.Y)) return pos })}
// layoutChip draws w, at its own size, in a layer of its own of kind, at// the point place picks for that size in a window of gtx's size. It is// drawn relative to the layer, so that as it moves, it isn't drawn again.// shadow is how far w's shadow reaches beyond its size.func (u *UI) layoutChip(gtx layout.Context, kind layerKind, shadow unit.Dp, w layout.Widget, place func(size, window image.Point) image.Point) { // Its size, to place the layer, from drawing it where it isn't // shown. u.measure.Reset() m := gtx m.Ops = &u.measure m.Constraints.Min = image.Point{} size := w(m).Size pos := place(size, gtx.Constraints.Max) s := gtx.Dp(shadow) gtx = u.layer(gtx, kind, "", scene.Spec{Bounds: image.Rectangle{Min: pos, Max: pos.Add(size).Add(image.Pt(s, s))}}) gtx.Constraints = layout.Exact(size) w(gtx)}