/* 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" ) type AbsoluteLayout struct { id string style tcell.Style x, y int w, h int bordered bool widgets []Widget wCoords map[Widget]Coord wAnchor map[Widget]LayoutFlag wManualSizes map[Widget]Coord wDynamicSizes map[Widget]LayoutFlag defAnchor LayoutFlag active bool visible bool focusable bool keyMap *KeyMap cursor int disableTab bool logger func(string, ...any) } var _ Widget = (*AbsoluteLayout)(nil) func NewAbsoluteLayout(id string, s tcell.Style) *AbsoluteLayout { ret := &AbsoluteLayout{} ret.Init(id, s) return ret } func (w *AbsoluteLayout) Init(id string, s tcell.Style) { w.id = id w.style = s w.visible = true w.defAnchor = LFAlignCenter w.keyMap = BlankKeyMap() w.wCoords = make(map[Widget]Coord) w.wAnchor = make(map[Widget]LayoutFlag) w.wManualSizes = make(map[Widget]Coord) w.wDynamicSizes = make(map[Widget]LayoutFlag) w.focusable = true } func (w *AbsoluteLayout) Id() string { return w.id } func (w *AbsoluteLayout) HandleResize(ev *tcell.EventResize) { w.w, w.h = ev.Size() w.updateWidgetLayouts() } func (w *AbsoluteLayout) GetKeyMap() *KeyMap { return w.keyMap } func (w *AbsoluteLayout) SetKeyMap(km *KeyMap) { w.keyMap = km } func (w *AbsoluteLayout) 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].SetActive(true) return true } } } // If we're here, we hit the end. if fndP == -1 { // But didn't even find the start return false } for i := 0; i < fndP; i++ { if w.widgets[i].Focusable() { w.widgets[i].SetActive(true) return true } } return false } for _, wi := range w.widgets { if wi.HandleKey(ev) { return true } } return false } func (w *AbsoluteLayout) HandleTime(ev *tcell.EventTime) { for _, wi := range w.widgets { wi.HandleTime(ev) } } func (w *AbsoluteLayout) Draw(screen tcell.Screen) { if !w.visible { return } p := w.GetPos() if w.bordered { wh.Border(p.X, p.Y, p.X+w.w, p.Y+w.h, wh.BRD_CSIMPLE, w.style, screen) } for _, wd := range w.widgets { p.DrawOffset(wd, screen) } } func (w *AbsoluteLayout) SetStyle(s tcell.Style) { w.style = s } func (w *AbsoluteLayout) Active() bool { return w.active } func (w *AbsoluteLayout) SetActive(a bool) { w.active = a } func (w *AbsoluteLayout) Visible() bool { return w.visible } func (w *AbsoluteLayout) SetVisible(a bool) { w.visible = a } func (w *AbsoluteLayout) Focusable() bool { return w.focusable } func (w *AbsoluteLayout) SetFocusable(b bool) { w.focusable = b } func (w *AbsoluteLayout) SetX(x int) { w.x = x } func (w *AbsoluteLayout) SetY(y int) { w.y = y } func (w *AbsoluteLayout) GetX() int { return w.x } func (w *AbsoluteLayout) GetY() int { return w.y } func (w *AbsoluteLayout) GetPos() Coord { return Coord{X: w.x, Y: w.y} } func (w *AbsoluteLayout) SetPos(c Coord) { w.x, w.y = c.X, c.Y } func (w *AbsoluteLayout) GetW() int { return w.w } func (w *AbsoluteLayout) GetH() int { return w.h } func (w *AbsoluteLayout) SetW(wd int) { w.w = wd } func (w *AbsoluteLayout) SetH(h int) { w.h = h } func (w *AbsoluteLayout) SetSize(c Coord) { w.w, w.h = c.X, c.Y } func (w *AbsoluteLayout) WantW() int { return w.w } func (w *AbsoluteLayout) WantH() int { return w.h } func (w *AbsoluteLayout) MinW() int { // Find the highest value for x in all widgets GetX() + MinW() var minW int for _, wd := range w.widgets { wrk := wd.GetX() + wd.MinW() if wrk > minW { minW = wrk } } return minW } func (w *AbsoluteLayout) MinH() int { // Find the highest value for y in all widgets GetY() + MinH() var minH int for _, wd := range w.widgets { wrk := wd.GetY() + wd.MinH() if wrk > minH { minH = wrk } } return minH } // Add a widget at x/y func (w *AbsoluteLayout) Add(n Widget, pos Coord) { w.AddAnchored(n, pos, w.defAnchor) } func (w *AbsoluteLayout) AddAnchored(n Widget, pos Coord, anchor LayoutFlag) { w.widgets = append(w.widgets, n) w.wCoords[n] = pos w.wAnchor[n] = anchor w.updateWidgetLayouts() } func (w *AbsoluteLayout) Clear() { w.widgets = []Widget{} w.wCoords = make(map[Widget]Coord) } func (w *AbsoluteLayout) SetDefaultAnchor(d LayoutFlag) { w.defAnchor = d } func (w *AbsoluteLayout) SetLogger(l func(string, ...any)) { w.logger = l } func (w *AbsoluteLayout) Log(txt string, args ...any) { if w.logger != nil { w.logger(txt, args...) } } func (w *AbsoluteLayout) SetBordered(b bool) { w.bordered = b } func (w *AbsoluteLayout) updateWidgetLayouts() { // In an Absolute Layout, widgets are given a definite position and anchor. // The anchor is a side of the layout (see LayoutFlag type) // Process: // 1. Update pos of all widgets // 2. Update manually sized widgets // 3. Update dynamically sized widgets for _, wd := range w.widgets { w.updateWidgetPos(wd) } var updated []Widget for wd := range w.wManualSizes { w.updateWidgetSize(wd) updated = append(updated, wd) } for wd := range w.wDynamicSizes { w.updateWidgetSize(wd) updated = append(updated, wd) } for i := range w.widgets { var next bool for j := range updated { if updated[j] == w.widgets[i] { next = true } } if next { continue } w.updateWidgetSize(w.widgets[i]) } } // Set a widgets position relative to the layout func (w *AbsoluteLayout) updateWidgetPos(wd Widget) { p := w.getRelPos(wd) w.Log("%s: Setting Pos: %s (%d,%d)", w.Id(), wd.Id(), p.X, p.Y) wd.SetPos(p) } func (w *AbsoluteLayout) updateWidgetSize(wd Widget) { if sz, ok := w.wManualSizes[wd]; ok { w.Log("%s: Updating Size: %s (%d,%d)", w.Id(), wd.Id(), sz.X, sz.Y) wd.SetW(sz.X) wd.SetH(sz.Y) return } // The available space is: // X: Layout Width - Widget X // Y: Layout Height - Widget Y available := Coord{X: w.GetW() - wd.GetX(), Y: w.GetH() - wd.GetY()} if wd.WantW() < available.X { wd.SetW(wd.WantW()) } else if wd.MinW() > available.X { wd.SetW(available.X) } else { wd.SetW(wd.MinW()) } if wd.WantH() < available.Y { wd.SetH(wd.WantH()) } else if wd.MinH() > available.Y { wd.SetH(available.Y) } else { wd.SetH(wd.MinH()) } } // Manually set the size of a widget, the Layout won't override it func (w *AbsoluteLayout) SetWidgetSize(wd Widget, sz Coord) { w.wManualSizes[wd] = sz } func (w *AbsoluteLayout) SetDynamicWidgetSize(wd Widget, flg LayoutFlag) { w.wDynamicSizes[wd] = flg } func (w *AbsoluteLayout) getRelPos(wd Widget) Coord { var p Coord var a LayoutFlag var ok bool if p, ok = w.wCoords[wd]; !ok { // Default to top-left corner p = Coord{X: 0, Y: 0} } if a, ok = w.wAnchor[wd]; !ok { a = w.defAnchor } leftX, topY, rightX, bottomY := 0, 0, w.w, w.h if w.bordered { leftX += 1 topY += 1 rightX -= 1 bottomY -= 1 } midX, midY := (w.w / 2), (w.h / 2) switch a { case LFAlignTopLeft: return p.Add(Coord{X: leftX, Y: topY}) case LFAlignTop: return p.Add(Coord{X: midX - (wd.GetW() / 2), Y: topY}) case LFAlignTopRight: return p.Add(Coord{X: rightX - wd.GetW(), Y: topY}) case LFAlignLeft: return p.Add(Coord{X: leftX, Y: midY - (wd.GetH() / 2)}) case LFAlignCenter: return p.Add(Coord{X: midX - (wd.GetW() / 2), Y: midY - (wd.GetH() / 2)}) case LFAlignRight: return p.Add(Coord{X: rightX - wd.GetW(), Y: midY - (wd.GetH() / 2)}) case LFAlignBottomLeft: return p.Add(Coord{X: leftX, Y: bottomY - wd.GetH()}) case LFAlignBottom: return p.Add(Coord{X: midX - (wd.GetW() / 2), Y: bottomY - wd.GetH()}) case LFAlignBottomRight: return p.Add(Coord{X: rightX - wd.GetW(), Y: bottomY - wd.GetH()}) } return p } func (w *AbsoluteLayout) getAbsPos(wd Widget) Coord { rel := w.getRelPos(wd) return rel.Add(Coord{X: w.x, Y: w.y}) } func (w *AbsoluteLayout) DeleteIndex(idx int) { if idx < len(w.widgets) { p := w.widgets[idx] w.widgets = append(w.widgets[:idx], w.widgets[idx+1:]...) delete(w.wCoords, p) delete(w.wAnchor, p) } } func (w *AbsoluteLayout) Delete(n Widget) { for i := 0; i < len(w.widgets); i++ { if w.widgets[i] == n { w.DeleteIndex(i) return } } } func (w *AbsoluteLayout) IndexOf(n Widget) int { for i := range w.widgets { if w.widgets[i] == n { return i } } return -1 } func (w *AbsoluteLayout) ActivateWidget(n Widget) { for i := range w.widgets { w.widgets[i].SetActive(w.widgets[i] == n) } } func (w *AbsoluteLayout) FindById(id string) Widget { for i := range w.widgets { if w.widgets[i].Id() == id { return w.widgets[i] } } return nil } func (w *AbsoluteLayout) Replace(n, with Widget) { idx := w.IndexOf(n) if idx == -1 { // 'n' isn't in layout. Bail out. return } coords := w.wCoords[n] anchor := w.wAnchor[n] w.Delete(n) w.AddAnchored(with, coords, anchor) }