Skip to content

Instantly share code, notes, and snippets.

View krzyzanowskim's full-sized avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
View StringOffsetSubscript.swift
extension StringProtocol {
subscript(_ offset: Int) -> String.Element {
if offset >= 0 {
self[index(startIndex, offsetBy: offset)]
} else {
self[index(endIndex, offsetBy: offset)]
krzyzanowskim / LineBreakPropose.swift
Last active October 28, 2023 22:45
"given this many pixels and a string with these attributes, tell me the optimal places to do a line break"
View LineBreakPropose.swift
import Cocoa
/// "given this many pixels and a string with these attributes, tell me the optimal places to do a line break"
class LineBreakPropose: NSObject, NSTextLayoutManagerDelegate {
var lineBreaks: [NSTextLocation] = []
init(_ attributedString: NSAttributedString, in size: CGSize, by lineBreakMode: NSLineBreakMode = .byWordWrapping) {
krzyzanowskim /
Created October 20, 2023 21:00
FB13291926: NSTextLayoutManager.usageBoundsForTextContainer observer is never trigerred

I noticed that NSTextLayoutManager.usageBoundsForTextContainer is no longer KVO observable, despite being documented as KVO-compliant.. I believe it worked around macOS 13.

var usageBoundsForTextContainerObserver: NSKeyValueObservation?

usageBoundsForTextContainerObserver = textLayoutManager.observe(\.usageBoundsForTextContainer, options: [.new]) { textLayoutManager, change in
    print("never called")
krzyzanowskim /
Last active October 21, 2023 16:57
NSTextContainer.lineFragmentPadding does not affect end of the fragment usageBoundsForTextContainer rectangle

I noticed that NSTextContainer.lineFragmentPadding value does not affect the NSTextLayoutManager.usageBoundsForTextContainer rectangle on both sizes.

Documentation of lineFragmentPadding says: "The padding appears at the beginning and end of the line fragment rectangles.", however the value of the usageBoundsForTextContainer does not resize appropriately.

Let's see how a different value of lineFragmentPadding affect the text container rectangle:

for lineFragmentPadding = 5 usageBoundsForTextContainer = (x: 5.0, y: 0.0, width: 66.73828125, height: 14.0)

for lineFragmentPadding = 0

View enumerateattributes.swift
// Documentation says:
// > Executes the specified block for each range of a particular attribute in the attributed string.
// documentation is not correct (or implementation, hard to tell). The block is executed at least once, not matter if particular attribute is or is not present in the attributed string.
import Foundation
import AppKit
/// Attributed string without any attribute, in particular no font attribute set
krzyzanowskim / reversible-formatstyle.swift
Last active July 21, 2023 14:21
ReversibleFormatStyle - like FormatStyle but reversible, more like Formatter
View reversible-formatstyle.swift
import Foundation
// like FormatStyle but reversible, more like Formatter
protocol ReversibleFormatStyle<FormatOutput>: FormatStyle {
func format(_ value: FormatOutput) -> FormatInput
// Implementation
struct ReversibleNumFormatStyle: ReversibleFormatStyle {
krzyzanowskim / box.swift
Created June 3, 2023 10:36
A SwiftUI wrapper around NSBox
View box.swift
/// A SwiftUI wrapper around `NSBox`.
public struct Box<Content>: View where Content: View {
private let title: String?
private let content: () -> Content
public init(_ title: String? = nil, @ViewBuilder content: @escaping () -> Content) {
self.title = title
self.content = content
krzyzanowskim / blogpost.txt
Last active May 4, 2023 08:25
Designing a Stunning macOS Icon for Your Text Editor: A Step-by-Step Guide
View blogpost.txt
Title: Designing a Stunning macOS Icon for Your Text Editor: A Step-by-Step Guide
In the world of macOS applications, an eye-catching icon plays a crucial role in attracting users and conveying the essence of your software. When it comes to a text editor, your macOS icon should reflect the simplicity, elegance, and functionality of your application. In this blog post, we will explore the art of designing a stunning macOS icon for your text editor, step by step. Let's dive in!
Step 1: Understand Your Brand and Target Audience:
Before starting the icon design process, it's important to have a clear understanding of your brand and target audience. Ask yourself questions like: What are the unique features of your text editor? What emotions or feelings do you want your icon to evoke? Understanding your brand and target audience will help you create an icon that resonates with your users.
Step 2: Research and Inspiration:
Take some time to research existing macOS icons, especially those in the text
krzyzanowskim /
Last active March 17, 2023 18:13
Swift Studio Investment Pitch

Dear [Name] Investments,

I am excited to introduce Swift Studio, a cutting-edge platform for Swift development, specifically tailored to Swift on Server. As you know, Swift is quickly gaining popularity as a language for developing server-side applications, and Swift Studio is the perfect tool for developers looking to take advantage of this trend.

Swift Studio is a comprehensive development environment that streamlines the development process by providing an intuitive user interface and powerful features. With Swift Studio, developers can easily create, test, and deploy Swift applications for the server, all in one place.

One of the key features of Swift Studio is its real-time collaboration functionality, which allows multiple developers to work on the same project simultaneously. This feature enhances productivity and promotes teamwork, making Swift Studio an ideal solution for larger development teams.

Additionally, Swift Studio integrates seamlessly with other popular development tools and technologi

View swipeActions.swift
/// NavigationSplitView default a List in left view to Sidebar style, and then swipeActions modifier stop working. It's a multi level regression. The swipeActions has been broken for a while, but in macOS 12 it didn't work in .sidebar list style neither
NavigationSplitView {
List {
ForEach(list, id: \.self) { item in
HStack {