Skip to content

Instantly share code, notes, and snippets.

View krzyzanowskim's full-sized avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
krzyzanowskim /
Created May 9, 2024 11:49
FB13789916: NSTextInputClient.setMarkedText provide bogus selection range for Chinese keyboard
  1. Open macOS TextEdit application
  2. enter "aaaaaaaaaaaaaaaaaaaa"
  3. move insertion point at location 0
  4. switch system keyboard to Pinyin - Simplified (Chinese)
  5. on the keyboard type letters "abcdefghij"
  6. 💥 observe bug: marked text is "abc " insertion point is at location 11


krzyzanowskim / TextKit2.swift
Last active April 7, 2024 13:10
Minimal setup of TextKit2 components to produce PDF document
import Cocoa
let loremIpsum = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque erat risus, laoreet et eros id, lobortis elementum sem. Nullam nec magna massa. Donec gravida felis at odio tincidunt pretium et in orci. Curabitur suscipit porta purus non faucibus. Cras dapibus felis eu enim rutrum, vel aliquet lacus volutpat. Quisque fermentum vulputate dictum. Morbi mattis orci quis dui posuere, id vulputate turpis tempus. Sed vulputate augue at ullamcorper feugiat. Phasellus nec lacus eget sapien congue lacinia vel et nulla. Phasellus sollicitudin gravida dapibus. Donec molestie ullamcorper lacus eu rutrum. Praesent lacinia dignissim dui eu ultricies. Integer in lacus lobortis, porttitor sem eu, tincidunt dolor. Cras dignissim nisl in maximus ullamcorper.
Proin nec vulputate magna. In interdum leo sit amet arcu consequat facilisis. Fusce ut malesuada ante, nec malesuada nisl. Mauris porta velit quis tortor mollis, in suscipit augue efficitur. Praesent eleifend mollis neque, non sempe
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"
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

// 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
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
/// 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
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