Skip to content

Code refactoring around GMenuModel-bindings

Gaël Bonithon requested to merge Tamaranch/mousepad:gmenumodel-bindings into master

Depends on !46 (merged).

After !48 (merged), this MR is an other kind of complement to !30 (merged), and also to !38 (merged), following in particular !38 (comment 16887). Like it, it aims above all to make Mousepad code easier to maintain after the switch to GtkApplication, by depending as little as possible on GTK 3 specific widgets (and in any case only extrinsically).

This time, it's all about bindings between GMenuModel and the objects built from it, using the only signal we have to do so: "items-changed".

  • The first one is an additional GMenuModel-GtkMenu binding (they are already bind in gtk_menubar_new_from_model()), to automatize a bit more tooltip updates, and localize a bit more this process in mousepad_window_menu_set_tooltips().
  • The second one is a GMenuModel-GMenuModel binding, which not only involves the use of the "items-changed" signal, but also a trick in the way to write the GtkBuilder menu files (mousepad/resources/gtk/*.ui). The goal is to remove duplicates in these files, by defining a menu item only once (with all its attributes: label, icon, tooltip, action, accel, etc.), and then reusing it in any menu where it should appear, using a syntax close to the expected but missing <item id="my-already-defined-item"/>.
  • The third and last one is a GMenuModel-GtkToolBar binding, in the form of the mousepad_window_toolbar_new_from_model() function, thus addressing more precisely the request in !38 (comment 16887).

A typical example of use of these three bindings together is the "Reload/Revert" action. It is used in the menubar ("File" menu), in the tab context menu, and in the toolbar. So currently, one has first to initialize the label, icon and tooltip associated to this action for these three locations by hand (once in mousepad-window.c, twice in menus.ui). Then, when the "modified-changed" signal is emitted on the buffer, one has to update these properties for these three locations also by hand (this is currently done in mousepad_window_modified_changed()).

With the new bindings, it is sufficient to initialize and update by hand only the shared menu item "item.file.reload", then all locations where it is used are automatically updated.

Merge request reports