Skip to content

Commit

Permalink
split panes: rebalance sizes on insert/removal
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmartin committed Oct 2, 2016
1 parent cc59137 commit c0c6a21
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions lib/reducers/term-groups.js
Expand Up @@ -38,6 +38,24 @@ const setActiveGroup = (state, action) => {
.setIn(['activeSessions', rootGroup.uid], action.uid);
};

// Reduce existing sizes to fit a new split:
const insertRebalance = (oldSizes, index) => {
const newSize = 1 / (oldSizes.length + 1);
// We spread out how much each pane should be reduced
// with based on their existing size:
const balanced = oldSizes.map(size => size - (newSize * size));
return [...balanced.slice(0, index), newSize, ...balanced.slice(index)];
};

// Spread out the removed size to all the existing sizes:
const removalRebalance = (oldSizes, index) => {
const removedSize = oldSizes[index];
const increase = removedSize / (oldSizes.length - 1);
return oldSizes
.filter((_size, i) => i !== index)
.map(size => size + increase);
};

const splitGroup = (state, action) => {
const { splitDirection, uid, activeUid } = action;
const activeGroup = findBySession(state, activeUid);
Expand Down Expand Up @@ -84,11 +102,17 @@ const splitGroup = (state, action) => {
// Insert the new child pane right after the active one:
const index = children.indexOf(activeGroup.uid) + 1;
const newChildren = [...children.slice(0, index), newSession.uid, ...children.slice(index)];
return state
.setIn(['termGroups', parentGroup.uid], parentGroup.merge({
direction: splitDirection,
children: newChildren
}));
state = state.setIn(['termGroups', parentGroup.uid], parentGroup.merge({
direction: splitDirection,
children: newChildren
}));

if (parentGroup.sizes) {
const newSizes = insertRebalance(parentGroup.sizes, index);
state = state.setIn(['termGroups', parentGroup.uid, 'sizes'], newSizes);
}

return state;
};

// Replace the parent by the given child in the tree,
Expand Down Expand Up @@ -134,6 +158,11 @@ const removeGroup = (state, uid) => {
state = replaceParent(state, parent, child);
} else {
state = state.setIn(['termGroups', group.parentUid, 'children'], newChildren);
if (parent.sizes) {
const childIndex = parent.children.indexOf(uid);
const newSizes = removalRebalance(parent.sizes, childIndex);
state = state.setIn(['termGroups', group.parentUid, 'sizes'], newSizes);
}
}
}

Expand Down

0 comments on commit c0c6a21

Please sign in to comment.