Class: Shoes::Swt::LinkSegment

Inherits:
Object
  • Object
show all
Defined in:
shoes-swt/lib/shoes/swt/link_segment.rb

Defined Under Namespace

Classes: Region

Instance Method Summary collapse

Constructor Details

#initialize(text_segment, range) ⇒ LinkSegment

Returns a new instance of LinkSegment.



6
7
8
9
10
11
12
13
14
15
16
# File 'shoes-swt/lib/shoes/swt/link_segment.rb', line 6

def initialize(text_segment, range)
  @regions = []
  @range = range
  @text_segment = text_segment

  # Don't create regions for empty links!
  return unless @range.count.positive?

  add_regions_for_lines(lines_for_link)
  offset_regions(text_segment.element_left, text_segment.element_top)
end

Instance Method Details

#add_region(left, top, right, bottom) ⇒ Object



65
66
67
# File 'shoes-swt/lib/shoes/swt/link_segment.rb', line 65

def add_region(left, top, right, bottom)
  @regions << Region.new(left, top, right, bottom)
end

#add_region_for_single_line(first_line) ⇒ Object



46
47
48
49
# File 'shoes-swt/lib/shoes/swt/link_segment.rb', line 46

def add_region_for_single_line(first_line)
  add_region(start_position.x, start_position.y,
             end_position.x,   end_position.y + first_line.height)
end

#add_regions_for_first_and_last_lines(first_line, last_line) ⇒ Object



51
52
53
54
55
56
# File 'shoes-swt/lib/shoes/swt/link_segment.rb', line 51

def add_regions_for_first_and_last_lines(first_line, last_line)
  add_region(start_position.x, start_position.y,
             layout.width,     start_position.y + first_line.height)
  add_region(0,              end_position.y,
             end_position.x, end_position.y + last_line.height)
end

#add_regions_for_lines(lines) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'shoes-swt/lib/shoes/swt/link_segment.rb', line 18

def add_regions_for_lines(lines)
  first_line = lines.shift
  last_line  = lines.pop

  if last_line.nil?
    add_region_for_single_line(first_line)
  else
    add_regions_for_first_and_last_lines(first_line, last_line)
    add_regions_for_remaining_lines(lines)
  end
end

#add_regions_for_remaining_lines(lines) ⇒ Object



58
59
60
61
62
63
# File 'shoes-swt/lib/shoes/swt/link_segment.rb', line 58

def add_regions_for_remaining_lines(lines)
  lines.each do |line|
    add_region(line.x,              line.y,
               line.x + line.width, line.y + line.height)
  end
end

#end_positionObject



83
84
85
# File 'shoes-swt/lib/shoes/swt/link_segment.rb', line 83

def end_position
  @text_segment.get_location(@range.last, true)
end

#in_bounds?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'shoes-swt/lib/shoes/swt/link_segment.rb', line 75

def in_bounds?(x, y)
  @regions.any? { |region| region.in_bounds?(x, y) }
end

#layoutObject



87
88
89
# File 'shoes-swt/lib/shoes/swt/link_segment.rb', line 87

def layout
  @text_segment.layout
end

#line_boundsObject



36
37
38
39
40
# File 'shoes-swt/lib/shoes/swt/link_segment.rb', line 36

def line_bounds
  (0..layout.line_count - 1).map do |index|
    layout.get_line_bounds(index)
  end
end

#line_in_bounds?(bound) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'shoes-swt/lib/shoes/swt/link_segment.rb', line 42

def line_in_bounds?(bound)
  bound.y >= start_position.y && bound.y <= end_position.y
end


30
31
32
33
34
# File 'shoes-swt/lib/shoes/swt/link_segment.rb', line 30

def lines_for_link
  line_bounds.select do |bound|
    line_in_bounds?(bound)
  end
end

#offset_regions(left, top) ⇒ Object



69
70
71
72
73
# File 'shoes-swt/lib/shoes/swt/link_segment.rb', line 69

def offset_regions(left, top)
  @regions.each do |region|
    region.offset_by!(left, top)
  end
end

#start_positionObject



79
80
81
# File 'shoes-swt/lib/shoes/swt/link_segment.rb', line 79

def start_position
  @text_segment.get_location(@range.first, false)
end