Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317package ui
import ( "image"
"gioui.org/f32" "gioui.org/io/event" "gioui.org/io/pointer" "gioui.org/layout" "gioui.org/op" "gioui.org/op/clip" "gioui.org/unit" "gioui.org/widget"
"100gex/internal/rex" "100gex/internal/rexsession" "100gex/internal/theme")
// tabGap is the space between tabs.const tabGap unit.Dp = 10
// dragSlop is how far the pointer must move, pressed, before a press on a// tab becomes a drag rather than a click.const dragSlop unit.Dp = 4
// tabDrag follows a press on a tab, and the drag it may become. Its// positions are in the tab strip's coordinates, which don't move while the// tabs do.type tabDrag struct { tag tag pid pointer.ID window string // the window whose tab was pressed, or "" for none start f32.Point // where the press was grab float32 // how far right of the tab's left edge it was x float32 // where the pointer is now active bool // past dragSlop: the tab follows the pointer}
// tabSlot is where a tab was drawn, in the strip's coordinates.type tabSlot struct { window string rect image.Rectangle}
// laidTab is a tab laid out, but not yet drawn.type laidTab struct { id string call op.CallOp size image.Point}
// layoutTabs draws a tab for each window, filling the height of// gtx.Constraints.Max, with its origin at origin in the window. Pressing a// tab and dragging it sideways moves its window to another place in the// order; clicking it switches to it.func (u *UI) layoutTabs(gtx layout.Context, f rexsession.Frame, origin image.Point) layout.Dimensions { var windows []rex.Window if f.Window != nil { windows = f.View.Windows } u.updateTabDrag(gtx, windows)
tabs := make([]laidTab, len(windows)) height := gtx.Constraints.Max.Y gtx.Constraints.Min = image.Point{} for i := range windows { w := &windows[i] click := u.tabs[w.WindowID] if click == nil { click = new(widget.Clickable) u.tabs[w.WindowID] = click } if click.Clicked(gtx) && u.session != nil { u.session.FocusWindow(w.WindowID) } m := op.Record(gtx.Ops) dims := theme.Button{ Theme: u.theme, Clickable: click, Label: u.tabLabel(f, w, i), Selected: w.WindowID == f.View.ActiveWindowID, }.Layout(gtx) tabs[i] = laidTab{id: w.WindowID, call: m.Stop(), size: dims.Size} }
gap := gtx.Dp(tabGap) natural := make([]int, len(tabs)) // each tab's left edge, undragged width := 0 for i, t := range tabs { if i > 0 { width += gap } natural[i] = width width += t.size.X }
// Where each tab goes: in order, or, while one is dragged, the others // in order with room for it where it would land, and it under the // pointer. xs := natural dragged := -1 if d := &u.tabDrag; d.active { for i, t := range tabs { if t.id == d.window { dragged = i } } } var lifted int if dragged >= 0 { lifted = u.draggedTabX(tabs[dragged].size.X, width) to := dropIndex(tabs, natural, dragged, lifted) xs = make([]int, len(tabs)) x := 0 for k, i := 0, 0; i < len(tabs); i++ { if i == dragged { continue } if k == to { x += tabs[dragged].size.X + gap } xs[i] = x x += tabs[i].size.X + gap k++ } xs[dragged] = lifted }
// The strip's drag handler is beneath the tabs, so that their // Clickables get presses too, and see clicks that don't become drags. // The area reaches past the strip for the lifted tab's shadow. lift := gtx.Dp(theme.Shadow) area := clip.Rect{Min: image.Pt(-lift, 0), Max: image.Pt(width+lift, height)}.Push(gtx.Ops) event.Op(gtx.Ops, &u.tabDrag.tag) if dragged >= 0 { pointer.CursorGrabbing.Add(gtx.Ops) } u.tabSlots = u.tabSlots[:0] clear(u.tabRects) draw := func(i int) { t := tabs[i] pos := image.Pt(xs[i], (height-t.size.Y)/2) if i == dragged { // Lifted off the bar: up and to the left, with a deeper // shadow than the button's own. own := gtx.Dp(theme.Shadow) / 2 pos = pos.Sub(image.Pt(lift-own, lift-own)) face := t.size.Sub(image.Pt(own, own)) theme.Fill(gtx, theme.Ink, image.Rectangle{Max: face}.Add(pos).Add(image.Pt(lift, lift))) } st := op.Offset(pos).Push(gtx.Ops) t.call.Add(gtx.Ops) st.Pop() r := image.Rectangle{Min: pos, Max: pos.Add(t.size)} u.tabSlots = append(u.tabSlots, tabSlot{window: t.id, rect: r}) u.tabRects[t.id] = r.Add(origin) } for i := range tabs { if i != dragged { draw(i) } } if dragged >= 0 { draw(dragged) // on top } area.Pop()
// Forget tabs for windows that have closed. for id := range u.tabs { if f.View.Window(id) == nil { delete(u.tabs, id) } } return layout.Dimensions{Size: image.Pt(width, height)}}
// draggedTabX is where the dragged tab's left edge is, kept within the// strip, which is width wide.func (u *UI) draggedTabX(tabWidth, width int) int { d := &u.tabDrag x := int(d.x - d.grab) return max(0, min(x, width-tabWidth))}
// dropIndex is the place among the other tabs where the dragged one would// land if dropped with its left edge at x. A tab before it moves after it// once its left edge passes the tab's middle; a tab after it, once its right// edge does. So both ends can be reached, whatever the tabs' widths.func dropIndex(tabs []laidTab, natural []int, dragged, x int) int { to := 0 for i, t := range tabs { middle := natural[i] + t.size.X/2 switch { case i < dragged && x > middle: to++ case i > dragged && x+tabs[dragged].size.X > middle: to++ } } return to}
// updateTabDrag reads the pointer's events on the tab strip: a press on a// tab, a drag past dragSlop that takes the pointer from the tab's button,// and the release that drops it.func (u *UI) updateTabDrag(gtx layout.Context, windows []rex.Window) { d := &u.tabDrag for { ev, ok := gtx.Event(pointer.Filter{ Target: &d.tag, Kinds: pointer.Press | pointer.Drag | pointer.Release | pointer.Cancel, }) if !ok { break } e, ok := ev.(pointer.Event) if !ok { continue } switch e.Kind { case pointer.Press: if d.window != "" || e.Source == pointer.Mouse && e.Buttons != pointer.ButtonPrimary { continue } p := e.Position.Round() for _, s := range u.tabSlots { if p.In(s.rect) { *d = tabDrag{pid: e.PointerID, window: s.window, start: e.Position, grab: e.Position.X - float32(s.rect.Min.X), x: e.Position.X} } } case pointer.Drag: if d.window == "" || e.PointerID != d.pid { continue } d.x = e.Position.X 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 { // Taking the pointer cancels the button's click. gtx.Execute(pointer.GrabCmd{Tag: &d.tag, ID: e.PointerID}) d.active = true } } case pointer.Release: if e.PointerID == d.pid && d.active { d.x = e.Position.X u.dropTab(gtx, windows) } *d = tabDrag{} case pointer.Cancel: *d = tabDrag{} } } // A window that closes while its tab is dragged ends the drag. if d.window != "" { found := false for i := range windows { found = found || windows[i].WindowID == d.window } if !found { *d = tabDrag{} } }}
// dropTab moves the dragged tab's window to where the tab was dropped.func (u *UI) dropTab(gtx layout.Context, windows []rex.Window) { if u.session == nil { return } d := &u.tabDrag // The tabs as they were last drawn, in order, to place the drop // among: their widths haven't changed since. gap := gtx.Dp(tabGap) tabs := make([]laidTab, len(windows)) natural := make([]int, len(windows)) dragged, width := -1, 0 for i := range windows { id := windows[i].WindowID var size image.Point for _, s := range u.tabSlots { if s.window == id { size = s.rect.Size() } } if id == d.window { dragged = i } if i > 0 { width += gap } tabs[i], natural[i] = laidTab{id: id, size: size}, width width += size.X } if dragged < 0 { return } to := dropIndex(tabs, natural, dragged, u.draggedTabX(tabs[dragged].size.X, width)) if to == dragged { return // where it was } // The window that will come after it, counting without it. before := "" for k, i := 0, 0; i < len(windows); i++ { if i == dragged { continue } if k == to { before = windows[i].WindowID break } k++ } u.session.MoveWindow(d.window, before)}