/* 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 ( "fmt" "time" wh "git.bullercodeworks.com/brian/tcell-widgets/helpers" "github.com/gdamore/tcell" ) // ScrollingWidgetList lays out all widgets added one after the other // It will fill as much space as you give it type ScrollingWidgetList struct { id string style tcell.Style orientation ScrollingWidgetListOrient x, y int w, h int widgets []Widget emptyWidget Widget active bool visible bool focusable bool disableTab bool insetBorder bool keyMap *KeyMap logger func(string, ...any) } type ScrollingWidgetListOrient int const ( SWLLayV = ScrollingWidgetListOrient(iota) SWLLayH ) var _ Widget = (*ScrollingWidgetList)(nil) func NewScrollingWidgetList(id string, s tcell.Style) *ScrollingWidgetList { ret := &ScrollingWidgetList{} ret.Init(id, s) return ret } func (w *ScrollingWidgetList) Init(id string, s tcell.Style) { w.id = id w.style = s w.visible = true w.focusable = true w.keyMap = NewKeyMap(NewKey(BuildEK(tcell.KeyTab), func(ev *tcell.EventKey) bool { active := w.findActive() if active == nil && len(w.widgets) > 0 { // No widget is active, but we do have some for i := range w.widgets { if w.widgets[i].Focusable() { w.widgets[i].SetActive(true) return true } } return false } return w.ActivateNext() })) w.keyMap.Add(NewKey(BuildEK(tcell.KeyBacktab), func(ev *tcell.EventKey) bool { active := w.findActive() if active == nil && len(w.widgets) > 0 { // No widget is active, but we do have some for i := len(w.widgets) - 1; i >= 0; i-- { if w.widgets[i].Focusable() { w.widgets[i].SetActive(true) return true } } return false } return w.ActivatePrev() })) e := NewText(fmt.Sprintf("%s-empty", w.id), w.style) e.SetText("Nothing to Display") w.emptyWidget = e } func (w *ScrollingWidgetList) Id() string { return w.id } func (w *ScrollingWidgetList) HandleResize(ev *tcell.EventResize) { w.w, w.h = ev.Size() w.updateWidgetLayouts() } func (w *ScrollingWidgetList) GetKeyMap() *KeyMap { return w.keyMap } func (w *ScrollingWidgetList) SetKeyMap(km *KeyMap) { w.keyMap = km } func (w *ScrollingWidgetList) HandleKey(ev *tcell.EventKey) bool { if !w.active || w.disableTab { return false } active := w.findActive() if active != nil { if active.HandleKey(ev) { return true } } return w.keyMap.Handle(ev) } func (w *ScrollingWidgetList) GetActiveWidgetIdx() int { return w.findActiveIdx() } func (w *ScrollingWidgetList) GetActiveWidget() Widget { return w.findActive() } func (w *ScrollingWidgetList) HandleTime(ev *tcell.EventTime) { for _, wi := range w.widgets { wi.HandleTime(ev) } } func (w *ScrollingWidgetList) Draw(screen tcell.Screen) { if !w.visible { return } pos := w.GetPos() if w.insetBorder { wh.Border(pos.X, pos.Y, pos.X+w.w, pos.Y+w.h, wh.BRD_CSIMPLE, w.style, screen) } for _, wd := range w.widgets { pos.DrawOffset(wd, screen) } } func (w *ScrollingWidgetList) SetStyle(s tcell.Style) { w.style = s } func (w *ScrollingWidgetList) Active() bool { return w.active } func (w *ScrollingWidgetList) SetActive(a bool) { w.active = a if w.active { act := w.findActiveOrFirst() if act != nil { act.SetActive(true) } } } func (w *ScrollingWidgetList) Visible() bool { return w.visible } func (w *ScrollingWidgetList) SetVisible(a bool) { w.visible = a } func (w *ScrollingWidgetList) Focusable() bool { return w.focusable } func (w *ScrollingWidgetList) SetFocusable(b bool) { w.focusable = b } func (w *ScrollingWidgetList) SetX(x int) { w.x = x } func (w *ScrollingWidgetList) SetY(y int) { w.y = y } func (w *ScrollingWidgetList) GetX() int { return w.x } func (w *ScrollingWidgetList) GetY() int { return w.y } func (w *ScrollingWidgetList) GetPos() Coord { return Coord{X: w.x, Y: w.y} } func (w *ScrollingWidgetList) SetPos(c Coord) { w.x, w.y = c.X, c.Y } func (w *ScrollingWidgetList) GetW() int { return w.w } func (w *ScrollingWidgetList) GetH() int { return w.h } func (w *ScrollingWidgetList) SetW(wd int) { w.w = wd } func (w *ScrollingWidgetList) SetH(h int) { w.h = h } func (w *ScrollingWidgetList) getSize() Coord { return Coord{X: w.w, Y: w.h} } func (w *ScrollingWidgetList) SetSize(c Coord) { w.w, w.h = c.X, c.Y } func (w *ScrollingWidgetList) WantW() int { var wantW int for _, wd := range w.widgets { switch w.orientation { case SWLLayV: // Find the highest want of all widgets wantW = wh.Max(wd.WantW(), wantW) case SWLLayH: // Find the sum of all widgets wants wantW = wantW + wd.WantW() } } if w.insetBorder { wantW += 2 } return wantW } func (w *ScrollingWidgetList) WantH() int { var wantH int for _, wd := range w.widgets { switch w.orientation { case SWLLayV: // Find the sum of all widgets wants wantH = wantH + wd.WantH() case SWLLayH: // Find the highest want of all widgets wantH = wh.Max(wd.WantH(), wantH) } } if w.insetBorder { wantH += 2 } return wantH } func (w *ScrollingWidgetList) MinW() int { var minW int for _, wd := range w.widgets { switch w.orientation { case SWLLayV: // Find the highest minimum width of all widgets minW = wh.Max(wd.MinW(), minW) case SWLLayH: // Find the sum of all widget minimum widgets minW = minW + wd.MinW() } } return minW } func (w *ScrollingWidgetList) MinH() int { var minH int for _, wd := range w.widgets { switch w.orientation { case SWLLayV: minH = minH + wd.MinH() case SWLLayH: minH = wh.Max(wd.MinH(), minH) } } return minH } // Find the currently active widget, there should be only one. func (w *ScrollingWidgetList) findActiveIdx() int { for i := range w.widgets { if w.widgets[i].Active() { return i } } return -1 } func (w *ScrollingWidgetList) findActive() Widget { for i := range w.widgets { if w.widgets[i].Active() { return w.widgets[i] } } return nil } func (w *ScrollingWidgetList) findActiveOrFirst() Widget { if act := w.findActive(); act != nil { return act } // Didn't find one, return the first if len(w.widgets) > 0 { return w.widgets[0] } return nil } func (w *ScrollingWidgetList) ActivateWidget(n Widget) { for i := range w.widgets { w.widgets[i].SetActive(w.widgets[i] == n) } } func (w *ScrollingWidgetList) ActivateNext() bool { var found bool for i := range w.widgets { if found && w.widgets[i].Focusable() { w.widgets[i].SetActive(true) return true } else if w.widgets[i].Active() { found = true w.widgets[i].SetActive(false) } } return false } func (w *ScrollingWidgetList) ActivatePrev() bool { var found bool for i := len(w.widgets) - 1; i >= 0; i-- { if found && w.widgets[i].Focusable() { w.widgets[i].SetActive(true) return true } else if w.widgets[i].Active() { found = true w.widgets[i].SetActive(false) } } return false } func (w *ScrollingWidgetList) SetOrientation(o ScrollingWidgetListOrient) { w.orientation = o } func (w *ScrollingWidgetList) WidgetCount() int { return len(w.widgets) } func (w *ScrollingWidgetList) IndexOf(n Widget) int { for i := range w.widgets { if w.widgets[i] == n { return i } } return -1 } func (w *ScrollingWidgetList) FindById(id string) Widget { for i := range w.widgets { if w.widgets[i].Id() == id { return w.widgets[i] } } return nil } func (w *ScrollingWidgetList) Contains(n Widget) bool { return w.IndexOf(n) >= 0 } func (w *ScrollingWidgetList) Replace(n, with Widget) { idx := w.IndexOf(n) if idx == -1 { // 'n' isn't in layout. Bail out. return } w.Delete(n) w.Insert(with, idx) } func (w *ScrollingWidgetList) AddAll(n Widget, more ...Widget) { w.Add(n) for i := range more { w.Add(more[i]) } } func (w *ScrollingWidgetList) Add(n Widget) { if w.Contains(n) { // If the widget is already in the layout, move it to the end w.Delete(n) } w.widgets = append(w.widgets, n) w.updateWidgetLayouts() } func (w *ScrollingWidgetList) 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-- } w.Delete(n) } w.widgets = append(w.widgets[:idx], append([]Widget{n}, w.widgets[idx:]...)...) } // Remove all children from this widget func (w *ScrollingWidgetList) Clear() { w.widgets = []Widget{} } func (w *ScrollingWidgetList) Delete(n Widget) { for i := 0; i < len(w.widgets); i++ { if w.widgets[i] == n { w.DeleteIndex(i) return } } } func (w *ScrollingWidgetList) DeleteIndex(idx int) { if idx < len(w.widgets) { w.widgets = append(w.widgets[:idx], w.widgets[idx+1:]...) } } func (w *ScrollingWidgetList) updateWidgetLayouts() { switch w.orientation { case SWLLayV: for _, wd := range w.widgets { wd.HandleResize((&Coord{X: w.w, Y: wd.WantH()}).ResizeEvent()) w.updateSWLVWidgetPos(wd) } case SWLLayH: for _, wd := range w.widgets { wd.HandleResize((&Coord{X: wd.WantW(), Y: w.h}).ResizeEvent()) w.updateSWLHWidgetPos(wd) } } } // The Layout should have a static Size set at this point that we can use // All widgets are being aligned to the Top-Left // // 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 *ScrollingWidgetList) updateSWLVWidgetPos(wd Widget) { c := Coord{} for i := range w.widgets { if w.widgets[i] == wd { if i > 0 { c.Y += 1 } break } // We only care about the Y & H of the widget before this one. if w.widgets[i].Visible() { c.Y = w.widgets[i].GetY() + w.widgets[i].GetH() } } if wd.GetW() < w.w { c.X = int((float64(w.w) / 2) - (float64(wd.GetW()) / 2)) } else { c.X = 0 } if w.insetBorder { c.X += 1 c.Y += 1 } wd.SetPos(c) } func (w *ScrollingWidgetList) updateSWLHWidgetPos(wd Widget) { c := Coord{} for i := range w.widgets { if w.widgets[i] == wd { if i > 0 { c.X += 1 } break } if w.widgets[i].Visible() { c.X = w.widgets[i].GetX() + w.widgets[i].GetW() } } // Is the Y of this tricky? c.Y = 0 if w.insetBorder { c.X += 1 c.Y += 1 } wd.SetPos(c) } func (w *ScrollingWidgetList) SetTabDisabled(b bool) { w.disableTab = b } func (w *ScrollingWidgetList) SetBordered(b bool) { w.insetBorder = b } func (w *ScrollingWidgetList) SetLogger(l func(string, ...any)) { w.logger = l } func (w *ScrollingWidgetList) Log(txt string, args ...any) { if w.logger != nil { txt = fmt.Sprintf("%s:%s", time.Now().Format(time.TimeOnly), txt) w.logger(txt, args...) } } func (w *ScrollingWidgetList) SetEmptyWidget(wd Widget) { w.emptyWidget = wd }