package app
import (
"time"
"github.com/egoist/mygo/ui"
)
// 图标全部内嵌为 SVG,24 栅格自绘,不依赖任何字体字形。
//
// ui.Icon 会把图形画成文字颜色,所以 SVG 里的颜色只是占位:
// 需要实心的形状写 fill,线框形状保持 fill="none"。
var iconMarkup = map[string]string{
"folder": ``,
"refresh": ``,
"collapse": ``,
"expand": ``,
"plus": ``,
"outline": ``,
"chevron": ``,
// 靶心加十字准星:定位当前文件在树里的位置。
"locate": ``,
"build": ``,
"run": ``,
"test": ``,
"stop": ``,
"clear": ``,
"hide": ``,
"panel": ``,
"continue": ``,
"over": ``,
"into": ``,
"out": ``,
"close": ``,
"error": ``,
"warning": ``,
"debug": ``,
}
// icons 是解析好的图标,进程启动时建一次。
var icons = map[string]*ui.SVG{}
func init() {
for name, body := range iconMarkup {
src := ``
svg, err := ui.ParseSVG([]byte(src))
if err != nil {
logf("图标 %s 解析失败: %v", name, err)
continue
}
icons[name] = svg
}
}
// icon 画一个图标,size 是像素高度,color 是颜色。
func icon(c *ui.Context, name string, size float32, color ui.Color) ui.Element {
svg := icons[name]
if svg == nil {
return ui.Box(c).Width(size).Height(size)
}
return ui.Icon(c, svg).Width(size).Height(size).TextColor(color)
}
// iconButtonEl 是带图标的按钮,带悬停与按下的过渡动效。
func iconButtonEl(c *ui.Context, name, label, tip string, color ui.Color) ui.Element {
t := c.Theme()
if color == (ui.Color{}) {
color = t.Text
}
base := ui.ButtonBase(c).Padding(2, 5).Radius(4)
// 悬停淡入一层底色,按下再深一档并轻微缩小。
bgTarget := ui.RGBA(0, 0, 0, 0)
switch {
case base.Pressed():
bgTarget = t.SurfacePressed
case base.Hovered():
bgTarget = t.SurfaceHover
}
bg := base.AnimateColor("iconbg", bgTarget, ui.Animation{Duration: 120 * time.Millisecond})
base = base.Background(bg)
scaleTarget := float32(1)
if base.Pressed() {
scaleTarget = 0.92
}
scale := base.AnimateTo("iconscale", scaleTarget, ui.Animation{Duration: 90 * time.Millisecond})
base = base.Scale(scale, scale)
base.Children(func() {
ui.Row(c).Gap(4).AlignItems(ui.Center).Children(func() {
icon(c, name, 13, color)
if label != "" {
ui.Text(c, label).FontSize(11).TextColor(color)
}
})
})
if tip != "" {
base = base.Tooltip(tip)
}
return base
}
// iconButton 只需要知道是否被点时用。
func iconButton(c *ui.Context, name, label, tip string, color ui.Color) bool {
return iconButtonEl(c, name, label, tip, color).Clicked()
}