package app import ( "fmt" "path/filepath" "strings" "time" "github.com/egoist/mygo/ui" "github.com/egoist/mygo/ui/editor" ) // islandGap 是所有浮岛之间的统一间距;窗口四周另有边距。 const islandGap float32 = 6 // railWidth 是左侧竖条的宽度,标题栏的标识按它对齐。 const railWidth float32 = 40 // sidebarMin 是项目浮岛的最小宽度:再窄标题栏那排按钮就该挤在一起了。 const sidebarMin float32 = 190 // view 是整个窗口的内容,每帧重建。 // // 布局按 Islands Dark 的思路分层:最外层是窗口底色,只负责在岛屿之间 // 露出「沟」;内容整体内缩 8,侧栏与主区各是一张圆角卡片,浮在底色上。 func (a *App) view(c *ui.Context) { c.SetTheme(a.th) t := c.Theme() // 最大化、还原这类整窗跳变,框架会在 WM_SIZE 处理里先用上一帧的布局 // 撑满新尺寸画一帧(surface.go 的 drawNow,为的是拖动窗口时内容跟手), // 视觉上就是内容先被放大一下再恢复。这一帧只铺底色,把明显的拉伸 // 换成一次纯背景过渡;正常拖动时每帧变化远小于阈值,不会触发。 w, h := c.Size() jump := false if a.lastSize[0] > 0 { rw, rh := w/a.lastSize[0], h/a.lastSize[1] jump = rw > 1.2 || rw < 0.83 || rh > 1.2 || rh < 0.83 } a.lastSize[0], a.lastSize[1] = w, h if jump { ui.Box(c).Fill().Background(t.Background) return } ui.Column(c).Fill().Background(t.Background).Children(func() { // 自绘标题栏代替系统标题栏,紧随其下才是岛屿。 a.titleBar(c) // 自绘弹出菜单:覆盖层,要挂在根级。 a.popMenu(c) // 弹窗挂在这个 Column 里是实测能居中的位置(Modal 的 overlay // 以当前父级为参照,挂到根级反而会掉到编辑器右下角)。 if a.newOpen { a.newEntryDialog(c) } // 命令面板、重命名与提交弹窗都是覆盖层。 a.paletteView(c) a.renameDialog(c) a.renameEntryDialog(c) a.commitDialog(c) a.settingsDialog(c) a.breakpointCondDialog(c) a.runConfigDialog(c) if a.root == "" { ui.Column(c).Grow(1).Children(func() { a.welcomeView(c) }) return } ui.Row(c).Grow(1).Gap(islandGap).Padding(8, 8, 8, 8).AlignItems(ui.Stretch).Children(func() { a.toolRail(c) ui.Column(c).Grow(1).FillHeight().Gap(islandGap).Children(func() { // 项目岛、自绘分隔条、编辑岛横排。 // 不用 ui.Split:它在分段边界裁剪,贴边卡片的圆角会被切平, // 而且分隔条固定 1 DIP、宽度还会被内容撑开。 ui.Row(c).Grow(1).FillHeight().AlignItems(ui.Stretch).Children(func() { if a.sidebarOn { ui.Column(c).Width(a.sidebar).Shrink(0).FillHeight().Children(func() { a.sidebarView(c) }) a.splitter(c) } a.mainView(c) }) a.statusCard(c) }) }) }) } // welcomeView 是未打开工作区时的引导页。 func (a *App) welcomeView(c *ui.Context) { t := c.Theme() ui.Column(c).Fill().Center().Gap(10).Children(func() { logoCard(c, 104) ui.Box(c).Height(6) ui.Text(c, "MyGoland").FontSize(34).Bold().TextColor(t.Text) ui.Text(c, "轻量 Go IDE · mygo + Go · v"+Version).FontSize(13).TextColor(t.TextMuted) ui.Box(c).Height(16) if ui.PrimaryButton(c, "打开文件夹").Clicked() { go a.OpenFolderDialog() } ui.Text(c, "或按 Ctrl+O").FontSize(11).TextColor(t.TextMuted) a.recentView(c) }) } // sidebarView 是左侧工具窗在 Split 里的那一格。 // // 右侧让出 4 DIP 的沟,与主区左侧的 4 DIP 合起来才是 Islands 那种明显的 // 间距——Split 自带的分隔线只有 1 DIP,单靠它做不出岛屿之间的空隙。 func (a *App) sidebarView(c *ui.Context) { ui.Column(c).FillHeight().Children(func() { if a.panelKind == 1 { a.outlineView(c) return } a.sidebarPanel(c) }) } // toolRail 是窗口最左侧的竖条,用来切换工具窗,和 IDE 的惯例一致。 func (a *App) toolRail(c *ui.Context) { t := c.Theme() // 竖条也是一块岛屿:独立卡片、圆角、和工具窗之间留缝。 ui.Column(c).Width(railWidth).FillHeight().Radius(t.Radius).Background(t.Surface).Clip(). Gap(2).Padding(6, 0, 6, 0).AlignItems(ui.Center).Justify(ui.Start).Children(func() { a.railButton(c, "folder", "项目", 0) a.railButton(c, "outline", "大纲", 1) }) } // railButton 画竖条上的一个入口;点当前项会收起工具窗。 func (a *App) railButton(c *ui.Context, name, tip string, kind int) { t := c.Theme() active := a.sidebarOn && a.panelKind == kind // 不画任何底色块:选中与悬停只靠图标颜色区分。 el := ui.ButtonBase(c).Width(30).Height(30).Radius(6) color := t.TextMuted if active { color = t.Accent } el.Children(func() { ui.Row(c).Fill().Justify(ui.Center).AlignItems(ui.Center).Children(func() { icon(c, name, 16, color) }) }) if tip != "" { el = el.Tooltip(tip) } if el.Clicked() { if active { a.sidebarOn = false } else { a.sidebarOn = true a.panelKind = kind } a.win.Update(func() {}) } } // sidebarPanel 是左侧的「项目」工具窗本体,画成一张独立岛屿。 func (a *App) sidebarPanel(c *ui.Context) { t := c.Theme() ui.Column(c).FillHeight().FillWidth().Radius(t.Radius).Background(t.Surface).Clip().Children(func() { // 工具窗标题栏。 ui.Row(c).Padding(6, 8).Gap(4).AlignItems(ui.Center). BorderWidth(0, 0, 1, 0).BorderColor(t.Border).Children(func() { ui.Text(c, "项目").FontSize(11).Bold().TextColor(t.TextMuted) ui.Box(c).Grow(1) if iconButton(c, "locate", "", "定位当前文件", t.TextMuted) { a.RevealActive() } plus := iconButtonEl(c, "plus", "", "新建文件或文件夹", t.TextMuted) if plus.Clicked() { a.openPop(plus, a.addEntries()) } if iconButton(c, "refresh", "", "刷新文件树", t.TextMuted) { a.Refresh() } if iconButton(c, "collapse", "", "全部折叠", t.TextMuted) { a.CollapseAll() } if iconButton(c, "expand", "", "展开全部", t.TextMuted) { a.ExpandAll() } }) // 根目录名。 ui.Row(c).Padding(4, 10).AlignItems(ui.Center).Children(func() { ui.Text(c, strings.ToUpper(filepath.Base(a.root))).FontSize(11). TextColor(t.TextMuted).SingleLine() }) ui.Scroll(c).Grow(1).Children(func() { a.treeNode(c, a.root, 0) }) // 底部状态:工作区路径。 ui.Row(c).Padding(4, 10).BorderWidth(1, 0, 0, 0).BorderColor(t.Border).Children(func() { ui.Text(c, a.root).FontSize(10).TextColor(t.TextMuted).SingleLine() }) }) } // RevealActive 在左侧文件树里定位当前打开的文件:逐级展开父目录、 // 高亮那一行,并滚动到可见处。 func (a *App) RevealActive() { d := a.doc() if d == nil || a.root == "" { return } // 树里的路径来自 filepath.Join(Windows 上是反斜杠),而 a.root 可能是 // 命令行传进来的正斜杠路径,直接比前缀永远不匹配,所以两边先 Clean 统一。 root := filepath.Clean(a.root) path := filepath.Clean(d.path) if !strings.HasPrefix(path, root) { return } // 从文件所在目录一路展开到根。 for dir := filepath.Dir(path); strings.HasPrefix(dir, root); dir = filepath.Dir(dir) { *a.openFlag(dir) = true if dir == root { break } } a.revealPath = path a.revealPending = true a.win.Update(func() {}) } // activePath 返回当前打开文件的规范化路径,没有打开文件时为空。 // // 要先 Clean:树里的路径来自 filepath.Join(Windows 是反斜杠),而文件路径 // 可能来自命令行参数(正斜杠),不统一就永远不相等。 func (a *App) activePath() string { d := a.doc() if d == nil { return "" } return filepath.Clean(d.path) } // treeNode 递归画出一层目录,depth 决定缩进层级。 // // 不用 ui.Tree/ui.TreeItem:TreeItem 的标签只接受一段文字,挂不了类型图标。 // 这里自己排版——缩进用空白 Box、折叠箭头用字符、图标交给 fileIcon。 func (a *App) treeNode(c *ui.Context, dir string, depth int) { for _, e := range a.entries(dir) { p := filepath.Join(dir, e.Name()) if e.IsDir() { if skipDir(e.Name()) { continue } open := a.openFlag(p) if a.treeRow(c, p, e.Name(), depth, true, *open) { *open = !*open a.win.Update(func() {}) } if *open { a.treeNode(c, p, depth+1) } continue } if skipFile(e.Name()) { continue } if a.treeRow(c, p, e.Name(), depth, false, false) { go a.OpenFile(p) } } } // treeRow 画文件树的一行:缩进、折叠箭头、类型图标、文件名。 // 返回这一帧是否被点击——目录用来切换展开,文件用来打开。 func (a *App) treeRow(c *ui.Context, path, name string, depth int, isDir, open bool) bool { t := c.Theme() active := path != "" && path == a.activePath() // Justify(Start) 不能省:Row 默认把内容居中,那样缩进块不起作用, // 长短不同的文件名起点还会左右飘。 row := ui.ButtonBase(c).FillWidth().Padding(1, 4).Gap(3).Radius(3). Justify(ui.Start).AlignItems(ui.Center).Cursor(ui.CursorPointer) target := float32(0) if open { target = 90 } chevronAngle := row.AnimateTo("chevron", target, ui.Animation{Duration: 140 * time.Millisecond, Ease: ui.EaseOut}) row.Children(func() { // 整行底色:一个**始终存在**的绝对定位块,只切颜色、不增删元素。 // // 之前试过事后给行设 Background/TextColor/Draw,结果样式串到了相邻的 // 上一行(名字是对的,颜色错行)——那是 mygo 即时模式的元素复用问题。 // 改成「按内容位置绘制的独立元素」就落得准了。 bgc := ui.RGBA(0, 0, 0, 0) fg := t.Text switch { case a.selected(path): // 选中的行比「当前文件」更重,多选时一眼看清范围。 bgc = t.Accent.Alpha(0.30) fg = t.Text case active: bgc = t.Accent.Alpha(0.16) fg = t.Accent } // git 状态优先于普通文字色,但让位给选中/当前行。 if st, ok := a.gst.info.Files[path]; ok && !a.selected(path) && !active { fg = a.gitColor(st) } ui.Box(c).Absolute().Left(0).Top(0).Right(0).Bottom(0). Background(bgc).Radius(3) if depth > 0 { ui.Box(c).Width(float32(depth) * 12) } if isDir { // 展开时箭头转 90°,带过渡,避免瞬间跳变。 icon(c, "chevron", 10, t.TextMuted).Rotate(chevronAngle).Width(11) } else { // 文件行留出与箭头同宽的空位,让图标竖着对齐。 ui.Box(c).Width(11) } a.fileIcon(c, name, isDir) ui.Text(c, name).FontSize(12).TextColor(fg).SingleLine() if st, ok := a.gst.info.Files[path]; ok { if b := gitBadge(st); b != "" { ui.Text(c, b).FontSize(10).TextColor(a.gitColor(st)) } } }) // 被「定位」选中的那一行铺一层强调色。 // 滚动只做一次——每帧都调 ScrollIntoView 会把滚动条钉死,用户翻不动。 // 右键:在这个目录下新建文件/文件夹。 // 文件行取它所在的目录,目录行就用它自己。 if row.RightClicked() { a.openPop(row, a.treeEntries(path, isDir)) } if path == a.revealPath && a.revealPending { a.revealPending = false row.ScrollIntoView() } if !row.Clicked() { return false } mods := c.Modifiers() switch { case mods&ui.Ctrl != 0: // Ctrl 点击只改选中,不打开也不展开。 a.toggleSelect(path) a.win.Update(func() {}) return false case mods&ui.Shift != 0: a.selectRange(path) a.win.Update(func() {}) return false } a.selectOnly(path) return true } // mainView 是右侧那半边的容器:左侧让出 4 DIP 的沟。 func (a *App) mainView(c *ui.Context) { a.mainPanel(c) } // splitter 是项目岛与编辑岛之间的分隔条:宽度与岛屿间距一致, // 全程不画任何颜色(拖动时也不染色),只靠光标形状提示可拖动。 // // 拖拽用「起点宽度 + 累计位移」算绝对值,拖动期间绝不复位位移: // 一旦每帧归零,框架会认为元素被拖回原位,产生假的负数增量, // 侧栏会被一路顶到最小值(踩过一次)。 func (a *App) splitter(c *ui.Context) { if a.splitDrag == nil { a.splitDrag = &ui.Point{} } el := ui.Box(c).Width(islandGap).FillHeight().Cursor(ui.CursorResizeEW). Draggable(a.splitDrag, ui.DraggableOptions{Axis: ui.Horizontal}) _, _, held := el.Dragged() if held { if !a.splitHeld { a.splitHeld = true a.splitBase = a.sidebar } w := a.splitBase + a.splitDrag.X if w < sidebarMin { w = sidebarMin } if max, _ := c.Size(); max > 0 && w > max-420 { w = max - 420 } a.sidebar = w return } if a.splitHeld { // 松手:结束这次拖动并复位位移,元素回到布局位置。 a.splitHeld = false a.splitDrag.X = 0 a.splitDrag.Y = 0 a.saveSession() } } // statusCard 是底部状态栏浮岛。 func (a *App) statusCard(c *ui.Context) { t := c.Theme() ui.Row(c).FillWidth().Radius(t.Radius).Background(t.Surface).Clip().Children(func() { if d := a.doc(); d != nil { a.statusBar(c, d) } else { a.emptyStatusBar(c) } }) } // mainPanel 是标签栏、编辑区与底部面板组成的岛屿。 // 状态栏不在这里:它归窗口底色,由 view 画。 func (a *App) mainPanel(c *ui.Context) { t := c.Theme() ui.Column(c).Grow(1).FillWidth().Radius(t.Radius).Background(t.Surface).Clip().Children(func() { // 没有打开文件时不画标签栏:空标签栏里只剩一条撑开的空白和它的 // 下边框,会在编辑区顶部留下一条莫名其妙的横线。 if len(a.docs) > 0 { a.tabBar(c) } if a.panel { ui.SplitVertical(c, &a.panelH, func() { a.editorArea(c, false) }, func() { a.panelView(c) }).Grow(1) } else { a.editorArea(c, true) } }) } // editorArea 画编辑区;grow 为真时占满剩余高度。 func (a *App) editorArea(c *ui.Context, grow bool) { t := c.Theme() // 差异视图 / 历史视图开着时占满编辑区。 if a.histViewActive() { a.historyView(c) return } if a.diffActive() { a.renderDiff(c) return } d := a.doc() if d == nil { e := ui.Column(c).Center().Gap(6).Children(func() { ui.Text(c, "没有打开的文件").FontSize(14).TextColor(t.TextMuted) ui.Text(c, "在左侧单击文件即可打开").FontSize(12).TextColor(t.TextMuted) }) if grow { e.Grow(1) } else { e.Fill() } return } a.runPendingCmd(d) a.decorate(d) // 关掉「可以滚过末尾」:这是编辑器滚动条在自己内部唯一的判据 // (scrollbars() 里 `ed.scrollMax() > 0`,而 scrollMax 在 scrollPastEnd // 为真时是 `total - lineHeight`,短文本也大于 0,于是短文件照样画滚动条)。 // 关掉后 scrollMax 变成 `total - viewH`,内容不满一页时被 max(0, …) 归零, // 滚动条自然消失;长文件不受影响,仍然照常滚动。 // // mygo v0.7.4 起滚动条的滑块改成「指针在编辑器上或拖着它时才画」 // (paintScrollbars 里 `shown := ed.pointerIn || ed.scrollbar.active`), // 但判据本身没变:不关的话,短文件只要鼠标一移进来,仍会冒出整条滑块。 d.st.NoScrollPastEnd = true // 上面是面包屑,下面是编辑器 + 缩略图。 ui.Column(c).Fill().Children(func() { a.breadcrumb(c, d) // 编辑器与缩略图并排:编辑器吃满剩余宽度,缩略图固定窄条。 ui.Row(c).Grow(1).AlignItems(ui.Stretch).Children(func() { e := editor.View(c, d.st).FillHeight().Grow(1) // AutoFocus 只在元素**首次出现**的那一帧生效——编辑器一直都在, // 所以弹窗/面板关掉后调它没有任何作用。要抢回焦点得用 Focus, // 但它「每帧调用就每帧保持」,只能在我们真正需要时调一次。 if a.wantFocus { e.Focus() a.wantFocus = false } else if grow { e.AutoFocus() } // Bounds 是上一帧布局的矩形,首帧为 0,缩略图会跳过视口框。 a.minimap(c, d, e.Bounds().H) }) }) a.lspSync(d) } // tabBar 是打开文件的标签栏,每个标签自带关闭按钮。 func (a *App) tabBar(c *ui.Context) { t := c.Theme() ui.Row(c).Padding(4, 6).Gap(2).AlignItems(ui.Center). BorderWidth(0, 0, 1, 0).BorderColor(t.Border).Children(func() { for _, d := range a.docs { d := d active := d.path == a.active bg := t.Background fg := t.TextMuted if active { bg = t.Surface fg = t.Text } ui.Row(c).Radius(3).Background(bg).AlignItems(ui.Center). Children(func() { label := a.tabLabel(d) if d.buf.Modified() { label = "● " + label } open := ui.ButtonBase(c).Padding(3, 7).Children(func() { ui.Text(c, label).FontSize(12).TextColor(fg).SingleLine() }) // 右键:关闭左边/右边/其他/全部/已保存(tabmenu.go)。 // 先把这条设为 active,是因为菜单本身没有标题,用户得靠 // 标签高亮确认「关闭左边」是相对哪一个算的。 if open.RightClicked() { a.active = d.path a.openPop(open, a.tabEntries(d.path)) } if open.Clicked() { a.active = d.path } close := ui.ButtonBase(c).Padding(3, 5).Radius(3).Children(func() { icon(c, "close", 11, t.TextMuted) }).Tooltip("关闭") if close.Clicked() { go a.CloseDoc(d.path) } }).Padding(0, 1) } ui.Box(c).Grow(1) }) } // statusBar 是底部状态栏,左侧光标与行数,右侧诊断、编码与语言。 func (a *App) statusBar(c *ui.Context, d *Doc) { t := c.Theme() pos := d.st.CursorPos() lang := "纯文本" if l := d.buf.Language(); l != nil { lang = l.Name } eol := "LF" if strings.Contains(d.buf.LineEnding(), "\r") { eol = "CRLF" } errs, warns := 0, 0 for _, p := range d.problems { switch p.Severity { case editor.SeverityError: errs++ case editor.SeverityWarning: warns++ } } ui.Row(c).Padding(0, 2).AlignItems(ui.Center).Height(24).Children(func() { segment(c, fmt.Sprintf("行 %d,列 %d", pos.Line+1, pos.Col+1), t.TextMuted) divider(c) segment(c, fmt.Sprintf("%d 行", d.buf.LineCount()), t.TextMuted) divider(c) statusCount(c, "error", errs, severityColor(t, errs, t.Danger)) statusCount(c, "warning", warns, severityColor(t, warns, t.Warning)) ui.Box(c).Grow(1) if a.gst.info.OK { segment(c, "⎇ "+a.gst.info.Branch, t.TextMuted) divider(c) if n := a.gst.info.Dirty; n > 0 { segment(c, fmt.Sprintf("%d 处改动", n), t.Warning) divider(c) } } if a.lspState != "" { segment(c, a.lspState, t.TextMuted) divider(c) } segment(c, eol, t.TextMuted) divider(c) if cv := a.coverStatText(d.path); cv != "" { segment(c, cv, t.TextMuted) divider(c) } if stat := a.diffStatText(d.path); stat != "" { segment(c, stat, t.TextMuted) divider(c) } segment(c, encName(d.enc), t.TextMuted) divider(c) if d.truncated { // 截断过就说明看到的不是全文,必须说清楚。 segment(c, "只读·已截断", t.Warning) divider(c) } segment(c, lang, t.TextMuted) divider(c) a.statusPanelHint(c) }) } // emptyStatusBar 在没有打开文件时也占住状态栏高度。 func (a *App) emptyStatusBar(c *ui.Context) { t := c.Theme() ui.Row(c).Padding(0, 8).AlignItems(ui.Center).Height(24).Children(func() { ui.Box(c).Grow(1) if a.gst.info.OK { segment(c, "⎇ "+a.gst.info.Branch, t.TextMuted) divider(c) if n := a.gst.info.Dirty; n > 0 { segment(c, fmt.Sprintf("%d 处改动", n), t.Warning) divider(c) } } if a.lspState != "" { segment(c, a.lspState, t.TextMuted) divider(c) } a.statusPanelHint(c) }) } // segment 是状态栏的一段文字。 func segment(c *ui.Context, text string, color ui.Color) { ui.Text(c, text).FontSize(11).TextColor(color).Padding(0, 7) } // divider 是状态栏分段之间的竖线。 func divider(c *ui.Context) { ui.Box(c).Width(1).Height(12).Background(c.Theme().Border) } // severityColor 在数量为 0 时用弱化色。 func severityColor(t *ui.Theme, n int, on ui.Color) ui.Color { if n == 0 { return t.TextMuted } return on } // statusCount 画一个带图标的计数。 func statusCount(c *ui.Context, name string, n int, color ui.Color) { ui.Row(c).Gap(3).Padding(0, 6).AlignItems(ui.Center).Children(func() { icon(c, name, 11, color) ui.Textf(c, "%d", n).FontSize(11).TextColor(color) }) } // activeIndex 返回当前文件在标签栏中的下标,没有则为 -1。 func (a *App) activeIndex() int { for i, d := range a.docs { if d.path == a.active { return i } } if len(a.docs) > 0 { return 0 } return -1 } // CollapseAll 收起文件树的所有目录。 func (a *App) CollapseAll() { apply := func() { for _, p := range a.open { *p = false } } if a.win == nil { apply() return } a.win.Update(apply) } // ToggleSidebar 显示或隐藏左侧项目工具窗。 func (a *App) ToggleSidebar() { a.sidebarOn = !a.sidebarOn a.win.Update(func() {}) } // ToggleMinimap 显示或隐藏编辑器右侧的代码缩略图。 func (a *App) ToggleMinimap() { a.minimapOn = !a.minimapOn a.win.Update(func() {}) }