Skip to content

Instantly share code, notes, and snippets.

View mminer's full-sized avatar

Matthew Miner mminer

View GitHub Profile
@mminer
mminer / TabNavigator.cs
Created April 11, 2018 21:44
Unity component to allow navigating between UI components using tab.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
/// <summary>
/// Allows moving between UI components using tab and shift-tab.
/// </summary>
public class TabNavigator : MonoBehaviour
{
EventSystem system;
@mminer
mminer / findUnusedPort.js
Created January 4, 2018 21:28
Node.js function to find a free port.
const net = require('net');
function findUnusedPort() {
return new Promise(resolve => {
const server = net.createServer();
// When the port is omitted, the OS provides an unused one.
// https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback
server.listen(() => {
const { port } = server.address();
@mminer
mminer / Reducer.swift
Created November 13, 2017 02:32
Example of a Redux-esque store powered by RxSwift.
typealias Reducer<StateType, ActionType> = (_ state: StateType, _ action: ActionType) -> StateType
@mminer
mminer / NSImage+Oval.swift
Last active November 5, 2022 23:02
Creates a new NSImage with a circular mask.
import AppKit
extension NSImage {
/// Copies this image to a new one with a circular mask.
func oval() -> NSImage {
let image = NSImage(size: size)
image.lockFocus()
NSGraphicsContext.current?.imageInterpolation = .high
@mminer
mminer / MyService.swift
Last active April 23, 2024 23:00
Components of XPC service.
import Foundation
class MyService: NSObject, MyServiceProtocol {
func upperCaseString(_ string: String, withReply reply: @escaping (String) -> Void) {
let response = string.uppercased()
reply(response)
}
}
@mminer
mminer / NSApplication+Restart.swift
Created May 2, 2017 19:49
Extension function to restart a Cocoa application.
import AppKit
extension NSApplication {
/// Restarts the application.
func restart() {
let process = Process()
process.launchPath = "/bin/sh"
process.arguments = ["-c", "sleep 1; open '\(Bundle.main.bundlePath)'"]
process.launch()
@mminer
mminer / NSMutableAttributedString+Hyperlink.swift
Last active October 17, 2017 21:16
Extension to NSMutableAttributedString that creates a link.
@mminer
mminer / HyperlinkTextView.swift
Last active September 4, 2025 05:43
NSTextView subclass that displays and opens hyperlinks.
// You don't necessarily need this subclass if your NSTextView is selectable.
// If it isn't though, this allows you to have an uneditable, unselectable label where links work as expected.
import AppKit
class HyperlinkTextView: NSTextView {
override func mouseDown(with event: NSEvent) {
super.mouseDown(with: event)
openClickedHyperlink(with: event)
@mminer
mminer / Collection+SafeAccess.swift
Last active October 18, 2017 04:37
Collections extension for avoiding out-of-bounds exceptions.
extension Collection {
/// Returns the element at the specified index if it is within bounds, or nil if it's outside.
subscript(safe index: Index) -> Iterator.Element? {
return indices.contains(index) ? self[index] : nil
}
}
@mminer
mminer / String+Ranges.swift
Created March 1, 2017 00:52
String extension to find the ranges of occurrences of a given string.
import Foundation
extension String {
/// Finds and returns the ranges of occurrences of a given string within a given range of the `String`.
func ranges(of searchString: String, options: CompareOptions = [], range searchRange: Range<Index>? = nil, locale: Locale? = nil) -> [Range<Index>] {
let searchRange = searchRange ?? startIndex..<endIndex
if let foundRange = range(of: searchString, options: options, range: searchRange, locale: locale) {
let nextRange = foundRange.upperBound..<searchRange.upperBound