How to structure a Gtk app

When looking at Gyk examples a lot of the apps use global variables. How can this be avoided while keeping access to the model values(like data, a figure object,…)

What I saw

using Gtk

win = GtkWindow()

f = Figure()

function on_event(w)
   %do something with f

In object orientet languages this is often done using properties. In julia I only see globals or closures as a possibility. Are there better options? Passing the model around is not useful as the api for Gtk is not designed that way.

function main()
 win = GtkWindow()

 f = Figure()
 function on_event()
    % do something with f

use structs or mutable structs? We also have in the documentation an example how to create a composite widget using a struct. I structure all my code like this.

1 Like

Thank you for the tip. How would I apply this to GtkCanvas. As I noticed GtkCanvas is not an abstract structure so I can’t derive a structure from it.

GtkCanvas is not a Gtk type but a custom creation within Gtk.jl. You can just put it into a Box or a Grid and then you can follow Custom/Composed Widgets · Gtk.jl

By the way: This “inheriting” is not necessary if you don’t want your struct to behave like a widget. Your main application, for instance, does not need that. It would usually look like:

mutable struct MyApp
  ...  # further widgets...