Class: Clogs::EditLine

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, #multiline?, #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) ⇒ EditLine

Returns a new instance of EditLine.



359
360
361
362
# File 'lib/clogs/drawables/controls.rb', line 359

def initialize(properties)
  super
  editable_init
end

Instance Method Details

#displayed_textObject



364
365
366
# File 'lib/clogs/drawables/controls.rb', line 364

def displayed_text
  style(:secret) ? "" * text_value.length : text_value
end

#draw(painter, x, y) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/clogs/drawables/controls.rb', line 375

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

  st = text_style(color: Style.color(style(:stroke), [0, 0, 0, 255]))
  text = displayed_text
  inner_x = x + 6
  inner_y = y + (@height - @line_height) / 2.0

  painter.save do |p|
    p.clip_rect(x + 2, y + 2, @width - 4, @height - 4)
    if (range = selection_range)
      sx = Paragraph.measure(text[0...range[0]], st)[0]
      sw = Paragraph.measure(text[range[0]...range[1]], st)[0]
      p.fill_rect(inner_x + sx - @scroll_x, inner_y, sw, @line_height, [180, 210, 255, 255])
    end
    unless text.empty?
      block = TextBlock.new([Run.new(text: text, size: st.size, family: st.family, color: st.color)],
        -1, default_family: st.family, default_size: st.size)
      block.draw(p, inner_x - @scroll_x, inner_y)
      block.free
    end
    if focused
      cx = inner_x + Paragraph.measure(text[0...@caret], st)[0] - @scroll_x
      p.line(cx, inner_y, cx, inner_y + @line_height, [0, 0, 0, 255], thickness: 1)
    end
  end
end

#index_for_offset(text, offset, st) ⇒ Object

Walk the string until the measured prefix passes the click point.



416
417
418
419
420
421
422
423
# File 'lib/clogs/drawables/controls.rb', line 416

def index_for_offset(text, offset, st)
  return 0 if offset <= 0

  (0..text.length).each do |i|
    return [i - 1, 0].max if Paragraph.measure(text[0...i], st)[0] > offset
  end
  text.length
end

#measure(available_width) ⇒ Object



368
369
370
371
372
373
# File 'lib/clogs/drawables/controls.rb', line 368

def measure(available_width)
  @width = requested_width(available_width) || [available_width, 200].min
  st = text_style
  @line_height = Paragraph.line_height(st)
  @height = requested_height(available_width) || (@line_height + PADDING_Y * 2).ceil
end

#on_click(x, y, _button) ⇒ Object



406
407
408
409
410
411
412
413
# File 'lib/clogs/drawables/controls.rb', line 406

def on_click(x, y, _button)
  st = text_style
  local = x - @abs_x - 6 + @scroll_x
  @caret = index_for_offset(displayed_text, local, st)
  @anchor = nil
  _ = y
  redraw!
end