forked from richardwilkes/unison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline_border.go
61 lines (54 loc) · 1.81 KB
/
line_border.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
// 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/filltype"
"github.com/richardwilkes/unison/enums/paintstyle"
)
var _ Border = &LineBorder{}
// LineBorder private a lined border.
type LineBorder struct {
insets Insets
ink Ink
cornerRadius float32
noInset bool
}
// NewLineBorder creates a new line border. The cornerRadius specifies the amount of rounding to use on the corners. The
// insets represent how thick the border will be drawn on that edge. If noInset is true, the Insets() method will return
// zeroes.
func NewLineBorder(ink Ink, cornerRadius float32, insets Insets, noInset bool) *LineBorder {
return &LineBorder{
insets: insets,
ink: ink,
cornerRadius: cornerRadius,
noInset: noInset,
}
}
// Insets returns the insets describing the space the border occupies on each side.
func (b *LineBorder) Insets() Insets {
if b.noInset {
return Insets{}
}
return b.insets
}
// Draw the border into rect.
func (b *LineBorder) Draw(canvas *Canvas, rect Rect) {
clip := rect.Inset(b.insets)
path := NewPath()
path.SetFillType(filltype.EvenOdd)
if b.cornerRadius > 0 {
path.RoundedRect(rect, b.cornerRadius, b.cornerRadius)
radius := max(b.cornerRadius-((b.insets.Width()+b.insets.Height())/4), 1)
path.RoundedRect(clip, radius, radius)
} else {
path.Rect(rect)
path.Rect(clip)
}
canvas.DrawPath(path, b.ink.Paint(canvas, rect, paintstyle.Fill))
}