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.
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
- #go_down ⇒ Object
- #go_up ⇒ Object
-
#initialize(parent, options = {}, &completer) ⇒ HoverCompletion
constructor
A new instance of HoverCompletion.
- #layout ⇒ Object
- #pick ⇒ Object
- #select(index) ⇒ Object
- #setup_events ⇒ Object
- #setup_keymap ⇒ Object
- #setup_widgets ⇒ Object
- #submit ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(parent, options = {}, &completer) ⇒ HoverCompletion
Returns a new instance of HoverCompletion.
9 10 11 12 13 14 15 |
# File 'lib/ver/hover_completion.rb', line 9 def initialize(parent, = {}, &completer) @parent, @options, @completer = parent, , completer setup_keymap setup_events update end |
Instance Attribute Details
#choices ⇒ Object
Returns the value of attribute choices.
7 8 9 |
# File 'lib/ver/hover_completion.rb', line 7 def choices @choices end |
#completer ⇒ Object
Returns the value of attribute completer.
7 8 9 |
# File 'lib/ver/hover_completion.rb', line 7 def completer @completer end |
#from ⇒ Object
Returns the value of attribute from.
7 8 9 |
# File 'lib/ver/hover_completion.rb', line 7 def from @from end |
#list ⇒ Object (readonly)
Returns the value of attribute list.
6 7 8 |
# File 'lib/ver/hover_completion.rb', line 6 def list @list end |
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/ver/hover_completion.rb', line 7 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
6 7 8 |
# File 'lib/ver/hover_completion.rb', line 6 def parent @parent end |
#to ⇒ Object
Returns the value of attribute to.
7 8 9 |
# File 'lib/ver/hover_completion.rb', line 7 def to @to end |
Instance Method Details
#cancel ⇒ Object
124 125 126 127 |
# File 'lib/ver/hover_completion.rb', line 124 def cancel list.destroy parent.focus end |
#continue_completion ⇒ Object
50 51 52 53 |
# File 'lib/ver/hover_completion.rb', line 50 def continue_completion pick [:continue] ? update : cancel end |
#go_down ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/ver/hover_completion.rb', line 33 def go_down index = list.curselection.first + 1 max = list.size return unless index < max select index end |
#go_up ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/ver/hover_completion.rb', line 42 def go_up index = list.curselection.first - 1 return unless index >= 0 select index end |
#layout ⇒ Object
88 89 90 91 92 93 94 95 96 97 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 |
# File 'lib/ver/hover_completion.rb', line 88 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 height: -1 # fit as many into the list as possible # let's place it so we can see how big it is. list.place x: x, y: y, height: height, in: parent, anchor: "#{hemisphere}#{side}" end |
#pick ⇒ Object
60 61 62 63 64 |
# File 'lib/ver/hover_completion.rb', line 60 def pick index = list.curselection.first replacement = list.get(index) parent.replace(from, to, replacement) end |
#select(index) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/ver/hover_completion.rb', line 80 def select(index) list.selection_clear(0, :end) list.selection_set(index) list.see(index) Tk::Event.generate(list, '<<ListboxSelect>>') end |
#setup_events ⇒ Object
28 29 30 31 |
# File 'lib/ver/hover_completion.rb', line 28 def setup_events list.bind('<<ListboxSelect>>'){ layout } list.bind('<Expose>'){ layout } end |
#setup_keymap ⇒ Object
22 23 24 25 26 |
# File 'lib/ver/hover_completion.rb', line 22 def setup_keymap keymap_name = VER..keymap @list_keymap = Keymap.get(name: keymap_name, receiver: self, widget: list, mode: :hover_completion) end |
#setup_widgets ⇒ Object
17 18 19 20 |
# File 'lib/ver/hover_completion.rb', line 17 def @list = Tk::Listbox.new(parent, borderwidth: 0, selectmode: :single) @list.focus end |
#submit ⇒ Object
55 56 57 58 |
# File 'lib/ver/hover_completion.rb', line 55 def submit pick cancel end |
#update ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ver/hover_completion.rb', line 66 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 select 0 submit if choices.size == 1 else cancel end end |