github.com/Seikaijyu/gio@v0.0.1/layout/context.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package layout
     4  
     5  import (
     6  	"image"
     7  	"time"
     8  
     9  	"github.com/Seikaijyu/gio/io/event"
    10  	"github.com/Seikaijyu/gio/io/system"
    11  	"github.com/Seikaijyu/gio/op"
    12  	"github.com/Seikaijyu/gio/unit"
    13  )
    14  
    15  // Context carries the state needed by almost all layouts and widgets.
    16  // A zero value Context never returns events, map units to pixels
    17  // with a scale of 1.0, and returns the zero time from Now.
    18  type Context struct {
    19  	// Constraints track the constraints for the active widget or
    20  	// layout.
    21  	Constraints Constraints
    22  
    23  	Metric unit.Metric
    24  	// By convention, a nil Queue is a signal to widgets to draw themselves
    25  	// in a disabled state.
    26  	Queue event.Queue
    27  	// Now is the animation time.
    28  	Now time.Time
    29  
    30  	// Locale provides information on the system's language preferences.
    31  	// BUG(whereswaldon): this field is not currently populated automatically.
    32  	// Interested users must look up and populate these values manually.
    33  	Locale system.Locale
    34  
    35  	*op.Ops
    36  }
    37  
    38  // NewContext is a shorthand for
    39  //
    40  //	Context{
    41  //	  Ops: ops,
    42  //	  Now: e.Now,
    43  //	  Queue: e.Queue,
    44  //	  Config: e.Config,
    45  //	  Constraints: Exact(e.Size),
    46  //	}
    47  //
    48  // NewContext calls ops.Reset and adjusts ops for e.Insets.
    49  func NewContext(ops *op.Ops, e system.FrameEvent) Context {
    50  	ops.Reset()
    51  
    52  	size := e.Size
    53  
    54  	if e.Insets != (system.Insets{}) {
    55  		left := e.Metric.Dp(e.Insets.Left)
    56  		top := e.Metric.Dp(e.Insets.Top)
    57  		op.Offset(image.Point{
    58  			X: left,
    59  			Y: top,
    60  		}).Add(ops)
    61  
    62  		size.X -= left + e.Metric.Dp(e.Insets.Right)
    63  		size.Y -= top + e.Metric.Dp(e.Insets.Bottom)
    64  	}
    65  
    66  	return Context{
    67  		Ops:         ops,
    68  		Now:         e.Now,
    69  		Queue:       e.Queue,
    70  		Metric:      e.Metric,
    71  		Constraints: Exact(size),
    72  	}
    73  }
    74  
    75  // Dp 函数将单位 Dp 转换为像素。
    76  func (c Context) Dp(v unit.Dp) int {
    77  	return c.Metric.Dp(v)
    78  }
    79  
    80  // Sp 函数将单位 Sp 转换为像素。
    81  func (c Context) Sp(v unit.Sp) int {
    82  	return c.Metric.Sp(v)
    83  }
    84  
    85  // Events 返回可用于键的事件。如果没有配置队列,Events 返回 nil。
    86  func (c Context) Events(k event.Tag) []event.Event {
    87  	if c.Queue == nil {
    88  		return nil
    89  	}
    90  	return c.Queue.Events(k)
    91  }
    92  
    93  // Disabled 返回此上下文的一个副本,副本中的队列为 nil,可以阻止事件传递到使用它的小部件。
    94  //
    95  // 按照惯例,nil 队列是指示小部件以禁用状态绘制自身的信号。
    96  func (c Context) Disabled() Context {
    97  	c.Queue = nil
    98  	return c
    99  }