Class: Glimmer::SWT::SpinnerProxy

Inherits:
WidgetProxy show all
Defined in:
lib/glimmer/swt/spinner_proxy.rb

Constant Summary

Constants inherited from WidgetProxy

WidgetProxy::DEFAULT_INITIALIZERS, WidgetProxy::JS_KEY_CODE_TO_SWT_KEY_CODE_MAP, WidgetProxy::JS_LOCATION_TO_SWT_KEY_LOCATION_MAP, WidgetProxy::SWT_CURSOR_TO_CSS_CURSOR_MAP

Instance Attribute Summary collapse

Attributes inherited from WidgetProxy

#args, #background, #children, #cursor, #disposed?, #enabled, #focus, #font, #foreground, #menu, #menu_requested, #menu_x, #menu_y, #parent, #path, #rendered

Instance Method Summary collapse

Methods inherited from WidgetProxy

#add_content_on_render, #add_css_class, #add_css_classes, #add_observer, #apply_property_type_converters, #attach, #build_dom, #can_handle_observation_request?, #clear_css_classes, #content, #content_on_render_blocks, #css_classes, #default_observation_request_to_event_mapping, #dialog_ancestor, #dispose, #dom_element, #effective_observation_request_to_event_mapping, #event_handling_suspended?, #event_listener_proxies, for, #get_data, #handle_javascript_observation_request, #handle_observation_request, #has_style?, #id, #id=, #listener_dom_element, #listener_path, #listeners, #listeners_for, max_id_number_for, max_id_numbers, #method_missing, #name, next_id_number_for, #observation_requests, #parent_dom_element, #parent_path, #parents, #post_add_content, #post_dispose_child, #post_initialize_child, #print, #property_type_converters, #reattach, #remove_all_listeners, #remove_css_class, #remove_css_classes, #remove_event_listener_proxies, #render, reset_max_id_numbers!, #resume_event_handling, #selector, #set_attribute, #set_data, #set_focus, #shell, #skip_content_on_render_blocks?, #style_element, #suspend_event_handling, #swt_data, #swt_widget, underscored_widget_name, widget_class, widget_exists?, widget_handling_listener, #widget_property_listener_installers

Methods included from PropertyOwner

#attribute_getter, #attribute_setter, #get_attribute, #set_attribute

Constructor Details

#initialize(parent, args, block) ⇒ SpinnerProxy

Returns a new instance of SpinnerProxy.



29
30
31
32
33
34
# File 'lib/glimmer/swt/spinner_proxy.rb', line 29

def initialize(parent, args, block)
  super(parent, args, block)
  @increment = 1
  @digits = 0
  dom_element.spinner
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Glimmer::SWT::WidgetProxy

Instance Attribute Details

#digitsObject

Returns the value of attribute digits.



27
28
29
# File 'lib/glimmer/swt/spinner_proxy.rb', line 27

def digits
  @digits
end

#incrementObject

Returns the value of attribute increment.



27
28
29
# File 'lib/glimmer/swt/spinner_proxy.rb', line 27

def increment
  @increment
end

#maximumObject

Returns the value of attribute maximum.



27
28
29
# File 'lib/glimmer/swt/spinner_proxy.rb', line 27

def maximum
  @maximum
end

#minimumObject

Returns the value of attribute minimum.



27
28
29
# File 'lib/glimmer/swt/spinner_proxy.rb', line 27

def minimum
  @minimum
end

#page_incrementObject

Returns the value of attribute page_increment.



27
28
29
# File 'lib/glimmer/swt/spinner_proxy.rb', line 27

def page_increment
  @page_increment
end

Instance Method Details

#dividerObject



86
87
88
# File 'lib/glimmer/swt/spinner_proxy.rb', line 86

def divider
  ('1' + '0'*@digits.to_i).to_f
end

#domObject



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/glimmer/swt/spinner_proxy.rb', line 159

def dom
  text_text = @text
  text_id = id
  text_style = "min-width: 27px; #{css}"
  text_class = name
  # TODO `:style` key is duplicated twice
  options = {type: 'text', id: text_id, style: text_style, class: text_class, value: text_text}
  options = options.merge('disabled': 'disabled') unless @enabled
  @dom ||= html {
    input(options)
  }.to_s
end

#elementObject



95
96
97
# File 'lib/glimmer/swt/spinner_proxy.rb', line 95

def element
  'input'
end

#observation_request_to_event_mappingObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/glimmer/swt/spinner_proxy.rb', line 99

def observation_request_to_event_mapping
  {
    'on_widget_selected' => [
      {
        event: 'change',
        event_handler: -> (event_listener) {
          -> (event) {
            self.selection = event.target.value
            event_listener.call(event)
          }
        }
      },
      {
        event: 'keyup',
        event_handler: -> (event_listener) {
          -> (event) {
            @keyup = true # ensures spinstop event does not set selection if caused by key up entry
          }
        }
      },
      {
        event: 'spin',
        event_handler: -> (event_listener) {
          -> (event) {
            @keyup = false
          }
        }
      },
      {
        event: 'spinstop',
        event_handler: -> (event_listener) {
          -> (event) {
            self.set_selection(event.target.value, format_digits: !@keyup)
            @keyup = false
            event_listener.call(event)
          }
        }
      },
    ],
    'on_modify_text' => {
      event: 'keyup',
      event_handler: -> (event_listener) {
        -> (event) {
          # TODO consider unifying this event handler with on_key_pressed by relying on its result instead of hooking another keyup event
          if @last_key_pressed_event.nil? || @last_key_pressed_event.doit
            self.text = event.target.value
            event_listener.call(event)
          else
            # TODO Fix doit false, it's not stopping input
            event.prevent
            event.prevent_default
            event.stop_propagation
            event.stop_immediate_propagation
          end
        }
      }
    },
  }
end

#selectionObject



54
55
56
# File 'lib/glimmer/swt/spinner_proxy.rb', line 54

def selection
  @selection && @selection * divider
end

#selection=(value, format_digits: true) ⇒ Object Also known as: set_selection



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/glimmer/swt/spinner_proxy.rb', line 36

def selection=(value, format_digits: true)
  old_value = @selection.to_f
  if @selection.nil?
    @selection = value.to_f / divider
  else
    @selection = value.to_f
  end
  if value.to_i != old_value.to_i
    if format_digits && @digits.to_i > 0
      new_value = "%0.#{@digits.to_i}f" % @selection
      dom_element.value = new_value
    else
      dom_element.value = @selection if @selection != 0
    end
  end
end

#textObject



62
63
64
# File 'lib/glimmer/swt/spinner_proxy.rb', line 62

def text
  self.selection.to_s
end

#text=(value) ⇒ Object



58
59
60
# File 'lib/glimmer/swt/spinner_proxy.rb', line 58

def text=(value)
  self.selection = value.to_f
end