Glimmer DSL Framework

repository·master·Indexed 21 days ago

https://github.com/andyobtiva/glimmer

A comprehensive Domain-Specific Language (DSL) framework for Ruby that provides an engine for building custom internal DSLs and a bidirectional data-binding library for synchronizing models with user interfaces. It supports multiple platform-specific DSLs, including Glimmer DSL for LibUI (MRI Ruby), Glimmer DSL for SWT (JRuby), Glimmer DSL for Web, Glimmer DSL for GTK, and Glimmer DSL for XML/CSS.

Tokens
10.8K
Snippets
33
Records
49
Agent score
73%

What's inside Glimmer

  1. Create a native GUI with Glimmer DSL for LibUI

    master

    Glimmer DSL for LibUI provides a prerequisite-free way to build native, platform-independent Ruby desktop applications. It uses libui (a thin C wrapper) to render native controls without the overhead of a JVM (unlike SWT). It is ideal for small, simple applications that need to start instantly and have a small memory footprint.

    Features include:

    • Declarative DSL syntax mapping to widget hierarchy.
    • Bidirectional Data-Binding.
    • Native-Executable packaging for Mac, Windows, and Linux.
    require 'glimmer-dsl-libui'
    
    include Glimmer
    
    window('hello world').show
  2. Glimmer DSL for SWT overview

    master
    Glimmer DSL for SWT is a JRuby-based framework for building native-GUI cross-platform desktop applications using the Eclipse SWT library. It provides a declarative Ruby DSL for UI authoring and includes built-in data-binding support to synchronize GUIs with domain models. It also supports packaging applications into native formats like DMG/PKG (macOS) and MSI/EXE (Windows).
  3. What is Glimmer DSL Framework?

    master

    Glimmer is a Domain-Specific Language (DSL) Framework for Ruby that provides two core capabilities:

    1. DSL Engine: Enables the creation of embedded internal DSLs within Ruby for various domains such as GUI, XML, or CSS.
    2. Data-Binding Library: Provides tools to synchronize GUI elements with Model Attributes bidirectionally. As of v2, it supports the Shine data-binding syntax.

    To use Glimmer's DSL capabilities, you must mix in the Glimmer module into your classes. This ensures that the DSL syntax is scoped to your class and does not pollute the global namespace.

  4. Use bidirectional data-binding in Glimmer DSL for Tk

    master

    You can declaratively wire GUI widgets to a Ruby object's attributes using the <=> operator. This automatically synchronizes the widget's state with the model.

    Example using a combobox bound to a Person object's country attribute:

    include Glimmer
    
    class Person
      attr_accessor :country
      def initialize; @country = "Canada"; end
    end
    
    person = Person.new
    
    root {
      combobox {
        text <=> [person, :country]
      }
    }.open
  5. Compare Glimmer GUI DSLs to choose the right toolkit

    master

    Glimmer is a DSL framework that provides multiple specialized DSLs for different GUI toolkits. Choosing the right one depends on your Ruby runtime (MRI vs JRuby) and your platform requirements:

    By Ruby Runtime

    • JRuby (JVM): Use glimmer-dsl-swt (feature-complete, native widgets via Eclipse SWT) or glimmer-dsl-jfx (JavaFX).
    • MRI (CRuby): Use glimmer-dsl-libui, glimmer-dsl-gtk, glimmer-dsl-tk, glimmer-dsl-fx, or glimmer-dsl-wx.

    By Native Widget Support

    • Full Native Support (Mac, Windows, Linux): glimmer-dsl-swt (via JRuby) and glimmer-dsl-libui (via MRI).
    • Platform Specific/Partial Native: glimmer-dsl-gtk (native on Linux/Gnome), glimmer-dsl-wx (native on Windows).
    • Non-Native (Java-based): glimmer-dsl-swing and glimmer-dsl-jfx.

    By Ease of Setup

    • Zero Prerequisites: glimmer-dsl-libui can be used immediately after installing the gem.
    • Requires Dependencies: glimmer-dsl-gtk, glimmer-dsl-tk, glimmer-dsl-fx, and glimmer-dsl-wx generally require additional system-level libraries.
  6. Core classes for Glimmer Data-Binding

    master

    Glimmer's data-binding library uses several key classes to manage synchronization between Models and Views, primarily following the Observer and Model-View-Presenter (MVP) patterns:

    • Glimmer::DataBinding::Observer: Provides general observer support. Key methods: call, register (alias: observe), unregister (alias: unobserve or deregister). Use ignore_frozen: true with register to silently ignore frozen objects.
    • Glimmer::DataBinding::Observable: Super-module for all observables. Key methods: add_observer, remove_observer.
    • Glimmer::DataBinding::ObservableModel: Mixin for Object, Struct, or OpenStruct. Automatically enhances attribute setters (ending in =) to notify observers. Supports custom attribute_writer_type (default: :attribute=).
    • Glimmer::DataBinding::ObservableArray: Mixin for arrays. Notifies observers on mutation (e.g., push, delete). Supports recursive: true or recursive: [integer] for nested observation.
    • Glimmer::DataBinding::ObservableHash: Mixin for hashes. Notifies observers on mutation (e.g., hash[key]=value, merge!) and handles nested ObservableArray values.
    • Glimmer::DataBinding::ModelBinding: High-level abstraction for basic, nested, and computed data-binding.
    • Glimmer::DataBinding::Shine: Enables the <=> (bidirectional) and <= (unidirectional) syntax.
  7. Understand the Glimmer DSL architecture and benefits

    master

    Glimmer is designed as a declarative DSL framework that abstracts low-level, imperative GUI toolkit APIs into a high-level, productive developer experience.

    Key Architectural Advantages

    • Standard Ruby Closures: Unlike older DSLs (like Shoes) that change self inside blocks, Glimmer DSL blocks are standard Ruby closures. This allows you to use variables defined outside the blocks in a standard Ruby way.
    • MVC/MVP Support: Glimmer encourages a proper separation of concerns, supporting Model-View-Controller or Model-View-Presenter architectures by default.
    • Data Binding: Provides built-in bidirectional and unidirectional data-binding support to keep View components in sync with Model data with minimal syntax.
    • Extensibility: Developers can build custom View components (widgets, shells, or canvas shapes) to expand the DSL's vocabulary.
    • Hybrid Usage: Glimmer allows you to use the DSL for 80% of your work while still providing the ability to drop down to the low-level native toolkit API (e.g., using SWT directly) for the remaining 20% of specialized cases.
  8. Use computed data-binding in Glimmer DSL for Opal

    master

    In Glimmer DSL for Opal, you can use the bind method to create reactive UI elements. You can also define computed properties that automatically update when their dependencies change using the computed_by option.

    Key binding patterns:

    • bind(object, :attribute): Binds a widget property to an object attribute.
    • bind(object, :computed_attr, computed_by: [:dep1, :dep2]): Binds to a method that recalculates whenever :dep1 or :dep2 changes.
    • bind(object, :attribute, on_write: :type_conversion): Binds with an automatic type conversion on write (e.g., :to_i).
    # Example of computed and type-converted binding
    label {
      text bind(@contact, :name, computed_by: [:first_name, :last_name])
    }
    
    label {
      text bind(@contact, :age, on_write: :to_i, computed_by: [:year_of_birth])
    }
  9. How Multi-DSL support works

    master

    The Glimmer DSL Engine allows you to mix multiple DSLs within a single application. This is achieved by detecting 'top-level keywords' (expressions denoted as TopLevelExpression).

    When a top-level keyword from a specific DSL is encountered (e.g., html for XML or css for CSS), Glimmer automatically activates that DSL. Once the nested DSL block is finished, Glimmer automatically switches back to the previous DSL context. By default, all loaded DSL gems are enabled.

    require 'glimmer-dsl-swt'
    require 'glimmer-dsl-xml'
    require 'glimmer-dsl-css'
    
    include Glimmer
    
    shell {
      browser {
        text html {
          head {
            style {
              css {
                h1 { background 'yellow' }
              }
            }
          }
          body { h1 { "Hello, World!" } }
        }
      }
    }.open
  10. How the Glimmer DSL syntax works

    master

    Glimmer uses a Domain Specific Language (DSL) to define interfaces. A DSL expression typically consists of four components:

    1. Keywords: The primary command (e.g., table).
    2. Styles/Arguments: Optional arguments passed to a keyword (e.g., table(:multi) where :multi is a style).
    3. Content: Nested properties, keywords, or listeners defined within a block (e.g., { table_column { text 'Name' } }).
    4. Methods: Actions performed on objects (e.g., shell.show).

    Example: Hello World in Glimmer DSL for SWT

    include Glimmer
    
    shell(:no_resize) { # keyword + style arg
      text "Glimmer" # attribute content
    
      label { # keyword content
        text "Hello, World!" # attribute content
      }
    }.open
  11. Integrate Glimmer Calculator into a Rails app

    master

    To run the Glimmer Calculator web version in a Rails environment:

    1. Add the gem to your Gemfile without requiring it immediately:
      gem 'glimmer-cs-calculator', require: false
    2. Require the launch module in your Rails asset pipeline:
      # In app/assets/javascripts/application.rb
      require 'glimmer-cs-calculator/launch'
    3. Start your server with rails s and visit http://localhost:3000.
    # Gemfile
    gem 'glimmer-cs-calculator', require: false
    
    # app/assets/javascripts/application.rb
    require 'glimmer-cs-calculator/launch'
  12. Webify desktop apps with Glimmer DSL for Opal

    master

    Glimmer DSL for Opal is an experimental web GUI adapter that allows you to run Glimmer desktop apps (built with glimmer-dsl-swt) in a web browser via Opal Ruby and Rails. It reuses the core Glimmer DSL engine, meaning desktop apps can be 'webified' without changing code and inherit full data-binding capabilities. Once running in the browser, apps can be styled using standard CSS.

    To use this, install the glimmer-dsl-opal gem as described on its project page.

    # Example: Adding a sample to a Rails app
    # In app/assets/javascripts/application.rb
    require 'samples/hello/hello_computed'