doc.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // SPDX-License-Identifier: Unlicense OR MIT
  2. /*
  3. Package layout implements layouts common to GUI programs.
  4. # Constraints and dimensions
  5. Constraints and dimensions form the interface between layouts and
  6. interface child elements. This package operates on Widgets, functions
  7. that compute Dimensions from a a set of constraints for acceptable
  8. widths and heights. Both the constraints and dimensions are maintained
  9. in an implicit Context to keep the Widget declaration short.
  10. For example, to add space above a widget:
  11. var gtx layout.Context
  12. // Configure a top inset.
  13. inset := layout.Inset{Top: 8, ...}
  14. // Use the inset to lay out a widget.
  15. inset.Layout(gtx, func() {
  16. // Lay out widget and determine its size given the constraints
  17. // in gtx.Constraints.
  18. ...
  19. return layout.Dimensions{...}
  20. })
  21. Note that the example does not generate any garbage even though the
  22. Inset is transient. Layouts that don't accept user input are designed
  23. to not escape to the heap during their use.
  24. Layout operations are recursive: a child in a layout operation can
  25. itself be another layout. That way, complex user interfaces can
  26. be created from a few generic layouts.
  27. This example both aligns and insets a child:
  28. inset := layout.Inset{...}
  29. inset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
  30. align := layout.Alignment(...)
  31. return align.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
  32. return widget.Layout(gtx, ...)
  33. })
  34. })
  35. More complex layouts such as Stack and Flex lay out multiple children,
  36. and stateful layouts such as List accept user input.
  37. */
  38. package layout