()
| 84 | } |
| 85 | |
| 86 | func (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 |
nothing calls this directly
no test coverage detected