mirror of
https://github.com/rivo/tview.git
synced 2026-04-26 13:25:51 +03:00
[GH-ISSUE #1000] What is the correct way to programmatically change CurrentNode in TreeView #722
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/tview#722
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @jnathanh on GitHub (Jun 17, 2024).
Original GitHub issue: https://github.com/rivo/tview/issues/1000
I need to programmatically change the current node of a TreeView and am not able to find a recommended way of doing it. I got it to work by directly calling
app.Draw(), but saw in the function comments that it can deadlock the application, so want to learn if there is a better way to get the same result.My use-case is that I'm building a side-by-side differ with a TreeView on the left and right, and I want to set an addition line on the right tree as current node when the associated removal line on the left is selected as current node.
The example below shows the type of behavior I want with a single tree for simplicity.
@Macmod commented on GitHub (Jun 18, 2024):
According to https://github.com/rivo/tview/wiki/Concurrency, calling
app.Draw()is always safe inside a goroutine. I think your implementation is fine?@jnathanh commented on GitHub (Jun 18, 2024):
Thanks @Macmod. I missed that link in the function comment.
I did a little more reading and sharing here in case its useful to others. I think the advice can be summed up as follows.
In a callback (main thread), use
ForceDraw()In a goroutine, always use
Draw()In the Concurrency section of the wiki it says:
Based on the Application.Draw() comment and docs.go, I think this is implying
The deadlock warning in the function comment says:
I think this is because
DrawcallsQueueUpdate, which is writing to a channel that is only emptied in the main thread. If the channel buffer is full, thenQueueUpdatewill block the main thread until the buffer is emptied, which never happens because the channel write blocks the next invocation of the channel read (and emptying of the buffer).QueueUpdate
Run
I also found the ForceDraw() function, which does not use QueueUpdate. Even though it says
again, I think it is implied to only mean "in a goroutine", because in the main thread
ForceDrawwould avoid deadlocks compared toDrawwhich could cause them.Do these conclusions look correct?
I will go ahead and close the issue because I think my question was answered.