gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/widget/bool.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package widget
     4  
     5  import (
     6  	"gioui.org/io/semantic"
     7  	"gioui.org/layout"
     8  )
     9  
    10  type Bool struct {
    11  	Value bool
    12  
    13  	clk Clickable
    14  }
    15  
    16  // Update the widget state and report whether Value was changed.
    17  func (b *Bool) Update(gtx layout.Context) bool {
    18  	changed := false
    19  	for b.clk.clicked(b, gtx) {
    20  		b.Value = !b.Value
    21  		changed = true
    22  	}
    23  	return changed
    24  }
    25  
    26  // Hovered reports whether pointer is over the element.
    27  func (b *Bool) Hovered() bool {
    28  	return b.clk.Hovered()
    29  }
    30  
    31  // Pressed reports whether pointer is pressing the element.
    32  func (b *Bool) Pressed() bool {
    33  	return b.clk.Pressed()
    34  }
    35  
    36  func (b *Bool) History() []Press {
    37  	return b.clk.History()
    38  }
    39  
    40  func (b *Bool) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions {
    41  	b.Update(gtx)
    42  	dims := b.clk.layout(b, gtx, func(gtx layout.Context) layout.Dimensions {
    43  		semantic.SelectedOp(b.Value).Add(gtx.Ops)
    44  		semantic.EnabledOp(gtx.Enabled()).Add(gtx.Ops)
    45  		return w(gtx)
    46  	})
    47  	return dims
    48  }