...

Source file src/code.rocket9labs.com/tslocum/etk/frame.go

Documentation: code.rocket9labs.com/tslocum/etk

     1  package etk
     2  
     3  import "image"
     4  
     5  // Frame is a widget container. All children are displayed at once. Children are
     6  // not repositioned by default. Repositioning may be enabled via SetPositionChildren.
     7  type Frame struct {
     8  	*Box
     9  	padding          int
    10  	positionChildren bool
    11  }
    12  
    13  // NewFrame returns a new Frame widget.
    14  func NewFrame(w ...Widget) *Frame {
    15  	f := &Frame{
    16  		Box: NewBox(),
    17  	}
    18  	f.AddChild(w...)
    19  	return f
    20  }
    21  
    22  // SetPadding sets the amount of padding around widgets in the frame.
    23  func (f *Frame) SetPadding(padding int) {
    24  	f.Lock()
    25  	defer f.Unlock()
    26  
    27  	f.padding = padding
    28  	f.reposition()
    29  }
    30  
    31  // SetRect sets the position and size of the widget.
    32  func (f *Frame) SetRect(r image.Rectangle) {
    33  	f.Lock()
    34  	defer f.Unlock()
    35  
    36  	f.rect = r
    37  	f.reposition()
    38  }
    39  
    40  // SetPositionChildren sets a flag that determines whether child widgets are
    41  // repositioned when the Frame is repositioned.
    42  func (f *Frame) SetPositionChildren(position bool) {
    43  	f.Lock()
    44  	defer f.Unlock()
    45  
    46  	f.positionChildren = position
    47  	f.reposition()
    48  }
    49  
    50  // AddChild adds a child to the widget.
    51  func (f *Frame) AddChild(w ...Widget) {
    52  	f.Lock()
    53  	defer f.Unlock()
    54  
    55  	f.children = append(f.children, w...)
    56  
    57  	if f.positionChildren {
    58  		r := f.rect.Inset(f.padding)
    59  		for _, wgt := range w {
    60  			wgt.SetRect(r)
    61  		}
    62  	}
    63  }
    64  
    65  func (f *Frame) reposition() {
    66  	if !f.positionChildren {
    67  		return
    68  	}
    69  	r := f.rect.Inset(f.padding)
    70  	for _, w := range f.children {
    71  		w.SetRect(r)
    72  	}
    73  }
    74  

View as plain text