...

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  	maxWidth         int
    12  	maxHeight        int
    13  }
    14  
    15  // NewFrame returns a new Frame widget.
    16  func NewFrame(w ...Widget) *Frame {
    17  	f := &Frame{
    18  		Box: NewBox(),
    19  	}
    20  	f.AddChild(w...)
    21  	return f
    22  }
    23  
    24  // SetPadding sets the amount of padding around widgets in the frame.
    25  func (f *Frame) SetPadding(padding int) {
    26  	f.Lock()
    27  	defer f.Unlock()
    28  
    29  	f.padding = padding
    30  	f.repositionAll()
    31  }
    32  
    33  // SetRect sets the position and size of the widget.
    34  func (f *Frame) SetRect(r image.Rectangle) {
    35  	f.Lock()
    36  	defer f.Unlock()
    37  
    38  	f.rect = r
    39  	f.repositionAll()
    40  }
    41  
    42  // SetPositionChildren sets a flag that determines whether child widgets are
    43  // repositioned when the Frame is repositioned.
    44  func (f *Frame) SetPositionChildren(position bool) {
    45  	f.Lock()
    46  	defer f.Unlock()
    47  
    48  	f.positionChildren = position
    49  	f.repositionAll()
    50  }
    51  
    52  // SetMaxWidth sets the maximum width of widgets within the frame. This will
    53  // only have an effect after SetPositionChildren(true) is called. 0 to disable.
    54  func (f *Frame) SetMaxWidth(w int) {
    55  	f.Lock()
    56  	defer f.Unlock()
    57  
    58  	f.maxWidth = w
    59  	f.repositionAll()
    60  }
    61  
    62  // SetMaxHeight sets the maximum height of widgets within the frame. This will
    63  // only have an effect after SetPositionChildren(true) is called. 0 to disable.
    64  func (f *Frame) SetMaxHeight(h int) {
    65  	f.Lock()
    66  	defer f.Unlock()
    67  
    68  	f.maxHeight = h
    69  	f.repositionAll()
    70  }
    71  
    72  // AddChild adds a child to the widget.
    73  func (f *Frame) AddChild(w ...Widget) {
    74  	f.Lock()
    75  	defer f.Unlock()
    76  
    77  	f.children = append(f.children, w...)
    78  
    79  	if !f.positionChildren {
    80  		return
    81  	}
    82  	for _, child := range w {
    83  		f.repositionChild(child)
    84  	}
    85  }
    86  
    87  func (f *Frame) repositionChild(w Widget) {
    88  	r := f.rect.Inset(f.padding)
    89  	if f.maxWidth > 0 && r.Dx() > f.maxWidth {
    90  		r.Max.X = r.Min.X + f.maxWidth
    91  	}
    92  	if f.maxHeight > 0 && r.Dy() > f.maxHeight {
    93  		r.Max.Y = r.Min.Y + f.maxHeight
    94  	}
    95  	w.SetRect(r)
    96  }
    97  
    98  func (f *Frame) repositionAll() {
    99  	if !f.positionChildren {
   100  		return
   101  	}
   102  	for _, w := range f.children {
   103  		f.repositionChild(w)
   104  	}
   105  }
   106  

View as plain text