gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/layout/context.go (about)

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