-
Notifications
You must be signed in to change notification settings - Fork 1
/
slider_linux.go
137 lines (118 loc) · 3.81 KB
/
slider_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package goey
import (
"unsafe"
"bitbucket.org/rj/goey/base"
"bitbucket.org/rj/goey/internal/syscall"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
type sliderElement struct {
Control
value float64
min, max float64
onChange func(float64)
shChange glib.SignalHandle
onFocus focusSlot
onBlur blurSlot
}
func (w *Slider) mount(parent base.Control) (base.Element, error) {
control, err := gtk.ScaleNewWithRange(gtk.ORIENTATION_HORIZONTAL, w.Min, w.Max, (w.Max-w.Min)/10)
if err != nil {
return nil, err
}
parent.Handle.Add(control)
control.SetValue(w.Value)
control.SetDrawValue(false)
control.SetSensitive(!w.Disabled)
retval := &sliderElement{
Control: Control{&control.Widget},
value: w.Value,
min: w.Min,
max: w.Max,
onChange: w.OnChange,
}
control.Connect("destroy", sliderOnDestroy, retval)
retval.shChange = setSignalHandler(&control.Widget, 0, retval.onChange != nil, "value-changed", sliderOnChangeValue, retval)
retval.onFocus.Set(&control.Widget, w.OnFocus)
retval.onBlur.Set(&control.Widget, w.OnBlur)
control.Show()
return retval, nil
}
func sliderOnDestroy(widget *gtk.Scale, mounted *sliderElement) {
mounted.handle = nil
}
func sliderOnChangeValue(widget *gtk.Scale, mounted *sliderElement) {
value := widget.GetValue()
if value != mounted.value {
mounted.value = value
mounted.onChange(value)
}
}
func (w *sliderElement) Props() base.Widget {
pb := w.scale()
return &Slider{
Value: pb.GetValue(),
Min: w.min,
Max: w.max,
Disabled: !pb.GetSensitive(),
OnFocus: w.onFocus.callback,
OnBlur: w.onBlur.callback,
}
}
// Layout determines the best size for an element that satisfies the
// constraints.
func (w *sliderElement) Layout(bc base.Constraints) base.Size {
if !bc.HasBoundedWidth() && !bc.HasBoundedHeight() {
// No need to worry about breaking the constraints. We can take as
// much space as desired.
width := w.MinIntrinsicWidth(base.Inf)
_, height := w.handle.GetPreferredHeight()
// Dimensions may need to be increased to meet minimums.
return bc.Constrain(base.Size{width, base.FromPixelsY(height)})
}
if !bc.HasBoundedHeight() {
// No need to worry about height. Find the width that best meets the
// widgets preferred width.
width := bc.ConstrainWidth(w.MinIntrinsicWidth(base.Inf))
// Get the best height for this width.
_, height := syscall.WidgetGetPreferredHeightForWidth(w.handle, width.PixelsX())
// Height may need to be increased to meet minimum.
return base.Size{width, bc.ConstrainHeight(base.FromPixelsY(height))}
}
// Not clear the following is the best general approach given GTK layout
// model.
_, height2 := w.handle.GetPreferredHeight()
if height := base.FromPixelsY(height2); height < bc.Max.Height {
width := w.MinIntrinsicWidth(height)
return bc.Constrain(base.Size{width, height})
}
height := base.FromPixelsY(height2)
width := w.MinIntrinsicWidth(height)
return bc.Constrain(base.Size{width, height})
}
// MinIntrinsicWidth returns the minimum width that this element requires
// to be correctly displayed.
func (w *sliderElement) MinIntrinsicWidth(base.Length) base.Length {
width, _ := w.handle.GetPreferredWidth()
if limit := base.FromPixelsX(width); limit < 160*DIP {
return 160 * DIP
}
return base.FromPixelsX(width)
}
func (w *sliderElement) scale() *gtk.Scale {
return (*gtk.Scale)(unsafe.Pointer(w.handle))
}
func (w *sliderElement) updateProps(data *Slider) error {
pb := w.scale()
w.value = data.Value
w.min = data.Min
w.max = data.Max
pb.SetRange(data.Min, data.Max)
pb.SetValue(data.Value)
pb.SetSensitive(!data.Disabled)
w.onChange = data.OnChange
w.shChange = setSignalHandler(w.handle, w.shChange, w.onChange != nil, "value-changed", sliderOnChangeValue, w)
w.onFocus.Set(w.handle, data.OnFocus)
w.onBlur.Set(w.handle, data.OnBlur)
return nil
}