turbo-editors/turbo-corepublic Fork 0
v0.9.0
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

🛟 Updated. 28d5985 · on v0.9.0 · k33g · 20h ago
tree.go · 78 lines · 2.2 KBGo Blame HistoryRaw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// The project tree window: the files of the project the editor was started
// in, shown as a tree you can open a file from.

package app

import (
	"os"

	"codeberg.org/turbo-editors/turbo-core/filetree"
	"codeberg.org/turbo-editors/turbo-core/ui"
)

// ProjectTree opens a window showing the project's files, or brings the one
// already open to the front.
//
// There is at most one, because a second tree on the same project would be two
// views of one thing with nothing to tell them apart — and the project cannot
// change while the editor runs.
//
// The tree is rooted at the directory the editor was started in, the same rule
// .turbo-go/settings.toml follows, so "the project" means one thing everywhere
// in the editor.
func (a *App) ProjectTree() {
	if a.treeWindow != nil {
		a.desktop.Focus(a.treeWindow)
		return
	}

	root, err := os.Getwd()
	if err != nil {
		a.ShowMessage("Project tree", "Cannot tell which directory this is:\n"+err.Error())
		return
	}

	view, err := filetree.NewView(root)
	if err != nil {
		a.ShowMessage("Project tree", err.Error())
		return
	}
	view.OnOpen = a.Open

	window := ui.NewWindow(view.Title(), view)
	window.SetBounds(a.newWindowBounds())
	window.OnClose = func() bool { a.closeTree(); return true }

	a.desktop.Add(window)
	a.windowsOpened++
	a.treeWindow, a.treeView = window, view
}

// closeTree takes the tree window away and forgets it, so the next F9 opens a
// fresh one rather than trying to focus a window that has gone.
func (a *App) closeTree() {
	if a.treeWindow == nil {
		return
	}
	a.desktop.Remove(a.treeWindow)
	a.treeWindow, a.treeView = nil, nil
	a.completion.Hide()
}

// isTreeWindow reports whether a window holds the project tree. The file
// actions use it to leave the tree alone.
func (a *App) isTreeWindow(window *ui.Window) bool {
	return window != nil && window == a.treeWindow
}

// refreshTree re-reads the project, so a file just written turns up without
// anyone asking for it.
//
// It runs after a save rather than on a timer or a filesystem watch: saving is
// the one moment the editor knows the project has changed, and watching the
// disk would mean a third dependency.
func (a *App) refreshTree() {
	if a.treeView != nil {
		a.treeView.Refresh()
	}
}