forked from richardwilkes/unison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.go
98 lines (93 loc) · 2.72 KB
/
link.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
// 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/toolbox"
)
// DefaultLinkTheme holds the default Link theme values.
var DefaultLinkTheme = LinkTheme{
TextDecoration: TextDecoration{
Font: LabelFont,
Foreground: LinkColor,
Underline: true,
},
RolloverInk: LinkRolloverColor,
PressedInk: LinkPressedColor,
}
// LinkTheme holds theming data for a link.
type LinkTheme struct {
TextDecoration
RolloverInk Ink
PressedInk Ink
}
// NewLink creates a new RichLabel that can be used as a hyperlink.
func NewLink(title, tooltip, target string, theme LinkTheme, clickHandler func(Paneler, string)) *RichLabel {
link := NewRichLabel()
link.Text = NewText(title, &theme.TextDecoration)
if tooltip != "" {
link.Tooltip = NewTooltipWithText(tooltip)
}
link.UpdateCursorCallback = func(where Point) *Cursor {
return PointingCursor()
}
link.MouseEnterCallback = func(where Point, mod Modifiers) bool {
link.Text.AdjustDecorations(func(decoration *TextDecoration) {
decoration.Foreground = theme.RolloverInk
})
link.MarkForRedraw()
return true
}
link.MouseExitCallback = func() bool {
link.Text.AdjustDecorations(func(decoration *TextDecoration) {
decoration.Foreground = theme.Foreground
})
link.MarkForRedraw()
return true
}
in := false
link.MouseDownCallback = func(where Point, button, clickCount int, mod Modifiers) bool {
link.Text.AdjustDecorations(func(decoration *TextDecoration) {
decoration.Foreground = theme.PressedInk
})
link.MarkForRedraw()
in = true
return true
}
link.MouseDragCallback = func(where Point, button int, mod Modifiers) bool {
now := where.In(link.ContentRect(true))
if now != in {
in = now
link.Text.AdjustDecorations(func(decoration *TextDecoration) {
if in {
decoration.Foreground = theme.PressedInk
} else {
decoration.Foreground = theme.Foreground
}
})
link.MarkForRedraw()
}
return true
}
link.MouseUpCallback = func(where Point, button int, mod Modifiers) bool {
ink := theme.Foreground
inside := where.In(link.ContentRect(true))
if inside {
ink = theme.RolloverInk
}
link.Text.AdjustDecorations(func(decoration *TextDecoration) {
decoration.Foreground = ink
})
link.MarkForRedraw()
if inside && clickHandler != nil {
toolbox.Call(func() { clickHandler(link, target) })
}
return true
}
return link
}