Pythonista UI constraints driven by the Key Value Observing (KVO) protocol
Project description
Pythonista UI constraints driven by the Key Value Observing (KVO) protocol
Installation
pip install pythonista-anchors
History
First version of UI constraints for Pythonista was created as a wrapper around Apple NSLayoutConstraint class. While functional, it suffered from the same restrictions as the underlying Apple class, and was somewhat inconvenient to develop with, due to the "either frames or constraints" mindset and some mystical crashes.
Second version was built on top of the scripter, utilizing the ui.View
update
method that gets called several times a second. Constraints could now be designed freely, and this version acted as an excellent proof of concept. There was the obvious performance concern due to the constraints being checked constantly, even if nothing was happening in the UI – easily few thousand small checks per second for a realistic UI.
This version replaces the update
method with the KVO (Key Value Observing) protocol, running the constraint checks only when the position or the frame of the view changes. Thus we avoid the performance overhead while retaining the freedom of custom constraints.
Usage
Examples in this section assume that you have imported anchors:
from anchors import *
To cover the main anchor features, please refer to this picture with handy numbering:
Features:
-
at
is the basic workhorse. Applied to views, you can set layout constraints that hold when the UI otherwise changes. In the example, to fix the left edge of the blue viewfix_left
to the vertical bar:at(fix_left).left = at(vertical_bar).right
Now, if the vertical bar moves for some reason, our fixed view goes right along.
-
The small gap between the view and the vertical bar is an Apple Standard gap of 8 points, and it is included between anchored views by default. You can add any modifiers to an anchor to change this gap. If you specifically want the views to be flush against each other, there is a constant for that, as demonstrated by our second example view,
flex
:at(flex).left = at(vertical_bar).right + At.TIGHT
There is an option of changing the gap between views globally, to
0
if you want, by setting theAt.gap
class variable at the top of your program. -
If we fix the other end of the view as well, the view width is adjusted as needed. The example demonstrates fixing the other end to the edge of the containing view, which looks very similar to the previous examples:
at(flex).right = at(containing_view).right
The full set of
at
attributes is:- Edges:
left, right, top, bottom
- Center:
center, center_x, center_y
- Size:
width, height, size
- Position:
position
- Position and size:
frame, bounds
- "Exotics":
heading, fit_size, fit_width, fit_height
Instead of the
at
function on the right, you can also provide a constant or a function:at(vertical_bar).center_y = at(at_area).center_y at(vertical_bar).top = 30
With the center fixed, this effectively means that the top and the bottom of the vertical bar are always 30 pixels away from the edges of the superview.
If you use just a function in your constraint, it should not expect any parameters.
- Edges:
-
As an experiment in what can be done beyond the previous Apple's UI constraint implementation, there is an anchor that will keep a view pointed to the center of another view:
at(pointer).heading = at(target).center
Implementation assumes that the pointer is initially pointed to 0, or right. If your graphic is actually initially pointed to, say, down, you can make the
heading
constraint work by telling it how much the initial angle needs to be adjusted (in radians):at(pointer).heading_adjustment = -math.pi/2
would mean a 90 degree turn counterclockwise. -
Generalizing the basic anchor idea, I included an
attr
function that can be used to "anchor" any attribute of any object. In the example, we anchor thetext
attribute of a nearby label to always show the heading of the pointer. Because the heading is a number and the label expects a string, there is an option of including a translation function like this:attr(heading_label).text = at(pointer).heading + str
Actually, since the plain radians look a bit ugly, a little bit more complicated conversion is needed:
attr(heading_label).text = at(pointer).heading + ( lambda heading: f'{int(math.degrees(heading))%360:03}°' )
Because Key Value Observing cannot in general be applied to random attributes,
attr()
is useful as a target only (i.e. on the left), as then it will be updated when the source (on the right) changes. -
Docking or placing a view in some corner or some other position relative to its superview is very common. Thus there is a
dock
convenience function specifically for that purpose. For example, to attach thetop_center_view
to the top center of thecontainer
view:dock(top_center_view).top_center(container)
Full set of superview docking functions is:
all, bottom, top, right, left
top_left, top_right, bottom_left, bottom_right
sides
(left and right),vertical
(top and bottom)top_center, bottom_center, left_center, right_center
center
For your convenience,
dock
will also add the view as a subview of the container. -
Often, it is convenient to set the same anchor for several views at once, or just not repeat the anchor name when it is the same for both the source and the target.
align
function helps with this, in this example aligning all the labels in thelabels
array with the vertical center of the container view:align(*labels).center_y(container)
align
attributes match all theat
attributes for which the concept of setting several anchors at once may make sense:left, right, top, bottom, center, center_x, center_y, width, height, position, size, frame, bounds, heading
. -
Filling an area with similarly-sized containers can be done with the
fill
function. In the example we create, in thecontent_area
superview, 4 areas in 2 columns:fill_with( at_area, dock_area, pointer_area, align_area, ).from_top(content_area, count=2)
Default value of
count=1
fills a single column or row. Filling can be started from any of the major directions:from_top, from_bottom, from_left, from_right
. -
flow
function is another layout helper that lets you add a number of views which get placed side by side, then wrapped at the end of the row, mimicking the basic LTR text flow when you start from the top left, like in the example:flow(*buttons).from_top_left(button_area)
The full set of flow functions support starting from different corners and flowing either horizontally or vertically:
- Horizontal:
from_top_left, from_bottom_left, from_top_right, from_bottom_right
- Vertical:
from_left_down, from_right_down, from_left_up, from_right_up
- Horizontal:
-
For sizing a superview according to its contents, you can use the
fit_size, fit_width
orfit_height
anchor attributes. In our example we make the button container resize according to how much space the buttons need (with the content area above stretching to take up any available space):at(button_area).height = at(button_area).fit_height at(content_area).bottom = at(button_area).top - At.TIGHT
If you use this, it is good to consider how volatile your anchors make the views within the container. For example, you cannot have a vertical
flow
in a view that automatically resizes its height. -
With the KVO "engine", there is a millisecond timing issue that means that the safe area layout guides are not immediately available after you have
present
ed the root view. Thus the anchors cannot rely on them.Thus
anchors
provides aSafeAreaView
that never overlaps the non-safe areas of your device display. You use it like you would a standardui.View
, as a part of your view hierarchy or as the root:root = SafeAreaView( name='root', background_color='black', ) root.present('fullscreen', hide_title_bar=True, animated=False, )
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pythonista-anchors-2020.10.22.tar.gz
.
File metadata
- Download URL: pythonista-anchors-2020.10.22.tar.gz
- Upload date:
- Size: 15.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.22.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2997ff0fdfb3369b310d9a942d392b7716051d67939d835330dabb4de225b6f4 |
|
MD5 | c953ec112e11e8272008482195443386 |
|
BLAKE2b-256 | 114c91a4b9a91c9f8178bff345b1a9416ee0b937c4be86cf2a10061f54b5d142 |
File details
Details for the file pythonista_anchors-2020.10.22-py2.py3-none-any.whl
.
File metadata
- Download URL: pythonista_anchors-2020.10.22-py2.py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.22.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7feedc76e4cce0c09ae0245503f50eb3af348a345b4d8279d8dd251c35ff4f2a |
|
MD5 | 1c35287a0d60228506d8181e77fe9ae8 |
|
BLAKE2b-256 | 46c3001252be45ae537abfe29627b63682e7245d015ee4d25162a4b26de9eced |