github.com/Seikaijyu/gio@v0.0.1/widget/material/switch.go (about) 1 // SPDX-License-Identifier: Unlicense OR MIT 2 3 package material 4 5 import ( 6 "image" 7 "image/color" 8 9 "github.com/Seikaijyu/gio/internal/f32color" 10 "github.com/Seikaijyu/gio/io/semantic" 11 "github.com/Seikaijyu/gio/layout" 12 "github.com/Seikaijyu/gio/op" 13 "github.com/Seikaijyu/gio/op/clip" 14 "github.com/Seikaijyu/gio/op/paint" 15 "github.com/Seikaijyu/gio/widget" 16 ) 17 18 type SwitchStyle struct { 19 Description string 20 Color struct { 21 Enabled color.NRGBA 22 Disabled color.NRGBA 23 Track color.NRGBA 24 } 25 Switch *widget.Bool 26 } 27 28 // Switch is for selecting a boolean value. 29 func Switch(th *Theme, swtch *widget.Bool, description string) SwitchStyle { 30 sw := SwitchStyle{ 31 Switch: swtch, 32 Description: description, 33 } 34 sw.Color.Enabled = th.Palette.ContrastBg 35 sw.Color.Disabled = th.Palette.Bg 36 sw.Color.Track = f32color.MulAlpha(th.Palette.Fg, 0x88) 37 return sw 38 } 39 40 // Layout updates the switch and displays it. 41 func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions { 42 s.Switch.Update(gtx) 43 trackWidth := gtx.Dp(36) 44 trackHeight := gtx.Dp(16) 45 thumbSize := gtx.Dp(20) 46 trackOff := (thumbSize - trackHeight) / 2 47 48 // Draw track. 49 trackCorner := trackHeight / 2 50 trackRect := image.Rectangle{Max: image.Point{ 51 X: trackWidth, 52 Y: trackHeight, 53 }} 54 col := s.Color.Disabled 55 if s.Switch.Value { 56 col = s.Color.Enabled 57 } 58 if gtx.Queue == nil { 59 col = f32color.Disabled(col) 60 } 61 trackColor := s.Color.Track 62 t := op.Offset(image.Point{Y: trackOff}).Push(gtx.Ops) 63 cl := clip.UniformRRect(trackRect, trackCorner).Push(gtx.Ops) 64 paint.ColorOp{Color: trackColor}.Add(gtx.Ops) 65 paint.PaintOp{}.Add(gtx.Ops) 66 cl.Pop() 67 t.Pop() 68 69 // Draw thumb ink. 70 inkSize := gtx.Dp(44) 71 rr := inkSize / 2 72 inkOff := image.Point{ 73 X: trackWidth/2 - rr, 74 Y: -rr + trackHeight/2 + trackOff, 75 } 76 t = op.Offset(inkOff).Push(gtx.Ops) 77 gtx.Constraints.Min = image.Pt(inkSize, inkSize) 78 t.Pop() 79 80 // Compute thumb offset. 81 if s.Switch.Value { 82 xoff := trackWidth - thumbSize 83 defer op.Offset(image.Point{X: xoff}).Push(gtx.Ops).Pop() 84 } 85 86 thumbRadius := thumbSize / 2 87 88 circle := func(x, y, r int) clip.Op { 89 b := image.Rectangle{ 90 Min: image.Pt(x-r, y-r), 91 Max: image.Pt(x+r, y+r), 92 } 93 return clip.Ellipse(b).Op(gtx.Ops) 94 } 95 // Draw hover. 96 if s.Switch.Hovered() || s.Switch.Focused() { 97 r := thumbRadius * 10 / 17 98 background := f32color.MulAlpha(s.Color.Enabled, 70) 99 paint.FillShape(gtx.Ops, background, circle(thumbRadius, thumbRadius, r)) 100 } 101 102 // Draw thumb shadow, a translucent disc slightly larger than the 103 // thumb itself. 104 // Center shadow horizontally and slightly adjust its Y. 105 paint.FillShape(gtx.Ops, argb(0x55000000), circle(thumbRadius, thumbRadius+gtx.Dp(.25), thumbRadius+1)) 106 107 // Draw thumb. 108 paint.FillShape(gtx.Ops, col, circle(thumbRadius, thumbRadius, thumbRadius)) 109 110 // Set up click area. 111 clickSize := gtx.Dp(40) 112 clickOff := image.Point{ 113 X: (thumbSize - clickSize) / 2, 114 Y: (trackHeight-clickSize)/2 + trackOff, 115 } 116 defer op.Offset(clickOff).Push(gtx.Ops).Pop() 117 sz := image.Pt(clickSize, clickSize) 118 defer clip.Ellipse(image.Rectangle{Max: sz}).Push(gtx.Ops).Pop() 119 s.Switch.Layout(gtx, func(gtx layout.Context) layout.Dimensions { 120 if d := s.Description; d != "" { 121 semantic.DescriptionOp(d).Add(gtx.Ops) 122 } 123 semantic.Switch.Add(gtx.Ops) 124 return layout.Dimensions{Size: sz} 125 }) 126 127 dims := image.Point{X: trackWidth, Y: thumbSize} 128 return layout.Dimensions{Size: dims} 129 }