Skip to content

Use GtkBuilder to create App Menu - #2589

Closed
nickleus27 wants to merge 92 commits into
synfig:masterfrom
nickleus27:GtkBuilder
Closed

nickleus27 wants to merge 92 commits into
synfig:masterfrom
nickleus27:GtkBuilder

Conversation

@nickleus27

@nickleus27 nickleus27 commented Mar 23, 2022 •

Copy link
Copy Markdown
Contributor

This is a draft to update deprecated Gtk::UIManager methods to create the applications menu bar. Instead this pull request uses Gtk::Builder to creae application menu bar. This is currently a work in progress.

UPD:
Discussion start here: #2498 (comment)

@nickleus27

Copy link
Copy Markdown
Contributor Author

@ice0 @rodolforg This is a draft for the new menu bar.

@ice0

ice0 commented Mar 23, 2022 •

Copy link
Copy Markdown
Collaborator

@nickleus27
You can switch PR to draft to show it's WIP.

@nickleus27
nickleus27 marked this pull request as draft March 23, 2022 15:30
@nickleus27

nickleus27 commented Mar 24, 2022 •

Copy link
Copy Markdown
Contributor Author

@ice0 @rodolforg Starting at L873 in Files Changed I added some comments //TODO . First do you have an idea of how to add menu items that is not permanent, and stores a list, like Open Recent or point me in a direction to learn about this. Second I am having a hard time finding some action call backs that I put in //TODO. Third, i am having trouble finding path to .png for custom icons. Thank you for your time and help on this issue.

@ice0

ice0 commented Mar 24, 2022

Copy link
Copy Markdown
Collaborator

//TODO1: ADD Open Recent Actions

There are two different approaches:

  1. Because ui_info is just a string, you can add new elements in place. Something like this (pseudocode):
Glib::ustring ui_info = "...";

// Add recent items
for (const auto recent_item : recent_list) {
  ui_info += 	"        <item>" +
    etl::strprintf("          <attribute name='label' translatable='no'>%s</attribute>", recent_item.label) +
    "        </item>";
}
// following menu items
ui_info += "...";

This is not the best idea because you need to rebuild the main menu every time the dynamic element changes.

  1. Attach new elements at runtime. This is more preferable.
    This is currently done in the MainWindow::on_recent_files_changed() method. It uses the ability of the UIManager class to merge UI elements.

And it looks like the same approach can be used with Gtk::Builder:

bool Gtk::Builder::add_from_string(const Glib::ustring & buffer)
Parses a string containing a GtkBuilder UI definition and merges it with the current contents of the builder.

//TODO2: need to find cb for save-all

action_group->add( Gtk::Action::create("save-all", Gtk::StockID("synfig-save_all"), _("Save All"), _("Save all opened documents")),
sigc::ptr_fun(save_all)
);

It calls void CanvasView::save_all().

//TODO3: need to find cb for render

Same here:

action_group->add( Gtk::Action::create("render", Gtk::StockID("synfig-render_options"), _("Render...")),
sigc::mem_fun0(render_settings,&RenderSettings::present)
);

@nickleus27

nickleus27 commented Mar 25, 2022 •

Copy link
Copy Markdown
Contributor Author

@ice0

Attach new elements at runtime. This is more preferable.
This is currently done in the MainWindow::on_recent_files_changed() method. It uses the ability of the UIManager class to merge UI elements.

I see how recent files can be added dynamically in the method MainWindow::on_recent_files_changed() I could add code there to add recent files into the Gtk::Builder My current problem is I am unsure how to have my elements to merge correctly into the xml string. I tried a test example at L997 in App::init_menu_builder() However, in the try catch block where I call add_from_string it throws an error. The string does not merge correctly. Could you look at my xml string and see what I need to do to get it to add items into the submenu with id=`recent` . the submenu is at L911. My apologies, I could not find any information on this subject.

Second, when I click on Close Document twice without opening a new document the app crashes and I get this error:

Assertion failed: (obj), function operator->, file _handle.h, line 740.
zsh: abort      ./output/Debug/bin/synfigstudio

I also noticed that the Close-Ducment label does not get greyed out (cannot click) after closing the instance like it does on the old menubar.

@rodolforg

Copy link
Copy Markdown
Contributor

