Skip to content

Instantly share code, notes, and snippets.

View sindresorhus's full-sized avatar

Sindre Sorhus sindresorhus

View GitHub Profile
extension UIImage {
private final class UIImageActivityItemSource: NSObject, UIActivityItemSource {
var image = UIImage()
var title: String?
var url: URL?
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
UIImage()
}
@sindresorhus
sindresorhus / machine-learning-for-my-apps.md
Last active July 14, 2022 21:47
Ideas for machine learning usage in my apps

Machine learning for my apps

I'm trying to think of ways I could use machine learning to enhance my apps. Feedback wanted!

You can find my apps here.

Dato

  • Natural language processing to parse a new event title, like Fantastical.
@sindresorhus
sindresorhus / IdentifiableByHashable.swift
Created March 10, 2021 10:30
Make a type `Identifiable` based on its `Hashable` hash value.
/**
Make a type `Identifiable` based on its `Hashable` hash value.
This can be useful when all the properties are required to represent its identifier.
```
struct Item: Hashable, IdentifiableByHashable {
let title: String
var message: String?
}
extension Image {
/**
```
Image(nsImageNamed: NSImage.addTemplateName)
```
- Note: The `size` parameter is there as resizing in SwiftUI creates a blurry image. Seems like a bug:
```
Image(nsImageNamed: NSImage.addTemplateName)
@sindresorhus
sindresorhus / esm-package.md
Last active May 13, 2024 11:37
Pure ESM package

Pure ESM package

The package that linked you here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
@sindresorhus
sindresorhus / MenuBarApp.swift
Last active July 18, 2020 14:52
Dream API for macOS menu bar apps
@main
struct MyApp: App {
var body: some Scene {
StatusBar {
StatusItem(id: "foo", systemImage: "gear") {
Menu {
Button("Toggle") {}
Divider()
Button("Quit") {}
}
import Combine
import SwiftUI
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
@propertyWrapper
public struct Model<Value>: DynamicProperty {
private final class _Box: ObservableObject {
let objectWillChange = ObservableObjectPublisher()
var value: Value {
@sindresorhus
sindresorhus / EmptyInitializable.swift
Created January 7, 2020 09:40
A type that can be initialized without any parameters.
/// Types that can be initialized without any parameters.
/// Useful if you need to accept an array of metatypes and then initialize them.
protocol EmptyInitializable {
init()
}
extension Int: EmptyInitializable {}
extension Int8: EmptyInitializable {}
extension Int16: EmptyInitializable {}
extension Int32: EmptyInitializable {}
@sindresorhus
sindresorhus / NativeButton.swift
Created October 11, 2019 08:58
Example of using NSButton in SwiftUI to access missing features like `keyEquivalent` (for example, to make the button the default and highlighted). Stack Overflow answer: https://stackoverflow.com/a/58337529/64949
/**
```
struct ContentView: View {
var body: some View {
NativeButton("Submit", keyEquivalent: .return) {
// Some action
}
.padding()
}
}