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"
"rickub.com/turbo-editors/turbo-core/filetree"
"rickub.com/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()
}
}
|