forked from controversial/Pythonista-Tweaks
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pythonista.py
185 lines (144 loc) · 4.81 KB
/
Pythonista.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# coding: utf-8
'''
Pythonista.py
Vision:
- bring together in one place the things its possible to do with UIApplication and objc_util
- make customising the Pythonista app, similar to, and as easy as, using the UI module
Credits:
- based on examples by @omz, @JonB, @Webmaster4o, etc
'''
from objc_util import *
import dialogs
import urllib
NSUserDefaults = ObjCClass('NSUserDefaults')
WKWebView = ObjCClass('WKWebView')
NSURLRequest = ObjCClass('NSURLRequest')
UIBarButtonItem = ObjCClass('UIBarButtonItem')
UISearchBar = ObjCClass('UISearchBar')
NSDataDetector = ObjCClass('NSDataDetector')
app = UIApplication.sharedApplication()
rootVC = app.keyWindow().rootViewController()
tabVC = rootVC.detailViewController()
consoleVC = app.delegate().consoleViewController()
userDefaults = NSUserDefaults.standardUserDefaults()
'''
Classes to support customising the Pythonista app in a similar way to using the UI Module.
'''
# objCClasses
@on_main_thread
def Button(s):
return UIButton.new().autorelease()
@on_main_thread
def WebView(s):
return WKWebView.new().autorelease()
@on_main_thread
def ButtonItem(s, image, action):
return UIBarButtonItem.alloc().initWithImage_style_target_action_(UIImage.imageNamed_(image), 0, s.newVC, sel(action))
@on_main_thread
def SearchBar(s):
sb = UISearchBar.alloc().initWithFrame_(((0, 0), (200, 32)))
sb.searchBarStyle = 2
sb.placeholder = 'Search or enter address'
sb.delegate = s.newVC
ObjCInstanceMethod(sb, 'setAutocapitalizationType:')(0)
return sb
# View classes
class View(object):
@on_main_thread
def __init__(self):
self.name = ''
self.right_button_items = list()
self.newVC = self.customVC()
self.makeSelf()
@on_main_thread
def makeSelf(self):
pass
@on_main_thread
def customVC(self):
return None
@on_main_thread
def present(self):
pass
class Tab(View):
@on_main_thread
def makeSelf(self):
self.name = 'Tab'
@on_main_thread
def customVC(self):
return create_objc_class('CustomViewController', ObjCClass('UIViewController'), methods=[], protocols=['OMTabContent',]).new().autorelease()
@on_main_thread
def present(self):
self.newVC.title = self.name
self.newVC.navigationItem().rightBarButtonItems = self.right_button_items
tabVC.addTabWithViewController_(self.newVC)
class WebTab(Tab):
@on_main_thread
def makeSelf(self):
self.name = 'WebTab'
biA = ButtonItem(self, image = 'Action', action ='wtShare')
self.right_button_items.append(biA)
biF = ButtonItem(self, image = 'Forward', action ='wtGoForward')
self.right_button_items.append(biF)
biB = ButtonItem(self, image = 'Back', action = 'wtGoBack')
self.right_button_items.append(biB)
sb = SearchBar(self)
self.newVC.navigationItem().titleView = sb
wv = WebView(self)
wv.loadRequest_(NSURLRequest.requestWithURL_(nsurl('http://www.google.com')))
self.newVC.view = wv
@on_main_thread
def customVC(self):
return create_objc_class('CustomViewController', ObjCClass('UIViewController'), methods=[wtShare, wtGoBack, wtGoForward, searchBarSearchButtonClicked_], protocols=['OMTabContent', 'UISearchBarDelegate']).new().autorelease()
# actions
def wtShare(_self, _cmd):
view = ObjCInstance(_self).view()
url = view.URL()
if url:
url_str = str(url.absoluteString())
dialogs.share_url(url_str)
def wtGoBack(_self, _cmd):
view = ObjCInstance(_self).view()
view.goBack()
def wtGoForward(_self, _cmd):
view = ObjCInstance(_self).view()
view.goForward()
def searchBarSearchButtonClicked_(_self, _cmd, _sb):
searchbar = ObjCInstance(_sb)
term = str(searchbar.text())
searchbar.resignFirstResponder()
if term:
det = NSDataDetector.dataDetectorWithTypes_error_(1<<5, None)
res = det.firstMatchInString_options_range_(term, 0, (0, len(term)))
view = ObjCInstance(_self).view()
if res:
view.loadRequest_(NSURLRequest.requestWithURL_(res.URL()))
searchbar.text = res.URL().absoluteString()
else:
view.loadRequest_(NSURLRequest.requestWithURL_(nsurl('http://www.google.com/search?q=' + urllib.quote(term))))
'''
Module level utility functions
'''
@on_main_thread
def setBadgeString(s):
app.setApplicationBadgeString_(s)
@on_main_thread
def setBadgeNumber(i):
app.setApplicationIconBadgeNumber_(i)
@on_main_thread
def openURL(s):
app._openURL_(nsurl(s))
@on_main_thread
def getConsoleFont():
consoleVC.view()
return consoleVC.outputFont()
@on_main_thread
def getDefaultFont():
return [str(userDefaults.stringForKey_('OutputFontName')), userDefaults.integerForKey_('OutputFontSize')]
if __name__ == "__main__":
#setBadgeString('test')
#setBadgeNumber(1)
#openURL('pythonita://')
#print getConsoleFont()
#print getDefaultFont()
Tab().present()
WebTab().present()