Skip to content

Commit

Permalink
Now handling rotated layers again.
Browse files Browse the repository at this point in the history
This fixes #10 and #14. And also introduces a new issue with
non-pixel-aligned layers, see README for details.
  • Loading branch information
PEZ committed Feb 11, 2017
1 parent 686a89d commit c7686af
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Distributor.sketchplugin/Contents/Sketch/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
]
},
"identifier" : "com.betterthantomorrow.sketch.distributor",
"version" : "1.0.3.1b",
"version" : "1.0.4",
"description" : "Distribute selected objects vertically or horizontally with exact spacing.",
"authorEmail" : "[email protected]",
"name" : "Distributor"
Expand Down
59 changes: 39 additions & 20 deletions Distributor.sketchplugin/Contents/Sketch/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ var Distributor = {
return label;
},

trimmedLayer: function(layer) {
//return MSSliceTrimming.trimmedRectForLayerAncestry(MSImmutableLayerAncestry.ancestryWithMSLayer(layer));
return [layer rect]
offsetLayerY: function(layer, yOffset) {
layer.frame().setY(layer.frame().y() + yOffset);
},

offsetLayerX: function(layer, xOffset) {
layer.frame().setX(layer.frame().x() + xOffset);
},

trimmedRectForLayer: function(layer) {
return MSSliceTrimming.trimmedRectForLayerAncestry(MSImmutableLayerAncestry.ancestryWithMSLayer(layer));
},

createChoices: function(msg) {
Expand Down Expand Up @@ -99,28 +106,40 @@ var Distributor = {
},

distribute: function(dimension, spacingString) {
var sortedByLeft = this.sortedArray(this.selection, "frame.left"),
sortedByTop = this.sortedArray(this.selection, "frame.top"),
firstLeft = sortedByLeft[0],
left = [[firstLeft frame] left],
firstTop = sortedByTop[0],
top = [[firstTop frame] top],
formatter = [[NSNumberFormatter alloc] init],
spacing = [formatter numberFromString:spacingString];
var formatter = [[NSNumberFormatter alloc] init],
spacing = [formatter numberFromString:spacingString];

if (spacing != null) {
if (String(dimension) === "Vertically") {
var loopV = [sortedByTop objectEnumerator];
while (layer = [loopV nextObject]) {
[[layer frame] setTop:(top + ([[layer frame] top] - CGRectGetMinY(Distributor.trimmedLayer(layer))))];
top = CGRectGetMinY(Distributor.trimmedLayer(layer)) + CGRectGetHeight(Distributor.trimmedLayer(layer)) + spacing;
if (String(dimension) == "Horizontally") {
var sortedByLeft = this.sortedArray(this.selection, "frame.left"),
loopH = [sortedByLeft objectEnumerator]
firstH = [loopH nextObject],
trimmedLayerRect = Distributor.trimmedRectForLayer(firstH),
trimmedLeft = CGRectGetMinX(trimmedLayerRect),
lastTrimmedRight = trimmedLeft + CGRectGetWidth(trimmedLayerRect);
while (layer = [loopH nextObject]) {
trimmedLayerRect = Distributor.trimmedRectForLayer(layer);
trimmedLeft = CGRectGetMinX(trimmedLayerRect);
Distributor.offsetLayerX(layer, lastTrimmedRight - trimmedLeft + spacing);
trimmedLayerRect = Distributor.trimmedRectForLayer(layer);
trimmedLeft = CGRectGetMinX(trimmedLayerRect);
lastTrimmedRight = trimmedLeft + CGRectGetWidth(trimmedLayerRect);
});
}
else {
var loopH = [sortedByLeft objectEnumerator];
while (layer = [loopH nextObject]) {
[[layer frame] setLeft:(left + ([[layer frame] left] - CGRectGetMinX(Distributor.trimmedLayer(layer))))];
left = CGRectGetMinX(Distributor.trimmedLayer(layer)) + CGRectGetWidth(Distributor.trimmedLayer(layer)) + spacing;
var sortedByTop = this.sortedArray(this.selection, "frame.top"),
loopV = [sortedByTop objectEnumerator]
firstV = [loopV nextObject],
trimmedLayerRect = Distributor.trimmedRectForLayer(firstV),
trimmedTop = CGRectGetMinY(trimmedLayerRect),
lastTrimmedBottom = trimmedTop + CGRectGetHeight(trimmedLayerRect);
while (layer = [loopV nextObject]) {
trimmedLayerRect = Distributor.trimmedRectForLayer(layer);
trimmedTop = CGRectGetMinY(trimmedLayerRect);
Distributor.offsetLayerY(layer, lastTrimmedBottom - trimmedTop + spacing);
trimmedLayerRect = Distributor.trimmedRectForLayer(layer);
trimmedTop = CGRectGetMinY(trimmedLayerRect);
lastTrimmedBottom = trimmedTop + CGRectGetHeight(trimmedLayerRect);
});
}
}
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ Note that you can use negative spacing to get overlap:

## Bugs / Comments / Questions / Suggestions

Is the plugin misbehaving? File an issue. Have a suggestion? File an issue. You can pretty much file an issue for whatever reason. There is also:
Is the plugin misbehaving? File an issue. Have a suggestion? File an issue. You can pretty much file an issue for whatever reason. Please include the version of the plugin you are using if you think it could be relevant (it most often is). There is also:

* A low-traffic chat room over at [![Join the chat at https://gitter.im/PEZ/SketchDistributor](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/PEZ/SketchDistributor?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
* Twitter: [@PappaPEZ](https://twitter.com/pappapez)

### Known Issues

The current version of the plugin will truncate any decimal points of the positions of the distributed objects. Say for instance that the leftmost layer in the selection is at X position `100.12` and you distribute horizontally with, say, `10` pixels. Then you would expect the following objects to be at `something.12` X positions after distribution, right? But they won't. They will be at `.0` X positions. I hope to be able to fix this issue soon. Most people use Sketch for pixel aligned stuff anyway, I think.

## Sketch Community Attention

This little plugin is surprisingly often present in various **Top X Sketch Productivity plugins** articles. Too many to list here, but here are a few that got me smiling and almost bursting:
Expand Down

0 comments on commit c7686af

Please sign in to comment.