Flashcards: Take 3, part 3

Linux
Software Development
Author

George Ramzi

Published

23 Dec 2020

Day 8

Continuing from the last post, Flashcards is now on GTK4!

Part 1 Part 2

The good news is that there’s loads of exciting new toys to play with! I’m incredibly excited to play with the transformations, as I’ve always pictured the card’s having a reveal animation where the widget flips over like a card, and doing this in GTK3 would’ve been an incredible amount of work.

The bad news is, everything is broken!

The Deck list is the first priority; users need to be able to select the deck that they want to view. Once this is working, I need to fix the card list; however the fix should be very similar. The Deck list is also a very simple list; The actual widget for each list is exceedingly basic with just 2 labels and the data is already in a GtkListStore, and this simplicity should make it a somewhat easy starting point. Previously (and I believe documented in this blog) I had turned each row into it’s own composite widget with GObject Properties that functioned as a getter and a setter that would edit the widget. Now I need to generate these lists using GtkListItemFactories. In theory, this should alleviate the worry I had before that using composite widgets would make long lists laggy! So I guess this isn’t actually bad news? As I needed to replace these anyway.

I decided to re-read the GTK blog posts about the new list views, and the first thing that stood out to me was the following quote:

Some of the early adopters of GTK4 have pointed out that the new list widgets are not the easiest to learn. In particular, GtkExpression and GtkBuilderListItemFactory are hard to wrap your head around.

The blog posts is very well written to help make this all make more sense, the only down side is that all the code snippets are in C, but oh well. It also appears you can do most of the heavy lifting for this in your .ui file? Which if so means even more heavy lifting in the app is being handled by GTK.

Day 9

Getting the list factory working has been interesting, but a headache. Better examples (especially Python) would be extremely helpful, but I do think I’m beginning to understand how it works. So in the blog linked in the previous post, there’s 2 examples given. The first example is using Gtk.SignalListItemFactory, where the factory emits signals that need a specifically crafter function for the “bind” and “setup” signals. The bind signal creates a widget that will be placed in the list, and the setup signal modifies that row with the data for your row.

The second example uses Gtk.BuilderListItemFactory, which does not emit either signal (or any for that matter), but instead uses a .ui file to specify the layout of the widgets in each row. Additionally, the UI file can contain some special tags (which as far as I know cannot be used in normal .ui files or wouldn’t serve a purpose) that allow you to define how data will be input into the row. For me this is much more in line with how I already manage Flashcards, so I want to use that!

So after experimenting with the builder way first, I discovered that it’s not the easiest thing in the world to debug. Most of the work is taken care of for you, but the result of that is that it is hidden away from you. I decided that the easier way to go would be to follow the blog’s walkthrough and set up using the signal factory. This way, whenever a signal is emitted we can see what object the factory is using and do whatever you want with those objects while debugging. Doing this, I managed to get a working trivial list factory! The deck list is now managed as a list of GObjects that I’ve created, and the values I want are stored as GObject properties.

class DeckRow(GObject.GObject):
    _deck_id    = -1
    _deck_name  = ""
    _card_count = -1
    
    def __init__(self, did, name, count, **kwargs):
        super().__init__()
        GObject.GObject.__init__(self)
        self.deck_id = did
        self.deck_name = name
        self.card_count = count
    
    @GObject.Property(type=str, nick="deck-name", default="")
    def deck_id(self):
        return self._deck_id
    
    @deck_id.setter
    def deck_id(self, did):
        self._deck_id = did
    
    @GObject.Property(type=str, nick="deck-name", default="")
    def deck_name(self):
        return self._deck_name
    
    @deck_name.setter
    def deck_name(self, dname):
        self._deck_name = dname
    
    @GObject.Property(type=int, default=-1)
    def card_count(self):
        return self._card_count
    
    @card_count.setter
    def card_count(self, count):
        self._card_count = count

It’s an exceedingly simple object, but it is being used to generate an exceedingly simple list. This is very much in line with how I managed the list rows before, but now GtkListItemFactory is generaing this all for me so if the list ever gets large enough it shouldn’t slow the interface. Again, the odds of someone having so many decks that it slows the interface is low, but this was a good chance to learn GtkListItemFactory and it’s eccentricities in a simple environment.

However, while this works perfectly well, I would much prefer that this list be handled with GtkBuilderListItemFactory rather than GtkSignalListItemFactory; not only does this mean I can understand how both of them work, but also this list is so incredibly simple that I might as well port it.

After a quick discussion in the GTK matrix room (Thank you Company!) I was pointed out that the issue was a missing __gtype_name__ = DeckRow. With that and the correct typing in the XML file, everything works perfectly! In case anyone else has a similar issue, here’s the XML I used:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <template class="GtkListItem">
    <property name="child">
      <object class="GtkBox">
        <property name="hexpand">1</property>
        <property name="height_request">64</property>
        <child>
          <object class="GtkLabel">
            <binding name="label">
              <lookup name="deck-name" type="DeckRow">
                <lookup name="item">GtkListItem</lookup>
              </lookup>
            </binding>
            <property name="can_focus">0</property>
            <property name="halign">start</property>
            <property name="margin_start">12</property>
            <property name="hexpand">1</property>
            <property name="xalign">0</property>
          </object>
        </child>
        <child type="end">
          <object class="GtkLabel">
            <binding name="label">
              <lookup name="card-count" type="DeckRow">
                <lookup name="item">GtkListItem</lookup>
              </lookup>
            </binding>
            <property name="margin_end">12</property>
          </object>
        </child>
      </object>
    </property>
  </template>
</interface>

Note that this XML is minimal, but slightly more complex than the example given in the blog post linked previously as we are looking for 2 properties for 2 different labels.

Lastly, before I commit this updated version I’m going to simplify the object. Firstly I removed some unnecessary GTK objects (like a box that’s only child was another box) and some unnecessary code from when I was building the ListFactory.

Also, because of it’s currently glitchy state (again, this is expected! it’s a pre-release patch to a library) I’ve changed the default window size to a desktop app size rather than a mobile app size I used to testing the UI’s mobile friendly~ness.

Day 10

Today I wanto to get the CardList ported over to using GtkSignalListItemFactory. Why Signal instead of Builder? Because from my understanding of how both work that I gained from implementing DeckList as both, I think the flexibility to edit the rows in code should be more flexible. I’m going to scrap the current implementation and rewrite it from scratch, and as the objects are reused I should also be able to have the row widget simply contain the editor; I didn’t do this before because I was concerned with the performance, but with ListFactory recycling the objects the performance impact should be minimal.