Friday, May 24, 2024

Glimmer DSL for WX 0.1.0 Hello, Data-Binding!

The wxRuby3 Ruby binding for wxWidgets (a very mature native cross-platform GUI toolkit) finally went version 1.0, so Glimmer DSL for WX, which runs on top of wxRuby3, has been upgraded to use version 1.0 by default. Also, a new feature has been added that is already available in most other Glimmer GUI DSLs: Unidirectional/Bidirectional Data-Binding. It enables effortless wiring of Views to Models for automatic data syncing with a single line of Ruby code per wired property, uniquely and intuitively relying on the `<=`  operator for Unidirectional Data-Binding and the `<=>` operator for Bidirectional Data-Binding. A new sample has been added to demonstrate that feature, called "Hello, Data-Binding!".


Hello, Data-Binding! Sample Code:

# Source: https://github.com/AndyObtiva/glimmer-dsl-wx?tab=readme-ov-file#hello-data-binding
require 'glimmer-dsl-wx'
class Donor
DONATION_AMOUNT_MIN = 0
DONATION_AMOUNT_MAX = 100
DONATION_AMOUNT_DEFAULT = 50
attr_accessor :first_name, :last_name, :donation_amount
def initialize
@last_name = @first_name = ''
@donation_amount = DONATION_AMOUNT_DEFAULT
end
def full_name
full_name_parts = [first_name, last_name]
full_name_string = full_name_parts.join(' ').strip
if full_name_string.empty?
'Anonymous'
else
full_name_string
end
end
def summary
"#{full_name} donated $#{donation_amount}"
end
end
donor = Donor.new
include Glimmer
frame(title: 'Hello, Data-Binding!') { |window|
panel {
v_box_sizer {
h_box_sizer {
sizer_args 0, Wx::DOWN, 10
static_text(label: 'First Name:') {
sizer_args 0, Wx::RIGHT, 10
}
text_ctrl {
sizer_args 0, Wx::RIGHT, 10
# data-bind donor.first_name to text_ctrl.value bidirectionally,
# ensuring changes to either attribute update the other attribute
value <=> [donor, :first_name]
}
}
h_box_sizer {
sizer_args 0, Wx::DOWN, 10
static_text(label: 'Last Name:') {
sizer_args 0, Wx::RIGHT, 10
}
text_ctrl {
sizer_args 0, Wx::RIGHT, 10
# data-bind donor.last_name to text_ctrl.value bidirectionally,
# ensuring changes to either attribute update the other attribute
value <=> [donor, :last_name]
}
}
h_box_sizer {
sizer_args 0, Wx::DOWN, 10
static_text(label: 'Donation Amount: $') {
sizer_args 0, Wx::RIGHT, 1
}
spin_ctrl {
sizer_args 0, Wx::RIGHT, 10
range Donor::DONATION_AMOUNT_MIN, Donor::DONATION_AMOUNT_MAX
# data-bind donor.donation_amount to spin_ctrl.value bidirectionally,
# ensuring changes to either attribute update the other attribute
value <=> [donor, :donation_amount]
}
}
h_box_sizer {
sizer_args 0, Wx::DOWN, 10
slider {
sizer_args 0, Wx::RIGHT, 10
range Donor::DONATION_AMOUNT_MIN, Donor::DONATION_AMOUNT_MAX
# data-bind donor.first_name to spinner.value bidirectionally,
# ensuring changes to either attribute update the other attribute
value <=> [donor, :donation_amount]
}
}
h_box_sizer {
sizer_args 0, Wx::DOWN, 10
static_text(label: '') {
sizer_args 0, Wx::RIGHT, 10
# data-bind donor.summary to static_text.label unidirectionally,
# with computed data-binding from donor summary dependencies: first_name, last_name, and donation_amount
# ensuring changes to donor.summary or any of its dependencies update static_text.label
label <= [donor, :summary, computed_by: [:first_name, :last_name, :donation_amount]]
}
}
}
}
}

Glimmer on!!!

3 comments:

Chris Irish said...

This looks cool. My first programming job 20 years ago was writing desktop apps/widgets for a company using Python and pyGTK GUI libraries. This reminds me of that but nicer with Ruby. Thanks for sharing!

Code Master said...

Actually, there is a much more feature-rich and complete version of this project called Glimmer DSL for LibUI (Prerequisite-Free Ruby Desktop Development Cross-Platform Native GUI Library), which won a Fukuoka Ruby 2022 Special Award by Matz, the creator of Ruby:
https://github.com/AndyObtiva/glimmer-dsl-libui

There is also a more mature version of the project that runs on JRuby called Glimmer DSL for SWT (JRuby Desktop Development Cross-Platform Native GUI Framework), based on the Eclipse SWT GUI Toolkit that was used to create the Eclipse IDE, and supporting native executable packaging of Ruby applications as APP/DMG/PKG on the Mac, EXE/MSI on Windows, and DEB/RPM on Linux:
https://github.com/AndyObtiva/glimmer-dsl-swt

Glimmer DSL for WX is a new Glimmer spinoff that is aiming to bring the same features of the aforementioned Ruby projects to the very mature cross-platform native wxWidgets GUI toolkit.

Code Master said...

I forgot to mention, there is also a Glimmer flavor for GTK called Glimmer DSL for GTK (Ruby-GNOME Desktop Development GUI Library) that aims at making GTK development much simpler and more productive than it is in any other programming languages:
https://github.com/AndyObtiva/glimmer-dsl-gtk