MCPcopy Create free account
hub / github.com/cogentcore/core / Init

Method Init

core/switch.go:86–216  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

84}
85
86func (sw *Switch) Init() {
87 sw.Frame.Init()
88 sw.Styler(func(s *styles.Style) {
89 s.SetAbilities(true, abilities.Activatable, abilities.Focusable, abilities.Hoverable, abilities.Checkable)
90 if !sw.IsReadOnly() {
91 s.Cursor = cursors.Pointer
92 }
93 s.Text.Align = styles.Start
94 s.Text.AlignV = styles.Center
95 s.Padding.SetVertical(units.Dp(4))
96 s.Padding.SetHorizontal(units.Dp(ConstantSpacing(4))) // needed for layout issues
97 s.Border.Radius = styles.BorderRadiusSmall
98 s.Gap.Zero()
99 s.CenterAll()
100
101 if sw.Type == SwitchChip {
102 if s.Is(states.Checked) {
103 s.Background = colors.Scheme.SurfaceVariant
104 s.Color = colors.Scheme.OnSurfaceVariant
105 } else if !s.Is(states.Focused) {
106 s.Border.Width.Set(units.Dp(1))
107 }
108 }
109 if sw.Type == SwitchSegmentedButton {
110 if !s.Is(states.Focused) {
111 s.Border.Width.Set(units.Dp(1))
112 }
113 if s.Is(states.Checked) {
114 s.Background = colors.Scheme.SurfaceVariant
115 s.Color = colors.Scheme.OnSurfaceVariant
116 }
117 }
118
119 if s.Is(states.Selected) {
120 s.Background = colors.Scheme.Select.Container
121 }
122 })
123
124 sw.HandleClickOnEnterSpace()
125 sw.OnFinal(events.Click, func(e events.Event) {
126 sw.SetChecked(sw.IsChecked())
127 if sw.Type == SwitchChip || sw.Type == SwitchSegmentedButton {
128 sw.updateStackTop() // must update here
129 sw.NeedsLayout()
130 } else {
131 sw.NeedsRender()
132 }
133 sw.SendChange(e)
134 })
135
136 sw.Maker(func(p *tree.Plan) {
137 if sw.IconOn == "" {
138 sw.IconOn = icons.ToggleOnFill // fallback
139 }
140 if sw.IconOff == "" {
141 sw.IconOff = icons.ToggleOff // fallback
142 }
143

Callers

nothing calls this directly

Calls 15

SetCheckedMethod · 0.95
IsCheckedMethod · 0.95
updateStackTopMethod · 0.95
DpFunction · 0.92
AddAtFunction · 0.92
EmFunction · 0.92
ConstantSpacingFunction · 0.85
StylerMethod · 0.80
SetAbilitiesMethod · 0.80
SetVerticalMethod · 0.80
SetHorizontalMethod · 0.80
CenterAllMethod · 0.80

Tested by

no test coverage detected