cvp
Aug 26, 2018 - 09:37
For some files types (.py, ...), console.open_in(path) does not present the "copy" action.
This little script uses Objective-C UIActivityViewController to present the IOS standard open_in where copy is included.
you can put it in site-packages.
The script shows the path in a button above.
2nd parameter is the ui.View wher to show the dialog.
If you want to tell the user which action he should tap, you may add an optional "image" parameter with the image file you want to display at left of text.
import ui
import time
from objc_util import *
def open_in_with_copy(path,uiview,image=None):
global handler_done
handler_done = False
def handler(_cmd, _data, _error):
global handler_done
handler_done = True
#print('done')
return
#========= UIActivityViewController to copy the .epub
vo = ObjCInstance(uiview)
SUIViewController = ObjCClass('SUIViewController')
root_vc = SUIViewController.viewControllerForView_(vo)
main_view = root_vc.view()
handler_block = ObjCBlock(handler, restype=None, argtypes=[c_void_p, c_void_p, c_void_p])
url_array = [nsurl(path)]
UIActivityViewController = ObjCClass('UIActivityViewController').alloc().initWithActivityItems_applicationActivities_(url_array,None)
UIActivityViewController.setCompletionHandler_(handler_block)
b = ui.Button()
dx = 400
dy = 400
x = int(uiview.width - dx)/2
y = 50
b.frame = (x,y,dx,50)
b.font= ('Courier-Bold',20)
b.corner_radius = 10
b.border_width = 2
b.border_color = 'blue'
b.background_color = .95, .95, .95
uiview.add_subview(b)
i = path.find('Documents/') # suppprts extended path or not
b.title = path[i:]
if image:
b.image = ui.Image.named(image).with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
UIActivityViewController.popoverPresentationController().sourceView = main_view
UIActivityViewController.popoverPresentationController().sourceRect = CGRect(CGPoint(int(dx/2),b.y+b.height-int(dy/2)-10), CGSize(200,200))
root_vc.presentViewController_animated_completion_(UIActivityViewController, True, None)
while not handler_done:
time.sleep(1)
uiview.remove_subview(b)
Example
import ui
import os
from objc_util import *
from open_in_with_copy import *
fil = 'Documents/t.py'
path = os.path.expanduser('~/'+fil)
v = ui.View()
v.frame = (0,0,600,600)
v.present('sheet',hide_title_bar=True)
open_in_with_copy(path,v,image='iob:ios7_copy_outline_32')
v.close()
