This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor_util.py
191 lines (138 loc) · 5.45 KB
/
editor_util.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
186
187
188
189
190
191
"""
Use this module to change the editor and console font programatically
"""
__author__ = "Lukas Kollmer<lukas.kollmer@gmail.com>"
__copyright__ = "Copyright (c) 2016 Lukas Kollmer<lukas.kollmer@gmail.com>"
from pythonista import _utils
from pythonista import defaults
_utils.guard_objc_util()
import objc_util
import ctypes
# --- Custom Fonts
UIFont = objc_util.ObjCClass("UIFont")
OMBarButton = objc_util.ObjCClass('OMBarButton')
UIButton = objc_util.ObjCClass('UIButton')
PA2UniversalTextEditorViewController = objc_util.ObjCClass("PA2UniversalTextEditorViewController")
PA2EmptyTabViewController = objc_util.ObjCClass("PA2EmptyTabViewController")
CTFontManagerRegisterFontsForURL = objc_util.c.CTFontManagerRegisterFontsForURL
CTFontManagerRegisterFontsForURL.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p]
CTFontManagerRegisterFontsForURL.restype = ctypes.c_bool
CFURLCreateWithString = objc_util.c.CFURLCreateWithString
CFURLCreateWithString.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
CFURLCreateWithString.restype = ctypes.c_void_p
def load_custom_font(file):
#https://marco.org/2012/12/21/ios-dynamic-font-loading
font_url = CFURLCreateWithString(None, objc_util.ns(file), None)
error = ctypes.c_void_p(None)
success = CTFontManagerRegisterFontsForURL(objc_util.ObjCInstance(font_url), 0, ctypes.byref(error))
#print(objc_util.ObjCInstance(error))
#print("success:", success)
#font = UIFont.fontWithName_size_("SFMono-Regular", 15)
#print(font)
def set_editor_font(name, size=15):
"""
NOTE: This still requires a restart
"""
defaults.set("EditorFontName", name)
defaults.set("EditorFontSize", size)
#font = objc_util.UIFont.fontWithName_size_(name, size)
# --- Custom Button Items
_DEFAULT_BAR_BUTTON_POSITION = "left"
UIBarButtonItem = objc_util.ObjCClass('UIBarButtonItem')
def _add_button_item(item, position=_DEFAULT_BAR_BUTTON_POSITION):
"""
Do not call this directly, it just adds the item to the view.
You'll need to add the action to the view manually
"""
tabVC = _utils._application.keyWindow().rootViewController().detailViewController()
if position == "left":
buttonItems = tabVC.persistentLeftBarButtonItems().mutableCopy()
buttonItems.insert(Object=item, atIndex=0)
tabVC.setPersistentLeftBarButtonItems_(buttonItems)
tabVC.reloadBarButtonItemsForSelectedTab()
elif position == "right":
toolbar = tabVC.toolbar()
button = UIButton.new()
button.setTitle_("title")
toolbar.addSubview_(button)
def add_text_button_item(text, action, position=_DEFAULT_BAR_BUTTON_POSITION):
tabVC = _utils._application.keyWindow().rootViewController().detailViewController()
selector = _utils.add_method(action, tabVC)
barButtonItem = UIBarButtonItem.alloc().init(Title=text, style=0, target=tabVC, action=selector)
#print(barButtonItem)
_add_button_item(barButtonItem, position)
def add_image_button_item(image, action, position=_DEFAULT_BAR_BUTTON_POSITION):
barButtonItem = UIBarButtonItem.alloc().init(Image=image, style=0, target=tabVC, action=selector)
#print(barButtonItem)
_add_button_item(barButtonItem, position)
# --- Editor Keyboard Shortcuts
""" Moved to the `shortcuts` submodule """
# --- Tab Management
def close_tab(tab):
if isinstance(tab, objc_util.ObjCInstance):
if tab.isKindOfClass_(PA2UniversalTextEditorViewController):
tabVC = _utils._application.keyWindow().rootViewController().detailViewController()
tabVC.closeTab_(tab)
if isinstance(tab, int):
tabVC = _utils._application.keyWindow().rootViewController().detailViewController()
tab = tabVC.tabViewControllers()[tab]
print(tab)
close_tab(tab)
def close_current_tab():
import editor
tab = editor._get_editor_tab()
close_tab(tab)
def open_tab(path_or_index=None):
tabVC = _utils._application.keyWindow().rootViewController().detailViewController()
# Open a new empty tab
if isinstance(path_or_index, type(None)):
tabVC.addTab_(None)
# Open a new tab with a file
if isinstance(path_or_index, str):
import editor
#tabVC.open(File=path, inNewTab=True, withPreferredEditorType=True, forceReload=False)
editor.open_file(path_or_index, new_tab=True, force_reload=False)
# Switch to an existing tab
if isinstance(path_or_index, int):
# This doesn't really work well, since just the editor will change tab,
# but Pythonista won't select a different tab in the tab bar
#tabVC.selectTabAtIndex_(path)
# from editor.py
tabs = []
for tab in tabVC.tabViewControllers():
if tab.isKindOfClass_(PA2EmptyTabViewController):
tabs.append(None)
if tab.isKindOfClass_(PA2UniversalTextEditorViewController):
if not tab.isViewLoaded():
tabs.append(None)
continue
tab_path = str(tab.filePath())
tabs.append(tab_path)
path = tabs[path_or_index]
if not path is None:
import editor
editor.open_file(path, new_tab=True, force_reload=False)
else:
raise ValueError("Can't open empty path")
# --- Quick Open
def show_quick_open():
pass
# --- Demo
if __name__ == "__main__":
# Test custom fonts
import os
font_path = "file://" + os.path.expanduser("~/Documents/Fonts/SFMono/SFMono-Regular.otf")
#load_custom_font(font_path)
font_name = "Menlo-Regular"
#set_editor_font(font_name, 15)
"""
# Test bar buttons
def button_action(_self, _sel):
print("button tapped!!!")
add_text_button_item("hi", button_action, "left")
# Test keyboard shortcuts
def commandGHandler(_self, _sel):
print("Command G pressed")
register_shortcut("cmd+g", commandGHandler, "")
"""
open_tab(2)