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
#create_tk_widget, #each, #init_id, #initialize_utilities, #normalize_childs, #push, #tk_class, #tk_widget
Constructor Details
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
#text ⇒ Object
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
#additional_height_needed_for_textfield ⇒ Object
41
42
43
44
45
|
# File 'lib/widgets/auto_resize_text.rb', line 41
def additional_height_needed_for_textfield
@text.accumulated_border_and_padding_height +
accumulated_border_and_padding_height
end
|
#additional_width_needed_for_textfield ⇒ Object
width of cursor + borders (textfield + frame) + padding (textfield + frame)
35
36
37
38
39
|
# File 'lib/widgets/auto_resize_text.rb', line 35
def additional_width_needed_for_textfield
@text.opts.insertwidth +
@text.accumulated_border_and_padding_width +
accumulated_border_and_padding_width
end
|
#autoresize ⇒ Object
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/widgets/auto_resize_text.rb', line 88
def autoresize
return unless @text.tk_widget.modified?
width = [[min_width, width_needed_for_textfield].max, max_width].min
height = [min_height, height_needed_for_textfield].max
resize(height: height, width: width)
@text.tk_widget.modified(false)
end
|
#build(parent, **args) ⇒ Object
26
27
28
29
30
31
32
|
# 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_childs ⇒ Object
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
|
#height_needed_for_lines(num_lines) ⇒ Object
51
52
53
|
# File 'lib/widgets/auto_resize_text.rb', line 51
def height_needed_for_lines(num_lines)
@text.height_of_lines(num_lines) + additional_height_needed_for_textfield
end
|
#height_needed_for_textfield ⇒ Object
72
73
74
|
# File 'lib/widgets/auto_resize_text.rb', line 72
def height_needed_for_textfield
@text.height_of_lines + additional_height_needed_for_textfield
end
|
#max_width ⇒ Object
76
77
78
|
# File 'lib/widgets/auto_resize_text.rb', line 76
def max_width
[@cell.bbox[2], 0].max
end
|
#min_height ⇒ Object
84
85
86
|
# File 'lib/widgets/auto_resize_text.rb', line 84
def min_height
height_needed_for_lines(@min_number_lines)
end
|
#min_width ⇒ Object
80
81
82
|
# File 'lib/widgets/auto_resize_text.rb', line 80
def min_width
[max_width, width_needed_for_chars(@min_number_chars)].min
end
|
#resize(height: nil, width: nil, lines: nil, chars: nil) ⇒ Object
60
61
62
63
64
65
66
|
# File 'lib/widgets/auto_resize_text.rb', line 60
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
|
#resize_width ⇒ Object
55
56
57
58
|
# File 'lib/widgets/auto_resize_text.rb', line 55
def resize_width
opts.width = width_needed_for_textfield
@parent.tk_widget.update
end
|
#width_needed_for_chars(num_chars) ⇒ Object
47
48
49
|
# File 'lib/widgets/auto_resize_text.rb', line 47
def width_needed_for_chars(num_chars)
text.font.char_width * num_chars + additional_width_needed_for_textfield
end
|
#width_needed_for_textfield ⇒ Object
68
69
70
|
# File 'lib/widgets/auto_resize_text.rb', line 68
def width_needed_for_textfield
@text.longest_line_width + additional_width_needed_for_textfield
end
|