package app import ( "fmt" "path/filepath" "strings" "github.com/egoist/mygo/ui" ) // 输出面板与问题面板的右键菜单。 // // 终端不用管:plugins/terminal 的 View 自带 ContextMenu(默认 // RightClickContextMenu),从 mygo v0.7.4 起就有,只是原生菜单在 // automation 插座里看不到——别再把「openPop 没有入口」当成「没有菜单」。 // 这两个面板是我们自己用 ui.List 画的,框架不会给菜单。 // outLinesText 把输出行拼成可复制的纯文本。 func outLinesText(lines []OutLine) string { if len(lines) == 0 { return "" } out := make([]string, 0, len(lines)) for _, l := range lines { out = append(out, l.Text) } return strings.Join(out, "\n") } // problemLineText 是一条问题的纯文本,和 problemRow 的显示保持一致。 // 抽出来是为了复制和渲染不各写一份、不会慢慢漂移。 func problemLineText(p Problem) string { tag := "" if p.Source != "" { tag = "[" + p.Source + "] " } return fmt.Sprintf("%s%s:%d %s", tag, filepath.Base(p.Path), p.Line, p.Text) } func problemLinesText(ps []Problem) string { if len(ps) == 0 { return "" } out := make([]string, 0, len(ps)) for _, p := range ps { out = append(out, problemLineText(p)) } return strings.Join(out, "\n") } // outputMenuEntries 是「运行」面板的右键项。 func (a *App) outputMenuEntries() []menuEntry { sel, all := "", outLinesText(a.out) if a.outSel >= 0 && a.outSel < len(a.out) { sel = a.out[a.outSel].Text } return []menuEntry{ {label: "复制选中行", disabled: sel == "", action: func() { a.copyToClipboard(sel) }}, {label: "复制全部输出", disabled: all == "", action: func() { a.copyToClipboard(all) }}, {divider: true}, {label: "清空输出", disabled: len(a.out) == 0, action: a.ClearOutput}, } } // problemMenuEntries 是「问题」面板的右键项。 // 问题来自诊断,没有「清空」这回事,所以只给复制。 func (a *App) problemMenuEntries(ps []Problem) []menuEntry { sel, all := "", problemLinesText(ps) if a.probSel >= 0 && a.probSel < len(ps) { sel = problemLineText(ps[a.probSel]) } return []menuEntry{ {label: "复制选中行", disabled: sel == "", action: func() { a.copyToClipboard(sel) }}, {label: "复制全部问题", disabled: all == "", action: func() { a.copyToClipboard(all) }}, } } // openListPop 在列表的右键位置弹出菜单。 // // List 的 Bounds 是一整块,直接当锚点会把菜单顶到窗口外再折回来,位置 // 离指针很远。这里用指针在列表内的坐标构造一个零宽矩形,让菜单贴着鼠标。 func (a *App) openListPop(list ui.Element, entries []menuEntry) { b := list.Bounds() x, y, over := list.PointerPosition() if over { b.X += x b.Y += y b.W, b.H = 0, 0 } a.openPopAt(b, entries) }