1
0
mirror of https://github.com/rivo/tview.git synced 2025-05-08 19:29:39 +08:00

fix(grid): order of adding items should not affect drawing

This commit is contained in:
Har Jing Daryl 2024-05-24 10:26:33 +08:00 committed by GitHub
parent 0ac5f73025
commit 8b81388d2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

10
grid.go
View File

@ -272,8 +272,8 @@ func (g *Grid) Draw(screen tcell.Screen) {
x, y, width, height := g.GetInnerRect() x, y, width, height := g.GetInnerRect()
screenWidth, screenHeight := screen.Size() screenWidth, screenHeight := screen.Size()
// Make a list of items which apply. // Make a map of items that apply.
items := make([]*gridItem, 0, len(g.items)) items := map[Primitive]*gridItem{}
ItemLoop: ItemLoop:
for _, item := range g.items { for _, item := range g.items {
item.visible = false item.visible = false
@ -282,7 +282,7 @@ ItemLoop:
} }
// Check for overlaps and multiple layouts of the same item. // Check for overlaps and multiple layouts of the same item.
for index, existing := range items { for _, existing := range items {
// Do they overlap or are identical? // Do they overlap or are identical?
if item.Item != existing.Item && if item.Item != existing.Item &&
(item.Row >= existing.Row+existing.Height || item.Row+item.Height <= existing.Row || (item.Row >= existing.Row+existing.Height || item.Row+item.Height <= existing.Row ||
@ -304,12 +304,12 @@ ItemLoop:
if itemMin < existingMin { if itemMin < existingMin {
continue ItemLoop // This one isn't. Drop it. continue ItemLoop // This one isn't. Drop it.
} }
items[index] = item // This one is. Replace the other. items[item.Item] = item // This one is. Replace the other.
continue ItemLoop continue ItemLoop
} }
// This item will be visible. // This item will be visible.
items = append(items, item) items[item.Item] = item
} }
// How many rows and columns do we have? // How many rows and columns do we have?