[PR #1103] Improving Grid's focus handling and primitive nil checks #1126

Open
opened 2026-03-04 01:09:34 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/rivo/tview/pull/1103
Author: @zveinn
Created: 7/8/2025
Status: 🔄 Open

Base: masterHead: clearing-grid-focus


📝 Commits (2)

  • 7257a43 Clearing grid focus when a new focused item is added
  • c35c049 adding nil check for p

📊 Changes

1 file changed (+10 additions, -0 deletions)

View changed files

📝 grid.go (+10 -0)

📄 Description

Hey, love the library. I was playing around with the Grid view and noticed that the Grid.AddItem wouldn't respect the focus boolean sometimes. Then I checked the Draw method for the Grid and noticed that only the one element is picked for focus during a draw and it's not necessarily the latest one added.

A small sidequest: When a nil primitive is added to the Grid, it will panic when trying to deal with inputHandling. So I just added a check for p == nil to prevent nil primitives from being added to the table. This way, whomever is using this library can just insert things into the Grid without having to nil check all over the place.

You can test this by making a split pane grid and then adding two tables into each pane. It'll work the first time you call AddItem on any Primitive, but subsiquent AddItems call will mess up the focus. (maybe i'm using it wrong ?)

my test repo: https://github.com/zveinn/term-test

	// Calculate primitive positions.
	var focus *gridItem // The item which has focus.
	for _, item := range items {
		px := columnPos[item.Column]
		py := rowPos[item.Row]
		var pw, ph int
		for index := 0; index < item.Height; index++ {
			ph += rowHeight[item.Row+index]
		}
		for index := 0; index < item.Width; index++ {
			pw += columnWidth[item.Column+index]
		}
		if g.borders {
			pw += item.Width - 1
			ph += item.Height - 1
		} else {
			pw += (item.Width - 1) * g.gapColumns
			ph += (item.Height - 1) * g.gapRows
		}
		item.x, item.y, item.w, item.h = px, py, pw, ph
		item.visible = true
		if item.Item.HasFocus() {
			focus = item
		}
	}

So I wrote a small loop to clear focus on all elements to force focus on the added item (only if focus is true)

I'm not sure if it's intended to have multiple elements within a grid that can have focus at the same time, if so then feel free to close the PR.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/rivo/tview/pull/1103 **Author:** [@zveinn](https://github.com/zveinn) **Created:** 7/8/2025 **Status:** 🔄 Open **Base:** `master` ← **Head:** `clearing-grid-focus` --- ### 📝 Commits (2) - [`7257a43`](https://github.com/rivo/tview/commit/7257a4373c1a4dab54b5922f975644d492d12c89) Clearing grid focus when a new focused item is added - [`c35c049`](https://github.com/rivo/tview/commit/c35c04979c58651b2b2f65decdbce7a5382c5a51) adding nil check for p ### 📊 Changes **1 file changed** (+10 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `grid.go` (+10 -0) </details> ### 📄 Description Hey, love the library. I was playing around with the Grid view and noticed that the Grid.AddItem wouldn't respect the focus boolean sometimes. Then I checked the Draw method for the Grid and noticed that only the one element is picked for focus during a draw and it's not necessarily the latest one added. A small sidequest: When a nil primitive is added to the Grid, it will panic when trying to deal with inputHandling. So I just added a check for p == nil to prevent nil primitives from being added to the table. This way, whomever is using this library can just insert things into the Grid without having to nil check all over the place. You can test this by making a split pane grid and then adding two tables into each pane. It'll work the first time you call AddItem on any Primitive, but subsiquent AddItems call will mess up the focus. (maybe i'm using it wrong ?) my test repo: https://github.com/zveinn/term-test ```go // Calculate primitive positions. var focus *gridItem // The item which has focus. for _, item := range items { px := columnPos[item.Column] py := rowPos[item.Row] var pw, ph int for index := 0; index < item.Height; index++ { ph += rowHeight[item.Row+index] } for index := 0; index < item.Width; index++ { pw += columnWidth[item.Column+index] } if g.borders { pw += item.Width - 1 ph += item.Height - 1 } else { pw += (item.Width - 1) * g.gapColumns ph += (item.Height - 1) * g.gapRows } item.x, item.y, item.w, item.h = px, py, pw, ph item.visible = true if item.Item.HasFocus() { focus = item } } ``` So I wrote a small loop to clear focus on all elements to force focus on the added item (only if focus is true) I'm not sure if it's intended to have multiple elements within a grid that can have focus at the same time, if so then feel free to close the PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#1126
No description provided.