PEZ / SketchDistributor

Sketch plugin that distributes selected objects vertically or horizontally with a given spacing
MIT License
354 stars 26 forks source link

Sketch 46.1: Folder Retains Original Dimensions After Distribute #24

Closed VigazKhw4pP closed 6 years ago

VigazKhw4pP commented 7 years ago

Not sure if this a problem with Sketch or with SketchDistributor but if you distribute multiple objects within a group, the folder retains the original dimensions instead of adjusting to the new dimensions of the objects within.

image

PS — I love love love this plugin. It is as essential to me as aligning objects. I would honestly put shortcuts in the main menubar for SketchDistributor if I could.

PEZ commented 7 years ago

Thanks for this feedback. I'll investigate how to fix this.

DWilliames commented 6 years ago

Hey @PEZ, I know this was a while ago; but I was working on a Sketch Plugin recently that had a similar issue.

After a bunch of looking around, I found a solution — so thought I'd share it with you too.

The trick is to run layerDidEndResize() on the group.

I tried it in your plugin and it seems to fix the bug.

distribute: function(dimension, spacingString) {
        var formatter = [[NSNumberFormatter alloc] init],
            spacing   = [formatter numberFromString:spacingString];

        if (spacing != null) {
            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);

                    // The new bit I added here to fix it
                    if (layer.parentGroup()) layer.parentGroup().layerDidEndResize();
                });
            }
            else {
                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);

                    // The new bit I added here to fix it
                    if (layer.parentGroup()) layer.parentGroup().layerDidEndResize();
                });
            }
        }
        else {
            log("Wrong number format for spacing: " + spacingString);
            [(this.app) displayDialog:("Wrong number format for spacing: " + spacingString) withTitle:"Distributor is sad"];
        }
    }
PEZ commented 6 years ago

Thanks a lot, @DWilliames!

PEZ commented 6 years ago

Alright, with @DWilliames help I have now fixed this bug, as far as I can see. Please let me know if it is not fixed by reopening the issue.

PEZ commented 6 years ago

And, many thanks for the feedback again, @christoyc. As well as the praise for the plugin. It is awesome to know people use it and love it.