Sha256: cbc7bed6db751c9a2394497e0e83663e94b3d59fb78efd83e816cf35732f93af

Contents?: true

Size: 1.46 KB

Versions: 4

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true
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

4 entries across 4 versions & 1 rubygems

Version Path
shoes-swt-4.0.0.pre12 lib/shoes/swt/text_block/cursor_painter.rb
shoes-swt-4.0.0.pre11 lib/shoes/swt/text_block/cursor_painter.rb
shoes-swt-4.0.0.pre10 lib/shoes/swt/text_block/cursor_painter.rb
shoes-swt-4.0.0.pre9 lib/shoes/swt/text_block/cursor_painter.rb