// 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() } }