...

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

Documentation: code.rocketnine.space/tslocum/cview

     1  package cview
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/gdamore/tcell/v2"
     7  )
     8  
     9  // Configuration values.
    10  const (
    11  	FlexRow = iota
    12  	FlexColumn
    13  )
    14  
    15  // flexItem holds layout options for one item.
    16  type flexItem struct {
    17  	Item       Primitive // The item to be positioned. May be nil for an empty item.
    18  	FixedSize  int       // The item's fixed size which may not be changed, 0 if it has no fixed size.
    19  	Proportion int       // The item's proportion.
    20  	Focus      bool      // Whether or not this item attracts the layout's focus.
    21  }
    22  
    23  // Flex is a basic implementation of the Flexbox layout. The contained
    24  // primitives are arranged horizontally or vertically. The way they are
    25  // distributed along that dimension depends on their layout settings, which is
    26  // either a fixed length or a proportional length. See AddItem() for details.
    27  type Flex struct {
    28  	*Box
    29  
    30  	// The items to be positioned.
    31  	items []*flexItem
    32  
    33  	// FlexRow or FlexColumn.
    34  	direction int
    35  
    36  	// If set to true, Flex will use the entire screen as its available space
    37  	// instead its box dimensions.
    38  	fullScreen bool
    39  
    40  	sync.RWMutex
    41  }
    42  
    43  // NewFlex returns a new flexbox layout container with no primitives and its
    44  // direction set to FlexColumn. To add primitives to this layout, see AddItem().
    45  // To change the direction, see SetDirection().
    46  //
    47  // Note that Flex will have a transparent background by default so that any nil
    48  // flex items will show primitives behind the Flex.
    49  // To disable this transparency:
    50  //
    51  //   flex.SetBackgroundTransparent(false)
    52  func NewFlex() *Flex {
    53  	f := &Flex{
    54  		Box:       NewBox(),
    55  		direction: FlexColumn,
    56  	}
    57  	f.SetBackgroundTransparent(true)
    58  	f.focus = f
    59  	return f
    60  }
    61  
    62  // GetDirection returns the direction in which the contained primitives are
    63  // distributed. This can be either FlexColumn (default) or FlexRow.
    64  func (f *Flex) GetDirection() int {
    65  	f.RLock()
    66  	defer f.RUnlock()
    67  	return f.direction
    68  }
    69  
    70  // SetDirection sets the direction in which the contained primitives are
    71  // distributed. This can be either FlexColumn (default) or FlexRow.
    72  func (f *Flex) SetDirection(direction int) {
    73  	f.Lock()
    74  	defer f.Unlock()
    75  
    76  	f.direction = direction
    77  }
    78  
    79  // SetFullScreen sets the flag which, when true, causes the flex layout to use
    80  // the entire screen space instead of whatever size it is currently assigned to.
    81  func (f *Flex) SetFullScreen(fullScreen bool) {
    82  	f.Lock()
    83  	defer f.Unlock()
    84  
    85  	f.fullScreen = fullScreen
    86  }
    87  
    88  // AddItem adds a new item to the container. The "fixedSize" argument is a width
    89  // or height that may not be changed by the layout algorithm. A value of 0 means
    90  // that its size is flexible and may be changed. The "proportion" argument
    91  // defines the relative size of the item compared to other flexible-size items.
    92  // For example, items with a proportion of 2 will be twice as large as items
    93  // with a proportion of 1. The proportion must be at least 1 if fixedSize == 0
    94  // (ignored otherwise).
    95  //
    96  // If "focus" is set to true, the item will receive focus when the Flex
    97  // primitive receives focus. If multiple items have the "focus" flag set to
    98  // true, the first one will receive focus.
    99  //
   100  // A nil value for the primitive represents empty space.
   101  func (f *Flex) AddItem(item Primitive, fixedSize, proportion int, focus bool) {
   102  	f.Lock()
   103  	defer f.Unlock()
   104  
   105  	if item == nil {
   106  		item = NewBox()
   107  		item.SetVisible(false)
   108  	}
   109  
   110  	f.items = append(f.items, &flexItem{Item: item, FixedSize: fixedSize, Proportion: proportion, Focus: focus})
   111  }
   112  
   113  // AddItemAtIndex adds an item to the flex at a given index.
   114  // For more information see AddItem.
   115  func (f *Flex) AddItemAtIndex(index int, item Primitive, fixedSize, proportion int, focus bool) {
   116  	f.Lock()
   117  	defer f.Unlock()
   118  	newItem := &flexItem{Item: item, FixedSize: fixedSize, Proportion: proportion, Focus: focus}
   119  
   120  	if index == 0 {
   121  		f.items = append([]*flexItem{newItem}, f.items...)
   122  	} else {
   123  		f.items = append(f.items[:index], append([]*flexItem{newItem}, f.items[index:]...)...)
   124  	}
   125  }
   126  
   127  // RemoveItem removes all items for the given primitive from the container,
   128  // keeping the order of the remaining items intact.
   129  func (f *Flex) RemoveItem(p Primitive) {
   130  	f.Lock()
   131  	defer f.Unlock()
   132  
   133  	for index := len(f.items) - 1; index >= 0; index-- {
   134  		if f.items[index].Item == p {
   135  			f.items = append(f.items[:index], f.items[index+1:]...)
   136  		}
   137  	}
   138  }
   139  
   140  // ResizeItem sets a new size for the item(s) with the given primitive. If there
   141  // are multiple Flex items with the same primitive, they will all receive the
   142  // same size. For details regarding the size parameters, see AddItem().
   143  func (f *Flex) ResizeItem(p Primitive, fixedSize, proportion int) {
   144  	f.Lock()
   145  	defer f.Unlock()
   146  
   147  	for _, item := range f.items {
   148  		if item.Item == p {
   149  			item.FixedSize = fixedSize
   150  			item.Proportion = proportion
   151  		}
   152  	}
   153  }
   154  
   155  // Draw draws this primitive onto the screen.
   156  func (f *Flex) Draw(screen tcell.Screen) {
   157  	if !f.GetVisible() {
   158  		return
   159  	}
   160  
   161  	f.Box.Draw(screen)
   162  
   163  	f.Lock()
   164  	defer f.Unlock()
   165  
   166  	// Calculate size and position of the items.
   167  
   168  	// Do we use the entire screen?
   169  	if f.fullScreen {
   170  		width, height := screen.Size()
   171  		f.SetRect(0, 0, width, height)
   172  	}
   173  
   174  	// How much space can we distribute?
   175  	x, y, width, height := f.GetInnerRect()
   176  	var proportionSum int
   177  	distSize := width
   178  	if f.direction == FlexRow {
   179  		distSize = height
   180  	}
   181  	for _, item := range f.items {
   182  		if item.FixedSize > 0 {
   183  			distSize -= item.FixedSize
   184  		} else {
   185  			proportionSum += item.Proportion
   186  		}
   187  	}
   188  
   189  	// Calculate positions and draw items.
   190  	pos := x
   191  	if f.direction == FlexRow {
   192  		pos = y
   193  	}
   194  	for _, item := range f.items {
   195  		size := item.FixedSize
   196  		if size <= 0 {
   197  			if proportionSum > 0 {
   198  				size = distSize * item.Proportion / proportionSum
   199  				distSize -= size
   200  				proportionSum -= item.Proportion
   201  			} else {
   202  				size = 0
   203  			}
   204  		}
   205  		if item.Item != nil {
   206  			if f.direction == FlexColumn {
   207  				item.Item.SetRect(pos, y, size, height)
   208  			} else {
   209  				item.Item.SetRect(x, pos, width, size)
   210  			}
   211  		}
   212  		pos += size
   213  
   214  		if item.Item != nil {
   215  			if item.Item.GetFocusable().HasFocus() {
   216  				defer item.Item.Draw(screen)
   217  			} else {
   218  				item.Item.Draw(screen)
   219  			}
   220  		}
   221  	}
   222  }
   223  
   224  // Focus is called when this primitive receives focus.
   225  func (f *Flex) Focus(delegate func(p Primitive)) {
   226  	f.Lock()
   227  
   228  	for _, item := range f.items {
   229  		if item.Item != nil && item.Focus {
   230  			f.Unlock()
   231  			delegate(item.Item)
   232  			return
   233  		}
   234  	}
   235  
   236  	f.Unlock()
   237  }
   238  
   239  // HasFocus returns whether or not this primitive has focus.
   240  func (f *Flex) HasFocus() bool {
   241  	f.RLock()
   242  	defer f.RUnlock()
   243  
   244  	for _, item := range f.items {
   245  		if item.Item != nil && item.Item.GetFocusable().HasFocus() {
   246  			return true
   247  		}
   248  	}
   249  	return false
   250  }
   251  
   252  // MouseHandler returns the mouse handler for this primitive.
   253  func (f *Flex) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
   254  	return f.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
   255  		if !f.InRect(event.Position()) {
   256  			return false, nil
   257  		}
   258  
   259  		// Pass mouse events along to the first child item that takes it.
   260  		for _, item := range f.items {
   261  			if item.Item == nil {
   262  				continue
   263  			}
   264  
   265  			consumed, capture = item.Item.MouseHandler()(action, event, setFocus)
   266  			if consumed {
   267  				return
   268  			}
   269  		}
   270  
   271  		return
   272  	})
   273  }
   274  

View as plain text