Skip to content

Instantly share code, notes, and snippets.

View matthewreagan's full-sized avatar

Matt Reagan matthewreagan

View GitHub Profile
@matthewreagan
matthewreagan / gist:8760631
Last active August 29, 2015 13:55
-isKindOfClass Macro Shortcut
#define EXPECT(anObj, aClass)\
aClass * anObj##_valid = nil;\
BOOL anObj##_isClass = [anObj isKindOfClass:[aClass class]];\
if (! anObj##_isClass) { NSLog(@"Warning: Invalid class, '%s' is %@, expecting %@", #anObj, [anObj class], [aClass class]); }\
if ( anObj##_isClass )\
{ anObj##_valid = (aClass *) anObj; }\
if ( anObj##_isClass )
// ...replaces this:
@matthewreagan
matthewreagan / gist:2f3a30b8b229e9e2aa7c
Last active November 8, 2022 23:34
Topmost window via CGWindow
NSWindow *topmostAppWindowAtPoint(CGPoint screenPoint)
{
const CGWindowLevel kScreensaverWindowLevel = CGWindowLevelForKey(kCGScreenSaverWindowLevelKey);
/* This function returns a pointer to the app's topmost NSWindow that
the point `screenPoint` is over. The important distinction here is that
this function takes _all_ system windows into consideration and will return
`nil` if there is a system window (or NSMenu etc.) that the cursor
is over which is atop the app window, which is information that
can't otherwise be gleaned by checking against `[NSApp orderedWindows]` etc.
@matthewreagan
matthewreagan / controlclick.m
Last active September 23, 2015 18:01
Fixing control-click vs right-click contextual menus in NSView
- (void)mouseDown:(NSEvent *)theEvent
{
/* Hard-coding this behavior isn't great, and shouldn't be necessary. (It should
be up to OS X to define what a contextual-menu click is/isn't, and we should
only care about vending our NSMenu via `menuForEvent:`). But this fixes the
discrepancy between control and right clicks in NSView. */
if ((theEvent.modifierFlags & NSControlKeyMask))
{
[NSMenu popUpContextMenu:[self menuForEvent:theEvent] withEvent:theEvent forView:self];
@matthewreagan
matthewreagan / controlclick.swift
Last active August 2, 2024 07:42
Fixing control-click vs right-click contextual menus in NSView
override func mouseDown(theEvent: NSEvent)
{
/* Hard-coding this behavior isn't great, and shouldn't be necessary. (It should
be up to OS X to define what a contextual-menu click is/isn't, and we should
only care about vending our NSMenu via `menuForEvent:`). */
let modifierFlags = theEvent.modifierFlags;
if (modifierFlags.contains(.ControlKeyMask))
{
@matthewreagan
matthewreagan / style.css
Created September 25, 2015 00:26
Pretty web text
body, input, textarea, select, button, {
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-moz-font-feature-settings: "liga", "kern";
}
@matthewreagan
matthewreagan / XCUIElement+Additions.m
Created February 11, 2016 02:20
Click-drag on XCUIElement category method
- (void)clickAtPoint:(CGPoint)pointInElement thenDragTo:(CGPoint)point2InElement
{
CGRect frame = self.frame;
CGFloat width = CGRectGetWidth(frame);
CGFloat height = CGRectGetHeight(frame);
XCUICoordinate *coord1 = [self coordinateWithNormalizedOffset:CGVectorMake(pointInElement.x / width, pointInElement.y / height)];
XCUICoordinate *coord2 = [coord1 coordinateWithOffset:CGVectorMake(point2InElement.x - pointInElement.x, point2InElement.y - pointInElement.y)];
[coord1 clickForDuration:0.1 thenDragToCoordinate:coord2];
@matthewreagan
matthewreagan / XCTestCase+Additions.m
Last active February 11, 2016 03:37
Waiting for XCUIElement to exist
//Usage example:
//XCUIElement *newFileButton = self.app.windows.buttons[@"New File"];
//[self waitForElementToExist:newFileButton timeout:20.0];
//[newFileButton click];
- (void)waitForElementToExist:(XCUIElement *)element timeout:(NSTimeInterval)timeout
{
NSPredicate *existsPredicate = [NSPredicate predicateWithFormat:@"exists == 1"];
[self expectationForPredicate:existsPredicate evaluatedWithObject:element handler:nil];
@matthewreagan
matthewreagan / ExampleXCTestCase.m
Created February 16, 2016 02:03
Example XCTestCase Interruption Handler
id systemAlertMonitor = [self addUIInterruptionMonitorWithDescription:@"Alert Handler" handler:^BOOL(XCUIElement * _Nonnull interruptingElement) {
//Exampe: Check for expected UI indicating a particular system alert
if (interruptingElement.staticTexts[@"<Alert text example>"].exists &&
interruptingElement.buttons[@"OK"].exists)
{
//Dismiss the alert (e.g., by calling -click on the target button of interruptingElement)
[interruptingElement.buttons[@"OK"] click];
@matthewreagan
matthewreagan / SKCrashWindowserver.swift
Created August 3, 2018 18:29
SpriteKit macOS 10.13 Windowserver Crash
// The following code reliably crashes windowserver on macOS 10.13,
// forcing logout of the current user.
// Setup: Create a simple, default SKView() and present an
// SKScene() and add it to a window on macOS
let sprite = SKSpriteNode(imageNamed: "mySpriteImage")
scene.addChild(sprite)
let warpGridSize = 100
func geometryGridPositions(byWarping: Bool) -> [float2] {
@matthewreagan
matthewreagan / Perlin
Last active March 30, 2024 16:18
Perlin noise generation in Swift
// Perlin.swift
// Created by Matthew Reagan on 8/7/18.
//
// Quick implementation of the the classic Perlin noise
// generation function, useful for creating organic textures
// or terrain. Perlin noise can also be easily generated with
// Apple's GameplayKit framework, this code is mostly for
// experimentation purposes. (Disclaimer: This code is not
// optimized, nor particularly elegant, but it can be used as
// a jumping off point for writing custom noise functions.)