Class: TkWrapper::Widgets::AutoResizeText

Inherits:
Frame show all
Defined in:
lib/widgets/auto_resize_text.rb

Instance Attribute Summary collapse

Attributes inherited from Base::Widget

#cell, #childs, #config, #font, #ids, #manager, #opts, #parent, #winfo

Instance Method Summary collapse

Methods inherited from Frame

#accumulated_border_and_padding_height, #accumulated_border_and_padding_width, #tk_class

Methods inherited from Base::Widget

#create_tk_widget, #each, #init_id, #initialize_utilities, #normalize_childs, #push, #tk_class, #tk_widget

Constructor Details

#initialize(**args) ⇒ AutoResizeText

Returns a new instance of AutoResizeText.



8
9
10
11
12
13
14
15
16
# File 'lib/widgets/auto_resize_text.rb', line 8

def initialize(**args)
  @min_number_chars = 8
  @min_number_lines = 2

  super(**args)

  @longest_line_width = 0
  @config.merge({ grid: :onecell }, overwrite: false)
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text.



6
7
8
# File 'lib/widgets/auto_resize_text.rb', line 6

def text
  @text
end

Instance Method Details

#build(parent, **args) ⇒ Object



26
27
28
29
30
31
# File 'lib/widgets/auto_resize_text.rb', line 26

def build(parent, **args)
  super(parent, **args)
  @text.bind('<Modified>', &method(:autoresize))
  TkGrid.propagate(tk_widget, 0)
  resize(lines: @min_number_lines, chars: @min_number_chars)
end

#create_childsObject



18
19
20
21
22
23
24
# File 'lib/widgets/auto_resize_text.rb', line 18

def create_childs
  @text = Text.new(config: {
    grid:   { sticky: 'nsew' },
    width:  @min_number_chars,
    height: @min_number_lines
  })
end

#resize(height: nil, width: nil, lines: nil, chars: nil) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/widgets/auto_resize_text.rb', line 33

def resize(height: nil, width: nil, lines: nil, chars: nil)
  width  = width_needed_for_chars(chars)  if chars
  height = height_needed_for_lines(lines) if lines

  opts.width  = width  if width
  opts.height = height if height
end