DEV Community

Andy Maleh
Andy Maleh

Posted on

The Simplest Button Counter on Earth!

Glimmer DSL for SWT (Ruby Desktop GUI) and Glimmer DSL for Opal (Ruby Web GUI) had a new release with support for a new sample: Hello, Button!

Desktop GUI:
Desktop GUI

Web GUI:


class HelloButton
  include Glimmer

  attr_accessor :count

  def initialize
    @count = 0

  def launch
    shell {
      text 'Hello, Button!'

      button {
        text bind(self, :count) {|value| "Click To Increment: #{value}  "}

        on_widget_selected {
          self.count += 1
Enter fullscreen mode Exit fullscreen mode

Notice how utterly simple and intuitive the code is.

It is simply leveraging a standard Ruby attribute on a class (:count), data-binding to it in the GUI (text bind(self, :count)), and then using a block to dynamically customize the data-bound property on read from the attribute before displaying in the button text ({|value| "Click To Increment: #{value} "}).

Otherwise, the on_widget_selected event, which gets triggered on click of the button simply calls the += operator in Ruby to do the increment work in the model (self.count += 1), and that automatically updates the text in the GUI.

None of the web template mixing nonsense you see in web examples of it since it is written fully in pure Ruby, thanks to the Glimmer GUI DSL.

As such, it is the most concise, understandable, and maintainable code for a button counter. Beautiful, isn't it?!

Originally posted at the Code Master blog:

Glimmer DSL for SWT (Ruby Desktop GUI):

Glimmer DSL for Opal (Ruby Web GUI):

Top comments (0)