/
handle.go
80 lines (70 loc) · 2.08 KB
/
handle.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
// Copyright (c) 2023, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package core
import (
"cogentcore.org/core/abilities"
"cogentcore.org/core/colors"
"cogentcore.org/core/cursors"
"cogentcore.org/core/events"
"cogentcore.org/core/math32"
"cogentcore.org/core/styles"
"cogentcore.org/core/units"
)
// Handle represents a draggable handle that can be used to
// control the size of an element. The [Handle.Styles.Direction]
// controls the direction in which the handle moves.
type Handle struct {
Box
// Min is the minimum value that the handle can go to
// (typically the lower bound of the dialog/splits)
Min float32
// Max is the maximum value that the handle can go to
// (typically the upper bound of the dialog/splits)
Max float32
// Pos is the current position of the handle on the
// scale of [Handle.Min] to [Handle.Max]
Pos float32
}
func (hl *Handle) OnInit() {
hl.WidgetBase.OnInit()
hl.HandleEvents()
hl.SetStyles()
}
func (hl *Handle) SetStyles() {
hl.Style(func(s *styles.Style) {
s.SetAbilities(true, abilities.Clickable, abilities.Focusable, abilities.Hoverable, abilities.Slideable)
s.Border.Radius = styles.BorderRadiusFull
s.Background = colors.C(colors.Scheme.OutlineVariant)
})
hl.StyleFinal(func(s *styles.Style) {
if s.Direction == styles.Row {
s.Min.X.Dp(6)
s.Min.Y.Em(2)
s.Margin.SetHorizontal(units.Dp(6))
} else {
s.Min.X.Em(2)
s.Min.Y.Dp(6)
s.Margin.SetVertical(units.Dp(6))
}
if !hl.IsReadOnly() {
if s.Direction == styles.Row {
s.Cursor = cursors.ResizeEW
} else {
s.Cursor = cursors.ResizeNS
}
}
})
}
func (hl *Handle) HandleEvents() {
hl.On(events.SlideMove, func(e events.Event) {
pos := hl.ParentWidget().PointToRelPos(e.Pos())
hl.Pos = math32.Vector2FromPoint(pos).Dim(hl.Styles.Direction.Dim())
hl.SendChange(e)
})
}
// Value returns the value on a normalized scale of 0-1,
// based on [Handle.Pos], [Handle.Min], and [Handle.Max].
func (hl *Handle) Value() float32 {
return hl.Pos / (hl.Max - hl.Min)
}