kasper / phoenix

A lightweight macOS window and app manager scriptable with JavaScript
https://kasper.github.io/phoenix/
Other
4.39k stars 128 forks source link

Cannot extend Rectangle? #273

Closed rcarmo closed 3 years ago

rcarmo commented 3 years ago

So I'm trying to extend Rectangle:

Rectangle.prototype.pad = () => {
    return new Rectangle(
        this.x + PADDING/2, 
        this.y + PADDING/2,
        this.width - PADDING,
        this.height - PADDING
    );
};

... and I get ReferenceError: Can't find variable: Rectangle (Line:Column)

Since I can hack Screen, shouldn't I be able to do the same for Rectangle, so that I can take a Window.frame and manipulate it directly like this?

kasper commented 3 years ago

Rectangle is NSRect (https://developer.apple.com/documentation/foundation/nsrect) under the hood. It’s a struct and not an object, so that’s the reason you get the error. I’m surprised nobody else has noticed this from documentation before. 😄 I’ll fix that.

rcarmo commented 3 years ago

Ah, ok. So I can't override it and do nice things like I do for Window and Screen. Hmmm.

kasper commented 3 years ago

@rcarmo You can of course pass it to functions, in a more functional approach.

const pad = (rect) => ({
  ...rect,
  x: rect.x + PADDING,
});
rcarmo commented 3 years ago

Yeah, I wanted to make it smarter. Am now using something called a Frame, which can .displace, .pad and .snap, so I can do neat stuff like foo.snap(screen, WEST).pad()

kasper commented 3 years ago

I’ve clarified the documentation in this regard.