package app import ( "strings" "github.com/egoist/mygo/ui" "github.com/egoist/mygo/ui/editor" ) const ( // minimapWidth 是缩略图条的宽度。 minimapWidth float32 = 88 // minimapLineH 是每一行在缩略图里占的高度。 // 行数少时按它铺、铺不满就留白(IDEA 也是这样),行多了才压到编辑器高度; // 若不管行数一律撑满,十几行的文件会被拉成几根粗竖条,反而看不出结构。 minimapLineH float32 = 3 // minimapBlocks 是缩略图纵向切成的可点击块数上限。 // mygo 的点击事件只给布尔值、不给坐标,没法把「点在哪一像素」直接换算成 // 行号,所以切成若干等高块,每块自己带点击,点中即滚到该块首行。 minimapBlocks = 72 ) // minimap 画右侧的代码缩略图。 // // mygo 的编辑器没有内置 minimap,但暴露了 Buffer.Line、ScrollTo、ScrollY // 与 LineTop,足够自己拼一个。editorH 是编辑器实际高度(Element.Bounds), // 用来判断内容要不要压缩、以及视口框该多高。 func (a *App) minimap(c *ui.Context, d *Doc, editorH float32) { t := c.Theme() total := d.buf.LineCount() if total <= 0 || !a.minimapOn || editorH <= 0 { return } // 整篇都看得见时不画缩略图:此时它没有任何导航价值,只会在右上角 // 留一小撮线条,反而像块补丁。行高先按编辑器实际排版取。 totalH := d.st.LineTop(total) if totalH > 0 { if lineH := totalH / float64(total); lineH > 0 { if float64(editorH) >= lineH*float64(total) { return } } } contentH := float32(total) * minimapLineH if contentH > editorH { contentH = editorH } if contentH < 10 { return } frac, scroll := 1.0, 0.0 if totalH > 0 { if lineH := totalH / float64(total); lineH > 0 { visible := float64(editorH) / lineH if visible < float64(total) { frac = visible / float64(total) } } scroll = d.st.ScrollY() / totalH if scroll < 0 { scroll = 0 } if scroll+frac > 1 { scroll = 1 - frac } } // 每块至少 16 DIP:ButtonBase 有约 14 的最小高度,切太密会被顶回来, // 整条撑出可视区,点击位置随之错位。 blocks := int(contentH / 16) if blocks < 1 { blocks = 1 } if blocks > minimapBlocks { blocks = minimapBlocks } if blocks > total { blocks = total } // 缩略图里的两种线色跟随主题:代码行用次要文字色,注释再淡一档。 codeCol, noteCol := t.TextMuted, t.TextMuted.Alpha(0.55) ui.Row(c).Width(minimapWidth).MinWidth(minimapWidth).MaxWidth(minimapWidth). FillHeight().AlignItems(ui.Start).Children(func() { // 一条细分隔线,把缩略图和代码区分开。 ui.Box(c).Width(1).FillHeight().Background(c.Theme().Border.Alpha(0.55)) ui.Column(c).Grow(1).Height(contentH). Clip(). DrawOver(func(p *ui.Painter, r ui.Rect) { // 整篇都看得见时不画视口框:没有滚动,框住全部反而多余。 if frac >= 0.999 { return } y0 := r.Y + r.H*float32(scroll) h := r.H * float32(frac) if h < 3 { h = 3 } p.Fill(ui.Rect{X: r.X, Y: y0, W: r.W, H: h}, c.Theme().Accent.Alpha(0.10), 0) p.Line(r.X, y0, r.X+r.W, y0, 1, c.Theme().Accent.Alpha(0.42)) p.Line(r.X, y0+h, r.X+r.W, y0+h, 1, c.Theme().Accent.Alpha(0.42)) }). Children(func() { for i := 0; i < blocks; i++ { start := i * total / blocks end := (i + 1) * total / blocks if end <= start { continue } // ButtonBase 才有状态槽:普通 Box 的 Clicked 永远返回 false。 cell := ui.ButtonBase(c).FillWidth().Grow(1).MinHeight(0). Padding(0).Radius(0).Background(ui.RGBA(0, 0, 0, 0)). Cursor(ui.CursorPointer) cell.Draw(func(p *ui.Painter, r ui.Rect) { drawMinimapCell(p, r, d.buf, start, end, codeCol, noteCol) }) if cell.Clicked() { d.st.ScrollTo(start) } } }) }) } // drawMinimapCell 把一个行区间画成若干细线:缩进决定起点,长度决定宽。 // // 拿不到语法 token 的颜色(editor 没导出 highlighter),所以只区分注释与 // 代码两种,靠明暗差把注释块和代码块分开——在缩略图这个尺度已经够辨认 // 结构,没必要为了上色去反射内部状态。 func drawMinimapCell(p *ui.Painter, r ui.Rect, buf *editor.Buffer, start, end int, codeCol, noteCol ui.Color) { n := end - start if n <= 0 || r.H <= 0 { return } lineH := r.H / float32(n) // 行高不足 1.5 px 时逐行画会糊成一片,按步长抽样。 step := 1 if lineH < 1.5 { step = int(1.5/lineH) + 1 } // 1.0 是把 88 列代码铺满 88 px:Go 代码多数在 80 列上下, // 系数再小线条就只占半条,缩略图会很空。 const charW = 1.0 for y := start; y < end; y += step { text := buf.Line(y) trimmed := strings.TrimSpace(text) if trimmed == "" { continue } indent := 0 for _, ch := range text { switch ch { case ' ': indent++ case '\t': indent += 4 default: goto drawn } } drawn: x0 := r.X + 3 + float32(indent)*charW if x0 > r.X+r.W-4 { x0 = r.X + r.W - 4 } w := float32(displayWidth(trimmed)) * charW room := r.X + r.W - 3 - x0 if w > room { w = room } if w < 1.5 { w = 1.5 } col := codeCol if isCommentLine(trimmed) { col = noteCol } h := lineH * float32(step) * 0.62 if h < 1 { h = 1 } p.Fill(ui.Rect{X: x0, Y: r.Y + float32(y-start)*lineH, W: w, H: h}, col, 0) } } // displayWidth 数一行代码占多少列:ASCII 算 1 列,中文等宽字符算 2 列。 // 直接用 rune 数会把中文行算短一半,缩略图里注释行会莫名其妙比代码行还短。 func displayWidth(s string) int { n := 0 for _, r := range s { if r < 128 { n++ continue } n += 2 } return n } // isCommentLine 判断这一行是不是注释,用来在缩略图里压暗。 func isCommentLine(s string) bool { switch { case strings.HasPrefix(s, "//"), strings.HasPrefix(s, "/*"), strings.HasPrefix(s, "*"), strings.HasPrefix(s, "#"), strings.HasPrefix(s, "--"): return true } return false }