So the topic of the day is EggStateMachine. This is a handy class for managing various states in your UI that might have complex transitions. One example might be the GNOME 3 style search pattern. The search pattern has a few states.
- Inital state which contains the content overview
- The active search state, including type-ahead search
- Object selection state
- Object selection state with a selection
When we switch between the above states, we might need to:
- Toggle search and object-selection toggle buttons
- Toggle search revealer visibility
- Toggle action bar visibility
- Toggle action bar button sensitivity
- Show checkbuttons on content overview
- Alter headerbar CSS classes
- Toggle visibility of close button on header bar
- Toggle visibility of cancel button on header bar
You get the idea. Getting everything right is critical to the UX, and that becomes unpleasant code rather quickly.
EggStateMachine allows you to do much of above all from your Gtk+ .ui template file. Additionally, you can use the native C API if using .ui templates is not your thing. For those people, you can jump to the header file to get the idea, link at the bottom.
For the rest of you, take a look at this snippet.
<object class="EggStateMachine"> <property name="state">browse</property> <states> <state name="browse"> <object id="titlebar"> <property name="show-close-button">true</property> </object> <object id="cancel_button"> <property name="visible">false</property> </object> <object id="action_bar"> <property name="visible">false</property> </object> </state> <state name="selection"> <object id="titlebar"> <property name="show-close-button">false</property> <style> <class name="selection-mode"/> </style> </object> </state> </states> </object>
This is just a snippet, and shows two states. The “browse” state and the “selection” state. You can see that you can reference other objects in your .ui definition using the <object id=""> syntax. While I didn’t show it here, you can use the binding syntax to bind properties only in a given state.
You can also define style-classes in the state. When the state transitions, the style-class will automatically be added or removed.
To perform a state transition, set the EggStateMachine:state property. You can also create an action for this using egg_state_machine_create_action(). If you add that action to your widget hierarchy (such as win.state), then you can remove the need to perform complex toggles in your application code as well. Take a look at the following snippet.
<object class="GtkToggleButton" id="selection_button"> <property name="action-name">win.state</property> <property name="action-target">'selection'</property> <child> <object class="GtkImage"> <property name="visible">true</property> <property name="icon-name">object-select-symbolic</property> </object> </child> </object>
To add the state action to your widget hierarchy, do something like:
GAction *action; action = egg_state_machine_create_action (state_machine, "state"); g_action_map_add_action (G_ACTION_MAP (my_window), action); g_object_unref (action);
Now the button will be toggled when the state matches "selection".
And for practical use, see Builder’s greeter implementation.