Tips for high level structuring of a Gtk.jl UI

I am using Gtk.jl to work on a UI. There will be multiple windows and components with the box layouts in different sections. I am wondering what is the recommended structuring for the definition and organization of the components. I have had the individual components as global variables am now putting them into a dictionary which is set to global as well.

My question is whether there is a recommended or preferred way to do this? Should I set the dictionary in the main function which is passed around for the updating? Should I have the components in a struct instead? Should the different windows be in different dictionaries or structs?

There is not really a recommended structure yet and I am playing around with this myself. What I usually do is to put substructures into a common custom widget, which is basically always a grid with various widgets insides. I use this approach:

Custom/Composed Widgets · Gtk.jl

where the new widget haves just like a regular Gtk widget. Each of these custom widgets gets a dedicated Glade UI file and from within the constructor I pull out the widgets and put them into fields of the struct. Another pattern is this


mutable struct DataViewerWidget <: Gtk.GtkBox
  handle::Ptr{Gtk.GObject}
  builder::Builder
  ...
end

getindex(m::DataViewerWidget, w::AbstractString) = G_.object(m.builder, w)


function DataViewerWidget()
  uifile = joinpath(@__DIR__,"..","builder","dataviewer.ui")

  b = Builder(filename=uifile)
  mainBox = G_.object(b, "boxDataViewer")
  m = DataViewerWidget( mainBox.handle, b, ...)
  
  # Now I can do this:

  m["myWidgetInTheGladeFile"]

  return m
end

1 Like