Sha256: 2eab9f18c0b414a8c0fd214a3cd3e2ae822c35adba8f15db5fb38c9036003817

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 KB

Contents

class Shoes
  module Swt
    class TextBlock
      class CursorPainter
        def initialize(text_block_dsl, collection)
          @text_block_dsl = text_block_dsl
          @collection = collection
        end

        def draw
          if @text_block_dsl.cursor
            draw_textcursor
          else
            remove_textcursor
          end
        end

        def draw_textcursor
          segment = @collection.segment_at_text_position(@text_block_dsl.cursor)
          relative_cursor = @collection.relative_text_position(@text_block_dsl.cursor)
          position = segment.get_location(relative_cursor)

          move_if_necessary(segment.element_left + position.x,
                            segment.element_top + position.y)
        end

        # It's important to only move when necessary to avoid constant redraws
        def move_if_necessary(x, y)
          unless textcursor.left == x && textcursor.top == y
            move_textcursor(x, y)
          end
        end

        def move_textcursor(x, y)
          height = textcursor.height
          textcursor.move(x, y, x, y + height)
          textcursor.show
        end

        def textcursor
          @text_block_dsl.textcursor @collection.cursor_height
        end

        def remove_textcursor
          return unless @text_block_dsl.textcursor?

          @text_block_dsl.textcursor.remove
          @text_block_dsl.textcursor = nil
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
shoes-swt-4.0.0.pre8 lib/shoes/swt/text_block/cursor_painter.rb
shoes-swt-4.0.0.pre7 lib/shoes/swt/text_block/cursor_painter.rb
shoes-swt-4.0.0.pre6 lib/shoes/swt/text_block/cursor_painter.rb
shoes-swt-4.0.0.pre5 lib/shoes/swt/text_block/cursor_painter.rb
shoes-swt-4.0.0.pre4 lib/shoes/swt/text_block/cursor_painter.rb