Repository: kivy/kivy

Issue:
TextInput: app crashes when selecting using handles after deleting some text
**Software Versions**
* Python: 3.8.5
* OS: Linux Mint 20 (based on Ubuntu 20.04 LTS) or Android 10
* Kivy: 2.0.0rc4

**Describe the bug**
App crashes when the following actions are performed in the TextInput (described in the code below):

1. Select 'aaaa bbbb' substring (by double tap and then using selection handles), then delete it.
2. Select 'cccc' word (by double tap).
3. Pull the right selection handle right.

The traceback is placed below.

**Code**
```python
from kivy.app import App
from kivy.uix.textinput import TextInput
class Sample(App):
    def build(self):
        return TextInput(text='aaaa bbbb cccc '+'d'*200, use_handles=True)
Sample().run()
```

**Traceback**
```
Traceback (most recent call last):
   File "/usr/lib/python3.8/pdb.py", line 1704, in main
     pdb._runscript(mainpyfile)
   File "/usr/lib/python3.8/pdb.py", line 1573, in _runscript
     self.run(statement)
   File "/usr/lib/python3.8/bdb.py", line 580, in run
     exec(cmd, globals, locals)
   File "<string>", line 1, in <module>
   File "/home/al/Desktop/KivyBugs/bug6.py", line 1, in <module>
     from kivy.app import App
   File "/home/al/kivy_venv/lib/python3.8/site-packages/kivy/app.py", line 950, in run
     runTouchApp()
   File "/home/al/kivy_venv/lib/python3.8/site-packages/kivy/base.py", line 573, in runTouchApp
     EventLoop.mainloop()
   File "/home/al/kivy_venv/lib/python3.8/site-packages/kivy/base.py", line 347, in mainloop
     self.idle()
   File "/home/al/kivy_venv/lib/python3.8/site-packages/kivy/base.py", line 397, in idle
     Clock.tick_draw()
   File "/home/al/kivy_venv/lib/python3.8/site-packages/kivy/clock.py", line 783, in tick_draw
     self._process_events_before_frame()
   File "kivy/_clock.pyx", line 658, in kivy._clock.CyClockBase._process_events_before_frame
   File "kivy/_clock.pyx", line 701, in kivy._clock.CyClockBase._process_events_before_frame
   File "kivy/_clock.pyx", line 697, in kivy._clock.CyClockBase._process_events_before_frame
   File "kivy/_clock.pyx", line 218, in kivy._clock.ClockEvent.tick
   File "/home/al/kivy_venv/lib/python3.8/site-packages/kivy/uix/textinput.py", line 2099, in _update_graphics
     self._update_graphics_selection()
   File "/home/al/kivy_venv/lib/python3.8/site-packages/kivy/uix/textinput.py", line 2137, in _update_graphics_selection
     draw_selection(r.pos, r.size, line_num, (s1c, s1r),
   File "/home/al/kivy_venv/lib/python3.8/site-packages/kivy/uix/textinput.py", line 2162, in _draw_selection
     x2 = (x - self.scroll_x) + _get_text_width(lines[:s2c],
 TypeError: slice indices must be integers or None or have an __index__ method
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /home/al/kivy_venv/lib/python3.8/site-packages/kivy/uix/textinput.py(2162)_draw_selection()
-> x2 = (x - self.scroll_x) + _get_text_width(lines[:s2c],
```



Discussion:

Resolving patch:
diff --git a/kivy/uix/textinput.py b/kivy/uix/textinput.py
--- a/kivy/uix/textinput.py
+++ b/kivy/uix/textinput.py
@@ -241,6 +241,7 @@ class Selector(ButtonBehavior, Image):
 
     def __init__(self, **kwargs):
         super().__init__(**kwargs)
+        self.always_release = True
         self.matrix = self.target.get_window_matrix()
 
         with self.canvas.before:
@@ -1620,10 +1621,10 @@ def _handle_released(self, instance):
         self._update_selection()
         self._show_cut_copy_paste(
             (
-                instance.right
+                self.x + instance.right
                 if instance is self._handle_left
-                else instance.x,
-                instance.top + self.line_height
+                else self.x + instance.x,
+                self.y + instance.top + self.line_height
             ),
             EventLoop.window
         )
@@ -1647,21 +1648,22 @@ def _handle_move(self, instance, touch):
             x,
             y + instance._touch_diff + (self.line_height / 2)
         )
+        self.cursor = cursor
 
         if instance != touch.grab_current:
             return
 
         if instance == handle_middle:
-            self.cursor = cursor
             self._position_handles(mode='middle')
             return
 
-        cindex = self.cursor_index(cursor=cursor)
+        cindex = self.cursor_index()
 
         if instance == handle_left:
             self._selection_from = cindex
         elif instance == handle_right:
             self._selection_to = cindex
+        self._update_selection()
         self._trigger_update_graphics()
         self._trigger_position_handles()
 


Test patch: