def _make_webview_transparent(webview):
    #
    # Any UI manipulation in ObjC must be done on the main thread, thus there's
    # @on_main_thread decorator to ensure that this function is always called
    # on the main thread.
    #
    # Pythonista usually wraps ObjC views / controls / ... in own
    # view. ui.WebView is not real UIWebView, it's kind of wrapper.
    # So we have to get ObjC instance of the wrapper view, traverse
    # subviews and check classes to find real UIWebView (ObjC).
    #
    # Actually ui.WebView (Python) consists of:
    #
    # SUIWebView_PY3 (ObjC)
    #    | UIWebView (ObjC)
    #    |     | _UIWebViewScrollView (ObjC)
    #    |     |     | UIWebBrowserView (ObjC)
    # ...
    #
    pythonista_wrapper_objc = ObjCInstance(webview)
    real_webview_objc = _find_real_webview(pythonista_wrapper_objc)
    if real_webview_objc:
        # UIWebView found

        # Make it transparent
        # https://developer.apple.com/documentation/uikit/uiview/1622622-opaque?language=objc
        real_webview_objc.setOpaque_(False)

        # Set background color to clear color
        # https://developer.apple.com/documentation/uikit/uicolor/1621945-clearcolor?language=objc
        clear_color = ObjCClass('UIColor').clearColor()
        real_webview_objc.setBackgroundColor_(clear_color)
def handler(_cmd, _data, _error, altitudes=None):
    if altitudes == None:
        altitudes = []
        information = str(ObjCInstance(_data))
        altitudes = information[len("Altitude "):len("Altitude ") + 9]
        print(altitudes)
        return altitudes
Example #3
0
def _blackmamba_keyCommands(_self, _cmd):
    """Swizzled version of keyCommands(). It calls original method to
    get Pythonista shortcuts and then appends custom ones."""
    obj = ObjCInstance(_self)
    commands = list(obj.originalkeyCommands() or [])
    commands.extend(_key_commands)
    return ns(commands).ptr
 def recordingStartedHandler(_self, _cmd, _error):
     if _error:
         error = ObjCInstance(_error)
         print error.localizedDescription()
     elif RPScreenRecorder.sharedRecorder().isRecording():
         screenRecorderItem.image = ns(
             ui.Image.named('iob:ios7_videocam_32'))
Example #5
0
def render_callback(
    inRefCon: ctypes.c_void_p,
    ioActionFlags: ctypes.POINTER(AudioUnitRenderActionFlags),
    inTimeStamp: ctypes.POINTER(AudioTimeStamp),
    inBusNumber: ctypes.c_uint32,
    inNumberFrames: ctypes.c_uint32,
    ioData: ctypes.POINTER(AudioBufferList)) -> ctypes.c_uint32:
  
  instance = ObjCInstance(inRefCon)
  amplitude = 0.9
  sampleRate = 44100
  frequency = instance.frequency
  theta = instance.theta
  theta_increment = 2.0 * pi * frequency / sampleRate
  buffer = ctypes.cast(ioData[0].mBuffers[0].mData,
  ctypes.POINTER(ctypes.c_float * inNumberFrames)).contents
  
  for frame in range(inNumberFrames):
    buffer[frame] = math.sin(theta) * amplitude
    theta += theta_increment
    if theta > 2.0 * pi:
      theta -= 2.0 * pi
  instance.buffer = buffer
  instance.theta = theta
  return 0
Example #6
0
    def reload_path(self, path):
        index = None
        for idx, item in enumerate(self.items):
            if item['path'] == path:
                index = idx
                break

        if index is None:
            issue('drag_and_drop.py: unable to item in reload_path')
            return

        item = self.items[index]
        node = item['node']
        node.invalidate_children()

        tv_objc = ObjCInstance(self.tableview)

        if node.path in self._expanded_node_paths:
            # Folder expanded, reload the whole section
            self._items = None
            index_set = NSIndexSet.alloc().initWithIndex_(self._folder_section)
            tv_objc.reloadSections_withRowAnimation_(ns(index_set), 0)
        else:
            # Not expanded, just reload folder row, so the triangle is expanded
            index_paths = []
            index_paths.append(
                NSIndexPath.indexPathForRow_inSection_(index,
                                                       self._folder_section))
            tv_objc.reloadRowsAtIndexPaths_withRowAnimation_(
                ns(index_paths), 0)
Example #7
0
    def __init__(self, **kwargs):

        self.scrollView = ui.ScrollView(
            delegate=self,
            paging_enabled=True,
            shows_horizontal_scroll_indicator=False,
            bounces=False,
            frame=self.bounds,
            flex='WH',
        )

        self.pageControl = UIPageControl.alloc().init().autorelease()
        self._target = ChangePageClass.new().autorelease()
        self._target.page_control = self
        self.pageControl.addTarget_action_forControlEvents_(
            self._target, 'changePage', 1 << 12)  #1<<12 = 4096
        self.pageControl.numberOfPages = len(self.scrollView.subviews)
        self.pageControl.currentPage = 0
        self.pageControl.hidesForSinglePage = True

        self._prev_page = 0

        super().add_subview(self.scrollView)
        ObjCInstance(self).addSubview_(self.pageControl)

        super().__init__(**kwargs)
Example #8
0
 def __init__(self):
     self.activity = ui.ActivityIndicator(name='activity', hides_when_stopped=True, style=ui.ACTIVITY_INDICATOR_STYLE_WHITE_LARGE, touch_enabled=False)
     self.activity.center = self.center
     self.add_subview(self.activity)
     self.touch_enabled = False
     self._objc = ObjCInstance(self.activity)
     self._objc.setColor_(UIColor.grayColor())
Example #9
0
 def info_cb(_blk,info):
     nonlocal nowPlayingInfo
     try:
         if info:
             nowPlayingInfo = nsDicToPyDic(ObjCInstance(info))
     finally:
         e.set()
 def hide_close(self, state=True):
     from objc_util import ObjCInstance
     v = ObjCInstance(self.view)
     # Find close button.  I'm sure this is the worst way to do it
     for x in v.subviews():
         if str(x.description()).find('UIButton >= 0'):
             x.setHidden(state)
	def hide_close(self, state=True):
		from objc_util import ObjCInstance
		v = ObjCInstance(self.view)
		for x in v.subviews():
			#if 'UIButton' in x.description():
			if str(x.description()).find('UIButton') >= 0:
				x.setHidden(state)
Example #12
0
    def __init__(self):
        global _datasource

        self.name = 'Drag & Drop'

        self.width = min(ui.get_window_size()[0] * 0.8, 700)
        self.height = ui.get_window_size()[1] * 0.8

        path = editor.get_path()
        if path:
            expanded_folder = os.path.dirname(path)
            files = tab.get_paths()
        else:
            expanded_folder = None
            files = None

        root_node = FileNode(os.path.expanduser('~/Documents'), ignore=ignore)
        _datasource = FolderPickerDataSource(root_node, expanded_folder, files)

        tv = ui.TableView(frame=self.bounds, flex='WH')
        tv.delegate = _datasource
        tv.data_source = _datasource
        tv.allows_multiple_selection = False
        tv.allows_selection = True
        tv.allows_multiple_selection_during_editing = False
        tv.allows_selection_during_editing = False
        tv.separator_color = 'clear'
        self.add_subview(tv)

        methods = [tableView_itemsForBeginningDragSession_atIndexPath_]
        protocols = ['UITableViewDragDelegate']
        DragDelegate = create_objc_class('DragDelegate',
                                         methods=methods,
                                         protocols=protocols)
        self._drag_delegate = DragDelegate.alloc().init()

        methods = [
            tableView_canHandleDropSession_,
            tableView_dropSessionDidUpdate_withDestinationIndexPath_,
            tableView_performDropWithCoordinator_
        ]
        protocols = ['UITableViewDropDelegate']
        DropDelegate = create_objc_class('DropDelegate',
                                         methods=methods,
                                         protocols=protocols)
        self._drop_delegate = DropDelegate.alloc().init()

        tv_objc = ObjCInstance(tv)
        tv_objc.setDragDelegate_(self._drag_delegate)
        tv_objc.setDropDelegate_(self._drop_delegate)

        def handle_escape():
            self.close()

        self._handlers = [
            register_key_event_handler(UIEventKeyCode.ESCAPE, handle_escape),
            register_key_event_handler(UIEventKeyCode.DOT,
                                       handle_escape,
                                       modifier=UIKeyModifier.COMMAND)
        ]
Example #13
0
 def __init__(self, player=None, pause=True, autoplay=False):
     self._pause_on_dismiss = pause
     self._objc = ObjCInstance(self)
     self._playerViewController = AVPlayerViewController.new()
     self._objc.addSubview_(self._playerViewController.view())
     self.player = player
     self._autoplay = autoplay
Example #14
0
def tableView_performDropWithCoordinator_(_self, _cmd, tv_ptr,
                                          coordinator_ptr):
    global _dropped_item_destination_path, _dropped_item_is_folder, _dropped_item_name

    coordinator = ObjCInstance(coordinator_ptr)

    index_path = coordinator.destinationIndexPath()
    if not index_path:
        return

    session = coordinator.session()

    for item in session.items():
        provider = item.itemProvider()
        name = provider.suggestedName()

        if not name:
            continue

        folder = provider.hasItemConformingToTypeIdentifier('public.folder')

        if not folder and not provider.hasItemConformingToTypeIdentifier(
                'public.data'):
            continue

        _dropped_item_destination_path = _path_items[index_path.section()][
            index_path.row()]
        _dropped_item_is_folder = folder
        _dropped_item_name = name

        provider.loadDataRepresentationForTypeIdentifier_completionHandler_(
            'public.folder' if folder else 'public.data', _load_dropped_data)
Example #15
0
def _drop_folder(data_ptr, path):
    try:
        if os.path.exists(path):
            console.alert(
                '{} exists'.format(os.path.basename(path)),
                'Do you want to replace existing {}?'.format(
                    'folder' if os.path.isdir(path) else 'file'), 'Replace')

            if os.path.isfile(path):
                os.remove(path)
            else:
                shutil.rmtree(path)

        data = ObjCInstance(data_ptr)
        zip_data = io.BytesIO(ctypes.string_at(data.bytes(), data.length()))
        zf = zipfile.ZipFile(zip_data)

        corrupted_file = zf.testzip()
        if corrupted_file:
            console.hud_alert('Corrupted ZIP file', 'error')

        zf.extractall(os.path.dirname(path))

        _datasource.reload_path(os.path.dirname(path))

        console.hud_alert('{} dropped'.format(os.path.basename(path)))

    except KeyboardInterrupt:
        pass
Example #16
0
        def handler(_cmd, _data, _error):
            global pressure
            pressure = ObjCInstance(_data).pressure()

            handler_block = ObjCBlock(handler,
                                      restype=None,
                                      argtypes=[c_void_p, c_void_p, c_void_p])

            CMAltimeter = ObjCClass('CMAltimeter')
            NSOperationQueue = ObjCClass('NSOperationQueue')
            if not CMAltimeter.isRelativeAltitudeAvailable():
                print('This device has no barometer.')
                return
            altimeter = CMAltimeter.new()
            main_q = NSOperationQueue.mainQueue()
            altimeter.startRelativeAltitudeUpdatesToQueue_withHandler_(
                main_q, handler_block)
            #print('Started altitude updates.')
            try:
                while pressure is None:
                    pass
            finally:
                altimeter.stopRelativeAltitudeUpdates()
                #print('Updates stopped.')
                return pressure
def remove_toolbar_button(index):
	global __persistent_views
	try:
		btn,action = __persistent_views.pop(index)
		btn.action= None
		ObjCInstance(btn).removeFromSuperview()
	except KeyError:
		pass
Example #18
0
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('https://google.com/search?q=' +
                          urllib.quote(term))))
def renderer_didAddNode_forAnchor_(_self, _cmd, renderer, node, anchor):
  get_anchor = repr(str(ObjCInstance(anchor)))
  center, extent, identifier = anchor_attribute(get_anchor)
  #print(center)
  #print(extent)
  #print(identifier)
  after_color = UIColor.colorWithRed_green_blue_alpha_(0.0, 0.2, 0.8, 1.0)
  view.vc.box_geometry.firstMaterial().diffuse().contents = after_color
Example #20
0
def assets_for_attachments(attachments):
    all_assets = photos.get_assets()
    matching_assets = []
    for a in all_assets:
        path = str(ObjCInstance(a).pathForOriginalFile())
        if path in attachments:
            matching_assets.append(a)
    return matching_assets
Example #21
0
    def getIcon(self, scale=2.0, form=10):

        i = UIImage._applicationIconImageForBundleIdentifier_format_scale_(
            self.appID, form, scale)
        o = ObjCInstance(i.akCGImage())
        img = UIImage.imageWithCGImage_(o)
        with BytesIO(uiimage_to_png(img)) as buffer:
            self.icon = Image.open(buffer)
Example #22
0
 def __init__(self, cla=''):
     self.obs = class_objects(cla)
     try:
         self.supercls = ObjCInstance(ObjCClass(cla).superclass())
     except AttributeError:
         self.supercls = None
     self.obs = [['Superclass', [str(self.supercls)]]] + self.obs
     pass
Example #23
0
def _blackmamba_handleKeyUIEvent(_self, _cmd, event):
    e = ObjCInstance(event)

    #    print('Down: {} Type: {} Subtype: {} Modifier flags: {} Keycode: {}'
    #        .format(e._isKeyDown(), e.type(), e.subtype(), e._modifierFlags(), e._keyCode()))

    if e.type() == UIEventTypePhysicalKeyboard and e.subtype(
    ) == 0 and not e._isKeyDown():
        for h in _key_event_handlers:
            if h.key_code == e._keyCode(
            ) and h.modifier_flags == e._modifierFlags():
                try:
                    h.fn()
                except Exception as ex:
                    print('Exception in key event handler {}'.format(h.fn))
                    print(ex)

    ObjCInstance(_self).originalhandleKeyUIEvent_(e)
Example #24
0
def chandle(result, func, args):
    '''chandle
	Handles c_void_p to objc type
	use as a errcheck for a ctypes function
	>>> cfunc.restype = c_void_p
	>>> cfunc.errcheck = chandle
	'''
    if isinstance(result, (c_void_p, int)) and result:
        return ObjCInstance(result)
Example #25
0
 def superclass(self, alloc=False):
     if 'superclass' in self.initializers:
         if ObjCClass(self.name).superclass():
             return ObjCClassInfo(
                 ObjCInstance(ObjCClass(self.name).superclass()), alloc)
         else:
             return None
     else:
         return None
 def previewControllerDidFinish_(_self, _cmd,
                                 _previewViewController):
     previewViewController = ObjCInstance(
         _previewViewController)
     rootVC = UIApplication.sharedApplication().keyWindow(
     ).rootViewController()
     on_main_thread(
         rootVC.dismissViewControllerAnimated_completion_)(True,
                                                           None)
Example #27
0
 def __init__(self):
   self.instance = ObjCInstance(self)
   self.py_audio = PyAudio(self.instance)
   self.instance.theta = 0
   self.instance.frequency = 440
   self.instance.buffer = []
   self.time_div = 120
   self.update_interval = 1 / self.time_div
   
   self.setup_ui()
Example #28
0
	def _close(self,sender):
		''' button callback. reset siblings, and close subview'''
		ObjCInstance(self).removeFromSuperview()
		for sib in self.siblings:
			f=sib.frame()
			if f.size.width==self.containerView.frame().size.width-self.width:
				f.size.width+=self.width
				sib.frame=f
		if hasattr(self,'will_close') and callable(self.will_close):
			self.will_close()
Example #29
0
def assets_for_attachments(attachments):
    all_assets = photos.get_assets()
    matching_assets = []
    for a in all_assets:
        objc_asset = ObjCInstance(a)
        path_orig = str(objc_asset.pathForOriginalFile())
        path_edit = str(objc_asset.pathForFullsizeRenderImageFile())
        if path_orig in attachments or path_edit in attachments:
            matching_assets.append(a)
    return matching_assets
Example #30
0
    def __init__(self, view):
        assert(isinstance(view, ui.View))

        self._view = view
        self.add_subview(self._view)

        self._view_objc = ObjCInstance(self._view)
        self._objc = ObjCInstance(self)

        self._view_objc.setTranslatesAutoresizingMaskIntoConstraints_(False)
        self._objc.setTranslatesAutoresizingMaskIntoConstraints_(False)

        attributes = [LayoutAttribute.left, LayoutAttribute.right, LayoutAttribute.top, LayoutAttribute.bottom]
        for attribute in attributes:
            constraint = _LayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_(
                self._view_objc, int(attribute), int(LayoutRelation.equal), self._objc, int(attribute), 1.0, 0
            )
            self._objc.addConstraint_(constraint)

        self._layout = None