Skip to content

Instantly share code, notes, and snippets.

View revblaze's full-sized avatar

Justin Bush revblaze

View GitHub Profile
@revblaze
revblaze / SoundUtility.swift
Created January 7, 2024 20:58
Play macOS system sounds within your application environment. Working as of Sonoma (macOS 14).
import AVFoundation
struct SoundUtility {
static func play(systemSound: SystemSound) {
if let sound = NSSound(contentsOfFile: systemSound.path, byReference: true) {
sound.play()
} else {
print("Failed to play system sound")
}
}
@revblaze
revblaze / Settings.swift
Last active July 7, 2023 18:28
UserDefaults template with embedded auto-completion
let defaults = UserDefaults.standard
// MARK: Keys
enum Keys: String, CaseIterable {
case firstName
case websiteUrl
case hasLaunchedAppBefore
/// The default value for a given UserDefaults key
var defaultValue: Any {
@revblaze
revblaze / .gitignore
Created November 11, 2021 16:02
Universal .gitignore for Xcode, Node.js and TypeScript projects
#
# JBush.gitignore v1 (Nov 11, 2021)
#
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
@revblaze
revblaze / Symbol.swift
Created July 8, 2021 17:34
Quick, typesafe SF Symbols extension for Swift(UI)
enum Symbol: String {
case add = "plus"
case trash = "trash"
case todo = "square"
case done = "checkmark.square"
}
/// Returns an image from the SF Symbols library
/// # Usage: Image(.trash) for Image(systemName: "trash")
extension Image {
@revblaze
revblaze / Device.swift
Last active February 1, 2021 22:56
Easy and quick device conditionals – a basic necessity.
// import UIKit
// import SwiftUI
struct Device {
static let phone = UIDevice.current.userInterfaceIdiom == .phone
static let pad = UIDevice.current.userInterfaceIdiom == .pad
static let mac = UIDevice.current.userInterfaceIdiom == .mac
static let tv = UIDevice.current.userInterfaceIdiom == .tv
@revblaze
revblaze / MacEditorTextView.swift
Created January 30, 2021 14:59 — forked from unnamedd/MacEditorTextView.swift
[SwiftUI] MacEditorTextView - A simple and small NSTextView wrapped by SwiftUI.
/**
* MacEditorTextView
* Copyright (c) Thiago Holanda 2020
* https://twitter.com/tholanda
*
* MIT license
*/
import Combine
import SwiftUI
@revblaze
revblaze / WKLargeNavBar.swift
Created January 12, 2021 16:01
Quick workaround for forcing large titles in UINavigationBar when working with an embedded WKWebView
/**
Quick workaround to fix UINavigationBar from automatically entering the default display style with a WKWebView object (despite being set to `prefersLargeTitles = .always`).
This works by first allowing WKWebView to take priority over the UINavigationBar, and then simulating user-scroll interaction to re-enable it.
*/
func adjustNavBarScroll() {
// webView.scrollView.bounces = false
// Scroll to make UINavigationBar title small (update with accurate properties)
let scrollPoint = CGPoint(x: 0, y: 100)
webView.scrollView.setContentOffset(scrollPoint, animated: false)
// Scroll 1 pixel above UINavigationController to activate Large Title
@revblaze
revblaze / JavaScriptString.swift
Last active December 21, 2020 23:31
Read a JavaScript file and return its contents as a String (Swift)
struct JS {
static let index = "index" // index.js (default)
static let path = "dist/" // path/to/file.js
/// Returns the generated JavaScript code for `index.js` as a String.
static func get() -> String {
return get(file: index, path: path)
}
@revblaze
revblaze / NSView+DarkMode.swift
Created December 8, 2020 18:59
Extension: Quickly return the macOS' current system appearance
extension NSView {
var hasDarkAppearance: Bool {
if #available(OSX 10.14, *) {
switch effectiveAppearance.name {
case .darkAqua, .vibrantDark, .accessibilityHighContrastDarkAqua, .accessibilityHighContrastVibrantDark:
return true
default:
return false
}
} else {
@revblaze
revblaze / NSView+BackgroundColor.swift
Created December 8, 2020 18:38
Extension: Easily set NSView background color
extension NSView {
var backgroundColor: NSColor? {
get {
if let colorRef = self.layer?.backgroundColor {
return NSColor(cgColor: colorRef)
} else {
return nil
}
}
set {