Bounds vrs Frame Center etc in ui
@omz, are the concepts of frames and bounds , Center not 100% correct?
Look if it's a little broken that's ok. But I am never sure if it is just me, oR I just don't understand your coord system. Maybe there is some missing documentation.
But to me, center for example seems wrong. If I set a buttons center to the Center of its view, it's off by the menu height / 2. The magic 44 number.
But it would be great to know if it still has kinks or I just misunderstand how it works.
@JonB , ok I will take some time to reflect a little and try and get my head around it. Maybe I will get the Eureka moment. I hope so. Frustrates the hell out of me. But I am always open I am just wrong as it turns out many times I am.
Actually, I guess the confusion is, perhaps, that the title bar is not part of your view's height. your button actually was centered (due to luck though, that would only have worked in init where you set your frame x and y to 0), but it looked off-center because of the title bar.
Since you cannot place anything in the title bar, it makes sense that it is not part of your view's content, otherwise you would have to layout your view differently depending on whether the bar is there or not. The idea here is that 0,0 starts at the top of your view's editable content.
My recollection of buggy resizing was actually for popover, not sheet. Sheet does resize to 576 if the view is too large, such that the title plus height exceed screen height, but it is perfectly happy to show a small view.
import ui button = ui.Button(title='button') button.present(hide_title_bar=True) print('center', button.center) print('bounds', button.bounds, button.bounds.center()) print(' frame', button.frame, button.frame.center())
# hide_title_bar=False ('center', Point(512.00, 416.00)) ('bounds', Rect(0.00, 0.00, 1024.00, 704.00), Point(512.00, 352.00)) (' frame', Rect(0.00, 64.00, 1024.00, 704.00), Point(512.00, 416.00)) # hide_title_bar=True ('center', Point(512.00, 384.00)) ('bounds', Rect(0.00, 0.00, 1024.00, 768.00), Point(512.00, 384.00)) (' frame', Rect(0.00, 0.00, 1024.00, 768.00), Point(512.00, 384.00))
phuket's question was about sheet, where you do have control of the frame.
In full screen, your frame gets... the full screen minus whatever title bar is presented. The values here look to be correct, and the way you would expect them to be.
fullscreen has its own issues, for instance convert_point does not work properly in most orientations.
Understood. My message was that printing out
Also that it is possible that
view.center != view.bounds.center() != view.frame.center().
Look, I will still reflect on this. But every time I have mentioned a preflight (for the want of a better word) it's just ignored. I just see this as the most logical answer. Yes we can do all our set up after present, but it doesn't make sense, and apparently it doesn't make sense to to it before.
In mind it's so simple. We have no idea what @omz display algorithm will size the eventual ui.View at. So, if we can call a function/ method with the present details, he just returns information about the size he will create without creating anything, when we end up calling present. I honestly can not see how it will be ever resolved otherwise.
Again if I am wrong , I am wrong. But I feel strongly about this point
A few last points. I know it works out if you use layout . But that's the only way.
Unless you use layout callback, it can never work. I suspect this is the way it was crafted in the first place. I don't even need to understand Python to know I don't know what I don't know.
If the end point for the view size/bounds whatever is present, then without using layout the only logical way is to position all your objects after present is called.
I have never seen any sample code here like that. I am happy to do it that way if that's the way.
But again, Center is only one issue. And yes, I understand I could have just misunderstood it. But I think the root problem is that the final size of your ui.View is unknown. Even if it was, next year, a new device and @omz modifies his algorithm, back to square one
We have no idea what @omz display algorithm will size the eventual ui.View at.
We do when
From the ui docs...
def layout(self): # This will be called when a view is resized. You should typically set the # frames of the view's subviews here, if your layout requirements cannot # be fulfilled with the standard auto-resizing (flex) attribute. pass
As I have written before, I used to avoid ui.View.layout() but now I embrace it. Those fleeting moments of sanity are a beautiful thing!
@ccc , really? Look, I understand if you write anything close to important you need to use layout. For orientation etc.. Doesn't mean the rest of it should be broken.
Again, I don't understand why my simple premise is not embrassed. This can all work. Just need to be able to call something like bounds = present.preflight('sheet')
Again, I don't get it... It's just so logical
For sheet, you know exactly what size your view will be (unless you make it too large). You can use layout() as a "preflight", or to call a preflight function, it is called whenever something is resized, use a flag if you only want it to run once, though be careful that you don't change the frame after creation. Or, use the flex attributes. For instance if you set btn.flex='lrtb' your original code would keep the button centered in all view types.
If you want to make ui's that are orientation friendly, you are forced to use layout or flex anyway, anything you hard code based in some preflight will be broken anyway once you rotate the device.
How would preflight know how you plan in presenting your view, and in what orientation?
It would be nice if the title bar sizes for the various modes and devices was documented, though more for testing what your ui looks like in a small device.
@JonB , well preflight could return a tuple for horizontal and vertical frames or bounds or whatever they are.
Again, I will reiterate, we can't know what we don't know. But Pytonista knows what is going to do. We just need to be able to ask it without it doing anything other than returning the results of the sizes of what it is going to based on what we are passing to it.
Apple Watch is an example.
I would put money on the way to resolve this issue is a preflight method. All the code is there already. It also makes omz more device independent from the api.
Here you go. I tested on iPad, but you should test this on iPhones, I believe the landscape title bar is only 32 instead of 64, as mentioned in the referenced thread.
For sheet, this function returns the max size... if you exceed width or height, presenting as sheet will result in a 576 square. Maybe this function should take a desired frame as an input, returning final frame size. Also, technically, sheet doesn't work on iPhones, so perhaps that should be sensed and accounted for here. These improvements are left as an exercise to the reader :)
Again, I can think of only a very few cases where this cannot be done, and done better with either flex or layout. I will admit flex can be annoying, as it takes many layered views to do anything complex. Frankly, for simple throwaways, I usually present first, the 50 msec of dark while the view sets up is not a concern.
At one time I started working on a set of Layout Managers, see my uicomponents git, BoxLayout and uicontainers (which has a FlowLayout container), that would make this easier. the major stumbling block was the lack of min/max component size attributes. Now that we can set arbitrary attributes, it would be possible to implement real layout managers, that let you specify desired size, and min/max sizes of components.
(edit fixed a few typos and slight mod to the code)
import objc_util def get_presented_size(mode,hide_title_bar=False): ''' see https://forum.omz-software.com/topic/1618/any-ios-device/7''' f=objc_util.ObjCClass('UIApplication').sharedApplication().keyWindow().frame() sz= ui.Size(f.size.width,f.size.height) if sz<=320: status_height=0 title_height=32 else: status_height=20 title_height=44 if mode=='sheet': maxsize=min( sz-(0,title_height*(not hide_title_bar))) return ui.Size(maxsize,maxsize) elif mode=='panel': return sz-(0,status_height+(title_height*(not hide_title_bar))) elif mode=='fullscreen': return sz-(0,(title_height*(not hide_title_bar)))
@JonB , nice. I am going to name it preflight 😀😱💋
But thanks, its really nice.
My mind is still doing back flips, as this conversation always confuses the hell out of me. Just when I think I have worked it out, I do something that undoes my thinking...
But, the mode 'sheet' does not work correctly on an iPad Pro.
If you do same as the others,
return sz-(0,status_height+(title_height*(not hide_title_bar))) It works. But I am sure done for a reason the way it is so I am sure something else must be done.
It would be nice if you could also pass your intended frame or (w/h) to the function so you are not getting the maximums but the real size. That will be presented.
Of course this can get effected if you ask for a size not support on a device.
But I don't trust my self, to tinker with code.
Feel free to use or modify this, as long as you don't call it preflight :)
I'll have to try this again (admittedly I didn't test sheet in this final form). For the 768x1024 devices, this should return something like 768x768 when title bar is hidden, and 704x704 when it is not. From what I can deduce, sheet present modes have to work in both orientations, so your size is limited to the narrowest dimension of your screen, minus the title bar height (if title bar is shown).
Can you run my little script in https://forum.omz-software.com/topic/1618/any-ios-device/7 and tell me what you get in both orientations on your Ipad Pro? What did this script return? I suspect my logic could be off for some of the newer iPhones as well.
@JonB , lol...deal
('full_screen', True) 0 (0.00, 0.00, 1024.00, 1366.00)
('full_screen', False) 0 (0.00, 64.00, 1024.00, 1302.00)
('panel', True) 0 (0.00, 0.00, 1024.00, 1270.00)
('panel', False) 0 (0.00, 0.00, 1024.00, 1270.00)
Will send iPhone 6s soon
('full_screen', True) 0 (0.00, 0.00, 375.00, 667.00)
('full_screen', False) 0 (0.00, 64.00, 375.00, 603.00)
('panel', True) 0 (0.00, 0.00, 375.00, 579.00)
('panel', False) 0 (0.00, 0.00, 375.00, 579.00)
@JonB , do you need it done on iPad Air 2?
I am pretty sure I concede. I thought if we knew the sizes before present we could force ui.View to do the right thing. I have been testing and testing...
Without present being called you just can't get it right. Well from what I can see.
You can say layout, but it does not matter. Layout is being called after present. So same thing.
So I concede, I am wrong. The magic preflight fixes nothing. It's in the Pythonista code.
I get it now.
Either you have a custom class and support layout, or if you are making a view ad hoc to display, to be 100% you need to present it first.
Not ideal, but that is the way it is I guess.
I can't remember the old Mac toolbox api , but we had something like suspend drawing. But basically, you could turn off any screen rendering. But the so called 'view'/window was already created.
It was like a transaction (I think I mentioned it last night)
But was something like
Do your stuff
But It could be
v.present('sheet', suspend_upates = True)
v.resume_updates = True
Or v.suspend_updates = false # causes a redraw
Anyway something like that. Just an illustration
Ui.animate does that. Anything you put inside the function sent to ui.animate will happen at once. Just set the time to 0 to have it happen instantly. IIRC anything happening in the main ui thread also will appear instantly(so inside a non backgrounded button action f
Is your view initting so complicated that you can see the subviews getting added, etc?or example), or perhaps using on_main_thread.