Skip to content

Instantly share code, notes, and snippets.

@schwa
schwa / SharedWindow.swift
Created December 15, 2023 19:28
SwiftUI example showing how to make a Shared Content Window…
import SwiftUI
import Observation
import os
/// A trick in SwiftUI to have a singls shared global window.
/// We want the user to be able to show and hide this window, and change the content in it. Hitting show multiply times should NOT open multiple windows but instead should bring the shared window forward.
/// The trick is to use a dummy Singleton type (here we embed it with the shared content) that we can pass to open/dismissWindow and the WindowGroup. The dummy SIngleton type must contain no properties and merely exists to trick SwiftUI into only ever showing one window for the type at a time.
@Observable
class SharedContent {
@gruber
gruber / Split Tabs to New Window.scpt
Last active May 5, 2024 16:58
An AppleScript for Safari to move all tabs in the frontmost window, from the current tab to the rightmost (last) tab, to a new window.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
(*
Original script: John Gruber (https://daringfireball.net/linked/2023/12/05/an-applescript-for-safari-split-tabs-to-new-window)
Much more elegant version: Leon Cowle (https://github.com/leoncowle)
Even more elegant version: https://stackoverflow.com/questions/54066100/applescript-to-split-safari-tabs-into-new-window/54089865#54089865
Worth a warning: "moving" tabs with this script doesn't actually move them like
drag-and-drop does. The tabs "moved" by this script will reload in the new window,
@steventroughtonsmith
steventroughtonsmith / VisionViewPlaygroundApp.swift
Created September 21, 2023 19:22
Trivial visionOS non-rectangular window content layout example
//
// VisionViewPlaygroundApp.swift
// VisionViewPlayground
//
// Created by Steven Troughton-Smith on 21/09/2023.
//
import SwiftUI
@main
@chockenberry
chockenberry / finder_icons.sh
Created March 16, 2023 20:00
Script to toggle Finder icons
#!/bin/sh
defaults read com.apple.finder CreateDesktop > /dev/null 2>&1
enabled=$?
if [ "$1" = "off" ]; then
if [ $enabled -eq 1 ]; then
osascript -e 'tell application "Finder" to quit'
defaults write com.apple.finder CreateDesktop false
open -a Finder
@amirdew
amirdew / View+MinimumPadding.swift
Created December 22, 2022 13:20
Minimum paddings for SwiftUI views
extension View {
func minimumPadding(edges: Edge.Set = .all, _ length: CGFloat = 8) -> some View {
GeometryReader { geo in
padding(.bottom, edges.contains(.bottom) ? max(length, geo.safeAreaInsets.bottom) : 0)
.padding(.top, edges.contains(.top) ? max(length, geo.safeAreaInsets.top) : 0)
.padding(.leading, edges.contains(.leading) ? max(length, geo.safeAreaInsets.leading) : 0)
.padding(.trailing, edges.contains(.trailing) ? max(length, geo.safeAreaInsets.trailing) : 0)
.ignoresSafeArea(edges: edges)
}
}
import SwiftUI
struct NewStopView: View {
let stop: Model.Stop
var body: some View {
Text("New Layout").font(.title)
RouteLayout(spacing: 8) {
ForEach(stop.routes ?? []) { route in
RouteView(route: route)
@rsms
rsms / example.txt
Last active March 11, 2023 23:00
source line-length histogram script
./linelen_hist.sh src '*.c'
COLS COUNT
2 1317 ████████████████████████████████████████████████████████████▌
4 583 ██████████████████████████▏
6 500 ██████████████████████▎
8 253 ███████████▊
10 264 ████████████▊
12 448 ████████████████████▋
14 417 ███████████████████▌
16 476 █████████████████████▍
const edits = [
[
"node_modules/ts-invariant/package.json",
{
type: "module",
exports: {
".": "./lib/invariant.esm.js",
"./process/index.js": "./process/index.js",
},
},
import UIKit
extension UITextField {
/// Add a trailing placeholder label that tracks the text as it changes
func addTrailingPlaceholder(_ placeholder: String) {
let label = UILabel()
label.text = placeholder
label.alpha = 0.3
label.isHidden = true
let container = UIView()
@lukaskubanek
lukaskubanek / Bundle+TestFlight.swift
Last active April 26, 2024 08:48
A code snippet for detecting the TestFlight environment for a macOS app at runtime
/// MIT License
///
/// Copyright (c) 2021 Lukas Kubanek, Structured Path GmbH
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions: