Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Notification/Callback/Hooks when ui.View is presented or closed?
-
So, another question I've come upon while exploring how to make UIs with Pythonista: is there any way to find out when a view is presented or closed? I mean is there some way to hook a callback or attach a notification to when a non-custom ui.View is shown with present() or closed with close()?
Related: when showing a view as a sheet or panel, or even full screen, there can be a "X" button which will close the view, but we don't have direct access to that titlebar or button, do we? There's no way to intercept the action when that is pressed? It's not a superview of the main displayed view, I looked for that.
Thoughts?
-
you can implement
update
that checkson_screen
. Also, the custom viewwill_close
gets called when the view will close. but i think only for the root view that was presented. I thinklayout
usually gets called when first presented... but maybe only for flex views. -
@JonB thanks...I was definitely thinking about doing it via update() with a delay of zero, and checking for on_screen. This is for non-custom views, since custom views definitely have the methods needed to know with did_load() and will_close(), etc.
I'm wondering if this is something that I really need to dive into the objc_util system to get notifications via the NSNotificationCenter system for observing objects. Or even the lower-level NSKeyValueObserving system. Has anyone has experience with that? I'm not up to speed on converting objective-c calling style to the python equivalents.
-
what specifically are you going for? are you interested in just 'sheet', or other types of present?
Somewhere around here, someone used objc to hijack the "X" button..
-
Mainly, I will need to be able to present a non-modal UI, most likely as a sheet, and have a different part of the UI be notified in some fashion when it closes.
I could, of course, implement my own notification system of some form, and use a custom view class for the sheet I present, so I have access to will_close()...but I was hoping not to have to make a custom view for this. Still, if that's the easiest way, perhaps I will.
I'm curious if anyone has actually hooked into the "normal" objective-C/Swift way of sending notifications around using the NSNotificationCenter via objc_util? Since that would make it pretty easy to attach notifications to changes in views.
-
@shinyformica A search of NSNotificationCenter in this forum shows some topics, like this one
-
Here is a way to capture all notifications:
https://github.com/jsbain/objc_hacks/blob/master/notification_capture.pyThis could be modified to observe a specific object, selector, etc. I believe there is a notification when a view is shown, but i didnt see an obvious one when the X is pushed.
The confusing thing about what you are asking is that
sheet
is already basically modal, since it blocks anything else happening. in which casewait_modal
might be what you want.For truly non-modal "windows", you might consider the Overlay, found here. This lets you create resizable views that float, and you can use
connect
to register actions that get called when the view is closed.Finally, it would be possible to delete the standard X, and replace it with your own button.
v.objc_instance.superview().superview().superview().subviews()[1].subviews()[1]
gives me the X button on ipad when presenting as sheet. This might be different on iPhone, and might be different if you have leftbuttonitems or rightbuttonitems defined.
But this lets you, for instance, hide the default X, and define your own using right_button_items.
v=ui.View() def c(sender): v.close() print('closed') v.right_button_items=[ui.ButtonItem(action=c,title='X')] v.present('sheet') v.objc_instance.superview().superview().superview().subviews()[1].subviews()[1].hidden=True