module Accessibility::TextHighlighter

Mix-in module

Public Instance Methods

highlight_text(text) click to toggle source

@note The implementation of this code makes assumptions about the

minimum size of text and may not work with very small fonts.

Highlights text in the field/area that matches the given ‘text`

The given ‘text` can be a `String`, `Range`, or `Regexp` that matches some range of text in the receiver. An exception will be raised if the `text` is not valid for any reason.

@example

text_field.highlight_text(0..5); type "\\DELETE"
text_area.highligtht_text("W"); type "W"
text_field.highlight_text(/is a lie?/i); type "is delicious and moist"

@param text [String,Regexp,Range]

# File lib/accessibility/text_highlighter.rb, line 25
def highlight_text text
  text = highlighter_range_for(text).to_a

  head = parameterized_attribute(:bounds_for_range, text.first..text.first)
  tail = parameterized_attribute(:bounds_for_range, text.last..text.last)

  if Accessibility.debug?
    highlight head, timeout: 5, colour: NSColor.yellowColor
    highlight tail, timeout: 5, colour: NSColor.brownColor
  end

  head_point = head.origin
  head_point.x += 1
  head_point.y += 1
  click

  tail_point = tail.origin
  tail_point.x += tail.size.width - 1
  tail_point.y += tail.size.height - 1
  drag_mouse_to tail_point
end

Private Instance Methods

highlighter_range_for(text) click to toggle source
# File lib/accessibility/text_highlighter.rb, line 50
def highlighter_range_for text
  value = @ref.value

  case text
  when Regexp
    match = value.match text
    if match
      pair = match.offset(0)
      pair[1] -= 1
      pair
    end
  when String
    start = value.index text
    [start, start.to_i + text.length - 1]
  when Range
    text
  else
    raise ArgumentError,
     "Cannot figure out what to highlight given #{text.inspect}"
  end
end