Creating simple GUI applications using GTK on rust.
(Note: inspiration and structure taken from gnome's fractal, see the code here)
We start by creating a GTK app in our main function, which we then initialize via a startup callback, and then we just run it. (See main.rs)
The startup code simply initializes the runtime we need to interact with the app (more on this later), which is just setting up the app's state (including setting up the ui) and attaching a channel to the main app context.
We have a state struct defining all the context we care for in our application, during the startup section from before, we have the main context own this state, we can then interact with it as expplained in the next section.
To have any sort of interaction with our app, we use the created
runtime from before, we can then use this to call update functions
on our app, which work by getting a mutable reference to the app state
and doing any work we need to do (e.g. state.main_menu()
changes the
current state to display the main menu).
The UI part of our application holds a builder (from a GTK builder
file) that we can use to get objects on demand (e.g. state.about()
uses the about
object to create the window).
Here we define any type of actions we want to interact with on our application, the global actions are instantiated when we first init the app's state when we set up the runtime.
One example is main_menu
, which is an action attached to a button
or any other object (like a key-binding) that has a callback that
updates the runtime by calling the state.main_menu()
function from
before.
- We start an application runtime
- Initializes app state
- We use said runtime to run update functions
- We use the state to change ui elements or set up callbacks
Once we have the working template, most work is done in the actions and the state modules (also check builder.ui to see what the object references are).