Add submenu

clscls
edited March 2015 in Ruby Scripting
Hi Matthias,

I'm very new on Ruby...
To refer your example about simple menu adding and try to add sub-menu on it

the code will look like...

app = RBA::Application.instance

$menu_handler = MenuHandler.new
$menu_handler.title = "RBA test"

menu = app.main_window.menu
menu.insert_menu("@toolbar.end", "Func", "Function")

menu.insert_item("@toolbar.Func.end", "rba_test1", $menu_handler)
menu.insert_item("@toolbar.Func.end", "rba_test2", $menu_handler)


and the result only show "rba_test2"
any mistake I make?

Comments

  • edited November -1

    Hi cls,

    You clearly followed the example on this page: http://www.klayout.de/0.22/doc/programming/traditional.html.

    You can do it with 'MenuHandler' as I talk more about below, but 'MenuHandler' is not necessary. Let's look at maybe the simplest example to get you started.

    app = RBA::Application.instance
    menu = app.main_window.menu
    menu.insert_menu("@toolbar.end", "Func", "Function")
    
    a = Action.new
    a.title = "A script"
    a.on_triggered { load "path/to/script.rb" } # or whatever
    b = Action.new
    b.title = "My other script"
    b.on_triggered { load "path/to/script2.rb" } # or whatever
    
    menu.insert_item("@toolbar.Func.end", "rba_test1", a)
    menu.insert_item("@toolbar.Func.end", "rba_test2", b)
    

    In the future, perhaps a constructor can be added to the Action class in RBA to take care of the 'a. ...' and 'b. ...' lines all in one fell swoop. But for right now you need at least three lines for each one. Or you could do it by extending the MenuHandler class with a new constructor (Google for 'def initialize').

    (Note, in this forum, you can indent each line by at least four spaces to get it to show up as code, as you see above. Google for "Markdown syntax")

    Lastly, I would avoid "$" global variables if unnecessary. In rare cases they are truly necessary or at least preferable, though.

    David

  • edited November -1

    Hello,

    thanks for these value comments, David!

    And sure, the constructor can be added. My list of TODO items is growing longer every day ...

    Matthias

Sign In or Register to comment.