-
Notifications
You must be signed in to change notification settings - Fork 19
/
KeyboardFrame.py
148 lines (125 loc) · 4.72 KB
/
KeyboardFrame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import ui
from uicontainer import FlowContainer
from PopupButton import PopupButton
class key(object):
#model of a key. view is popupbutton
# key has a title, value, action, and maybe subkeys
def __init__(self, val='',subkeys=[],title=None, action=None):
self.val=val
if title is None:
self.title=self.val
else:
self.title=title
self.subkeys=[key(s) if isinstance(s,str) else s for s in subkeys]
if action:
self.action=action
else:
self.action=self.default
def default(self,sender):
#default action: insert value into textview
if sender.superview:
txt=kb['text'] #ugly global...need a better way to set target
txt.replace_range(txt.selected_range,self.val)
def makeButton(self):
#return popup button view of this key
childButtons=[subkey.makeButton() for subkey in self.subkeys]
return PopupButton(title=self.title,childButtons=childButtons,action=self.action)
def notimplemented(sender):
import console
console.hud_alert('key action not implemented')
class KeyboardFrame(ui.View):
def __init__(self):
self.flex='WHT'
self.border_width=5
self.border_color=(0,1,0)
def setupkb(self):
#define keys
redokey=key(title='redo',action=self.redoaction)
undokey=key(title='undo',subkeys=[redokey], action=self.undoaction)
hidekey=key(title='hide',action=self.hideaction)
keymap=[key('\t',title='TAB'),key('_'),key('#',['@']),key('<',['<=']),key('>',['>=']),
key('{'),key('}'),key('['),key(']'),key("'",['"']),key('('),key(')'),
key(':',[';']), undokey]+[key(str(n)) for n in range(1,9)]+[key('0'),key('+',['%']),key('-'),key('/',['\\n','\\t','\\','/']),key('*'),key('=',['!=']), hidekey]
#customkb component
customkb=FlowContainer(frame=(0,self.height-100,self.width,100),flex='')
customkb.name='customkb'
self.add_subview(customkb)
minimizedkb=ui.Button(frame=(0,self.height-15,self.width,15),flex='',bg_color=(.7, .7, .7))
minimizedkb.action=self.showaction
minimizedkb.title=u'\u2550'*10
minimizedkb.name='minimizedkb'
self.add_subview(minimizedkb)
customkb.bring_to_front()
customkb.hidden=True
for k in keymap:
customkb.add_subview(k.makeButton())
customkb.flex='WT'
customkb.y=self.height-customkb.height
#contentframe
content=ui.View(frame=(0,0,self.width,self.height-15))
content.name='content'
self.add_subview(content)
content.send_to_back()
content.border_color=(0,0,1)
content.border_width=3
self.content=content
def add_content(self,subview, fill=True):
self.content.add_subview(subview)
if fill:
subview.width=self.content.width
subview.height=self.content.height
subview.flex='wh'
def hideaction(self,sender):
self['customkb'].hidden = True
self.layout()
def showaction(self,sender):
self['customkb'].hidden = False
self.layout()
def undoaction(self):
'''override '''
pass
def redoaction(self):
'''override'''
pass
def layout(self):
try:
self['customkb'].frame = (0,self.height-100, self.width, 100)
self['minimizedkb'].frame=(0,self.height-15, self.width, 15)
if self['customkb'].hidden:
self['content'].frame=(0,0, self.width, self.height-15)
else:
self['content'].frame=(0,0, self.width, self.height-105)
except AttributeError:
pass
#pass
def keyboard_frame_did_change(self,frame):
if not self.on_screen:
return
if self.superview:
frame=self.superview.get_keyboard_frame()
if frame[3]:
kbframe=self.superview.convert_rect(frame,None,self)
self.height=kbframe[1]
self.y=0
self['customkb'].hidden=False
else:
self.height=self.superview.height
self.y=0
self['customkb'].hidden=True
if __name__=='__main__':
#set up ui
import RootView
presentmode='fullscreen'
#presentmode='panel'
root=RootView.RootView()
root.flex='WH'
root.border_color=(1,0,0)
root.border_width=2
root.present(presentmode)
# set up example
kb=KeyboardFrame()
kb.frame=(0,0,root.width,root.height)
root.add_subview(kb)
kb.setupkb()
kb.bg_color=(.8,.8,.8)
kb.add_content(ui.TextView(name='text'))