[GH-ISSUE #1075] Redrawing the screen within SetFocusFunc or SetSelectionChangedFunc #779

Closed
opened 2026-03-04 01:07:41 +03:00 by kerem · 0 comments
Owner

Originally created by @lhs616 on GitHub (Feb 24, 2025).
Original GitHub issue: https://github.com/rivo/tview/issues/1075

I created two tables and set the focus to true on the first table. When I press the bottom button of the keyboard in the last row of the first table, the focus is set to the second table and I want to use the event in the second table. When the focus changes, I want to change the color of the table with focus True. Previously, I managed the table's ROW cursor as a separate variable, and when a keyboard event occurred, I managed the cursor variable in the SetInputCapture function, and when drawing the table, I drew it again using the Select() function and then drew it using the SetRoot method. However, since I wanted to utilize the Row, Col values ​​managed by the tableView, I tried to use the SetFocusFunc or SetSelectionChangedFunc function when the event occurred. The focus movement behavior works well, but when I Draw or SetRoot the screen again, a DeadLock occurs or the screen freezes. Is there no way to change the screen design in SetFocusFunc or SetSelectionChangedFunc and then draw the screen again?

table1.SetSelectionChangedFunc(func(row int, column int) {
	table1.SetBorderColor(tcell.ColorRed)
	table2.SetBorderColor(tcell.ColorDefault)
	app.QueueUpdateDraw(func() {}) //
	//app.SetRoot()
	//app.Draw()
})

table2.SetSelectionChangedFunc(func(row int, column int) {
	table2.SetBorderColor(tcell.ColorRed)
	table1.SetBorderColor(tcell.ColorDefault)
	app.QueueUpdateDraw(func() {}) //
	//app.SetRoot()
	//app.Draw()
})


table1.SetFocusFunc(func() {
	table1.SetBorderColor(tcell.ColorRed)
	table2.SetBorderColor(tcell.ColorDefault)
	app.QueueUpdateDraw(func() {})
	//app.SetRoot()
	//app.Draw()
})

table2.SetFocusFunc(func() {
	table1.SetBorderColor(tcell.ColorDefault)
	table2.SetBorderColor(tcell.ColorGreen)
	app.QueueUpdateDraw(func() {})
	//app.SetRoot()
	//app.Draw()
})


app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
	switch event.Key() {
	case tcell.KeyDown:
		row, _ := table1.GetSelection()
		if row >= len(data) {
			app.SetFocus(table2)
			return nil
		}
	case tcell.KeyUp:
		row, _ := table2.GetSelection()
		if row == 0 {
			app.SetFocus(table1)
			return nil
		}
	case tcell.KeyTab:
		if app.GetFocus() == table1 {
			app.SetFocus(table2)
		} else {
			app.SetFocus(table1)
		}
		return nil
	}
	return event
})

// layout
layout := tview.NewFlex().
	SetDirection(tview.FlexRow).
	AddItem(table1, 0, 1, true).
	AddItem(table2, 0, 1, false)

// app start
if err := app.SetRoot(layout, true).EnableMouse(true).Run(); err != nil {
	panic(err)
}

`

Originally created by @lhs616 on GitHub (Feb 24, 2025). Original GitHub issue: https://github.com/rivo/tview/issues/1075 I created two tables and set the focus to true on the first table. When I press the bottom button of the keyboard in the last row of the first table, the focus is set to the second table and I want to use the event in the second table. When the focus changes, I want to change the color of the table with focus True. Previously, I managed the table's ROW cursor as a separate variable, and when a keyboard event occurred, I managed the cursor variable in the SetInputCapture function, and when drawing the table, I drew it again using the Select() function and then drew it using the SetRoot method. However, since I wanted to utilize the Row, Col values ​​managed by the tableView, I tried to use the SetFocusFunc or SetSelectionChangedFunc function when the event occurred. The focus movement behavior works well, but when I Draw or SetRoot the screen again, a DeadLock occurs or the screen freezes. Is there no way to change the screen design in SetFocusFunc or SetSelectionChangedFunc and then draw the screen again? table1.SetSelectionChangedFunc(func(row int, column int) { table1.SetBorderColor(tcell.ColorRed) table2.SetBorderColor(tcell.ColorDefault) app.QueueUpdateDraw(func() {}) // //app.SetRoot() //app.Draw() }) table2.SetSelectionChangedFunc(func(row int, column int) { table2.SetBorderColor(tcell.ColorRed) table1.SetBorderColor(tcell.ColorDefault) app.QueueUpdateDraw(func() {}) // //app.SetRoot() //app.Draw() }) table1.SetFocusFunc(func() { table1.SetBorderColor(tcell.ColorRed) table2.SetBorderColor(tcell.ColorDefault) app.QueueUpdateDraw(func() {}) //app.SetRoot() //app.Draw() }) table2.SetFocusFunc(func() { table1.SetBorderColor(tcell.ColorDefault) table2.SetBorderColor(tcell.ColorGreen) app.QueueUpdateDraw(func() {}) //app.SetRoot() //app.Draw() }) app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { switch event.Key() { case tcell.KeyDown: row, _ := table1.GetSelection() if row >= len(data) { app.SetFocus(table2) return nil } case tcell.KeyUp: row, _ := table2.GetSelection() if row == 0 { app.SetFocus(table1) return nil } case tcell.KeyTab: if app.GetFocus() == table1 { app.SetFocus(table2) } else { app.SetFocus(table1) } return nil } return event }) // layout layout := tview.NewFlex(). SetDirection(tview.FlexRow). AddItem(table1, 0, 1, true). AddItem(table2, 0, 1, false) // app start if err := app.SetRoot(layout, true).EnableMouse(true).Run(); err != nil { panic(err) } `
kerem closed this issue 2026-03-04 01:07:41 +03:00
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#779
No description provided.