When using the new GMenu, dynamic menus should be implemented via Gio::MenuModel / Gio::Menu.
So the Open Recent menu specifically should use it explicitly.
Take a look at https://developer-old.gnome.org/glibmm/stable/classGio_1_1MenuModel.html#a0b0c42bcd34312965430750a2f56071a and https://developer-old.gnome.org/glibmm/stable/classGio_1_1MenuModel.html#afd1b2461f5e75317ce6fa58a39351870 (Gio::MenuModel::items_changed() and Gio::MenuModel::signal_items_changed())

@rodolforg

Copy link
Copy Markdown
Contributor

I also noticed that the Close-Ducment label does not get greyed out (cannot click) after closing the instance like it does on the old menubar.

They are (currently) controlled by an ActionGroup of the active CanvasView. Check CanvasView::activate(), CanvasView::deactivate(), CanvasView::~CanvasView().

@nickleus27

nickleus27 commented Mar 26, 2022 •

Copy link
Copy Markdown
Contributor Author

I have files adding dynamically now to Open Recent submenu. But duplicate files are added to the list. Here is before opening file:
2BDEA2F0-8241-4A21-B5AE-3C7234B5B81C
and here is after opening file:
40DCC069-82EF-4EF3-A766-A19C0C9FAD6D

Here is where I think duplicate files are prevented from going into menu:

	typedef std::vector< Glib::RefPtr<Gtk::ActionGroup> > ActionGroupList;
	ActionGroupList groups = App::ui_manager()->get_action_groups();
	for(ActionGroupList::const_iterator i = groups.begin(); i != groups.end(); ++i)
		if ((*i)->get_name() == action_group->get_name())
			App::ui_manager()->remove_action_group(*i);
	groups.clear();

This code snippet is in MainWindow::on_recent_file_changed()? If so, what would be an equivalent to get_action_groups that I could use with Gio::Menu and Gtk::Builder ? I see Gio::Menu has a remove method, but I am not sure how to iterate menu list for duplicates. Would it be
https://developer-old.gnome.org/glibmm/stable/classGio_1_1MenuModel.html#ababd702c0ecfe7f586256c366fb91f04
Thank you.

@ice0

ice0 commented Mar 27, 2022

Copy link
Copy Markdown
Collaborator

My current problem is I am unsure how to have my elements to merge correctly into the xml string.

I also did not find a solution using the GtkBuilder, so I'll ask the GTK team about that.

Here is where I think duplicate files are prevented from going into menu:

It looks like removing duplicates in action groups not in menus.

I see Gio::Menu has a remove method, but I am not sure how to iterate menu list for duplicates.

Since you are adding all recent items every time there is no need to remove duplicates, you can just remove all items in the menu using remove_all() method. Something like this:

auto menu_object = App::menu_builder()->get_object("recent-file");
auto recent_file_menu = Glib::RefPtr<Gio::Menu>::cast_dynamic(menu_object);
recent_file_menu.remove_all();

@nickleus27

nickleus27 commented Mar 27, 2022 •

Copy link
Copy Markdown
Contributor Author

@ice0

It looks like removing duplicates in action groups not in menus.

Do I need to worry about removing duplicate actions? I looked into this and I could not find a way to insert Gio::SimpleActionGroup into Gio::Application in a similar fashion that is already done with:

