Class: Glimmer::SWT::WidgetProxy

Inherits:
Object
  • Object
show all
Includes:
Glimmer, PropertyOwner
Defined in:
lib/glimmer/swt/widget_proxy.rb

Defined Under Namespace

Classes: Event

Constant Summary collapse

JS_KEY_CODE_TO_SWT_KEY_CODE_MAP =
{
  16 => SWTProxy[:shift],
  17 => SWTProxy[:ctrl],
  18 => SWTProxy[:alt],
  37 => SWTProxy[:arrow_left],
  38 => SWTProxy[:arrow_up],
  39 => SWTProxy[:arrow_right],
  40 => SWTProxy[:arrow_down],
}
JS_LOCATION_TO_SWT_KEY_LOCATION_MAP =
{
  0 => SWTProxy[:none],
  1 => SWTProxy[:left],
  2 => SWTProxy[:right],
  3 => SWTProxy[:keypad],
}
SWT_CURSOR_TO_CSS_CURSOR_MAP =
{
  wait: 'wait',
  sizenwse: 'nwse-resize',
  appstarting: 'progress',
  no: 'no-drop',
  sizenesw: 'nesw-resize',
  sizeall: 'all-scroll',
  help: 'help',
  sizee: 'e-resize',
  sizewe: 'ew-resize',
  sizen: 'n-resize',
  sizes: 's-resize',
  sizew: 'w-resize',
  cross: 'crosshair',
  sizese: 'se-resize',
  ibeam: 'text',
  arrow: 'default',
  sizesw: 'sw-resize',
  uparrow: 'alias',
  hand: 'grab',
  sizenw: 'nw-resize',
  sizene: 'ne-resize',
  sizens: 'ns-resize',
}
DEFAULT_INITIALIZERS =
{
  # TODO remove if built in class initializer is taking care of this
  composite: proc do |composite_proxy|
    if composite_proxy.get_layout.nil?
      the_layout = GridLayoutProxy.new(composite_proxy, [])
      composite_proxy.layout = the_layout
      the_layout.margin_width = 15
      the_layout.margin_height = 15
    end
  end,
#         scrolled_composite: proc do |scrolled_composite|
#           scrolled_composite.expand_horizontal = true
#           scrolled_composite.expand_vertical = true
#         end,
#         table: proc do |table|
#           table.setHeaderVisible(true)
#           table.setLinesVisible(true)
#         end,
  table_column: proc do |table_column_proxy|
    table_column_proxy.width = 80
  end,
#         group: proc do |group_proxy|
#           group_proxy.layout = GridLayoutProxy.new(group_proxy, []) if group.get_layout.nil?
#         end,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PropertyOwner

#attribute_getter, #attribute_setter, #get_attribute

Constructor Details

#initialize(parent, args, block) ⇒ WidgetProxy

Returns a new instance of WidgetProxy.



155
156
157
158
159
160
161
162
163
164
# File 'lib/glimmer/swt/widget_proxy.rb', line 155

def initialize(parent, args, block)
  @parent = parent
  @args = args
  @block = block
  # TODO consider changing children to an array (why is it a Set if order matters?)
  @children = Set.new # TODO consider moving to composite
  @enabled = true if @enabled.nil?
  DEFAULT_INITIALIZERS[self.class.underscored_widget_name(self).to_s.to_sym]&.call(self)
  @parent.post_initialize_child(self) # TODO rename to post_initialize_child to be closer to glimmer-dsl-swt terminology
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



921
922
923
924
925
926
927
# File 'lib/glimmer/swt/widget_proxy.rb', line 921

def method_missing(method, *args, &block)
  if method.to_s.start_with?('on_')
    handle_observation_request(method, block)
  else
    super(method, *args, &block)
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



78
79
80
# File 'lib/glimmer/swt/widget_proxy.rb', line 78

def args
  @args
end

#backgroundObject

Returns the value of attribute background.



78
79
80
# File 'lib/glimmer/swt/widget_proxy.rb', line 78

def background
  @background
end

#childrenObject (readonly)

Returns the value of attribute children.



78
79
80
# File 'lib/glimmer/swt/widget_proxy.rb', line 78

def children
  @children
end

#cursorObject

Returns the value of attribute cursor.



78
79
80
# File 'lib/glimmer/swt/widget_proxy.rb', line 78

def cursor
  @cursor
end

#disposed?Object (readonly) Also known as: isDisposed, is_disposed

Returns the value of attribute disposed?.



78
79
80
# File 'lib/glimmer/swt/widget_proxy.rb', line 78

def disposed?
  @disposed?
end

#enabledObject

Returns the value of attribute enabled.



78
79
80
# File 'lib/glimmer/swt/widget_proxy.rb', line 78

def enabled
  @enabled
end

#focusObject

Returns the value of attribute focus.



78
79
80
# File 'lib/glimmer/swt/widget_proxy.rb', line 78

def focus
  @focus
end

#fontObject

Returns the value of attribute font.



78
79
80
# File 'lib/glimmer/swt/widget_proxy.rb', line 78

def font
  @font
end

#foregroundObject

Returns the value of attribute foreground.



78
79
80
# File 'lib/glimmer/swt/widget_proxy.rb', line 78

def foreground
  @foreground
end

Returns the value of attribute menu.



79
80
81
# File 'lib/glimmer/swt/widget_proxy.rb', line 79

def menu
  @menu
end

Returns the value of attribute menu_requested.



79
80
81
# File 'lib/glimmer/swt/widget_proxy.rb', line 79

def menu_requested
  @menu_requested
end

Returns the value of attribute menu_x.



79
80
81
# File 'lib/glimmer/swt/widget_proxy.rb', line 79

def menu_x
  @menu_x
end

Returns the value of attribute menu_y.



79
80
81
# File 'lib/glimmer/swt/widget_proxy.rb', line 79

def menu_y
  @menu_y
end

#parentObject (readonly)

Returns the value of attribute parent.



78
79
80
# File 'lib/glimmer/swt/widget_proxy.rb', line 78

def parent
  @parent
end

#pathObject (readonly) Also known as: widget_path

Returns the value of attribute path.



78
79
80
# File 'lib/glimmer/swt/widget_proxy.rb', line 78

def path
  @path
end

#renderedObject (readonly) Also known as: rendered?

Returns the value of attribute rendered.



78
79
80
# File 'lib/glimmer/swt/widget_proxy.rb', line 78

def rendered
  @rendered
end

Class Method Details

.for(keyword, parent, args, block) ⇒ Object

Factory Method that translates a Glimmer DSL keyword into a WidgetProxy object



87
88
89
90
# File 'lib/glimmer/swt/widget_proxy.rb', line 87

def for(keyword, parent, args, block)
  the_widget_class = widget_class(keyword)
  the_widget_class.respond_to?(:create) ? the_widget_class.create(keyword, parent, args, block) : the_widget_class.new(parent, args, block)
end

.max_id_number_for(name) ⇒ Object



108
109
110
# File 'lib/glimmer/swt/widget_proxy.rb', line 108

def max_id_number_for(name)
  @max_id_numbers[name] = max_id_numbers[name] || 0
end

.max_id_numbersObject



112
113
114
# File 'lib/glimmer/swt/widget_proxy.rb', line 112

def max_id_numbers
  @max_id_numbers ||= reset_max_id_numbers!
end

.next_id_number_for(name) ⇒ Object



104
105
106
# File 'lib/glimmer/swt/widget_proxy.rb', line 104

def next_id_number_for(name)
  @max_id_numbers[name] = max_id_number_for(name) + 1
end

.reset_max_id_numbers!Object



116
117
118
# File 'lib/glimmer/swt/widget_proxy.rb', line 116

def reset_max_id_numbers!
  @max_id_numbers = {}
end

.underscored_widget_name(widget_proxy) ⇒ Object



120
121
122
# File 'lib/glimmer/swt/widget_proxy.rb', line 120

def underscored_widget_name(widget_proxy)
  widget_proxy.class.name.split(/::|\./).last.sub(/Proxy$/, '').underscore
end

.widget_class(keyword) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/glimmer/swt/widget_proxy.rb', line 92

def widget_class(keyword)
  class_name_main = "#{keyword.camelcase(:upper)}Proxy"
  Glimmer::SWT.const_get(class_name_main.to_sym)
rescue => e
  puts "Widget #{keyword} was not found!"
  nil
end

.widget_exists?(keyword) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/glimmer/swt/widget_proxy.rb', line 100

def widget_exists?(keyword)
  !!widget_class(keyword)
end

.widget_handling_listenerObject



124
125
126
# File 'lib/glimmer/swt/widget_proxy.rb', line 124

def widget_handling_listener
  @@widget_handling_listener
end

Instance Method Details

#add_content_on_render(&content_block) ⇒ Object



369
370
371
372
373
374
375
# File 'lib/glimmer/swt/widget_proxy.rb', line 369

def add_content_on_render(&content_block)
  if rendered?
    content_block.call
  else
    content_on_render_blocks << content_block
  end
end

#add_css_class(css_class) ⇒ Object



766
767
768
# File 'lib/glimmer/swt/widget_proxy.rb', line 766

def add_css_class(css_class)
  dom_element.add_class(css_class)
end

#add_css_classes(css_classes_to_add) ⇒ Object



770
771
772
# File 'lib/glimmer/swt/widget_proxy.rb', line 770

def add_css_classes(css_classes_to_add)
  css_classes_to_add.each {|css_class| add_css_class(css_class)}
end

#add_observer(observer, property_name) ⇒ Object



908
909
910
911
912
913
914
# File 'lib/glimmer/swt/widget_proxy.rb', line 908

def add_observer(observer, property_name)
  property_listener_installers = self.class&.ancestors&.to_a.map {|ancestor| widget_property_listener_installers[ancestor]}.compact
  widget_listener_installers = property_listener_installers.map{|installer| installer[property_name.to_s.to_sym]}.compact if !property_listener_installers.empty?
  widget_listener_installers.to_a.each do |widget_listener_installer|
    widget_listener_installer.call(observer)
  end
end

#apply_property_type_converters(attribute_name, args) ⇒ Object



934
935
936
937
938
939
940
941
942
943
944
# File 'lib/glimmer/swt/widget_proxy.rb', line 934

def apply_property_type_converters(attribute_name, args)
  if args.count == 1
    value = args.first
    converter = property_type_converters[attribute_name.to_sym]
    args[0] = converter.call(value) if converter
  end
#         if args.count == 1 && args.first.is_a?(ColorProxy)
#           g_color = args.first
#           args[0] = g_color.swt_color
#         end
end

#attach(the_parent_dom_element) ⇒ Object



353
354
355
# File 'lib/glimmer/swt/widget_proxy.rb', line 353

def attach(the_parent_dom_element)
  the_parent_dom_element.append(@dom)
end

#build_dom(layout: true) ⇒ Object



377
378
379
380
381
382
383
# File 'lib/glimmer/swt/widget_proxy.rb', line 377

def build_dom(layout: true)
  # TODO consider passing parent element instead and having table item include a table cell widget only for opal
  @dom = nil
  @dom = dom # TODO unify how to build dom for most widgets based on element, id, and name (class)
  @dom = @parent.get_layout.dom(@dom) if @parent.respond_to?(:layout) && @parent.get_layout
  @dom
end

#can_handle_observation_request?(observation_request) ⇒ Boolean

Returns:

  • (Boolean)


847
848
849
850
851
852
853
854
855
856
857
858
# File 'lib/glimmer/swt/widget_proxy.rb', line 847

def can_handle_observation_request?(observation_request)
  # TODO sort this out for Opal
  observation_request = observation_request.to_s
  if observation_request.start_with?('on_swt_')
    constant_name = observation_request.sub(/^on_swt_/, '')
    SWTProxy.has_constant?(constant_name)
  elsif observation_request.start_with?('on_')
#           event = observation_request.sub(/^on_/, '')
#           can_add_listener?(event) || can_handle_drag_observation_request?(observation_request) || can_handle_drop_observation_request?(observation_request)
    true # TODO filter by valid listeners only in the future
  end
end

#clear_css_classesObject



782
783
784
# File 'lib/glimmer/swt/widget_proxy.rb', line 782

def clear_css_classes
  css_classes.each {|css_class| remove_css_class(css_class)}
end

#content(&block) ⇒ Object



385
386
387
# File 'lib/glimmer/swt/widget_proxy.rb', line 385

def content(&block)
  Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::Opal::WidgetExpression.new, self.class.underscored_widget_name(self), &block)
end

#content_on_render_blocksObject



361
362
363
# File 'lib/glimmer/swt/widget_proxy.rb', line 361

def content_on_render_blocks
  @content_on_render_blocks ||= []
end

#css_classesObject



217
218
219
# File 'lib/glimmer/swt/widget_proxy.rb', line 217

def css_classes
  dom_element.attr('class').to_s.split
end

#default_observation_request_to_event_mappingObject



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# File 'lib/glimmer/swt/widget_proxy.rb', line 398

def default_observation_request_to_event_mapping
  myself = self
  mouse_event_handler = -> (event_listener) {
    -> (event) {
      # TODO generalize this solution to all widgets that support key presses
      event.define_singleton_method(:widget) {myself}
      event.define_singleton_method(:button, &event.method(:which))
      event.define_singleton_method(:count) {1} # TODO support double-click count of 2 in the future by using ondblclick
      event.define_singleton_method(:x, &event.method(:page_x))
      event.define_singleton_method(:y, &event.method(:page_y))
      doit = true
      event.define_singleton_method(:doit=) do |value|
        doit = value
      end
      event.define_singleton_method(:doit) { doit }
      
      if event.which == 1
#               event.prevent # TODO consider if this is needed
        event_listener.call(event)
      end
      
      # TODO Imlement doit properly for all different kinds of events
#             unless doit
#               event.prevent
#               event.stop
#               event.stop_immediate
#             end
    }
  }
  mouse_move_event_handler = -> (event_listener) {
    -> (event) {
      # TODO generalize this solution to all widgets that support key presses
      event.define_singleton_method(:widget) {myself}
      event.define_singleton_method(:button, &event.method(:which))
      event.define_singleton_method(:count) {1} # TODO support double-click count of 2 in the future by using ondblclick
      event.define_singleton_method(:x, &event.method(:page_x))
      event.define_singleton_method(:y, &event.method(:page_y))
      doit = true
      event.define_singleton_method(:doit=) do |value|
        doit = value
      end
      event.define_singleton_method(:doit) { doit }
      
      event_listener.call(event)
      
      # TODO Imlement doit properly for all different kinds of events
#             unless doit
#               event.prevent
#               event.stop
#               event.stop_immediate
#             end
    }
  }
  context_menu_handler = -> (event_listener) {
    -> (event) {
      # TODO generalize this solution to all widgets that support key presses
      event.define_singleton_method(:widget) {myself}
      event.define_singleton_method(:button, &event.method(:which))
      event.define_singleton_method(:count) {1} # TODO support double-click count of 2 in the future by using ondblclick
      event.define_singleton_method(:x, &event.method(:page_x))
      event.define_singleton_method(:y, &event.method(:page_y))
      doit = true
      event.define_singleton_method(:doit=) do |value|
        doit = value
      end
      event.define_singleton_method(:doit) { doit }
      
      if event.which == 3
        event.prevent
        event_listener.call(event)
      end
      # TODO Imlement doit properly for all different kinds of events
#             unless doit
#               event.prevent
#               event.stop
#               event.stop_immediate
#             end
    }
  }
  {
    'on_focus_gained' => {
      event: 'focus',
    },
    'on_focus_lost' => {
      event: 'blur',
    },
    'on_mouse_move' => [
      {
        event: 'mousemove',
        event_handler: mouse_move_event_handler,
      },
    ],
    'on_mouse_up' => [
      {
        event: 'mouseup',
        event_handler: mouse_event_handler,
      },
      {
        event: 'contextmenu',
        event_handler: context_menu_handler,
      },
    ],
    'on_mouse_down' => [
      {
        event: 'mousedown',
        event_handler: mouse_event_handler,
      },
      {
        event: 'contextmenu',
        event_handler: context_menu_handler,
      },
    ],
    'on_swt_mouseup' => [
      {
        event: 'mouseup',
        event_handler: mouse_event_handler,
      },
      {
        event: 'contextmenu',
        event_handler: context_menu_handler,
      },
    ],
    'on_swt_mousedown' => [
      {
        event: 'mousedown',
        event_handler: mouse_event_handler,
      },
      {
        event: 'contextmenu',
        event_handler: context_menu_handler,
      },
    ],
    'on_key_pressed' => {
      event: 'keypress',
      event_handler: -> (event_listener) {
        -> (event) {
          event.define_singleton_method(:widget) {myself}
          event.define_singleton_method(:keyLocation) do
            location = `#{event.to_n}.originalEvent.location`
            JS_LOCATION_TO_SWT_KEY_LOCATION_MAP[location] || location
          end
          event.define_singleton_method(:key_location, &event.method(:keyLocation))
          event.define_singleton_method(:keyCode) {
            JS_KEY_CODE_TO_SWT_KEY_CODE_MAP[event.which] || event.which
          }
          event.define_singleton_method(:key_code, &event.method(:keyCode))
          event.define_singleton_method(:character) {event.which.chr}
          event.define_singleton_method(:stateMask) do
            state_mask = 0
            state_mask |= SWTProxy[:alt] if event.alt_key
            state_mask |= SWTProxy[:ctrl] if event.ctrl_key
            state_mask |= SWTProxy[:shift] if event.shift_key
            state_mask |= SWTProxy[:command] if event.meta_key
            state_mask
          end
          event.define_singleton_method(:state_mask, &event.method(:stateMask))
          doit = true
          event.define_singleton_method(:doit=) do |value|
            doit = value
          end
          event.define_singleton_method(:doit) { doit }
          event_listener.call(event)
          
            # TODO Fix doit false, it's not stopping input
          unless doit
            event.prevent
            event.prevent_default
            event.stop_propagation
            event.stop_immediate_propagation
          end
          
          doit
        }
      }          },
    'on_key_released' => {
      event: 'keyup',
      event_handler: -> (event_listener) {
        -> (event) {
          event.define_singleton_method(:keyLocation) do
            location = `#{event.to_n}.originalEvent.location`
            JS_LOCATION_TO_SWT_KEY_LOCATION_MAP[location] || location
          end
          event.define_singleton_method(:key_location, &event.method(:keyLocation))
          event.define_singleton_method(:widget) {myself}
          event.define_singleton_method(:keyCode) {
            JS_KEY_CODE_TO_SWT_KEY_CODE_MAP[event.which] || event.which
          }
          event.define_singleton_method(:key_code, &event.method(:keyCode))
          event.define_singleton_method(:character) {event.which.chr}
          event.define_singleton_method(:stateMask) do
            state_mask = 0
            state_mask |= SWTProxy[:alt] if event.alt_key
            state_mask |= SWTProxy[:ctrl] if event.ctrl_key
            state_mask |= SWTProxy[:shift] if event.shift_key
            state_mask |= SWTProxy[:command] if event.meta_key
            state_mask
          end
          event.define_singleton_method(:state_mask, &event.method(:stateMask))
          doit = true
          event.define_singleton_method(:doit=) do |value|
            doit = value
          end
          event.define_singleton_method(:doit) { doit }
          event_listener.call(event)
          
            # TODO Fix doit false, it's not stopping input
          unless doit
            event.prevent
            event.prevent_default
            event.stop_propagation
            event.stop_immediate_propagation
          end
          
          doit
        }
      }
    },
    'on_swt_keydown' => [
      {
        event: 'keypress',
        event_handler: -> (event_listener) {
          -> (event) {
            event.define_singleton_method(:keyLocation) do
              location = `#{event.to_n}.originalEvent.location`
              JS_LOCATION_TO_SWT_KEY_LOCATION_MAP[location] || location
            end
            event.define_singleton_method(:key_location, &event.method(:keyLocation))
            event.define_singleton_method(:keyCode) {
              JS_KEY_CODE_TO_SWT_KEY_CODE_MAP[event.which] || event.which
            }
            event.define_singleton_method(:key_code, &event.method(:keyCode))
            event.define_singleton_method(:widget) {myself}
            event.define_singleton_method(:character) {event.which.chr}
            event.define_singleton_method(:stateMask) do
              state_mask = 0
              state_mask |= SWTProxy[:alt] if event.alt_key
              state_mask |= SWTProxy[:ctrl] if event.ctrl_key
              state_mask |= SWTProxy[:shift] if event.shift_key
              state_mask |= SWTProxy[:command] if event.meta_key
              state_mask
            end
            event.define_singleton_method(:state_mask, &event.method(:stateMask))
            doit = true
            event.define_singleton_method(:doit=) do |value|
              doit = value
            end
            event.define_singleton_method(:doit) { doit }
            event_listener.call(event)
            
              # TODO Fix doit false, it's not stopping input
            unless doit
              event.prevent
              event.prevent_default
              event.stop_propagation
              event.stop_immediate_propagation
            end
            
            doit
          }
        }
      },
      {
        event: 'keydown',
        event_handler: -> (event_listener) {
          -> (event) {
            event.define_singleton_method(:keyLocation) do
              location = `#{event.to_n}.originalEvent.location`
              JS_LOCATION_TO_SWT_KEY_LOCATION_MAP[location] || location
            end
            event.define_singleton_method(:key_location, &event.method(:keyLocation))
            event.define_singleton_method(:keyCode) {
              JS_KEY_CODE_TO_SWT_KEY_CODE_MAP[event.which] || event.which
            }
            event.define_singleton_method(:key_code, &event.method(:keyCode))
            event.define_singleton_method(:widget) {myself}
            event.define_singleton_method(:character) {event.which.chr}
            event.define_singleton_method(:stateMask) do
              state_mask = 0
              state_mask |= SWTProxy[:alt] if event.alt_key
              state_mask |= SWTProxy[:ctrl] if event.ctrl_key
              state_mask |= SWTProxy[:shift] if event.shift_key
              state_mask |= SWTProxy[:command] if event.meta_key
              state_mask
            end
            event.define_singleton_method(:state_mask, &event.method(:stateMask))
            doit = true
            event.define_singleton_method(:doit=) do |value|
              doit = value
            end
            event.define_singleton_method(:doit) { doit }
            event_listener.call(event) if event.which != 13 && (event.which == 127 || event.which <= 40)
            
              # TODO Fix doit false, it's not stopping input
            unless doit
              event.prevent
              event.prevent_default
              event.stop_propagation
              event.stop_immediate_propagation
            end
            doit
          }
        }
      }
    ],
    'on_swt_keyup' => {
      event: 'keyup',
      event_handler: -> (event_listener) {
        -> (event) {
          event.define_singleton_method(:keyLocation) do
            location = `#{event.to_n}.originalEvent.location`
            JS_LOCATION_TO_SWT_KEY_LOCATION_MAP[location] || location
          end
          event.define_singleton_method(:key_location, &event.method(:keyLocation))
          event.define_singleton_method(:widget) {myself}
          event.define_singleton_method(:keyCode) {
            JS_KEY_CODE_TO_SWT_KEY_CODE_MAP[event.which] || event.which
          }
          event.define_singleton_method(:key_code, &event.method(:keyCode))
          event.define_singleton_method(:character) {event.which.chr}
          event.define_singleton_method(:stateMask) do
            state_mask = 0
            state_mask |= SWTProxy[:alt] if event.alt_key
            state_mask |= SWTProxy[:ctrl] if event.ctrl_key
            state_mask |= SWTProxy[:shift] if event.shift_key
            state_mask |= SWTProxy[:command] if event.meta_key
            state_mask
          end
          event.define_singleton_method(:state_mask, &event.method(:stateMask))
          doit = true
          event.define_singleton_method(:doit=) do |value|
            doit = value
          end
          event.define_singleton_method(:doit) { doit }
          event_listener.call(event)
          
            # TODO Fix doit false, it's not stopping input
          unless doit
            event.prevent
            event.prevent_default
            event.stop_propagation
            event.stop_immediate_propagation
          end
          
          doit
        }
      }
    },
  }
end

#dialog_ancestorObject



271
272
273
# File 'lib/glimmer/swt/widget_proxy.rb', line 271

def dialog_ancestor
  parents.detect {|p| p.is_a?(DialogProxy)}
end

#disposeObject



221
222
223
224
225
226
227
228
# File 'lib/glimmer/swt/widget_proxy.rb', line 221

def dispose
  remove_all_listeners
  Document.find(path).remove
  parent&.post_dispose_child(self)
#         children.each(:dispose) # TODO enable this safely
  @disposed = true
  listeners_for('widget_disposed').each {|listener| listener.call(Event.new(widget: self))}
end

#dom_elementObject



790
791
792
793
# File 'lib/glimmer/swt/widget_proxy.rb', line 790

def dom_element
  # TODO consider making this pick an element in relation to its parent, allowing unhooked dom elements to be built if needed (unhooked to the visible page dom)
  Document.find(path)
end

#effective_observation_request_to_event_mappingObject



394
395
396
# File 'lib/glimmer/swt/widget_proxy.rb', line 394

def effective_observation_request_to_event_mapping
  default_observation_request_to_event_mapping.merge(observation_request_to_event_mapping)
end

#elementObject

Root element representing widget. Must be overridden by subclasses if different from div



251
252
253
# File 'lib/glimmer/swt/widget_proxy.rb', line 251

def element
  'div'
end

#event_handling_suspended?Boolean

Returns:

  • (Boolean)


835
836
837
# File 'lib/glimmer/swt/widget_proxy.rb', line 835

def event_handling_suspended?
  @event_handling_suspended
end

#event_listener_proxiesObject



823
824
825
# File 'lib/glimmer/swt/widget_proxy.rb', line 823

def event_listener_proxies
  @event_listener_proxies ||= []
end

#get_data(key = nil) ⇒ Object Also known as: getData, data



203
204
205
206
207
208
209
# File 'lib/glimmer/swt/widget_proxy.rb', line 203

def get_data(key = nil)
  if key.nil?
    @data
  else
    swt_data[key]
  end
end

#handle_javascript_observation_request(keyword, original_event_listener) ⇒ Object



869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
# File 'lib/glimmer/swt/widget_proxy.rb', line 869

def handle_javascript_observation_request(keyword, original_event_listener)
  return unless effective_observation_request_to_event_mapping.keys.include?(keyword)
  event = nil
  delegate = nil
  effective_observation_request_to_event_mapping[keyword].to_collection.each do |mapping|
    observation_requests[keyword] ||= Set.new
    observation_requests[keyword] << original_event_listener
    event = mapping[:event]
    event_handler = mapping[:event_handler]
    event_element_css_selector = mapping[:event_element_css_selector]
    potential_event_listener = event_handler&.call(original_event_listener)
    event_listener = potential_event_listener || original_event_listener
    async_event_listener = proc do |event|
      # TODO look into the issue with using async::task.new here. maybe put it in event listener (like not being able to call preventDefault or return false successfully )
      # maybe consider pushing inside the widget classes instead where needed only or implement universal doit support correctly to bypass this issue
#             Async::Task.new do
      @@widget_handling_listener = self
      # TODO also make sure to disable all widgets for suspension
      event_listener.call(event) unless dialog_ancestor&.event_handling_suspended?
      @widget_handling_listener = nil
#             end
    end
    the_listener_dom_element = event_element_css_selector ? Element[event_element_css_selector] : listener_dom_element
    unless the_listener_dom_element.empty?
      the_listener_dom_element.on(event, &async_event_listener)
      # TODO ensure uniqueness of insertion (perhaps adding equals/hash method to event listener proxy)
      
      event_listener_proxies << EventListenerProxy.new(element_proxy: self, selector: selector, dom_element: the_listener_dom_element, event: event, listener: async_event_listener, original_event_listener: original_event_listener)
    end
  end
end

#handle_observation_request(keyword, original_event_listener) ⇒ Object



860
861
862
863
864
865
866
867
# File 'lib/glimmer/swt/widget_proxy.rb', line 860

def handle_observation_request(keyword, original_event_listener)
  case keyword
  when 'on_widget_disposed'
    listeners_for(keyword.sub(/^on_/, '')) << original_event_listener.to_proc
  else
    handle_javascript_observation_request(keyword, original_event_listener)
  end
end

#has_style?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


786
787
788
# File 'lib/glimmer/swt/widget_proxy.rb', line 786

def has_style?(symbol)
  @args.include?(symbol) # not a very solid implementation. Bring SWT constants eventually
end

#idObject



752
753
754
# File 'lib/glimmer/swt/widget_proxy.rb', line 752

def id
  @id ||= "#{name}-#{WidgetProxy.next_id_number_for(name)}"
end

#id=(value) ⇒ Object

Sets id explicitly. Useful in cases of wanting to maintain a stable id



757
758
759
# File 'lib/glimmer/swt/widget_proxy.rb', line 757

def id=(value)
  @id = value
end

#listener_dom_elementObject



815
816
817
# File 'lib/glimmer/swt/widget_proxy.rb', line 815

def listener_dom_element
  Document.find(listener_path)
end

#listener_pathObject



811
812
813
# File 'lib/glimmer/swt/widget_proxy.rb', line 811

def listener_path
  path
end

#listenersObject



839
840
841
# File 'lib/glimmer/swt/widget_proxy.rb', line 839

def listeners
  @listeners ||= {}
end

#listeners_for(listener_event) ⇒ Object



843
844
845
# File 'lib/glimmer/swt/widget_proxy.rb', line 843

def listeners_for(listener_event)
  listeners[listener_event.to_s] ||= []
end

#nameObject



748
749
750
# File 'lib/glimmer/swt/widget_proxy.rb', line 748

def name
  self.class.name.split('::').last.underscore.sub(/_proxy$/, '').gsub('_', '-')
end

#observation_request_to_event_mappingObject

Subclasses must override with their own mappings



390
391
392
# File 'lib/glimmer/swt/widget_proxy.rb', line 390

def observation_request_to_event_mapping
  {}
end

#observation_requestsObject



819
820
821
# File 'lib/glimmer/swt/widget_proxy.rb', line 819

def observation_requests
  @observation_requests ||= {}
end

#parent_dom_elementObject



325
326
327
# File 'lib/glimmer/swt/widget_proxy.rb', line 325

def parent_dom_element
  Document.find(parent_path)
end

#parent_pathObject



321
322
323
# File 'lib/glimmer/swt/widget_proxy.rb', line 321

def parent_path
  @parent&.path
end

#parentsObject



261
262
263
264
265
266
267
268
269
# File 'lib/glimmer/swt/widget_proxy.rb', line 261

def parents
  parents_array = []
  current_widget = self
  until current_widget.parent.nil?
    current_widget = current_widget.parent
    parents_array << current_widget
  end
  parents_array
end

#post_add_contentObject

Executes at the closing of a parent widget curly braces after all children/properties have been added/set



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/glimmer/swt/widget_proxy.rb', line 178

def post_add_content
  if !menu.nil? && !is_a?(MenuProxy) && !is_a?(MenuItemProxy)
    # TODO consider in the future retaining listener registrations to deregister on unsetting of the menu
    on_mouse_move do |mouse_event|
      self.menu_x = mouse_event.x
      self.menu_y = mouse_event.y
    end
    on_mouse_down do |mouse_event|
      if mouse_event.button == 3 # right-click
        menu.visible = true
      end
    end
  end
end

#post_dispose_child(child) ⇒ Object

Executes for the parent of a child that just got disposed



173
174
175
# File 'lib/glimmer/swt/widget_proxy.rb', line 173

def post_dispose_child(child)
  @children&.delete(child)
end

#post_initialize_child(child) ⇒ Object

Executes for the parent of a child that just got added



167
168
169
170
# File 'lib/glimmer/swt/widget_proxy.rb', line 167

def post_initialize_child(child)
  @children << child
  child.render
end


275
276
277
278
# File 'lib/glimmer/swt/widget_proxy.rb', line 275

def print
  `window.print()`
  true
end

#property_type_convertersObject



946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
# File 'lib/glimmer/swt/widget_proxy.rb', line 946

def property_type_converters
  color_converter = proc do |value|
    if value.is_a?(Symbol) || value.is_a?(String)
      ColorProxy.new(value)
    else
      value
    end
  end
  @property_type_converters ||= {
    :background => color_converter,
#           :background_image => proc do |value|
#             if value.is_a?(String)
#               if value.start_with?('uri:classloader')
#                 value = value.sub(/^uri\:classloader\:\//, '')
#                 object = java.lang.Object.new
#                 value = object.java_class.resource_as_stream(value)
#                 value = java.io.BufferedInputStream.new(value)
#               end
#               image_data = ImageData.new(value)
#               on_event_Resize do |resize_event|
#                 new_image_data = image_data.scaledTo(@swt_widget.getSize.x, @swt_widget.getSize.y)
#                 @swt_widget.getBackgroundImage&.dispose
#                 @swt_widget.setBackgroundImage(Image.new(@swt_widget.getDisplay, new_image_data))
#               end
#               Image.new(@swt_widget.getDisplay, image_data)
#             else
#               value
#             end
#           end,
    :foreground => color_converter,
#           :font => proc do |value|
#             if value.is_a?(Hash)
#               font_properties = value
#               FontProxy.new(self, font_properties).swt_font
#             else
#               value
#             end
#           end,
    :text => proc do |value|
#             if swt_widget.is_a?(Browser)
#               value.to_s
#             else
        value.to_s
#             end
    end,
#           :visible => proc do |value|
#             !!value
#           end,
  }
end

#reattach(old_element) ⇒ Object



357
358
359
# File 'lib/glimmer/swt/widget_proxy.rb', line 357

def reattach(old_element)
  old_element.replace_with(@dom)
end

#remove_all_listenersObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/glimmer/swt/widget_proxy.rb', line 230

def remove_all_listeners
  effective_observation_request_to_event_mapping.keys.each do |keyword|
    effective_observation_request_to_event_mapping[keyword].to_collection.each do |mapping|
      observation_requests[keyword].to_a.each do |event_listener|
        event = mapping[:event]
        event_handler = mapping[:event_handler]
        event_element_css_selector = mapping[:event_element_css_selector]
        the_listener_dom_element = event_element_css_selector ? Element[event_element_css_selector] : listener_dom_element
        the_listener_dom_element.off(event, event_listener)
        # TODO improve to precisely remove the listeners that were added, no more no less. (or use the event_listener_proxies method instead or in collaboration)
      end
    end
  end
end

#remove_css_class(css_class) ⇒ Object



774
775
776
# File 'lib/glimmer/swt/widget_proxy.rb', line 774

def remove_css_class(css_class)
  dom_element.remove_class(css_class)
end

#remove_css_classes(css_classes_to_remove) ⇒ Object



778
779
780
# File 'lib/glimmer/swt/widget_proxy.rb', line 778

def remove_css_classes(css_classes_to_remove)
  css_classes_to_remove.each {|css_class| remove_css_class(css_class)}
end

#remove_event_listener_proxiesObject



901
902
903
904
905
906
# File 'lib/glimmer/swt/widget_proxy.rb', line 901

def remove_event_listener_proxies
  event_listener_proxies.each do |event_listener_proxy|
    event_listener_proxy.unregister
  end
  event_listener_proxies.clear
end

#render(custom_parent_dom_element: nil, brand_new: false) ⇒ Object Also known as: redraw



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/glimmer/swt/widget_proxy.rb', line 329

def render(custom_parent_dom_element: nil, brand_new: false)
  the_parent_dom_element = custom_parent_dom_element || parent_dom_element
  old_element = dom_element
  brand_new = @dom.nil? || old_element.empty? || brand_new
  build_dom(layout: !custom_parent_dom_element) # TODO handle custom parent layout by passing parent instead of parent dom element
  if brand_new
    # TODO make a method attach to allow subclasses to override if needed
    attach(the_parent_dom_element)
  else
    reattach(old_element)
  end
  observation_requests&.each do |keyword, event_listener_set|
    event_listener_set.each do |event_listener|
      handle_observation_request(keyword, event_listener)
    end
  end
  children.each do |child|
    child.render
  end
  @rendered = true
  content_on_render_blocks.each { |content_block| content(&content_block) } unless skip_content_on_render_blocks?
end

#resume_event_handlingObject



831
832
833
# File 'lib/glimmer/swt/widget_proxy.rb', line 831

def resume_event_handling
  @event_handling_suspended = false
end

#selectorObject

Subclasses can override with their own selector



762
763
764
# File 'lib/glimmer/swt/widget_proxy.rb', line 762

def selector
  "#{name}##{id}"
end

#set_attribute(attribute_name, *args) ⇒ Object



916
917
918
919
# File 'lib/glimmer/swt/widget_proxy.rb', line 916

def set_attribute(attribute_name, *args)
  apply_property_type_converters(attribute_name, args)
  super(attribute_name, *args) # PropertyOwner
end

#set_data(key = nil, value) ⇒ Object Also known as: setData, data=



193
194
195
196
197
198
199
# File 'lib/glimmer/swt/widget_proxy.rb', line 193

def set_data(key = nil, value)
  if key.nil?
    @data = value
  else
    swt_data[key] = value
  end
end

#set_focusObject Also known as: setFocus



316
317
318
# File 'lib/glimmer/swt/widget_proxy.rb', line 316

def set_focus
  self.focus = true
end

#shellObject



255
256
257
258
259
# File 'lib/glimmer/swt/widget_proxy.rb', line 255

def shell
  current_widget = self
  current_widget = current_widget.parent until current_widget.parent.nil?
  current_widget
end

#skip_content_on_render_blocks?Boolean

Returns:

  • (Boolean)


365
366
367
# File 'lib/glimmer/swt/widget_proxy.rb', line 365

def skip_content_on_render_blocks?
  false
end

#style_elementObject

TODO consider adding a default #dom method implementation for the common case, automatically relying on #element and other methods to build the dom html



797
798
799
800
801
802
803
804
805
806
807
808
809
# File 'lib/glimmer/swt/widget_proxy.rb', line 797

def style_element
  style_element_id = "#{id}-style"
  style_element_selector = "style##{style_element_id}"
  element = dom_element.find(style_element_selector)
  if element.empty?
    new_element = Element.new(:style)
    new_element.attr('id', style_element_id)
    new_element.attr('class', "#{name.gsub('_', '-')}-instance-style widget-instance-style")
    dom_element.prepend(new_element)
    element = dom_element.find(style_element_selector)
  end
  element
end

#suspend_event_handlingObject



827
828
829
# File 'lib/glimmer/swt/widget_proxy.rb', line 827

def suspend_event_handling
  @event_handling_suspended = true
end

#swt_dataObject



213
214
215
# File 'lib/glimmer/swt/widget_proxy.rb', line 213

def swt_data
  @swt_data ||= {}
end

#swt_widgetObject



929
930
931
932
# File 'lib/glimmer/swt/widget_proxy.rb', line 929

def swt_widget
  # only added for compatibility/adaptibility with Glimmer DSL for SWT
  self
end

#widget_property_listener_installersObject



997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
# File 'lib/glimmer/swt/widget_proxy.rb', line 997

def widget_property_listener_installers
  @swt_widget_property_listener_installers ||= {
#           WidgetProxy => {
#             :focus => proc do |observer|
#               on_focus_gained { |focus_event|
#                 observer.call(true)
#               }
#               on_focus_lost { |focus_event|
#                 observer.call(false)
#               }
#             end,
#           },
    MenuItemProxy => {
      :selection => proc do |observer|
        on_widget_selected { |selection_event|
          # TODO look into validity of this and perhaps move toggle logic to MenuItemProxy
          if check?
            observer.call(!selection)
          else
            observer.call(selection)
          end
        }
      end
    },
    ScaleProxy => {
      :selection => proc do |observer|
        on_widget_selected { |selection_event|
          observer.call(selection)
        }
      end
    },
    SliderProxy => {
      :selection => proc do |observer|
        on_widget_selected { |selection_event|
          observer.call(selection)
        }
      end
    },
    SpinnerProxy => {
      :selection => proc do |observer|
        on_widget_selected { |selection_event|
          observer.call(selection)
        }
      end
    },
    TextProxy => {
      :text => proc do |observer|
        on_modify_text { |modify_event|
          observer.call(text)
        }
      end,
#             :caret_position => proc do |observer|
#               on_event_keydown { |event|
#                 observer.call(getCaretPosition)
#               }
#               on_event_keyup { |event|
#                 observer.call(getCaretPosition)
#               }
#               on_event_mousedown { |event|
#                 observer.call(getCaretPosition)
#               }
#               on_event_mouseup { |event|
#                 observer.call(getCaretPosition)
#               }
#             end,
#             :selection => proc do |observer|
#               on_event_keydown { |event|
#                 observer.call(getSelection)
#               }
#               on_event_keyup { |event|
#                 observer.call(getSelection)
#               }
#               on_event_mousedown { |event|
#                 observer.call(getSelection)
#               }
#               on_event_mouseup { |event|
#                 observer.call(getSelection)
#               }
#             end,
#             :selection_count => proc do |observer|
#               on_event_keydown { |event|
#                 observer.call(getSelectionCount)
#               }
#               on_event_keyup { |event|
#                 observer.call(getSelectionCount)
#               }
#               on_event_mousedown { |event|
#                 observer.call(getSelectionCount)
#               }
#               on_event_mouseup { |event|
#                 observer.call(getSelectionCount)
#               }
#             end,
#             :top_index => proc do |observer|
#               @last_top_index = getTopIndex
#               on_paint_control { |event|
#                 if getTopIndex != @last_top_index
#                   @last_top_index = getTopIndex
#                   observer.call(@last_top_index)
#                 end
#               }
#             end,
    },
#           Java::OrgEclipseSwtCustom::StyledText => {
#             :text => proc do |observer|
#               on_modify_text { |modify_event|
#                 observer.call(getText)
#               }
#             end,
#           },
    DateTimeProxy => {
      :date_time => proc do |observer|
        on_widget_selected { |selection_event|
          observer.call(date_time)
        }
      end
    },
    RadioProxy => { #radio?
      :selection => proc do |observer|
        on_widget_selected { |selection_event|
          observer.call(selection)
        }
      end
    },
    TableProxy => {
      :selection => proc do |observer|
        on_widget_selected { |selection_event|
          observer.call(selection_event.table_item.get_data)  # TODO ensure selection doesn't conflict with editing
        }
      end,
    },
#           Java::OrgEclipseSwtWidgets::MenuItem => {
#             :selection => proc do |observer|
#               on_widget_selected { |selection_event|
#                 observer.call(getSelection)
#               }
#             end
#           },
  }
end