+/*
+ * Calculate the given container's new percent given a change in pixels.
+ *
+ */
+double px_resize_to_percent(Con *con, int px_diff) {
+ Con *parent = con->parent;
+ const orientation_t o = con_orientation(parent);
+ const int total = (o == HORIZ ? parent->rect.width : parent->rect.height);
+ /* deco_rect.height is subtracted from each child in render_con_split */
+ const int target = px_diff + (o == HORIZ ? con->rect.width : con->rect.height + con->deco_rect.height);
+ return ((double)target / (double)total);
+}
+
+/*
+ * Calculate the minimum percent needed for the given container to be at least 1
+ * pixel.
+ *
+ */
+double percent_for_1px(Con *con) {
+ Con *parent = con->parent;
+ const orientation_t o = con_orientation(parent);
+ const int total = (o == HORIZ ? parent->rect.width : parent->rect.height);
+ const int target = (o == HORIZ ? 1 : 1 + con->deco_rect.height);
+ return ((double)target / (double)total);
+}
+
+/*
+ * Resize the two given containers using the given amount of pixels or
+ * percentage points. One of the two needs to be 0. A positive amount means
+ * growing the first container while a negative means shrinking it.
+ * Returns false when the resize would result in one of the two containers
+ * having less than 1 pixel of size.
+ *
+ */
+bool resize_neighboring_cons(Con *first, Con *second, int px, int ppt) {
+ assert(px * ppt == 0);
+
+ Con *parent = first->parent;
+ double new_first_percent;
+ double new_second_percent;
+ if (ppt) {
+ new_first_percent = first->percent + ((double)ppt / 100.0);
+ new_second_percent = second->percent - ((double)ppt / 100.0);
+ } else {
+ new_first_percent = px_resize_to_percent(first, px);
+ new_second_percent = second->percent + first->percent - new_first_percent;
+ }
+ /* Ensure that no container will be less than 1 pixel in the resizing
+ * direction. */
+ if (new_first_percent < percent_for_1px(first) || new_second_percent < percent_for_1px(second)) {
+ return false;
+ }
+
+ first->percent = new_first_percent;
+ second->percent = new_second_percent;
+ con_fix_percent(parent);
+ return true;
+}