Created
March 11, 2015 00:37
-
-
Save matham/52956d9916ad9909bb5b to your computer and use it in GitHub Desktop.
scroll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.app import runTouchApp | |
from kivy.lang import Builder | |
from kivy.uix.textinput import TextInput | |
class TabTextInput(TextInput): | |
def __init__(self, *args, **kwargs): | |
super(TabTextInput, self).__init__(*args, **kwargs) | |
def keyboard_on_key_down(self, window, keycode, text, modifiers): | |
key, key_str = keycode | |
if key is not 8: | |
if key is 13 or self.cursor_col == 20: | |
self.insert_text('\n') | |
self.add_line() | |
return False | |
else: | |
if self.cursor_col==0 and self.cursor_row>0: | |
self.remove_line() | |
return super(TabTextInput, self).keyboard_on_key_down(window, keycode, text, modifiers) | |
def add_line(self): | |
self.height += self.line_height | |
def remove_line(self): | |
self.height -= self.line_height | |
def do_scroll(self, sv): | |
parent = self.parent | |
if not parent.height: | |
return | |
offset = self.height - (self.cursor[1] + 1) * self.line_height - self.padding_y[0] | |
for sib in parent.children: | |
if sib == self: | |
break | |
offset += sib.height | |
scroll = offset / float(parent.height - sv.height) | |
if sv.scroll_y > scroll: | |
sv.scroll_y = offset / float(parent.height - sv.height) | |
runTouchApp(Builder.load_string(""" | |
BoxLayout: | |
orientation: 'vertical' | |
ScrollView: | |
id: sv | |
on_scroll_y: print(self.scroll_y) | |
GridLayout: | |
cols: 1 | |
size_hint_y: None | |
height: self.minimum_height | |
on_height: tx1.do_scroll(sv) if tx1.focus else tx2.do_scroll(sv) | |
TabTextInput: | |
id: tx1 | |
font_size: 25 | |
size_hint_y: None | |
height: self.line_height * 3 / 2. | |
TabTextInput: | |
id: tx2 | |
font_size: 25 | |
size_hint_y: None | |
height: self.line_height * 3 / 2. | |
Button: | |
id: button_id | |
size_hint_y: .5 | |
text: 'Keyboard' | |
font_size: 120 | |
color: 0,0,0,1 | |
""")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment