...

Source file src/code.rocketnine.space/tslocum/cview/doc_test.go

Documentation: code.rocketnine.space/tslocum/cview

     1  package cview
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/gdamore/tcell/v2"
     7  )
     8  
     9  // Example of an application with multiple layouts.
    10  func ExampleNewApplication() {
    11  	// Initialize application.
    12  	app := NewApplication()
    13  	// Handle panics gracefully.
    14  	defer app.HandlePanic()
    15  
    16  	// Create shared TextView.
    17  	sharedTextView := NewTextView()
    18  	sharedTextView.SetTextAlign(AlignCenter)
    19  	sharedTextView.SetText("Widgets may be re-used between multiple layouts.")
    20  
    21  	// Create main layout using Grid.
    22  	mainTextView := NewTextView()
    23  	mainTextView.SetTextAlign(AlignCenter)
    24  	mainTextView.SetText("This is mainLayout.\n\nPress <Tab> to view aboutLayout.")
    25  
    26  	mainLayout := NewGrid()
    27  	mainLayout.AddItem(mainTextView, 0, 0, 1, 1, 0, 0, false)
    28  	mainLayout.AddItem(sharedTextView, 1, 0, 1, 1, 0, 0, false)
    29  
    30  	// Create about layout using Grid.
    31  	aboutTextView := NewTextView()
    32  	aboutTextView.SetTextAlign(AlignCenter)
    33  	aboutTextView.SetText("cview muti-layout application example\n\nhttps://code.rocketnine.space/tslocum/cview")
    34  
    35  	aboutLayout := NewGrid()
    36  	aboutLayout.AddItem(aboutTextView, 0, 0, 1, 1, 0, 0, false)
    37  	aboutLayout.AddItem(sharedTextView, 1, 0, 1, 1, 0, 0, false)
    38  
    39  	// Track the current layout.
    40  	currentLayout := 0
    41  
    42  	// Set an input capture function that switches between layouts when the tab
    43  	// key is pressed.
    44  	app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
    45  		if event.Key() == tcell.KeyTab {
    46  			if currentLayout == 0 {
    47  				currentLayout = 1
    48  
    49  				app.SetRoot(aboutLayout, true)
    50  			} else {
    51  				currentLayout = 0
    52  
    53  				app.SetRoot(mainLayout, true)
    54  			}
    55  
    56  			// Return nil to stop propagating the event to any remaining
    57  			// handlers.
    58  			return nil
    59  		}
    60  
    61  		// Return the event to continue propagating it.
    62  		return event
    63  	})
    64  
    65  	// Run the application.
    66  	app.SetRoot(mainLayout, true)
    67  	if err := app.Run(); err != nil {
    68  		panic(err)
    69  	}
    70  }
    71  
    72  // Example of an application with mouse support.
    73  func ExampleApplication_EnableMouse() {
    74  	// Initialize application.
    75  	app := NewApplication()
    76  	// Handle panics gracefully.
    77  	defer app.HandlePanic()
    78  
    79  	// Enable mouse support.
    80  	app.EnableMouse(true)
    81  
    82  	// Enable double clicks.
    83  	app.SetDoubleClickInterval(StandardDoubleClick)
    84  
    85  	// Create a textview.
    86  	tv := NewTextView()
    87  	tv.SetText("Click somewhere!")
    88  
    89  	// Set a mouse capture function which prints where the mouse was clicked.
    90  	app.SetMouseCapture(func(event *tcell.EventMouse, action MouseAction) (*tcell.EventMouse, MouseAction) {
    91  		if action == MouseLeftClick || action == MouseLeftDoubleClick {
    92  			actionLabel := "click"
    93  			if action == MouseLeftDoubleClick {
    94  				actionLabel = "double-click"
    95  			}
    96  
    97  			x, y := event.Position()
    98  
    99  			fmt.Fprintf(tv, "\nYou %sed at %d,%d! Amazing!", actionLabel, x, y)
   100  
   101  			// Return nil to stop propagating the event to any remaining handlers.
   102  			return nil, 0
   103  		}
   104  
   105  		// Return the event to continue propagating it.
   106  		return event, action
   107  	})
   108  
   109  	// Run the application.
   110  	app.SetRoot(tv, true)
   111  	if err := app.Run(); err != nil {
   112  		panic(err)
   113  	}
   114  }
   115  

View as plain text