/* Copyright © Brian Buller Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package widgets import ( wh "git.bullercodeworks.com/brian/tcell-widgets/helpers" "github.com/gdamore/tcell" ) // LinearLayout lays out all widgets added one after the other type LinearLayout struct { id string style tcell.Style orientation LinearLayoutOrient x, y int w, h int widgets []Widget layoutFlags map[Widget]LayoutFlag layoutWeights map[Widget]int totalWeight int active bool visible bool tabbable bool disableTab bool cursor int logger func(string, ...any) } type LinearLayoutOrient int const ( LinLayV = LinearLayoutOrient(iota) LinLayH ) var _ Widget = (*LinearLayout)(nil) func NewLinearLayout(id string, s tcell.Style) *LinearLayout { ret := &LinearLayout{} ret.Init(id, s) return ret } func (w *LinearLayout) Init(id string, s tcell.Style) { w.id = id w.style = s w.visible = true w.layoutFlags = make(map[Widget]LayoutFlag) w.layoutWeights = make(map[Widget]int) } func (w *LinearLayout) Id() string { return w.id } func (w *LinearLayout) HandleResize(ev *tcell.EventResize) { w.w, w.h = ev.Size() w.w = wh.Min(w.w, w.WantW()) w.h = wh.Min(w.h, w.WantH()) w.updateWidgetLayouts() } func (w *LinearLayout) HandleKey(ev *tcell.EventKey) bool { if !w.disableTab && ev.Key() == tcell.KeyTab { fndP := -1 for i := w.cursor; i < len(w.widgets); i++ { if fndP == -1 { if w.widgets[i].Active() { fndP = i w.widgets[i].SetActive(false) continue } } else { if w.widgets[i].Focusable() && w.widgets[i].Tabbable() { w.widgets[i].SetActive(true) return true } } } // If we're here, we hit the last widget, loop if fndP == -1 { // But didn't even find the active one return false } for i := 0; i < fndP; i++ { if w.widgets[i].Focusable() && w.widgets[i].Tabbable() { w.widgets[i].SetActive(true) return true } } return false } for _, wi := range w.widgets { if wi.HandleKey(ev) { return true } } return false } func (w *LinearLayout) HandleTime(ev *tcell.EventTime) { for _, wi := range w.widgets { wi.HandleTime(ev) } } func (w *LinearLayout) Draw(screen tcell.Screen) { if !w.visible { return } p := w.GetPos() for _, wd := range w.widgets { wd.DrawOffset(p, screen) } } func (w *LinearLayout) DrawOffset(c Coord, screen tcell.Screen) { p := w.GetPos() w.SetPos(p.Add(c)) w.Draw(screen) w.SetPos(p) } func (w *LinearLayout) Active() bool { return w.active } func (w *LinearLayout) SetActive(a bool) { w.active = a } func (w *LinearLayout) Visible() bool { return w.visible } func (w *LinearLayout) SetVisible(a bool) { w.visible = a } func (w *LinearLayout) Focusable() bool { return true } func (w *LinearLayout) SetTabbable(b bool) { w.tabbable = b } func (w *LinearLayout) Tabbable() bool { return w.tabbable } func (w *LinearLayout) SetX(x int) { w.x = x } func (w *LinearLayout) SetY(y int) { w.y = y } func (w *LinearLayout) GetX() int { return w.x } func (w *LinearLayout) GetY() int { return w.y } func (w *LinearLayout) GetPos() Coord { return Coord{X: w.x, Y: w.y} } func (w *LinearLayout) SetPos(c Coord) { w.x, w.y = c.X, c.Y } func (w *LinearLayout) GetW() int { return w.w } func (w *LinearLayout) GetH() int { return w.h } func (w *LinearLayout) SetW(wd int) { w.w = wd } func (w *LinearLayout) SetH(h int) { w.h = h } func (w *LinearLayout) SetSize(c Coord) { w.w, w.h = c.X, c.Y } func (w *LinearLayout) WantW() int { var wantW int for _, wd := range w.widgets { switch w.orientation { case LinLayV: // Find the highest want of all widgets wantW = wh.Max(wd.WantW(), wantW) case LinLayH: // Find the sum of all widget widgets wants wantW = wantW + wd.WantW() } } return wantW } func (w *LinearLayout) WantH() int { var wantH int for _, wd := range w.widgets { switch w.orientation { case LinLayV: // Find the sum of all widget widgets wants wantH = wantH + wd.WantH() case LinLayH: // Find the highest want of all widgets wantH = wh.Max(wd.WantH(), wantH) } } return wantH } func (w *LinearLayout) MinW() int { var minW int for _, wd := range w.widgets { switch w.orientation { case LinLayV: // Find the highest minimum width of all widgets minW = wh.Max(wd.MinW(), minW) case LinLayH: // Find the sum of all widget minimum widgets minW = minW + wd.MinW() } } return minW } func (w *LinearLayout) MinH() int { var minH int for _, wd := range w.widgets { switch w.orientation { case LinLayV: minH = minH + wd.MinH() case LinLayH: minH = wh.Max(wd.MinH(), minH) } } return minH } func (w *LinearLayout) SetOrientation(o LinearLayoutOrient) { w.orientation = o } func (w *LinearLayout) IndexOf(n Widget) int { for i := range w.widgets { if w.widgets[i] == n { return i } } return -1 } func (w *LinearLayout) Contains(n Widget) bool { return w.IndexOf(n) >= 0 } func (w *LinearLayout) Add(n Widget) { if w.Contains(n) { // If the widget is already in the layout, move it to the end pFlags, pWeight := w.layoutFlags[n], w.layoutWeights[n] w.Delete(n) w.layoutFlags[n], w.layoutWeights[n] = pFlags, pWeight } w.widgets = append(w.widgets, n) // If we don't already have a weight set, set it to 1 if _, ok := w.layoutWeights[n]; !ok { w.layoutWeights[n] = 1 } w.updateTotalWeight() } func (w *LinearLayout) Insert(n Widget, idx int) { if idx >= len(w.widgets) { w.Add(n) return } if pos := w.IndexOf(n); pos >= 0 { if pos < idx { idx-- } // Preserve the flags & weight pFlags, pWeight := w.layoutFlags[n], w.layoutWeights[n] w.Delete(n) w.layoutFlags[n], w.layoutWeights[n] = pFlags, pWeight } w.widgets = append(w.widgets[:idx], append([]Widget{n}, w.widgets[idx:]...)...) // If we don't already have a weight set, set it to 1 if _, ok := w.layoutWeights[n]; !ok { w.layoutWeights[n] = 1 } w.updateTotalWeight() } func (w *LinearLayout) Delete(n Widget) { for i := 0; i < len(w.widgets); i++ { if w.widgets[i] == n { w.DeleteIndex(i) return } } } func (w *LinearLayout) DeleteIndex(idx int) { if idx < len(w.widgets) { p := w.widgets[idx] w.widgets = append(w.widgets[:idx], w.widgets[idx+1:]...) delete(w.layoutFlags, p) delete(w.layoutWeights, p) w.updateTotalWeight() } } func (w *LinearLayout) AddFlag(wd Widget, f LayoutFlag) { if f.IsAlignH() { w.layoutFlags[wd].ClearAlignH() } else if f.IsAlignV() { w.layoutFlags[wd].ClearAlignV() } w.layoutFlags[wd].Add(f) } func (w *LinearLayout) RemoveFlag(wd Widget, f LayoutFlag) { // Removing an alignment flag centers that direction if f.IsAlignH() { w.layoutFlags[wd].ClearAlignH() } else if f.IsAlignV() { w.layoutFlags[wd].ClearAlignV() } } func (w *LinearLayout) SetWeight(wd Widget, wt int) { if !w.Contains(wd) { return } w.layoutWeights[wd] = wt w.updateTotalWeight() } func (w *LinearLayout) updateTotalWeight() { w.totalWeight = 0 for _, v := range w.layoutWeights { w.totalWeight += v } } func (w *LinearLayout) updateWidgetLayouts() { switch w.orientation { case LinLayV: for _, wd := range w.widgets { w.updateVerticalWidgetSize(wd) w.updateVerticalWidgetPos(wd) } case LinLayH: for _, wd := range w.widgets { w.updateHorizontalWidgetSize(wd) w.updateHorizontalWidgetPos(wd) } } } // The Layout should have a static Size set at this point that we can use // For now we're centering all views in the Layout (on the cross-axis) // // We need to determine the allowed size of this widget so we can determine // it's position func (w *LinearLayout) updateVerticalWidgetSize(wd Widget) { wd.HandleResize((&Coord{X: w.w, Y: w.getWeightedH(wd)}).ResizeEvent()) } func (w *LinearLayout) updateHorizontalWidgetSize(wd Widget) { wd.HandleResize((&Coord{X: w.getWeightedW(wd), Y: w.h}).ResizeEvent()) } // The Layout should have a static Size set at this point that we can use // For now we're centering all views in the Layout (on the cross-axis) // TODO: Use LayoutFlags to determine alignment in each 'cell' // // The position and size of each widget before this should be correct // This widget should also know its size by now. We just need to // position it relative to the layout. func (w *LinearLayout) updateVerticalWidgetPos(wd Widget) { w.Log("Calculating Widget Pos: %s", wd.Id()) c := Coord{} for i := range w.widgets { if w.widgets[i] == wd { break } c.Y += w.getWeightedH(w.widgets[i]) } w.Log("Set %s Y = %d", wd.Id(), c.Y) c.X = (w.w / 2) - (wd.GetW() / 2) wd.SetPos(c) } func (w *LinearLayout) updateHorizontalWidgetPos(wd Widget) { c := Coord{} for i := range w.widgets { c.X += w.getWeightedH(w.widgets[i]) } c.Y = (w.h / 2) - (wd.GetH() / 2) wd.SetPos(c) } func (w *LinearLayout) updateWidgetPos(wd Widget) { prevP, prevS := 0, 0 for _, wrk := range w.widgets { c := Coord{} switch w.orientation { case LinLayV: c.X, c.Y = w.w-(wd.GetW()/2), prevP+prevS+1 prevP, prevS = wrk.GetY(), wrk.GetH() case LinLayH: c.X, c.Y = prevP+prevS+1, w.h-(wd.GetH()/2) prevP, prevS = wrk.GetX(), wrk.GetW() } wd.SetPos(c) } } func (w *LinearLayout) getRelPos(wd Widget) Coord { _ = wd return Coord{} } func (w *LinearLayout) getAbsPos(wd Widget) Coord { rel := w.getRelPos(wd) return rel.Add(Coord{X: w.x, Y: w.y}) } func (w *LinearLayout) getWeightedH(wd Widget) int { if !w.Contains(wd) { return 0 } return w.h * (w.layoutWeights[wd] * w.totalWeight) } func (w *LinearLayout) getWeightedW(wd Widget) int { if !w.Contains(wd) { return 0 } return w.w * (w.layoutWeights[wd] * w.totalWeight) } func (w *LinearLayout) SetLogger(l func(string, ...any)) { w.logger = l } func (w *LinearLayout) Log(txt string, args ...any) { if w.logger != nil { w.logger(txt, args...) } }