void
MainWindow::on_recent_files_changed()
{
// TODO(ice0): switch to GtkRecentChooserMenu?
Glib::RefPtrGtk::ActionGroup action_group = Gtk::ActionGroup::create("mainwindow-recentfiles");

There is no Gio::Application->insert_action_group

But it seems that Gio::Applicationand Gio::SimpleActionGroup inherit from Gio::ActionMap interface which I believe is a hash map so adding the action again would just map to the same location?

…ove_all in MainWindow::on_recent_files_changed
@nickleus27

nickleus27 commented Mar 27, 2022 •

Copy link
Copy Markdown
Contributor Author

are the icon images in synfig-studio/images/? I have not been able to get them to work with the "<attribute name='icon'>" in the xml string.

I am having trouble with finding how to activate/deactivate the items in the edit menu. So far undo/redo is in app.cpp which calls synfigapp::Action::System::undo() / redo() I think. And cut/copy/paste methods are in layeractionmanager.cpp .

@ice0

ice0 commented Mar 28, 2022

Copy link
Copy Markdown
Collaborator

Do I need to worry about removing duplicate actions? I looked into this and I could not find a way to insert Gio::SimpleActionGroup into Gio::Application in a similar fashion that is already done with:
...
There is no Gio::Application->insert_action_group

From documentation:

Use Gio::ActionGroup and Gtk::Widget::insert_action_group() instead.

It looks like we can use App::main_window::insert_action_group() for this, but I'm not sure if that's correct, maybe we should attach it to the menu?

@rodolforg
Do you have any ideas about it?

are the icon images in synfig-studio/images/?

Icons are located in synfig/cmake-build-debug/output/Debug/share/synfig/icons/classic/

I have not been able to get them to work with the "<attribute name='icon'>" in the xml string.

Try to use full path to menu. Something like this:
<attribute name="icon">/Users/nick/projects/synfig/cmake-build-debug/output/Debug/share/synfig/icons/classic/animate_mode_off_icon.png</attribute>

https://developer.gnome.org/documentation/tutorials/menus.html

I am having trouble with finding how to activate/deactivate the items in the edit menu. So far undo/redo is in app.cpp which calls synfigapp::Action::System::undo() / redo() I think. And cut/copy/paste methods are in layeractionmanager.cpp .

You need to set sensitive property. Unfortunately I didn't find any documentation how to do this.

This can be done with Glade designer, but it saves the XML in a slightly different format, something like this:

    <object class="GtkMenuBar">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkMenuItem">
                <property name="visible">True</property>
                <property name="sensitive">False</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">_File</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkImageMenuItem">
                        <property name="label">gtk-new</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>

@nickleus27

Copy link
Copy Markdown
Contributor Author

@ice0 thank you. Looks like I might need to go with glade instead of .xml

@nickleus27

nickleus27 commented Mar 29, 2022 •

Copy link
Copy Markdown
Contributor Author

I cannot seem to get .glade to work, and I cannot seem to get the widget I need for inserting action groups with the .xml I have that does work.

If i am using glade then how do i set_menubar which takes a gio::menu as a parameter. Glade autogenerates the class objects different from Gio::Menu.

I tried using App::main_window for widget, but I wasn't able to get the actions to work in the menubar. I am confused how Gtk::widget::insert_action_group and remove_action_group take parameter name which is suppose to be the prefix of the actiongroup. For example app.action . But when I use any prefix other than app the actions don't work. But if I use remove_action_group("app") I think that would remove all the other app actions as well?

Try to use full path to menu. Something like this:
/Users/nick/projects/synfig/cmake-build-debug/output/Debug/share/synfig/icons/classic/animate_mode_off_icon.png

This full path to the icons worked

Edit:
Looks like I do not need to worry about removing actions or action groups with the recent files per the documentation:

Gio::ActionMap::add_action()
Adds an action to the action_map.
If the action map already contains an action with the same name as action then the old action is dropped from the action map.

But there is still the issue of remove actions that are no longer used.

@ice0

ice0 commented Mar 30, 2022

Copy link
Copy Markdown
Collaborator

If i am using glade then how do i set_menubar which takes a gio::menu as a parameter. Glade autogenerates the class objects different from Gio::Menu.

I just want to say that I need time to test this. I'll be back when there's news.

@ice0

ice0 commented May 12, 2022

Copy link
Copy Markdown
Collaborator

@nickleus27

Thank you for this great work!

I plan to start merging it step by step after the PR with themes is merged.
This will allow icon names to be used instead of filenames.

Before:

<attribute name='icon'>"+App::icon_theme()->lookup_icon(get_icon_name("new"), 128).get_filename()+"</attribute>

After:

<attribute name='icon'>new</attribute>

any ideas on the toggle_show_menubar in mainwindow.cpp? The toggle button is in the view menu. With the menubar now being a gio::menu and not a widget I don't think the menu can hide() and show() how it did previously?

This menu action can be disabled for MacOS using define:

#ifndef __APPLE__
...
#endif

There is currently no main menu on Linux, but I plan to solve this issue in the merge step.

@nickleus27

nickleus27 commented May 12, 2022 •

Copy link
Copy Markdown
Contributor Author

Thanks @ice0 ! That sounds great. Also, thank you for explaining how to remove show menubar from apple platform. I just pushed that commit. No need to have show menubar in the menu if it doesn't work on apple.

@nickleus27

nickleus27 commented May 12, 2022 •

Copy link
Copy Markdown
Contributor Author

@ice0 I tried building this PR on an linux (ubuntu) docker container and I see what you mean that there is no main menubar on. That is really weird, and I am not sure why that is. I am glad you have an idea how to fix it. I tried

	auto object = App::builder()->get_object("studio_menubar");
	auto gmenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
	auto visible_menubar = Gtk::make_managed<Gtk::MenuBar>(gmenu);
	//auto visible_menubar = App::ui_manager()->get_widget("/menubar-main");

in mainwindow.cpp but that didn't seem to add the menubar.

@ice0

ice0 commented May 17, 2022

Copy link
Copy Markdown
Collaborator

@nickleus27

I merged PR with IconTheme support.

You can now use icon names instead of file paths in UI descriptions.

Before:

<attribute name='icon'>"+App::icon_theme()->lookup_icon(get_icon_name("quit"), 128).get_filename()+"</attribute>"

After:

<attribute name='icon'>application-exit</attribute>"

This will allow to extract the UI description into an external file and merge it via separate PR (because it won't change the current behavior).

I also found a non-intrusive way to switch from Gtk::Action to Gio::Action. (Currently testing it).

@nickleus27

nickleus27 commented May 18, 2022 •

Copy link
Copy Markdown
Contributor Author

@ice0 @rodolforg hey all, when it comes time, I might need some help on how to squash these commits. Since I have merged with other commits. Newbie mistake I guess. Maybe with rebase --interactive ?

@ice0

ice0 commented May 18, 2022

Copy link
Copy Markdown
Collaborator

@ice0 @rodolforg hey all, when it comes time, I might need some help on how to squash these commits. Since I have merged with other commits. Newbie mistake I guess. Maybe with rebase --interactive?

Yes you can squash commits using git rebase --interactive.
Also there is an awesome GUI tool named SmartGit, it's free for non-commercial use, so I you can try it if you want.

After you will fix the merge conflicts, this should remove the already merged changes (I mean glade files).

@nickleus27

nickleus27 commented May 18, 2022 •

Copy link
Copy Markdown
Contributor Author

@ice0 @rodolforg sorry guys I let this commit history get so sloppy. I downloaded smart git, but I am not sure how to approach cleaning up commit history, because it is not just my commits in a straight line on the graph. I do not want to mess things up. The history graph has multiple branches, and I am not sure what to do. Any articles you can direct me to show me how to do this when it is not the last n commits that you are trying to squash. Thank you.

edit: i found this solution, (https://stackoverflow.com/a/17141512/1388104) , and I am not sure if this is a good idea or not. Also, I am not sure how important it is to clean up history, vs preserving history.

@ice0

ice0 commented May 19, 2022 •

Copy link
Copy Markdown
Collaborator

Also, I am not sure how important it is to clean up history, vs preserving history.

The commit history should help to understand why a particular change was made.
In my opinion, each commit should solve its own problem. If you clean up the code, make a separate commit for this task.
This helps to understand if it's safe to merge this commit without checking it, or if it might break something.
For example this PR: #2638. It can be merged as is because it doesn't change the code.

In the case of this PR, I suppose to leave things as they are. I plan to merge changes from it step by step as separate PRs until all changes are merged.

@rodolforg

rodolforg commented Aug 9, 2022 •

Copy link
Copy Markdown
Contributor

hi @nickleus27 :) do you still have interest on work on it?
I completely forgot about this amazing work you were doing here. In April and May I was very busy at work, so I couldn't follow this PR. I'm really sorry.

I have some suggestions here - even some important changes.

@nickleus27

Copy link
Copy Markdown
Contributor Author

@rodolforg sure whats up?

@rodolforg

Copy link
Copy Markdown
Contributor

Sorry my delay. again :(
Before my previous message here, I was working on a draft.
Could you take a look in the last 4 commits of https://github.com/rodolforg/synfig/commits/go-gactions- ?

I created a new class (badly) called ActionManager that is a database for binding action names to labels, (default) shortcuts, etc.
This allows us to save and load custom shortcut configs.

What do you think?

@nickleus27

Copy link
Copy Markdown
Contributor Author

@rodolforg Right on, this looks cool! Thank you.

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