intslider.go raw
1 package gel
2
3 import (
4 "fmt"
5
6 l "github.com/p9c/gio/layout"
7 )
8
9 type IntSlider struct {
10 *Window
11 min, max *Clickable
12 floater *Float
13 hook func(int)
14 value int
15 minV, maxV float32
16 }
17
18 func (w *Window) IntSlider() *IntSlider {
19 return &IntSlider{
20 Window: w,
21 min: w.Clickable(),
22 max: w.Clickable(),
23 floater: w.Float(),
24 hook: func(int) {},
25 }
26 }
27
28 func (i *IntSlider) Min(min float32) *IntSlider {
29 i.minV = min
30 return i
31 }
32
33 func (i *IntSlider) Max(max float32) *IntSlider {
34 i.maxV = max
35 return i
36 }
37
38 func (i *IntSlider) Hook(fn func(v int)) *IntSlider {
39 i.hook = fn
40 return i
41 }
42
43 func (i *IntSlider) Value(value int) *IntSlider {
44 i.value = value
45 i.floater.SetValue(float32(value))
46 return i
47 }
48
49 func (i *IntSlider) GetValue() int {
50 return int(i.floater.Value() + 0.5)
51 }
52
53 func (i *IntSlider) Fn(gtx l.Context) l.Dimensions {
54 return i.Flex().Rigid(
55 i.Button(
56 i.min.SetClick(func() {
57 i.floater.SetValue(i.minV)
58 i.hook(int(i.minV))
59 })).
60 Inset(0.25).
61 Color("Primary").
62 Background("Transparent").
63 Font("bariol regular").
64 Text("0").
65 Fn,
66 ).Flexed(1,
67 i.Inset(0.25,
68 i.Slider().
69 Float(i.floater.SetHook(func(fl float32) {
70 iFl := int(fl + 0.5)
71 i.value = iFl
72 i.floater.SetValue(float32(iFl))
73 i.hook(iFl)
74 })).
75 Min(i.minV).Max(i.maxV).
76 Fn,
77 ).Fn,
78 ).Rigid(
79 i.Button(
80 i.max.SetClick(func() {
81 i.floater.SetValue(i.maxV)
82 i.hook(int(i.maxV))
83 })).
84 Inset(0.25).
85 Color("Primary").
86 Background("Transparent").
87 Font("bariol regular").
88 Text(fmt.Sprint(int(i.maxV))).
89 Fn,
90 ).Fn(gtx)
91 }
92