Class: TkWrapper::Widgets::AutoResizeEntry

Inherits:
Entry show all
Defined in:
lib/widgets/auto_resize_entry.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 Entry

#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) ⇒ AutoResizeEntry

Returns a new instance of AutoResizeEntry.



7
8
9
10
11
# File 'lib/widgets/auto_resize_entry.rb', line 7

def initialize(**args)
  @min_width = args.dig(:config, :min_width) || 0
  @add_width = args.dig(:config, :add_width) || 0
  super(**args)
end

Instance Attribute Details

#add_widthObject

auto resizes on user input, only works if in the grid geometry manager of tk



5
6
7
# File 'lib/widgets/auto_resize_entry.rb', line 5

def add_width
  @add_width
end

#min_widthObject

auto resizes on user input, only works if in the grid geometry manager of tk



5
6
7
# File 'lib/widgets/auto_resize_entry.rb', line 5

def min_width
  @min_width
end

Instance Method Details

#build(parent, **args) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/widgets/auto_resize_entry.rb', line 13

def build(parent, **args)
  super(parent, **args)
  parent.bind('Configure') { resize }
  tk_widget.textvariable = TkVariable.new unless tk_widget.textvariable
  tk_widget.textvariable.trace('write') { resize }
  resize
end

#resizeObject



29
30
31
32
33
34
35
36
# File 'lib/widgets/auto_resize_entry.rb', line 29

def resize
  max_width = [@cell.bbox[2], 0].max
  text_width = @font.measure(value)
  new_width = [[@min_width, text_width + @add_width].max, max_width].min
  tk_widget.width = 0
  tk_widget.grid(ipadx: new_width / 2.0)
  @parent.tk_widget.update
end

#valueObject



25
26
27
# File 'lib/widgets/auto_resize_entry.rb', line 25

def value
  tk_widget.textvariable.value
end

#value=(value) ⇒ Object



21
22
23
# File 'lib/widgets/auto_resize_entry.rb', line 21

def value=(value)
  tk_widget.textvariable.value = value
end