Tuesday, April 14, 2020

Glimmer 0.5.3 Data-Binding Converters + Auto-Packing on Layout Change

Glimmer 0.5.3 desktop development library for Ruby shipped with 2 new exciting features:
- Data-Binding Converters
- Auto-Packing on Layout Change

Here are more details about Data-Binding Converters:

text bind(contact, 'address.street')

This example binds the text property of a widget like label to the nested street of the address of a contact.

text bind(contact, 'address.street', on_read: :upcase, on_write: :downcase)

This example adds on the one above it by specifying converters on read and write of the model property, like in the case of a text widget. The text widget will then displays the street upper case and the model will store it lower case. When specifying converters, read and write operations must be symmetric (to avoid an infinite update loop between the widget and the model since the widget checks first if value changed before updating)

text bind(contact, 'address.street', on_read: lambda { |s| s[0..10] })

This example also specifies a converter on read of the model property, but via a lambda, which truncates the street to 10 characters only. Note that the read and write operations are assymetric. This is fine in the case of formatting data for a read-only widget like label

text bind(contact, 'address.street') { |s| s[0..10] }

This is a block shortcut version of the syntax above it. It facilitates formatting model data for read-only widgets since it's a very common view concern. It also saves the developer from having to create a separate formatter/presenter for the model when the view can be an active view that handles common simple formatting operations directly.

Here are more details about Auto-Packing on Layout Change:

If you data-bind any layout or layout data properties, when they change, the shell containing their widget re-packs its children (calls #pack method automatically) to ensure proper relayout of all widgets.

Happy Glimmering!

No comments: