Class: MittensUi::Textbox

Inherits:
Core
  • Object
show all
Defined in:
lib/mittens_ui/textbox.rb

Overview

A single-line or multiline text input widget. In single-line mode (default), wraps Gtk::Entry. In multiline mode, wraps Gtk::TextView inside a Gtk::ScrolledWindow.

Examples:

Single-line textbox

tb = MittensUi::Textbox.new(can_edit: true, placeholder: "Enter name...")
puts tb.text

Password field

tb = MittensUi::Textbox.new(password: true)

Multiline textbox

tb = MittensUi::Textbox.new(multiline: true, width: :full)
puts tb.text

Text completion

tb = MittensUi::Textbox.new(can_edit: true)
tb.enable_text_completion(["Apple", "Banana", "Cherry"])

Instance Attribute Summary

Attributes inherited from Core

#core_widget

Instance Method Summary collapse

Methods inherited from Core

#hidden?, #hide, #keyboard_shortcut, #remove, #remove_keyboard_shortcut, #render, #shortcuts, #show

Methods included from Helpers

#icon_map, #list_system_icons, #set_margin_from_opts_for

Constructor Details

#initialize(options = {}) ⇒ Textbox

Creates a new Textbox widget.

Parameters:

  • options (Hash) (defaults to: {})

    configuration options

Options Hash (options):

  • :can_edit (Boolean) — default: true

    whether the text is editable

  • :multiline (Boolean) — default: false

    when true, renders a scrollable multiline text area instead of a single-line input

  • :max_length (Integer) — default: 200

    maximum character length. Only applies in single-line mode.

  • :password (Boolean) — default: false

    when true, obscures the input text. Only applies in single-line mode.

  • :placeholder (String) — default: ""

    placeholder text shown when empty. Only applies in single-line mode.

  • :height (Integer) — default: 100

    height of the scrolled window in pixels. Only applies in multiline mode.

  • :width (Symbol) — default: :full

    column width in the layout grid. Accepted values are :full, :half, :third, :quarter

  • :defer_render (Boolean) — default: false

    when true, skips auto-rendering into the layout. Use when passing to a container like HBox.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mittens_ui/textbox.rb', line 45

def initialize(options = {})
  @multiline = options.fetch(:multiline, false)

  if @multiline
    init_multiline(options)
  else
    init_single_line(options)
  end

  super(@gtk_widget, options)
end

Instance Method Details

#clearvoid

This method returns an undefined value.

Clears all text from the widget. Works in both single-line and multiline mode.

Examples:

tb = MittensUi::Textbox.new
tb.clear


88
89
90
91
92
93
94
# File 'lib/mittens_ui/textbox.rb', line 88

def clear
  if @multiline
    @text_buffer.text = ''
  else
    @textbox.text = ''
  end
end

#enable_text_completion(data) ⇒ void

This method returns an undefined value.

Enables autocomplete suggestions for single-line mode. Has no effect in multiline mode.

Examples:

tb = MittensUi::Textbox.new(can_edit: true)
tb.enable_text_completion(["Ruby", "Rails", "Rack"])

Parameters:

  • data (Array<String>)

    list of strings to suggest as completions



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mittens_ui/textbox.rb', line 104

def enable_text_completion(data)
  return if @multiline

  completion = Gtk::EntryCompletion.new
  model = Gtk::ListStore.new(String)
  data.each do |value|
    iter = model.append
    iter[0] = value
  end
  completion.model = model
  completion.text_column = 0
  @textbox.completion = completion
end

#textString

Returns the current text content of the widget. Works in both single-line and multiline mode.

Examples:

tb = MittensUi::Textbox.new
tb.text  # => ""

Returns:

  • (String)

    the current text



64
65
66
67
68
69
70
# File 'lib/mittens_ui/textbox.rb', line 64

def text
  if @multiline
    @text_buffer.text
  else
    @textbox.text
  end
end

#text=(value) ⇒ Object

Parameters:

  • value (String)

    A String value that the label gets set to



73
74
75
76
77
78
79
# File 'lib/mittens_ui/textbox.rb', line 73

def text=(value)
  if @multiline
    @text_buffer.text = value.to_s
  else
    @textbox.text = value.to_s
  end
end