Class: VER::HoverCompletion
- Inherits:
-
Object
- Object
- VER::HoverCompletion
- Defined in:
- lib/ver/hover_completion.rb
Overview
TODO: the positioning behaviour is still annoying, for some reason I just can’t get a handle on a changed position. Even using the tk-internal Expose event works only sometimes.
Defined Under Namespace
Classes: Listbox
Instance Attribute Summary collapse
-
#choices ⇒ Object
Returns the value of attribute choices.
-
#completer ⇒ Object
Returns the value of attribute completer.
-
#from ⇒ Object
Returns the value of attribute from.
-
#list ⇒ Object
readonly
Returns the value of attribute list.
-
#options ⇒ Object
Returns the value of attribute options.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#to ⇒ Object
Returns the value of attribute to.
Instance Method Summary collapse
- #cancel ⇒ Object
- #continue_completion ⇒ Object
-
#initialize(parent, options = {}, &completer) ⇒ HoverCompletion
constructor
A new instance of HoverCompletion.
- #layout ⇒ Object
- #pick ⇒ Object
- #setup_events ⇒ Object
- #setup_widgets ⇒ Object
- #submit ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(parent, options = {}, &completer) ⇒ HoverCompletion
Returns a new instance of HoverCompletion.
44 45 46 47 48 49 |
# File 'lib/ver/hover_completion.rb', line 44 def initialize(parent, = {}, &completer) @parent, @options, @completer = parent, , completer setup_events update end |
Instance Attribute Details
#choices ⇒ Object
Returns the value of attribute choices.
42 43 44 |
# File 'lib/ver/hover_completion.rb', line 42 def choices @choices end |
#completer ⇒ Object
Returns the value of attribute completer.
42 43 44 |
# File 'lib/ver/hover_completion.rb', line 42 def completer @completer end |
#from ⇒ Object
Returns the value of attribute from.
42 43 44 |
# File 'lib/ver/hover_completion.rb', line 42 def from @from end |
#list ⇒ Object (readonly)
Returns the value of attribute list.
41 42 43 |
# File 'lib/ver/hover_completion.rb', line 41 def list @list end |
#options ⇒ Object
Returns the value of attribute options.
42 43 44 |
# File 'lib/ver/hover_completion.rb', line 42 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
41 42 43 |
# File 'lib/ver/hover_completion.rb', line 41 def parent @parent end |
#to ⇒ Object
Returns the value of attribute to.
42 43 44 |
# File 'lib/ver/hover_completion.rb', line 42 def to @to end |
Instance Method Details
#cancel ⇒ Object
141 142 143 144 |
# File 'lib/ver/hover_completion.rb', line 141 def cancel list.destroy parent.focus end |
#continue_completion ⇒ Object
68 69 70 71 |
# File 'lib/ver/hover_completion.rb', line 68 def continue_completion pick [:continue] ? update : cancel end |
#layout ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ver/hover_completion.rb', line 98 def layout return unless choices && choices.size > 0 x, y, caret_height = parent.tk_caret.values_at(:x, :y, :height) height, width = parent.winfo_height, parent.winfo_width height -= parent.status.winfo_height # use side with most space, east or west if x > (width / 2) side = 'e' else side = 'w' end # use hemisphere with most space if y < (height / 3) hemisphere = 'n' y += caret_height height -= y elsif y < (height / 2) hemisphere = '' height -= ((height - y) / 2) else hemisphere = 's' height -= (height - y) end list.configure width: @longest_choice + 2, height: -1 height = [height, list.winfo_reqheight].min width = [width, list.winfo_reqwidth].min list.place( x: x, y: y, height: height, width: width, in: parent, anchor: "#{hemisphere}#{side}" ) end |
#pick ⇒ Object
78 79 80 81 82 |
# File 'lib/ver/hover_completion.rb', line 78 def pick index = list.curselection.first replacement = list.get(index) parent.replace(from, to, replacement) end |
#setup_events ⇒ Object
63 64 65 66 |
# File 'lib/ver/hover_completion.rb', line 63 def setup_events list.bind('<<ListboxSelect>>'){ layout } list.bind('<Expose>'){ layout } end |
#setup_widgets ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ver/hover_completion.rb', line 51 def @list = Listbox.new( parent, borderwidth: 0, selectmode: :single, font: parent..font ) @list.hover_completion = self @list.major_mode = :HoverCompletion @list.focus end |
#submit ⇒ Object
73 74 75 76 |
# File 'lib/ver/hover_completion.rb', line 73 def submit pick cancel end |
#update ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ver/hover_completion.rb', line 84 def update self.from, self.to, self.choices = completer.call @longest_choice = choices.map{|choice| choice.size }.max if choices && choices.size > 0 list.value = choices list.select 0 submit if choices.size == 1 else cancel end end |