Class: RubyJard::ScreenAdjuster

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_jard/screen_adjuster.rb

Overview

Implement elastic screen height. If a screen is shrinkable (which means) its current window is less than its height, it will be forced to give away those spaces to other screens. This layout adjustment is small, and should not affect the original generated layout too much, nor work with nested layout.

Instance Method Summary collapse

Constructor Details

#initialize(screens) ⇒ ScreenAdjuster

Returns a new instance of ScreenAdjuster.



11
12
13
# File 'lib/ruby_jard/screen_adjuster.rb', line 11

def initialize(screens)
  @screens = screens
end

Instance Method Details

#adjustObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_jard/screen_adjuster.rb', line 15

def adjust
  groups = @screens.group_by { |screen| screen.layout.parent_template }
  groups.each do |_, grouped_screens|
    next if grouped_screens.length <= 1
    next unless same_column?(grouped_screens)

    grouped_screens.sort_by! { |screen| screen.layout.box_y }
    shrinkable_screens = grouped_screens.select { |s| shrinkable?(s) }
    expandable_screens = grouped_screens.select { |s| expandable?(s) }

    next if shrinkable_screens.empty? || expandable_screens.empty?

    budget = shrinkable_screens.map { |s| shrinkable_height(s) }.sum
    expand_screens(expandable_screens, budget)
    shrink_screens(shrinkable_screens)
    compact_screens(grouped_screens)
  end
end