context.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // SPDX-License-Identifier: Unlicense OR MIT
  2. package layout
  3. import (
  4. "time"
  5. "gioui.org/io/event"
  6. "gioui.org/io/input"
  7. "gioui.org/io/system"
  8. "gioui.org/op"
  9. "gioui.org/unit"
  10. )
  11. // Context carries the state needed by almost all layouts and widgets.
  12. // A zero value Context never returns events, map units to pixels
  13. // with a scale of 1.0, and returns the zero time from Now.
  14. type Context struct {
  15. // Constraints track the constraints for the active widget or
  16. // layout.
  17. Constraints Constraints
  18. Metric unit.Metric
  19. // Now is the animation time.
  20. Now time.Time
  21. // Locale provides information on the system's language preferences.
  22. // BUG(whereswaldon): this field is not currently populated automatically.
  23. // Interested users must look up and populate these values manually.
  24. Locale system.Locale
  25. disabled bool
  26. input.Source
  27. *op.Ops
  28. }
  29. // Dp converts v to pixels.
  30. func (c Context) Dp(v unit.Dp) int {
  31. return c.Metric.Dp(v)
  32. }
  33. // Sp converts v to pixels.
  34. func (c Context) Sp(v unit.Sp) int {
  35. return c.Metric.Sp(v)
  36. }
  37. func (c Context) Event(filters ...event.Filter) (event.Event, bool) {
  38. if c.disabled {
  39. return nil, false
  40. }
  41. return c.Source.Event(filters...)
  42. }
  43. // Enabled reports whether this context is enabled. Disabled contexts
  44. // don't report events.
  45. func (c Context) Enabled() bool {
  46. return !c.disabled
  47. }
  48. // Disabled returns a copy of this context that don't deliver any events.
  49. func (c Context) Disabled() Context {
  50. c.disabled = true
  51. return c
  52. }