Skip to content

Commit

Permalink
Merge pull request #349 from cogentcore/app-bar
Browse files Browse the repository at this point in the history
Update to core app bar changes
  • Loading branch information
rcoreilly authored Sep 2, 2024
2 parents 748f1d6 + 9a8aaa0 commit a0741c3
Show file tree
Hide file tree
Showing 36 changed files with 392 additions and 361 deletions.
22 changes: 12 additions & 10 deletions ai/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,19 @@ import (

func main() { // TODO(config)
b := core.NewBody("Cogent AI")
b.AddAppBar(func(p *tree.Plan) {
tree.Add(p, func(w *core.Button) {
w.SetText("Install").SetIcon(icons.Download)
})
tree.Add(p, func(w *core.Button) {
w.SetText("Start server").SetIcon(icons.PlayArrow).OnClick(func(e events.Event) {
core.ErrorSnackbar(b, exec.Verbose().Run("ollama", "serve"))
b.AddTopBar(func(bar *core.Frame) {
core.NewToolbar(bar).Maker(func(p *tree.Plan) {
tree.Add(p, func(w *core.Button) {
w.SetText("Install").SetIcon(icons.Download)
})
tree.Add(p, func(w *core.Button) {
w.SetText("Start server").SetIcon(icons.PlayArrow).OnClick(func(e events.Event) {
core.ErrorSnackbar(b, exec.Verbose().Run("ollama", "serve"))
})
})
tree.Add(p, func(w *core.Button) {
w.SetText("Stop server").SetIcon(icons.Stop)
})
})
tree.Add(p, func(w *core.Button) {
w.SetText("Stop server").SetIcon(icons.Stop)
})
})

Expand Down
22 changes: 12 additions & 10 deletions canvas/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ func (cv *Canvas) Init() {
if !cv.EditState.Changed {
return false
}
d.AddTitle("Unsaved changes").
AddText(fmt.Sprintf("There are unsaved changes in %s", fsx.DirAndFile(string(cv.Filename))))
d.AddBottomBar(func(parent core.Widget) {
d.AddOK(parent).SetText("Close without saving").OnClick(func(e events.Event) {
d.SetTitle("Unsaved changes")
core.NewText(d).SetType(core.TextSupporting).SetText(fmt.Sprintf("There are unsaved changes in %s", fsx.DirAndFile(string(cv.Filename))))
d.AddBottomBar(func(bar *core.Frame) {
d.AddOK(bar).SetText("Close without saving").OnClick(func(e events.Event) {
cv.Scene.Close()
})
d.AddOK(parent).SetText("Save and close").OnClick(func(e events.Event) {
d.AddOK(bar).SetText("Save and close").OnClick(func(e events.Event) {
cv.SaveDrawing()
cv.Scene.Close()
})
Expand Down Expand Up @@ -230,11 +230,11 @@ func (cv *Canvas) PromptPhysSize() { //types:add
sv := cv.SVG()
sz := &PhysSize{}
sz.SetFromSVG(sv)
d := core.NewBody().AddTitle("SVG physical size")
d := core.NewBody("SVG physical size")
core.NewForm(d).SetStruct(sz)
d.AddBottomBar(func(parent core.Widget) {
d.AddCancel(parent)
d.AddOK(parent).OnClick(func(e events.Event) {
d.AddBottomBar(func(bar *core.Frame) {
d.AddCancel(bar)
d.AddOK(bar).OnClick(func(e events.Event) {
cv.SetPhysSize(sz)
sv.backgroundGridEff = -1
sv.UpdateView(true)
Expand Down Expand Up @@ -580,7 +580,9 @@ func NewWindow(fnm string) *Canvas {
b := core.NewBody(winm).SetTitle(winm)

cv := NewCanvas(b)
b.AddAppBar(cv.MakeToolbar)
b.AddTopBar(func(bar *core.Frame) {
core.NewToolbar(bar).Maker(cv.MakeToolbar)
})

b.OnShow(func(e events.Event) {
if fnm != "" {
Expand Down
32 changes: 17 additions & 15 deletions canvas/splits.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,23 @@ func SplitsView(pt *Splits) {
AvailableSplitsChanged = true
})

d.AddAppBar(func(p *tree.Plan) {
tree.Add(p, func(w *core.FuncButton) {
w.SetFunc(pt.SaveSettings).SetText("Save to settings").
SetIcon(icons.Save).SetKey(keymap.Save).
FirstStyler(func(s *styles.Style) {
s.SetEnabled(AvailableSplitsChanged && pt == &StandardSplits)
})
})
tree.Add(p, func(w *core.FuncButton) {
w.SetFunc(pt.Open).SetText("Open").SetIcon(icons.Open).SetKey(keymap.Open)
w.Args[0].SetTag(`extension:".toml"`)
})
tree.Add(p, func(w *core.FuncButton) {
w.SetFunc(pt.Save).SetText("Save as").SetIcon(icons.SaveAs).SetKey(keymap.SaveAs)
w.Args[0].SetTag(`extension:".toml"`)
d.AddTopBar(func(bar *core.Frame) {
core.NewToolbar(bar).Maker(func(p *tree.Plan) {
tree.Add(p, func(w *core.FuncButton) {
w.SetFunc(pt.SaveSettings).SetText("Save to settings").
SetIcon(icons.Save).SetKey(keymap.Save).
FirstStyler(func(s *styles.Style) {
s.SetEnabled(AvailableSplitsChanged && pt == &StandardSplits)
})
})
tree.Add(p, func(w *core.FuncButton) {
w.SetFunc(pt.Open).SetText("Open").SetIcon(icons.Open).SetKey(keymap.Open)
w.Args[0].SetTag(`extension:".toml"`)
})
tree.Add(p, func(w *core.FuncButton) {
w.SetFunc(pt.Save).SetText("Save as").SetIcon(icons.SaveAs).SetKey(keymap.SaveAs)
w.Args[0].SetTag(`extension:".toml"`)
})
})
})
d.RunWindow()
Expand Down
2 changes: 1 addition & 1 deletion canvas/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ func (sv *SVG) ReadMetaData() {

// EditNode opens a [core.Form] dialog on the given node.
func (sv *SVG) EditNode(n tree.Node) { //types:add
d := core.NewBody().AddTitle("Edit node")
d := core.NewBody("Edit node")
core.NewForm(d).SetStruct(n)
d.RunWindowDialog(sv)
}
Expand Down
145 changes: 64 additions & 81 deletions code/appbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,7 @@ import (
"cogentcore.org/core/tree"
)

func (cv *Code) MakeToolbar(p *tree.Plan) { //types:add
tree.AddInit(p, "app-chooser", func(w *core.Chooser) {
cv.AddChooserFiles(w)
cv.AddChooserSymbols(w)
p.Parent.(*core.Toolbar).AddOverflowMenu(cv.OverflowMenu)
w.OnFirst(events.KeyChord, func(e events.Event) {
kf := keymap.Of(e.KeyChord())
if kf == keymap.Abort {
w.ClearError()
cv.FocusActiveTextEditor()
}
})
})
func (cv *Code) MakeToolbar(p *tree.Plan) {
tree.Add(p, func(w *core.FuncButton) {
w.SetFunc(cv.UpdateFiles).SetText("").SetIcon(icons.Refresh).SetShortcut("Command+U")
})
Expand Down Expand Up @@ -284,82 +272,77 @@ func (cv *Code) OverflowMenu(m *core.Scene) {

core.NewFuncButton(m).SetFunc(cv.HelpWiki).SetText("Help").SetIcon(icons.Help)
})

core.NewSeparator(m)
}

// AddChooserFiles adds the files to the app chooser.
func (cv *Code) AddChooserFiles(ac *core.Chooser) {
ac.AddItemsFunc(func() {
if cv.Files == nil {
return
}
cv.Files.WidgetWalkDown(func(cw core.Widget, cwb *core.WidgetBase) bool {
fn := filetree.AsNode(cw)
if fn == nil || fn.IsIrregular() {
return tree.Continue
}
rpath := fn.RelativePath()
nmpath := fn.Name + ":" + rpath
switch {
case fn.IsDir():
ac.Items = append(ac.Items, core.ChooserItem{
Text: nmpath,
Icon: icons.Folder,
Func: func() {
fn.Open()
fn.ScrollToThis()
ac.CallItemsFuncs() // refresh avail files
},
})
case fn.IsExec():
ac.Items = append(ac.Items, core.ChooserItem{
Text: nmpath,
Icon: icons.FileExe,
Func: func() {
cv.FileNodeRunExe(fn)
},
})
default:
ac.Items = append(ac.Items, core.ChooserItem{
Text: nmpath,
Icon: fn.Info.Ic,
Func: func() {
cv.NextViewFileNode(fn)
ac.CallItemsFuncs() // refresh avail files
},
})
}
return tree.Continue
})
})
func (cv *Code) MenuSearch(items *[]core.ChooserItem) {
cv.addSearchFiles(items)
cv.addSearchSymbols(items)
}

// AddChooserSymbols adds the symbols to the app chooser.
func (cv *Code) AddChooserSymbols(ac *core.Chooser) {
ac.AddItemsFunc(func() {
tv := cv.ActiveTextEditor()
if tv == nil || tv.Buffer == nil || !tv.Buffer.Highlighter.UsingParse() {
return
}
pfs := tv.Buffer.ParseState.Done()
if len(pfs.ParseState.Scopes) == 0 {
return
func (cv *Code) addSearchFiles(items *[]core.ChooserItem) {
if cv.Files == nil {
return
}
cv.Files.WidgetWalkDown(func(cw core.Widget, cwb *core.WidgetBase) bool {
fn := filetree.AsNode(cw)
if fn == nil || fn.IsIrregular() {
return tree.Continue
}
pkg := pfs.ParseState.Scopes[0] // first scope of parse state is the full set of package symbols
syms := NewSymNode()
syms.SetName("syms")
syms.OpenSyms(pkg, "", "")
syms.WalkDown(func(k tree.Node) bool {
sn := k.(*SymNode)
ac.Items = append(ac.Items, core.ChooserItem{
Text: sn.Symbol.Label(),
Icon: sn.GetIcon(),
rpath := fn.RelativePath()
nmpath := fn.Name + ":" + rpath
switch {
case fn.IsDir():
*items = append(*items, core.ChooserItem{
Text: nmpath,
Icon: icons.Folder,
Func: func() {
SelectSymbol(cv, sn.Symbol)
fn.Open()
fn.ScrollToThis()
},
})
return tree.Continue
case fn.IsExec():
*items = append(*items, core.ChooserItem{
Text: nmpath,
Icon: icons.FileExe,
Func: func() {
cv.FileNodeRunExe(fn)
},
})
default:
*items = append(*items, core.ChooserItem{
Text: nmpath,
Icon: fn.Info.Ic,
Func: func() {
cv.NextViewFileNode(fn)
},
})
}
return tree.Continue
})
}

func (cv *Code) addSearchSymbols(items *[]core.ChooserItem) {
tv := cv.ActiveTextEditor()
if tv == nil || tv.Buffer == nil || !tv.Buffer.Highlighter.UsingParse() {
return
}
pfs := tv.Buffer.ParseState.Done()
if len(pfs.ParseState.Scopes) == 0 {
return
}
pkg := pfs.ParseState.Scopes[0] // first scope of parse state is the full set of package symbols
syms := NewSymNode()
syms.SetName("syms")
syms.OpenSyms(pkg, "", "")
syms.WalkDown(func(k tree.Node) bool {
sn := k.(*SymNode)
*items = append(*items, core.ChooserItem{
Text: sn.Symbol.Label(),
Icon: sn.GetIcon(),
Func: func() {
SelectSymbol(cv, sn.Symbol)
},
})
return tree.Continue
})
}
32 changes: 18 additions & 14 deletions code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ func (cv *Code) OpenRecent(filename core.Filename) { //types:add

// EditRecentPaths opens a dialog editor for editing the recent project paths list
func (cv *Code) EditRecentPaths() {
d := core.NewBody().AddTitle("Recent project paths").
AddText("You can delete paths you no longer use")
d := core.NewBody("Recent project paths")
core.NewText(d).SetType(core.TextSupporting).SetText("You can delete paths you no longer use")
core.NewList(d).SetSlice(&RecentPaths)
d.AddOKOnly().RunDialog(cv)
}
Expand Down Expand Up @@ -528,19 +528,19 @@ func (cv *Code) SaveAllCheck(cancelOpt bool, fun func()) bool {
}
return false
}
d := core.NewBody().AddTitle("There are Unsaved Files").
AddText(fmt.Sprintf("In Project: %v There are <b>%v</b> opened files with <b>unsaved changes</b> -- do you want to save all?", cv.Name, nch))
d.AddBottomBar(func(parent core.Widget) {
d := core.NewBody("There are Unsaved Files")
core.NewText(d).SetType(core.TextSupporting).SetText(fmt.Sprintf("In Project: %v There are <b>%v</b> opened files with <b>unsaved changes</b> -- do you want to save all?", cv.Name, nch))
d.AddBottomBar(func(bar *core.Frame) {
if cancelOpt {
d.AddCancel(parent).SetText("Cancel Command")
d.AddCancel(bar).SetText("Cancel Command")
}
core.NewButton(parent).SetText("Don't Save").OnClick(func(e events.Event) {
core.NewButton(bar).SetText("Don't Save").OnClick(func(e events.Event) {
d.Close()
if fun != nil {
fun()
}
})
core.NewButton(parent).SetText("Save All").OnClick(func(e events.Event) {
core.NewButton(bar).SetText("Save All").OnClick(func(e events.Event) {
d.Close()
cv.SaveAllOpenNodes()
if fun != nil {
Expand Down Expand Up @@ -614,13 +614,13 @@ func (cv *Code) AddCloseDialog() {
if nch == 0 {
return false
}
d.AddTitle("Unsaved files").
AddText(fmt.Sprintf("There are %d open files in %s with unsaved changes", nch, cv.Name))
d.AddBottomBar(func(parent core.Widget) {
d.AddOK(parent).SetText("Close without saving").OnClick(func(e events.Event) {
d.SetTitle("Unsaved files")
core.NewText(d).SetType(core.TextSupporting).SetText(fmt.Sprintf("There are %d open files in %s with unsaved changes", nch, cv.Name))
d.AddBottomBar(func(bar *core.Frame) {
d.AddOK(bar).SetText("Close without saving").OnClick(func(e events.Event) {
cv.Scene.Close()
})
core.NewButton(parent).SetText("Save and close").OnClick(func(e events.Event) {
core.NewButton(bar).SetText("Save and close").OnClick(func(e events.Event) {
cv.SaveAllOpenNodes()
cv.Scene.Close()
})
Expand Down Expand Up @@ -669,7 +669,11 @@ func NewCodeWindow(path, projnm, root string, doPath bool) *Code {
b := core.NewBody(winm).SetTitle(winm)
cv := NewCode(b)
cv.Defaults()
b.AddAppBar(cv.MakeToolbar)
b.AddTopBar(func(bar *core.Frame) {
tb := core.NewToolbar(bar)
tb.Maker(cv.MakeToolbar)
tb.AddOverflowMenu(cv.OverflowMenu)
})
cv.Update() // get first pass so settings stick

if doPath {
Expand Down
10 changes: 5 additions & 5 deletions code/commandbufs.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,17 @@ func (cv *Code) CommitNoChecks() {
}
cv.SetArgVarVals() // need to set before setting prompt string below..

d := core.NewBody().AddTitle("Commit message").
AddText("Please enter your commit message here. Remember that this is essential documentation. Author information comes from the Cogent Core User Settings.")
d := core.NewBody("Commit message")
core.NewText(d).SetType(core.TextSupporting).SetText("Please enter your commit message here. Remember that this is essential documentation. Author information comes from the Cogent Core User Settings.")
tf := core.NewTextField(d)
curval, _ := CmdPrompt1Vals["Commit"]
tf.SetText(curval)
tf.Styler(func(s *styles.Style) {
s.Min.X.Ch(100)
})
d.AddBottomBar(func(parent core.Widget) {
d.AddCancel(parent)
d.AddOK(parent).SetText("Commit").OnClick(func(e events.Event) {
d.AddBottomBar(func(bar *core.Frame) {
d.AddCancel(bar)
d.AddOK(bar).SetText("Commit").OnClick(func(e events.Event) {
val := tf.Text()
cv.ArgVals["{PromptString1}"] = val
CmdPrompt1Vals["Commit"] = val
Expand Down
Loading

0 comments on commit a0741c3

Please sign in to comment.