Glimmer DSL for LibUI 0.5.10 - Shape Listeners
Tags: ruby, desktop, gui, libui
Published: 2022-05-14 16:58:11

[Glimmer DSL for LibUI](https://github.com/AndyObtiva/glimmer-dsl-libui) 0.5.10 ([Fukuoka Award Winning](https://andymaleh.blogspot.com/2022/02/glimmer-dsl-for-libui-wins-fukuoka-ruby.html) Ruby Desktop Development GUI Library) has just been released with the following changes: 

Support nesting [area mouse listeners](https://github.com/AndyObtiva/glimmer-dsl-libui#area-listeners) underneath shapes directly given the newly added support for the include?(x, y) method, which can be used to detect if a mouse event fired for a specific shape
examples/shape_coloring.rb
In summary, you can now use [area mouse listeners](https://github.com/AndyObtiva/glimmer-dsl-libui#area-listeners) within shapes directly and [Glimmer DSL for LibUI](https://github.com/AndyObtiva/glimmer-dsl-libui) will ensure that they do not fire unless the mouse x,y coordinates land within the shape regions (it does so by leveraging the [Perfect Shape](https://github.com/AndyObtiva/perfect-shape) gem)

In fact, that has been demonstrated in the new example: [Shape Coloring](https://github.com/AndyObtiva/glimmer-dsl-libui#shape-coloring)

![screenshot](https://raw.githubusercontent.com/AndyObtiva/glimmer-dsl-libui/master/images/glimmer-dsl-libui-mac-shape-coloring-color-dialog.png)

```ruby
# From: https://github.com/AndyObtiva/glimmer-dsl-libui#shape-coloring

require 'glimmer-dsl-libui'

class ShapeColoring
  include Glimmer::LibUI::Application
  
  COLOR_SELECTION = Glimmer::LibUI.interpret_color(:red)
  
  before_body {
    @shapes = []
  }
  
  body {
    window('Shape Coloring', 200, 200) {
      margined false
      
      grid {
        label("Click a shape to select and\nchange color via color button") {
          left 0
          top 0
          hexpand true
          halign :center
          vexpand false
        }
        
        color_button { |cb|
          left 0
          top 1
          hexpand true
          vexpand false
          
          on_changed do
            @selected_shape&.fill = cb.color
          end
        }
      
        area {
          left 0
          top 2
          hexpand true
          vexpand true
          
          rectangle(0, 0, 600, 400) { # background shape
            fill :white
          }
          
          @shapes << colorable(:rectangle, 20, 20, 40, 20) { |shape|
            fill :lime
          }
          
          @shapes << colorable(:square, 80, 20, 20) { |shape|
            fill :blue
          }
          
          @shapes << colorable(:circle, 75, 70, 20, 20) { |shape|
            fill :green
          }
          
          @shapes << colorable(:arc, 120, 70, 40, 0, 145) { |shape|
            fill :orange
          }
          
          @shapes << colorable(:polygon, 120, 10, 120, 50, 150, 10, 150, 50) {
            fill :cyan
          }
          
          @shapes << colorable(:polybezier, 20, 40,
                     30, 100, 50, 80, 80, 110,
                     40, 120, 20, 120, 30, 91) {
            fill :pink
          }
        }
      }
    }
  }
  
  def colorable(shape_symbol, *args, &content)
    send(shape_symbol, *args) do |shape|
      on_mouse_up do |area_mouse_event|
        old_stroke = Glimmer::LibUI.interpret_color(shape.stroke).slice(:r, :g, :b)
        @shapes.each {|sh| sh.stroke = nil}
        @selected_shape = nil
        unless old_stroke == COLOR_SELECTION
          shape.stroke = COLOR_SELECTION.merge(thickness: 2)
          @selected_shape = shape
        end
      end
      
      content.call(shape)
    end
  end
end

ShapeColoring.launch
```

Happy [Glimmering](https://github.com/AndyObtiva/glimmer-dsl-libui)!