Sha256: 40cdff5d7cf6db457ce63fe890a53ce405d77c3c5dde526b3f38703ccaecc47c

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

require 'glimmer-dsl-libui'

class User
  ALL_ATTRIBUTES = [:first_name, :last_name, :email, :street, :city, :state, :zip_code, :country]
  
  attr_accessor :customizable_attributes, *ALL_ATTRIBUTES
  
  def initialize
    # allow customizing all attributes by default
    self.customizable_attributes = ALL_ATTRIBUTES.dup
  end
  
  def select_customizable_attribute(attribute, selected)
    if selected
      customizable_attributes.push(attribute)
    else
      customizable_attributes.delete(attribute)
    end
    customizable_attributes.sort_by! {|attribute| User::ALL_ATTRIBUTES.index(attribute)}
  end
end

class DynamicForm
  include Glimmer::LibUI::Application
  
  before_body do
    @user = User.new
  end
  
  body {
    window('Dynamic Form') {
      margined true
      
      vertical_box {
        horizontal_box {
          User::ALL_ATTRIBUTES.each do |attribute|
            checkbox(attribute.to_s) {
              checked <=> [@user, :customizable_attributes,
                           on_read: -> (attributes) { @user.customizable_attributes.include?(attribute) },
                           on_write: -> (checked_value) { @user.select_customizable_attribute(attribute, checked_value) }
                          ]
            }
          end
        }
        
        form {
          stretchy false
          
          # Control content data-binding allows dynamically changing content based on changes in a model attribute
          content(@user, :customizable_attributes) {
            @user.customizable_attributes.each do |attribute|
              entry {
                label attribute.to_s.split('_').map(&:capitalize).join(' ')
                text <=> [@user, attribute]
              }
            end
          }
        }

        button('Summarize') {
          on_clicked do
            summary = @user.customizable_attributes.map { |attribute| @user.send(attribute) }.join(', ')
            msg_box('Summary', summary)
          end
        }
      }
    }
  }
end

DynamicForm.launch

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
glimmer-dsl-libui-0.11.3 examples/dynamic_form.rb
glimmer-dsl-libui-0.11.2 examples/dynamic_form.rb
glimmer-dsl-libui-0.11.1 examples/dynamic_form.rb