Class: Clogs::EditBox

Inherits:
Control show all
Includes:
Editable
Defined in:
lib/clogs/drawables/controls.rb

Constant Summary

Constants inherited from Control

Control::PADDING_X, Control::PADDING_Y

Instance Attribute Summary

Attributes included from Editable

#anchor, #caret

Attributes inherited from Control

#focused, #hovered, #pressed

Attributes inherited from Drawable

#abs_x, #abs_y, #children, #height, #parent, #shoes_linkable_id, #styles, #width, #x, #y

Instance Method Summary collapse

Methods included from Editable

#caret_line_offset, #delete_selection, #editable_init, #focus_gained, #focus_lost, #focusable?, #handle_char, #handle_ctrl, #insert, #line_end, #line_start, #move_caret, #on_key, #selection_range, #text_value, #text_value=

Methods inherited from Control

#clickable?, #label, #on_mouse_enter, #on_mouse_leave, #text_style

Methods inherited from Drawable

#add_child, #app, #clickable?, #contains?, #destroy_self, #each_peer, #focus_gained, #focus_lost, #focusable?, for_shoes_class, #hidden?, #margin, #needs_layout!, #notify, #on_key, #on_mouse_move, #on_release, #paint, #positioned?, #properties_changed, #redraw!, #remove_child, #requested_height, #requested_width, #set_parent, #style

Constructor Details

#initialize(properties) ⇒ EditBox

Returns a new instance of EditBox.



429
430
431
432
# File 'lib/clogs/drawables/controls.rb', line 429

def initialize(properties)
  super
  editable_init
end

Instance Method Details

#draw(painter, x, y) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
# File 'lib/clogs/drawables/controls.rb', line 446

def draw(painter, x, y)
  painter.draw(fill: [255, 255, 255, 255], stroke: focused ? [60, 140, 230, 255] : [160, 160, 160, 255],
    thickness: focused ? 2 : 1) do |p|
    Clogs.rounded_rect(p, x + 0.5, y + 0.5, @width - 1, @height - 1, 3)
  end
  painter.save do |p|
    p.clip_rect(x + 2, y + 2, @width - 4, @height - 4)
    @paragraph.draw(p, x + 6, y + 4 - @scroll_y)
    draw_caret(p, x, y) if focused
  end
end

#draw_caret(painter, x, y) ⇒ Object



458
459
460
461
462
463
464
465
466
467
# File 'lib/clogs/drawables/controls.rb', line 458

def draw_caret(painter, x, y)
  item = @paragraph.placed.find { |i| @caret >= i.char_offset && @caret <= i.char_offset + i.text.length }
  return unless item

  st = item.style
  prefix = item.text[0...(@caret - item.char_offset)]
  cx = x + 6 + item.x + Paragraph.measure(prefix, st)[0]
  cy = y + 4 + item.y - @scroll_y
  painter.line(cx, cy, cx, cy + item.height, [0, 0, 0, 255], thickness: 1)
end

#measure(available_width) ⇒ Object



438
439
440
441
442
443
444
# File 'lib/clogs/drawables/controls.rb', line 438

def measure(available_width)
  @width = requested_width(available_width) || [available_width, 300].min
  st = text_style
  @line_height = Paragraph.line_height(st)
  @height = requested_height(available_width) || (@line_height * 6 + PADDING_Y * 2).ceil
  @paragraph = Paragraph.new([[text_value.empty? ? " " : text_value, st]], @width - 12)
end

#multiline?Boolean

Returns:

  • (Boolean)


434
435
436
# File 'lib/clogs/drawables/controls.rb', line 434

def multiline?
  true
end

#on_click(x, y, _button) ⇒ Object



469
470
471
472
473
474
# File 'lib/clogs/drawables/controls.rb', line 469

def on_click(x, y, _button)
  @caret = (@paragraph&.index_at(x - @abs_x - 6, y - @abs_y - 4 + @scroll_y) || 0)
    .clamp(0, text_value.length)
  @anchor = nil
  redraw!
end