Layouts rework
This issue tracks the progress of reworking the internals of layouts to reduce complexity and enable more features. It is currently in the planning phase; please don't hesitate to provide feedback! This document will be updated consistently to reflect the current state of development.
From the Contributing TODO list:
- Better layouts. For example it would be good if they were driven by hints rather than fixed positions and could be automatically reapplied after resize/split/kill. Pane options can be used.Motivation
As we begin to add more complex layout features, the burden of using fixed positions and sizes at each point of layout manipulation is becoming overwhelming. Moving to size hints will reduce complexity and open up more manipulation options for layouts for both users and developers.
Plan
This rework will happen in of stages to ensure there are no regressions. The idea is to have the tiled layout state represented more simply and contain cell geometry calculations to a single path so that more complex states can be tracked and applied. The cell calculations will at most be called once per server loop. The rework will start by transitioning to storing and using single values with a single layout calculation call site instead of doing complete calculations at each, and then adding size hints/constraints/policies over time. Below is an idea of how to transition away from the current system..
[IN PROGRESS] Write regression tests to help validate that no regressions have occured.Will need to cover all layout state changing commands with a variety of configurations, and selecting layouts.
- configurations: all rows, all columns, 2x2, middle splits. (with floats over the top)
- validations: top/bottom border validation, float/tile a single pane/entire nodes into the same or differing positions, resizing panes, -f flag.
The only new state that needs to be added to `struct layout_cell` at this point is a new field that tracks the percentage a cell occupies in the direction of the parents layout type (sometimes referred to as the 'split' size of the cell). Operations that change layout state would only need to work on this single value instead of calculating everything on the spot. For example, splitting a pane in half which covers 50 grid cells of the containers 'split' dimension would result in the value changing from 50 to 25 in the target cell, and a new adjacent cell with 25. The window's sizes are used as the initial seed of calculating layout geometry. Here is a sketch of what that calculation could look like:
/* Example of new layout_cell field. */
struct layout_cell {
...
u_int split; /* <- new */
...
};
/*
* Recursively called on layout nodes. initially called on the layout root with the
* window's dimensions. A key detail is that changes in child cell sizes that
* impact its parent's `split` are propagated to the parent, whos `split` is
* the actual one impacted. In this way, we only need to track the `split` for a
* layout cell's 'split' size. Currently, this function assumes that the
* `split` field values are correct. Eventually, size hint/constraint/policy logic
* will be added.
*/
void
layout_populate_geometry(struct layout_cell *lc, u_int sx, u_int sy, u_int ox,
u_int oy)
{
struct layout_cell *lcchild;
u_int offset;
/* Case when the layout root is a single pane */
if (lc->type == LAYOUT_WINDOW_PANE) {
layout_size_set(lc, sx, sy, ox, oy);
return;
}
/* Setting the dimensions of each child cell. */
if (lc->type = LAYOUT_TOPBOTTOM) {
offset = oy;
TAILQ_FOREACH(lcchild, &lc->cells, entry) {
if (!layout_cell_is_tiled(lcchild))
continue;
layout_set_size(lcchild, sx, lcchild->split, ox, offset);
offset += lcchild->sy + 1;
}
/* hint/constraint/policy checking and adjustment here */
} else {
offset = ox;
TAILQ_FOREACH(lcchild, &lc->cells, entry) {
if (!layout_cell_is_tiled(lcchild))
continue;
layout_set_size(lcchild, lcchild->split, sy, offset, oy);
offset += lcchild->sx + 1;
}
/* hint/constraint/policy checking and adjustment here */
}
/*
* Recursing on children that are also nodes. Note how the
* recursion bounds child's dimensions to the geometry of
* the parent node.
*/
TAILQ_FOREACH(lcchild, &lc->cells, entry) {
if (lcchild->type != LAYOUT_WINDOW_PANE)
layout_populate_geometry(lcchild, lc->sx, lc->sy,
lc->xoff, lc->yoff);
}
}A good first place to incorporate the logic may be in cmd-split-window.c. The main criteria for choosing is making the fewest number of changes with easy verifiability. cmd-split-window.c is nice in that it is easily verifiable, though a better choice may exist. This is accomplished by changing the logic in all the called paths to use hints, and then finishing the command with a call to the geometry propagation function. Some of the called logic may need full rewrites in the interim or totally depending on how coupled the paths are with other commands. We then go command by command hooking up the logic and testing until there is full coverage. At this point there may be extraneous calls to recalculate geometry which we would like to eliminate. The next step addresses this.
I'm thinking that a flag is set (new window/client? or maybe add it to the CLIENT_REDRAW* flag flow), and then the geometry is calculated before any screen buffers are populated. I am not knowledgeable enough on how this happens currently across all calls, so there may need to be more centralization of draw logic so that these two things can happen in sequence. This could end up being a larger step than I am anticipating.
And beyond...This is when we look towards adding more size hint features. The first thing that comes to mind is minimums/maximums for pane sx/sy and possibly up to something like dynamic float sizing with padding. The advantage to how we have organized things up until this point is that there is a single function can be adjusted to handle updates to the hint/geometry calculation logic. I also foresee this enabling fully customizable user-defined layouts that behave similarly to tiling window managers as layout state will more easily be represented, adjusted, and swapped between.
Other considerations
There are many details to work out which will likely be fully worked through during implementation and testing, like what happens to the redraw queue? Hopefully we can compile a record of the major considerations prior to breaking ground.
Source: tmux/tmux