Difference between revisions of "GtkAction migration"

From Inkscape Wiki
Jump to navigation Jump to search
(Added some notes)
(6 intermediate revisions by 2 users not shown)
Line 16: Line 16:
* Less code to write: we only need one signal handler for the "activate" signal; not multiple handlers for each GUI event
* Less code to write: we only need one signal handler for the "activate" signal; not multiple handlers for each GUI event
* Easier look-up: Each action is identified by a unique text ID, and so we can access it by name.
* Easier look-up: Each action is identified by a unique text ID, and so we can access it by name.
* It is trivial to allow actions (with or without arguments) to be called from the command line.
* There is a built in DBus interface.


== Background: Action implementations ==
== Background: Action implementations ==
Line 23: Line 25:
These are probably the simplest implementation, and the way we "should" be doing this.  The Gio::Action class [https://developer.gnome.org/glibmm/2.40/classGio_1_1SimpleAction.html] really just defines a name for the action, along with possible states and parameters.
These are probably the simplest implementation, and the way we "should" be doing this.  The Gio::Action class [https://developer.gnome.org/glibmm/2.40/classGio_1_1SimpleAction.html] really just defines a name for the action, along with possible states and parameters.


These actions need to be listed in an action-map, which is essentially a table that lists each action and the function that handles its "activate" signal.
These actions need to be listed in an action-map, which is essentially a table that lists each action and the function that handles its "activate" signal. There is an action map associated with the application (Gtk::Application or Gio::Application) and an action map associated with windows (Gtk::ApplicationWindow). The latter is useful for window dependent functions like zoom level where two different "views" of the same document can have different zoom levels. Another action map can be "manually" attached to a document to handle document level actions (such as snapping preferences).


== Already migrated ==
Actions are defined in src/actions
=== Widgets ===
 
* ege-adjustment-action => SpinButtonToolItem
== Verb to Action Migration ==
 
Inkscape has a custom built ''Verb'' class that duplicates much of the functionality of Actions. This class has some problems, the biggest is that most ''Verb''s require a desktop. This means that they cannot be used in a headless Inkscape. We are in the process of converting Verbs to Actions. This is a long term process as there are many verbs.
 
Actions are defined in the "src/actions" directory. For new actions, please follow the format of existing files. If possible, define all functionality in the actions-xxx.cpp file.
 
=== Best Practices ===
 
* Tie an action to the appropriate map: Application, Window, or Document. (We may add tool dependent maps in the future.) It is not always obvious which map an action should be tied to:
** Application:
*** Global actions:
*** Actions that operate on selections. There is always a "current selection" that is active. Note: a single document may have multiple independent selections, one for each window. There will also be an active selection in command line mode.
** Window actions:
*** Zoom level
** Document actions:
*** Preferences stored inside an Inkscape SVG file, e.g. snapping.
* Actions should not rely on Window (desktop) unless absolutely necessary (e.g. scale by window pixels). This will allow a future GUI free Inkscape version.
* If actions need to relay information to the user, use a standard (to be decided) mechanism to isolate dependency on desktop.
** boolop_display_error_message();
** selection_display_message();
** InkErrorHandle (Pops up Error Box)  (Derived from silly Inkscape::ErrorReporter class.)
** Note, a selection does have a link to desktop (which can be nullptr).
* Many actions work on a selection. A standard way to find the selection should be used. (Radical idea, a selection could have an action map.) Selections are tied to windows with a special selection tied to the document in command line mode.
** get_document_and_selection(app, &document, &selection);
** app->get_active_selection();
** At the moment we rely on INKSCAPE.action_context_for_document() to create a selection for a document in command-line mode. (INKSCAPE should disappear in the future.)
* When a Verb is converted to an action, remove any related code from "verb.cpp". Comment out the verb in "verbs.h" (so we can track progress in converting).
* DocumentUndo uses verbs to provide an icon next to the operation in the History dialog. For the moment, one can change any removed verbs to "nullptr". In the future, we may allow one to directly specify an icon.
 
== Migrating widgets ==
Use the following replacements for old GtkAction-based widgets:
 
* ege-adjustment-action => SpinButtonToolItem DONE
* SPWidget => Nothing... just put your widget directly in the toolbar DONE


=== Containers ===
=== Containers ===

Revision as of 17:20, 30 October 2020

Background: what are actions?

An "action" is a thing that Inkscape can do. In its simplest form, it contains two things:

  • The name of the action (e.g., "ObjectFlipHorizontally")
  • A pointer to some code that performs the action (e.g., a few lines that flip the object horizontally)

In the Inkscape application, we can attach actions to widgets so that when the user interacts with that widget (e.g. clicks on the "Flip horizontally" button in the toolbar), the action is triggered.

How is this different from GUI events?

GUI elements (buttons etc) provide their own signals (e.g, "clicked") that are emitted in response to user interaction. We then need to connect a callback function to handle that specific GUI event. If we have multiple GUI elements (e.g., a menu item, toolbar button etc) that all do the same thing, we need to hook up the callback function to each of the GUI elements.

With actions, it's a little different: the action has an "activated" signal. We now only need to connect the callback function to that one signal. Each GUI element is now just attached to the action rather than directly to the code that we want to run.

The advantages of actions are as follows:

  • Less code to write: we only need one signal handler for the "activate" signal; not multiple handlers for each GUI event
  • Easier look-up: Each action is identified by a unique text ID, and so we can access it by name.
  • It is trivial to allow actions (with or without arguments) to be called from the command line.
  • There is a built in DBus interface.

Background: Action implementations

There are a few different implementations of actions:

GAction or Gio::Action

These are probably the simplest implementation, and the way we "should" be doing this. The Gio::Action class [1] really just defines a name for the action, along with possible states and parameters.

These actions need to be listed in an action-map, which is essentially a table that lists each action and the function that handles its "activate" signal. There is an action map associated with the application (Gtk::Application or Gio::Application) and an action map associated with windows (Gtk::ApplicationWindow). The latter is useful for window dependent functions like zoom level where two different "views" of the same document can have different zoom levels. Another action map can be "manually" attached to a document to handle document level actions (such as snapping preferences).

Actions are defined in src/actions

Verb to Action Migration

Inkscape has a custom built Verb class that duplicates much of the functionality of Actions. This class has some problems, the biggest is that most Verbs require a desktop. This means that they cannot be used in a headless Inkscape. We are in the process of converting Verbs to Actions. This is a long term process as there are many verbs.

Actions are defined in the "src/actions" directory. For new actions, please follow the format of existing files. If possible, define all functionality in the actions-xxx.cpp file.

Best Practices

  • Tie an action to the appropriate map: Application, Window, or Document. (We may add tool dependent maps in the future.) It is not always obvious which map an action should be tied to:
    • Application:
      • Global actions:
      • Actions that operate on selections. There is always a "current selection" that is active. Note: a single document may have multiple independent selections, one for each window. There will also be an active selection in command line mode.
    • Window actions:
      • Zoom level
    • Document actions:
      • Preferences stored inside an Inkscape SVG file, e.g. snapping.
  • Actions should not rely on Window (desktop) unless absolutely necessary (e.g. scale by window pixels). This will allow a future GUI free Inkscape version.
  • If actions need to relay information to the user, use a standard (to be decided) mechanism to isolate dependency on desktop.
    • boolop_display_error_message();
    • selection_display_message();
    • InkErrorHandle (Pops up Error Box) (Derived from silly Inkscape::ErrorReporter class.)
    • Note, a selection does have a link to desktop (which can be nullptr).
  • Many actions work on a selection. A standard way to find the selection should be used. (Radical idea, a selection could have an action map.) Selections are tied to windows with a special selection tied to the document in command line mode.
    • get_document_and_selection(app, &document, &selection);
    • app->get_active_selection();
    • At the moment we rely on INKSCAPE.action_context_for_document() to create a selection for a document in command-line mode. (INKSCAPE should disappear in the future.)
  • When a Verb is converted to an action, remove any related code from "verb.cpp". Comment out the verb in "verbs.h" (so we can track progress in converting).
  • DocumentUndo uses verbs to provide an icon next to the operation in the History dialog. For the moment, one can change any removed verbs to "nullptr". In the future, we may allow one to directly specify an icon.

Migrating widgets

Use the following replacements for old GtkAction-based widgets:

  • ege-adjustment-action => SpinButtonToolItem DONE
  • SPWidget => Nothing... just put your widget directly in the toolbar DONE

Containers

  • ConnectorToolbar
  • DropperToolbar

Known bugs

  • SpinButtonToolItem: Pressing enter or escape does nothing (it should drop focus to canvas)