/
separator.go
89 lines (81 loc) · 2.34 KB
/
separator.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
// Copyright ©2021-2022 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import "github.com/richardwilkes/unison/enums/paintstyle"
// DefaultSeparatorTheme holds the default SeparatorTheme values for Separators. Modifying this data will not alter
// existing Separators, but will alter any Separators created in the future.
var DefaultSeparatorTheme = SeparatorTheme{
LineInk: DividerColor,
Vertical: false,
}
// SeparatorTheme holds theming data for a Separator.
type SeparatorTheme struct {
LineInk Ink
Vertical bool
}
// Separator provides a simple vertical or horizontal separator line.
type Separator struct {
Panel
SeparatorTheme
}
// NewSeparator creates a new separator line.
func NewSeparator() *Separator {
s := &Separator{SeparatorTheme: DefaultSeparatorTheme}
s.Self = s
s.SetSizer(s.DefaultSizes)
s.DrawCallback = s.DefaultDraw
return s
}
// DefaultSizes provides the default sizing.
func (s *Separator) DefaultSizes(hint Size) (minSize, prefSize, maxSize Size) {
if s.Vertical {
if hint.Height < 1 {
prefSize.Height = 1
} else {
prefSize.Height = hint.Height
}
minSize.Height = 1
maxSize.Height = DefaultMaxSize
minSize.Width = 1
prefSize.Width = 1
maxSize.Width = 1
} else {
if hint.Width < 1 {
prefSize.Width = 1
} else {
prefSize.Width = hint.Width
}
minSize.Width = 1
maxSize.Width = DefaultMaxSize
minSize.Height = 1
prefSize.Height = 1
maxSize.Height = 1
}
if border := s.Border(); border != nil {
insets := border.Insets().Size()
minSize = minSize.Add(insets)
prefSize = prefSize.Add(insets)
maxSize = maxSize.Add(insets)
}
return minSize, prefSize, maxSize
}
// DefaultDraw provides the default drawing.
func (s *Separator) DefaultDraw(canvas *Canvas, _ Rect) {
rect := s.ContentRect(false)
if s.Vertical {
if rect.Width > 1 {
rect.X += (rect.Width - 1) / 2
rect.Width = 1
}
} else if rect.Height > 1 {
rect.Y += (rect.Height - 1) / 2
rect.Height = 1
}
canvas.DrawRect(rect, s.LineInk.Paint(canvas, rect, paintstyle.Fill))
}