Skip to content

implement a global menubar#325

Open
Misko-2083 wants to merge 4 commits intov1cont:masterfrom
Misko-2083:master
Open

implement a global menubar#325
Misko-2083 wants to merge 4 commits intov1cont:masterfrom
Misko-2083:master

Conversation

@Misko-2083
Copy link
Contributor

@Misko-2083 Misko-2083 commented Dec 20, 2025

The Menubar in YAD option lets you create pretty sophisticated menu systems right from a single string in your script. It's super useful for building dynamic, interactive dialogs without needing a full-blown GUI toolkit. Here's what it can do:
Deep nesting: You can have as many submenu levels as you want, no practical limits.
Stateful toggles: Checkboxes that actually remember their state and show checked/unchecked icons.
Action binding: Items can run shell commands directly.
Visual stuff: It pulls in theme icons where possible, or you can embed custom ones.

How It's Built (The Parsing Logic)Under the hood, YAD uses a straightforward stack-based parser to handle the menu string. It's linear, it just goes left to right through the comma-separated items without any fancy recursion. They use a parent stack to keep track of the current menu level.
Here's roughly how it works:
The input string: Everything's separated by commas, and each item is typically in the format Label|Command|Type (or variations with icons, etc.).
The stack: Starts with the main menubar as the only parent. This tracks the current container you're adding items to.
Opening a submenu: If a label ends with ;; (or whatever marker they're using internally), it creates a new GtkMenu, attaches it as a submenu to the current parent, and pushes it onto the stack. From then on, new items go into that submenu.
Closing a submenu: An empty entry (like a double comma ,,) pops the top off the stack, so you're back to adding to the previous level.
Label part: A proper GTK label that supports mnemonics (like _File for Alt+F shortcuts).

Example:

yad --menubar="_File;;_New|notify-send 'New'|document-new,_Open|notify-send 'Open'|document-open,,_Quit|quit|application-exit,_Settings;;_Network|_Ethernet|notify-send 'Eth'|network-wired,_Wifi|notify-send 'Wifi'|network-wireless,,_Display|notify-send 'Display'|video-display,_Sound|notify-send 'Sound'|audio-volume-high,_Advanced|Hardw_are|notify-send 'HW'|check,Software|notify-send 'SW'|check" --text-info

@Misko-2083
Copy link
Contributor Author

Misko-2083 commented Dec 20, 2025

yad --menubar="File;; \
    New|item-new|document-new, \
    Open|item-open|document-open, \
    Save|item-save|document-save, \
    , \
    Exit|echo $YAD_PID|application-exit, \
Edit;; \
    Cut|item-cut|edit-cut, \
    Copy|item-copy|edit-copy, \
    Advanced|, \
    Clean Buffers|item-clean|edit-clear, \
    Optimize|item-opt|system-run, \
       , \
     View;; \
    Status Bar|item-stat|check, \
    Tool Bar|item-tool|check, \
Help;; \
    Docs|xdg-open https://github.com/v1cont/yad|help-browser, \
    About|yad --about|help-about" --scale
yad-menubar-file yad-menubar-scale

@cou645
Copy link

cou645 commented Dec 20, 2025 via email

@cou645
Copy link

cou645 commented Dec 20, 2025 via email

@Misko-2083
Copy link
Contributor Author

No, menubar will be added only on the main notebook/paned window.

#!/bin/bash

KEY=$RANDOM

       yad --plug=$KEY --tabnum=1 --text="first tab with text" &
       yad --plug=$KEY --tabnum=2 --text="second tab" --entry  &
       yad --paned  --key=$KEY --tab="Tab 1" --tab="Tab 2" \
                    --menubar="_File;;_New|touch n.txt|document-new,  \
                    _Quit|exit|gtk-quit, _Edit;;_Undo|undo-cmd|edit-undo"

KEY=$RANDOM
       yad --plug=$KEY --tabnum=1 --text="first tab with text" &
       yad --plug=$KEY --tabnum=2 --text="second tab" --entry  &
       yad --notebook  --key=$KEY --tab="Tab 1" --tab="Tab 2" \
                    --menubar="_File;;_New|touch n.txt|document-new, \
                                        _Quit|exit|gtk-quit, _Edit;;_Undo|undo-cmd|edit-undo"

yad --form --field="Menu":mb ...

It's not a form field.

It works as a general option:

yad  --menubar="Menu;;_Quit|exit|gtk-quit"  --form --field="Menu":TXT

Like --text or image: yad --text="$TXT" --image="$IMAGE" --menubar="$MENU" --text-info

@step-
Copy link
Contributor

step- commented Dec 20, 2025

Nice.

Quickly looking at the code it appears:

  • A menu item runs its command in the background without blocking YAD
  • Command output cannot feed back to YAD (which --form can do)
  • Menu bar is a "secondary" main widget, e.g. --menubar + --scale coexist in the same YAD instance
  • Implementation will need changes to work with GTK4 (so will other YAD widgets).

None of the above is meant to criticize @Misko-208's proposal. It's just a list of the features I noticed.

Therefore yad --menubar="_File;;_New|notify-send 'New'|document-new would execute like
yad --button="_File!document-new!sh -c \"notify-send 'New' &\"". Is this accurate? Of course, an advantage of --menubar vs --button is one can organize items hierarchically, without taking up all the space that many buttons will take.

Removed CSS loading function and adjusted menu item activation logic. Improved handling of command execution and menu item creation.
@Misko-2083
Copy link
Contributor Author

Misko-2083 commented Dec 27, 2025

Now it only needs a way to set initial check boxes.
chkbox

@Misko-2083
Copy link
Contributor Author

Therefore yad --menubar="_File;;_New|notify-send 'New'|document-new would execute like yad --button="_File!document-new!sh -c \"notify-send 'New' &\"". Is this accurate? Of course, an advantage of --menubar vs --button is one can organize items hierarchically, without taking up all the space that many buttons will take.

Yes it's the same as button yad --menubar="_File;;_New|sh -c \"notify-send 'New' &\""|document-new except they are hidden in menu and out of the way. And there is also a checkbox.

View;; \
    Status Bar|echo status|check, \
    Tool Bar|echo tool|check,

Currently it’s just a visual on/off switch that runs a command.
Indeed @step- a lot of thing are changed and deprecated in GTK4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments