Class: Glimmer::SWT::MenuProxy

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

Overview

Proxy for org.eclipse.swt.widgets.Menu

Functions differently from other widget proxies.

Glimmer automatically detects if this is a drop down menu or pop up menu from its parent if no SWT style is passed in.

There are 3 possibilities:

  • SWT :bar style is passed in: Menu Bar

  • Parent is ShellProxy: Pop Up Menu (having style :pop_up)

  • Parent is another Menu: Drop Down Menu (having style :drop_down)

In order to get the SWT Menu object, one must call ‘#swt_widget`.

In the case of a Drop Down menu, this automatically creates an SWT MenuItem object with style :cascade

In order to retrieve the menu item widget proxy, one must call ‘#menu_item_proxy`

Follows the Proxy Design Pattern

Constant Summary collapse

STYLE =
<<~CSS
  .menu.menu-bar {
    position: absolute;
    top: -30px;
    border-radius: 0;
    width: 100%;
  }
  .menu.menu-bar .menu {
    border-radius: 0;
  }
  .menu-bar .menu-item {
    width: 180px;
  }
  .menu-bar > .menu-item {
    display: inline-block;
    width: 150px;
  }
  li.menu-item {
    padding-left: initial;
    padding-right: initial;
  }
  .menu {
    /* TODO consider auto-sizing in the future */
    font-size: initial;
    border-radius: 5px;
  }
  .menu:not(.menu-bar) {
    width: 150px;
  }
  .menu-bar .ui-menu:not(.menu-bar) {
    width: 180px;
  }
  .ui-menu-item:first-child > .ui-menu-item-wrapper {
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
  }
  .ui-menu-item:last-child > .ui-menu-item-wrapper {
    border-bottom-left-radius: 5px;
    border-bottom-right-radius: 5px;
  }
  .menu-bar .ui-menu-item:first-child > .ui-menu-item-wrapper {
    border-top-left-radius: 0;
    border-top-right-radius: 0;
  }
  .menu-bar .ui-menu-item:last-child > .ui-menu-item-wrapper {
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
  }
  
CSS

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?, #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, #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, #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_request_to_event_mapping, #observation_requests, #parent_dom_element, #parent_path, #parents, #post_dispose_child, #print, #property_type_converters, #reattach, #remove_all_listeners, #remove_css_class, #remove_css_classes, #remove_event_listener_proxies, 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 = nil) ⇒ MenuProxy

Returns a new instance of MenuProxy.



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
# File 'lib/glimmer/swt/menu_proxy.rb', line 101

def initialize(parent, args, block = nil)
  # TODO refactor/simplify code below
  @children = []
  index = args.delete(args.last) if args.last.is_a?(Numeric)
  args = args.map {|arg| arg.is_a?(String) ? arg.to_sym : arg}
  if parent.is_a?(ShellProxy)
    args = args.unshift(:bar)
  elsif parent.is_a?(MenuProxy)
    args = args.unshift(:drop_down)
  else
    args = args.unshift(:pop_up)
  end
  if parent.is_a?(MenuProxy)
    @menu_item_proxy = SWT::WidgetProxy.for('menu_item', parent, [:cascade] + [index].compact, block)
    super(@menu_item_proxy, args, nil)
    @menu_item_proxy.menu = self
  elsif parent.is_a?(ShellProxy)
    super(parent, args, nil)
  else # widget pop up
    super(parent, args, nil)
  end
  
  if bar?
    # Assumes a parent shell
    parent.menu_bar = self
  elsif pop_up?
    parent.menu = self
  end
  
  # TODO IMPLEMENT PROPERLY
#         on_focus_lost {
#           dispose
#         }
end

Dynamic Method Handling

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

Instance Attribute Details

Returns the value of attribute menu_item_proxy.



99
100
101
# File 'lib/glimmer/swt/menu_proxy.rb', line 99

def menu_item_proxy
  @menu_item_proxy
end

Returns the value of attribute menu_parent.



99
100
101
# File 'lib/glimmer/swt/menu_proxy.rb', line 99

def menu_parent
  @menu_parent
end

Instance Method Details

#bar?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/glimmer/swt/menu_proxy.rb', line 136

def bar?
  args.include?(:bar)
end

#can_handle_observation_request?(observation_request, super_only: false) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
173
174
175
176
177
# File 'lib/glimmer/swt/menu_proxy.rb', line 170

def can_handle_observation_request?(observation_request, super_only: false)
  super_result = super(observation_request)
  if observation_request.start_with?('on_') && !super_result && !super_only
    return menu_item_proxy.can_handle_observation_request?(observation_request)
  else
    super_result
  end
end

#closeObject



257
258
259
260
261
# File 'lib/glimmer/swt/menu_proxy.rb', line 257

def close
  dom_element.remove
  Element['body'].off('click', @close_event_handler)
  @visible = false
end

#domObject



281
282
283
284
285
286
287
288
289
290
# File 'lib/glimmer/swt/menu_proxy.rb', line 281

def dom
  css_class = name
  css_class += ' menu-bar' if bar?
  css_class += ' menu-drop-down' if drop_down?
  css_class += ' menu-pop-up' if pop_up?
  @dom ||= html {
    ul(id: id, class: css_class) {
    }
  }.to_s
end

Returns:

  • (Boolean)


144
145
146
# File 'lib/glimmer/swt/menu_proxy.rb', line 144

def drop_down?
  args.include?(:drop_down)
end

#elementObject



277
278
279
# File 'lib/glimmer/swt/menu_proxy.rb', line 277

def element
  'ul'
end

#enabledObject



156
157
158
159
160
161
162
# File 'lib/glimmer/swt/menu_proxy.rb', line 156

def enabled
  if drop_down?
    menu_item_proxy.enabled
  else
    true
  end
end

#enabled=(value) ⇒ Object



164
165
166
167
168
# File 'lib/glimmer/swt/menu_proxy.rb', line 164

def enabled=(value)
  if drop_down?
    menu_item_proxy.enabled = value
  end
end

#handle_observation_request(observation_request, block) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/glimmer/swt/menu_proxy.rb', line 179

def handle_observation_request(observation_request, block)
  if can_handle_observation_request?(observation_request, super_only: true)
    super
  else
    menu_item_proxy.handle_observation_request(observation_request, block)
  end
end

#parent_menuObject



273
274
275
# File 'lib/glimmer/swt/menu_proxy.rb', line 273

def parent_menu
  parent.parent unless root_menu?
end

#pop_up?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/glimmer/swt/menu_proxy.rb', line 140

def pop_up?
  args.include?(:pop_up)
end

#post_add_contentObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/glimmer/swt/menu_proxy.rb', line 197

def post_add_content
  if bar?
    parent_dom_element.css('position', 'relative')
    parent_dom_element.css('margin-top', '30px')
    parent_dom_element.css('height', '114%')
    render
    `$(#{path}).menu({
      position: { my: "top", at: "bottom" },
      icons: { submenu: "ui-icon-blank" }
    });`
    the_element = dom_element
    the_element.on('mouseenter') do |event|
      if event.page_x.between?(the_element.offset.left, the_element.offset.left + the_element.width) and
         event.page_y.between?(the_element.offset.top, the_element.offset.top + the_element.height)
        `$(#{path}).menu('option', 'position', { my: 'left top', at: 'left bottom' })`
      end
    end
    the_element.on('mouseout') do |event|
      if event.page_x.between?(the_element.offset.left, the_element.offset.left + the_element.width) and
         event.page_y.between?(the_element.offset.top, the_element.offset.top + the_element.height)
        `$(#{path}).menu('option', 'position', { my: 'left top', at: 'left bottom' })`
       else
        `$(#{path}).menu('option', 'position', { my: 'left top', at: 'right top' })`
      end
    end
    minimum_width = children.to_a.map(&:dom_element).map(&:width).map(&:to_f).reduce(:+)
    the_element.css('min-width', minimum_width)
  end
end

#post_initialize_child(child) ⇒ Object



187
188
189
190
191
192
193
194
195
# File 'lib/glimmer/swt/menu_proxy.rb', line 187

def post_initialize_child(child)
  if child && !@children.include?(child)
    if child.is_a?(MenuItemProxy)
      @children << child
    else
      @children << child.menu_item_proxy
    end
  end
end

#render(custom_parent_dom_element: nil, brand_new: false) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/glimmer/swt/menu_proxy.rb', line 242

def render(custom_parent_dom_element: nil, brand_new: false)
  # TODO attach to top nav bar if parent is shell
  # TODO attach listener to parent to display on right click
  if parent.is_a?(MenuProxy) || parent.is_a?(MenuItemProxy) || parent.menu_requested? || parent.is_a?(ShellProxy)
    super(custom_parent_dom_element: custom_parent_dom_element, brand_new: brand_new)
    if root_menu? && !bar?
      `$(#{path}).menu();`
      @close_event_handler = lambda do |event|
        close if event.target != parent.dom_element && event.target.parents('.ui-menu').empty?
      end
      Element['body'].on('click', &@close_event_handler)
    end
  end
end

#root_menuObject



267
268
269
270
271
# File 'lib/glimmer/swt/menu_proxy.rb', line 267

def root_menu
  the_menu = self
  the_menu = the_menu.parent_menu until the_menu.root_menu?
  the_menu
end

#root_menu?Boolean

Returns:

  • (Boolean)


263
264
265
# File 'lib/glimmer/swt/menu_proxy.rb', line 263

def root_menu?
  !parent.is_a?(MenuProxy) && !parent.is_a?(MenuItemProxy)
end

#textObject



148
149
150
# File 'lib/glimmer/swt/menu_proxy.rb', line 148

def text
  @menu_item_proxy&.text
end

#text=(text_value) ⇒ Object



152
153
154
# File 'lib/glimmer/swt/menu_proxy.rb', line 152

def text=(text_value)
  @menu_item_proxy&.text = text_value
end

#visible=(value) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/glimmer/swt/menu_proxy.rb', line 227

def visible=(value)
  @visible = value
  if @visible
    parent.menu_requested = true
    parent.dom_element.css('position', 'relative')
    render
    dom_element.css('position', 'absolute')
    dom_element.css('left', parent.menu_x - parent.dom_element.offset.left)
    dom_element.css('top', parent.menu_y - parent.dom_element.offset.top)
    parent.menu_requested = false
  else
    close
  end
end