- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲) - 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表) - 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记 - Git:状态着色、选择性暂存提交、pull/push、历史并排对比 - 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复 - 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈 - 设计文档与验证记录见 docs/plans/
174 lines
4.3 KiB
Go
174 lines
4.3 KiB
Go
package app
|
||
|
||
import (
|
||
"github.com/egoist/mygo"
|
||
"github.com/egoist/mygo/ui"
|
||
)
|
||
|
||
// menuEntry 是自绘菜单里的一项。
|
||
//
|
||
// 为什么不用系统菜单:mygo 的 Menu/ContextMenu 在 Windows 上是**原生菜单**,
|
||
// 字号、行高、圆角全由系统画(150% 缩放下 9pt 显得又大又粗),应用改不了。
|
||
// 想跟 IDE 其他部分(12px 文字、8px 圆角、深色卡片)协调,只能自绘。
|
||
type menuEntry struct {
|
||
label string
|
||
shortcut string
|
||
disabled bool
|
||
divider bool
|
||
action func()
|
||
}
|
||
|
||
// entriesFromMenu 把 mygo 的菜单项翻译成自绘项。子菜单只支持一层。
|
||
func entriesFromMenu(items []*mygo.MenuItem) []menuEntry {
|
||
out := make([]menuEntry, 0, len(items))
|
||
for _, it := range items {
|
||
if it == nil || it.Hidden {
|
||
continue
|
||
}
|
||
if it.Type == mygo.MenuItemSeparator {
|
||
out = append(out, menuEntry{divider: true})
|
||
continue
|
||
}
|
||
e := menuEntry{
|
||
label: it.Label,
|
||
shortcut: shortcutText(it.Accelerator),
|
||
disabled: it.Disabled,
|
||
}
|
||
if it.Click != nil {
|
||
item, click := it, it.Click
|
||
e.action = func() { click(item, nil) }
|
||
}
|
||
out = append(out, e)
|
||
}
|
||
return out
|
||
}
|
||
|
||
// shortcutText 把 "CmdOrCtrl+Shift+M" 缩写成本平台的样子。
|
||
func shortcutText(acc string) string {
|
||
if acc == "" {
|
||
return ""
|
||
}
|
||
repl := map[string]string{
|
||
"CmdOrCtrl+": "Ctrl+", "CommandOrControl+": "Ctrl+",
|
||
"Cmd+": "Win+", "Command+": "Win+",
|
||
}
|
||
out := acc
|
||
for k, v := range repl {
|
||
if len(out) >= len(k) && out[:len(k)] == k {
|
||
out = v + out[len(k):]
|
||
break
|
||
}
|
||
}
|
||
return out
|
||
}
|
||
|
||
// openPop 在 anchor 下沿弹出自绘面板。
|
||
//
|
||
// 只记矩形、不记元素:ui.Element 只能在本帧使用(跨帧会 panic:
|
||
// "element from pass N used in pass N+1"),而面板是下一帧才画的。
|
||
func (a *App) openPop(anchor ui.Element, entries []menuEntry) {
|
||
if len(entries) == 0 || a.win == nil {
|
||
return
|
||
}
|
||
a.popRect = anchor.Bounds()
|
||
a.popEntries = entries
|
||
a.popOpen = true
|
||
a.popJustOpened = true
|
||
a.win.Update(func() {})
|
||
}
|
||
|
||
// closePop 收起菜单。
|
||
func (a *App) closePop() {
|
||
a.popOpen = false
|
||
a.popEntries = nil
|
||
}
|
||
|
||
// popMenu 画当前打开的菜单。要挂在根级(Popover 是覆盖层)。
|
||
func (a *App) popMenu(c *ui.Context) {
|
||
if !a.popOpen || len(a.popEntries) == 0 {
|
||
return
|
||
}
|
||
t := c.Theme()
|
||
|
||
// 位置:优先贴锚点下沿;超出窗口就往回收,避免面板被裁掉。
|
||
const panelW float32 = 190
|
||
x, y := a.popRect.X, a.popRect.Y+a.popRect.H+2
|
||
if w, h := c.Size(); w > 0 {
|
||
if x+panelW > w-6 {
|
||
x = w - panelW - 6
|
||
}
|
||
if x < 6 {
|
||
x = 6
|
||
}
|
||
// 高度按项数估:每项约 24,分隔线 11。
|
||
est := float32(8)
|
||
for _, en := range a.popEntries {
|
||
if en.divider {
|
||
est += 11
|
||
} else {
|
||
est += 24
|
||
}
|
||
}
|
||
if y+est > h-6 {
|
||
y = a.popRect.Y - est - 2
|
||
}
|
||
if y < 6 {
|
||
y = 6
|
||
}
|
||
}
|
||
|
||
ui.Overlay(c, func() {
|
||
// 遮罩:点它关闭。但打开的那一帧要跳过——判定会把这次点击当成
|
||
// 「点了外面」,面板刚出来就被收回去(表现为「点了没反应」)。
|
||
back := ui.Box(c).Absolute().Left(0).Top(0).Right(0).Bottom(0).Modal()
|
||
if a.popJustOpened {
|
||
a.popJustOpened = false
|
||
} else if back.Clicked() {
|
||
a.closePop()
|
||
a.win.Update(func() {})
|
||
return
|
||
}
|
||
|
||
ui.Column(c).Absolute().Left(x).Top(y).Width(panelW).
|
||
Padding(5).Gap(1).Radius(t.Radius).
|
||
Background(t.Surface).BorderWidth(1).BorderColor(t.Border).
|
||
Shadow(0, 8, 24, 0, ui.RGBA(0, 0, 0, 0.35)).
|
||
Children(func() {
|
||
for _, en := range a.popEntries {
|
||
en := en
|
||
if en.divider {
|
||
ui.Box(c).FillWidth().Height(1).Background(t.Border).
|
||
Margin(4, 0)
|
||
continue
|
||
}
|
||
fg := t.Text
|
||
if en.disabled {
|
||
fg = t.TextMuted
|
||
}
|
||
btn := ui.ButtonBase(c).FillWidth().Padding(4, 8).Radius(4).
|
||
Justify(ui.Start).Cursor(ui.CursorPointer)
|
||
btn.Children(func() {
|
||
ui.Row(c).FillWidth().Gap(14).AlignItems(ui.Center).Children(func() {
|
||
ui.Text(c, en.label).FontSize(12).TextColor(fg).SingleLine()
|
||
ui.Box(c).Grow(1)
|
||
if en.shortcut != "" {
|
||
ui.Text(c, en.shortcut).FontSize(10).
|
||
TextColor(t.TextMuted).SingleLine()
|
||
}
|
||
})
|
||
})
|
||
if en.disabled {
|
||
continue
|
||
}
|
||
if btn.Clicked() {
|
||
a.closePop()
|
||
if en.action != nil {
|
||
en.action()
|
||
}
|
||
a.win.Update(func() {})
|
||
}
|
||
}
|
||
})
|
||
})
|
||
}
|