Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instability/Inconsistency of grid, Newbutton Output #1071

Open
WhipMeHarder opened this issue Jan 13, 2025 · 0 comments
Open

Instability/Inconsistency of grid, Newbutton Output #1071

WhipMeHarder opened this issue Jan 13, 2025 · 0 comments

Comments

@WhipMeHarder
Copy link

The following experimental code is being reported - as seen below, which seems to produce component output instability and inconsistency. When used, there is no reliability that the movement or appearance of the selected button's blue background will follow the up and down input of the arrow keys from the keyboard. The top and first button is supposed to be selected. This happens intermittently, as does the selection of buttons when using the keyboard.

package main

import (
	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
	"fmt"
)

func main() {
    app := tview.NewApplication()

	// Create the buttons
	buttons := []string {"test 1", "test 2", "test 3", "test 4", "test 5"}
	grid := tview.NewGrid().SetColumns(45, 65, 45).SetRows(3, 3, 3, 3, 3, 3, 3, 3, 3, 3)

	// Add buttons to the grid
	for i, label := range buttons {
		grid.AddItem(tview.NewButton(label).SetBorder(true).SetTitle("[ "+label+" ]"), i, 0, 1, 1, 0, 0, false)
		
	}
	
	f := tview.NewInputField()
	f.SetBorder(true)
	f.SetTitle("Enter")

	g := tview.NewButton("Some text")	//.SetBorder(true)

	grid.AddItem(g, 5, 0, 1, 1, 0, 0, true)
	grid.AddItem(f, 6, 0, 1, 1, 0, 0, true)
	grid.AddItem(tview.NewTextView().SetText("Some small text").SetTextAlign(tview.AlignCenter), 7, 0, 1, 1, 0, 0, true)
	
	
	// Blue area button
	supr := tview.NewButton("test 1")
	supr.SetBackgroundColor(tcell.ColorBlue)
	supr.SetBorder(false)
	grid.AddItem(supr, 0, 0, 1, 1, 0, 0, true)

	currentRow := 0 // Track the current row of the blue area

	// Function to update the blue area position
	moveBlueArea := func(delta int) {
		newRow := currentRow + delta
		if newRow < 0 { newRow = 0 }
		if newRow >= len(buttons) { newRow = len(buttons) - 1}

		//if newRow >= 0 && newRow < len(buttons) {
			
			grid.RemoveItem(supr) // Remove from current position
			supr := tview.NewButton(buttons[newRow])
			grid.AddItem(supr, newRow, 0, 1, 1, 0, 0, true) // Add to new position
			
			currentRow = newRow
		//}
	}

	// Add the blue area initially
	grid.AddItem(supr, currentRow, 0, 1, 1, 0, 0, true)

	// Set up key handlers
	app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		switch event.Key() {
			case tcell.KeyUp:
				moveBlueArea(-1) // Move up
			case tcell.KeyDown:
				moveBlueArea(1) // Move down
			case tcell.KeyTAB:
				f.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey { fmt.Println("Key pressed:", e); return e })
			case tcell.KeyEnter:
				app.Stop() // Exit on Enter key
		}
		return event
	})

	// Set up the layout and run the application
	if err := app.SetRoot(grid, true).SetFocus(grid).Run(); err != nil {
		panic(err)
	}
}

Various experiments have been done to:

  1. Rewrite or rebuild the entire display - even with or without the exception of the selected button.
  2. To add or change the background colour properties, of each selected button, including those which are not; and then back to point number one.

However, regardless of what methods are used, the output results always remain the same and haphazard. Is there a way to make the interactive output stable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant