Sha256: 0a9107037ba97fff526b815c1ef20d2e19e5fce39c7af947f5ee87e5e8eb0fc4

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true
class Shoes
  module Swt
    class ListBox
      include Common::Remove
      include Common::Visibility
      include Common::UpdatePosition
      include ::Shoes::BackendDimensionsDelegations

      attr_reader :dsl, :app

      def initialize(dsl, app)
        @dsl = dsl
        @app = app
        @real = ::Swt::Widgets::Combo.new(
          @app.real,
          ::Swt::SWT::DROP_DOWN | ::Swt::SWT::READ_ONLY
        )
        @real.set_size dsl.element_width, dsl.element_height
        @real.add_selection_listener do |_event|
          @dsl.call_change_listeners
        end
        update_items

        # Set initial selection without triggering callbacks!
        choice = @dsl.style[:choose]
        @real.text = choice if choice
      end

      def update_items
        # Keep original selection to restore after resetting the list
        text = @real.text

        @real.set_items(*@dsl.items.to_a.map(&:to_s))
        @real.text = text
      end

      def text
        text = @real.text
        text == '' ? nil : text
      end

      def choose(item)
        @real.text = item
        @dsl.call_change_listeners
      end

      def enabled(value)
        @real.enable_widget value
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
shoes-swt-4.0.0.pre11 lib/shoes/swt/list_box.rb
shoes-swt-4.0.0.pre10 lib/shoes/swt/list_box.rb
shoes-swt-4.0.0.pre9 lib/shoes/swt/list_box.rb