From 48d1781fed3f9ea5cfd2b270bd92fd2826605636 Mon Sep 17 00:00:00 2001 From: Lukasz Kasprzak Date: Thu, 17 Sep 2026 10:56:19 +0200 Subject: gui: conditions as a nested tree, and a way to add a directory --- gui/internal/ui/forms.go | 173 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 144 insertions(+), 29 deletions(-) (limited to 'gui/internal/ui/forms.go') diff --git a/gui/internal/ui/forms.go b/gui/internal/ui/forms.go index 8e1fd33..18a8b13 100644 --- a/gui/internal/ui/forms.go +++ b/gui/internal/ui/forms.go @@ -655,18 +655,18 @@ func hitsText(h *model.RuleHits, root string) string { // formEditor is the widgets of one form, and can write it back. type formEditor struct { - kind model.FormKind - root *gtk.Box - name *gtk.Entry - conds *gtk.Box - rows []*condRow - acts *gtk.Box - arows []*actionRow - stop *gtk.CheckButton - cse *gtk.DropDown - fold *gtk.DropDown - onConf *gtk.DropDown - changed func() + kind model.FormKind + root *gtk.Box + name *gtk.Entry + conds *gtk.Box + conditions []*model.Cond + acts *gtk.Box + arows []*actionRow + stop *gtk.CheckButton + cse *gtk.DropDown + fold *gtk.DropDown + onConf *gtk.DropDown + changed func() } func newFormEditor(form model.Form, changed func()) *formEditor { @@ -685,15 +685,12 @@ func newFormEditor(form model.Form, changed func()) *formEditor { fe.root.Append(field("Name", fe.name)) } - fe.conds = gtk.NewBox(gtk.OrientationVertical, 4) + fe.conds = gtk.NewBox(gtk.OrientationVertical, 2) fe.root.Append(heading("Conditions - all of these must hold")) fe.root.Append(fe.conds) - addCond := gtk.NewButtonWithLabel("+ test") - addCond.ConnectClicked(func() { - fe.addCond(nil) - changed() - }) - fe.root.Append(leftAligned(addCond)) + fe.root.Append(leftAligned(fe.addMenu(func(kind string) { + fe.conditions = append(fe.conditions, &model.Cond{Kind: kind}) + }))) when := form.Rule.When if form.Kind == model.ExcludeForm { @@ -702,8 +699,9 @@ func newFormEditor(form model.Form, changed func()) *formEditor { when = nil } for _, c := range when { - fe.addCond(c) + fe.conditions = append(fe.conditions, model.ParseCond(c)) } + fe.drawConds() if form.Kind == model.RuleForm { fe.acts = gtk.NewBox(gtk.OrientationVertical, 4) @@ -738,11 +736,11 @@ func newFormEditor(form model.Form, changed func()) *formEditor { // text is the form as the printer writes it, from what the widgets hold. func (fe *formEditor) text() (string, error) { var when []*sexp.Node - for _, row := range fe.rows { - if row.gone { - continue + for _, c := range fe.conditions { + text := c.Text() + if text == "" { + return "", fmt.Errorf("(%s ...) has no condition under it", c.Kind) } - text := row.text() nodes, err := sexp.Parse("form", []byte(text)) if err != nil { return "", fmt.Errorf("%s: %v", text, err) @@ -774,11 +772,128 @@ func (fe *formEditor) text() (string, error) { return config.PrintRule(rule), nil } -// addCond adds a condition row, filled in from n when there is one. -func (fe *formEditor) addCond(n *sexp.Node) { - row := newCondRow(n, fe.changed) - fe.rows = append(fe.rows, row) - fe.conds.Append(row.root) +// drawConds rebuilds the condition tree: one row per condition, indented by +// how deep it sits, with the operators holding the conditions under them +// (GUI design ยง5.1, his choice 2026-09-17). +func (fe *formEditor) drawConds() { + for child := fe.conds.FirstChild(); child != nil; child = fe.conds.FirstChild() { + fe.conds.Remove(child) + } + for _, root := range fe.conditions { + root.Walk(0, nil, func(node, parent *model.Cond, depth int) { + fe.conds.Append(fe.condRow(node, parent, depth)) + }) + } +} + +// addMenu is the "+" that puts a condition somewhere: a test, or one of the +// three operators to hold more conditions under it. +func (fe *formEditor) addMenu(add func(kind string)) *gtk.MenuButton { + box := gtk.NewBox(gtk.OrientationVertical, 0) + pop := gtk.NewPopover() + pop.SetChild(box) + button := gtk.NewMenuButton() + button.SetLabel("+") + button.SetTooltipText("add a test here, or and / or / not to hold more conditions") + button.SetPopover(pop) + for _, item := range []struct{ label, kind string }{ + {"test", "type"}, + {"and (all of these)", "and"}, + {"or (any of these)", "or"}, + {"not (none of these)", "not"}, + } { + b := gtk.NewButtonWithLabel(item.label) + b.SetHasFrame(false) + b.SetHAlign(gtk.AlignFill) + kind := item.kind + b.ConnectClicked(func() { + pop.Popdown() + add(kind) + fe.drawConds() + fe.changed() + }) + box.Append(b) + } + return button +} + +// condRow is one line of the tree. +func (fe *formEditor) condRow(node, parent *model.Cond, depth int) *gtk.Box { + row := gtk.NewBox(gtk.OrientationHorizontal, 6) + row.SetMarginStart(depth * 24) + + kind := gtk.NewDropDownFromStrings(condItems()) + if i := indexOf(condKinds, node.Kind); i >= 0 { + kind.SetSelected(uint(i)) + } + kind.Connect("notify::selected", func() { + next := condKinds[kind.Selected()] + if next == node.Kind { + return + } + node.Kind = next + if !model.CondOps[next] { + node.Children = nil + } + fe.drawConds() + fe.changed() + }) + row.Append(kind) + + if model.CondOps[node.Kind] { + // An operator holds conditions rather than arguments. One button + // puts them under it: four side by side ran off the pane. + row.Append(fe.addMenu(func(kind string) { node.Add(kind) })) + row.Append(gtk.NewLabel("")) + } else { + args := gtk.NewEntry() + args.SetText(node.Args) + args.SetHExpand(true) + hint := condHints[node.Kind] + args.SetPlaceholderText(hint) + args.SetTooltipText(hint) + if isCompare(node.Kind) { + op := gtk.NewDropDownFromStrings(compareOps) + opText, value := splitCompare(node.Args) + if i := indexOf(compareOps, opText); i >= 0 { + op.SetSelected(uint(i)) + } + args.SetText(value) + write := func() { + node.Args = compareOps[op.Selected()] + " " + strings.TrimSpace(args.Text()) + fe.changed() + } + op.Connect("notify::selected", func() { write() }) + args.ConnectChanged(func() { write() }) + row.Append(op) + } else { + args.ConnectChanged(func() { + node.Args = strings.TrimSpace(args.Text()) + fe.changed() + }) + } + row.Append(args) + } + + remove := gtk.NewButtonWithLabel("-") + remove.SetHasFrame(false) + remove.SetTooltipText("take this condition out") + remove.ConnectClicked(func() { + if parent != nil { + parent.Remove(node) + } else { + for i, c := range fe.conditions { + if c == node { + fe.conditions = append(fe.conditions[:i], fe.conditions[i+1:]...) + break + } + } + } + fe.drawConds() + fe.changed() + }) + row.Append(remove) + return row } // addAction adds an action row. -- cgit v1.3