Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102import AppKitimport Foundation
@MainActorfinal class AppConfigViewer: NSObject { private var window: NSWindow? private var fileURL: URL? private let onFailure: @MainActor (any Error) -> Void
init(onFailure: @escaping @MainActor (any Error) -> Void) { self.onFailure = onFailure }
func show(fileURL: URL, line: Int?, reason: String) throws { precondition(!reason.isEmpty) let contents = try String(contentsOf: fileURL, encoding: .utf8) self.fileURL = fileURL let text = NSTextView() text.string = contents text.isEditable = false text.isSelectable = true text.isRichText = false text.font = .monospacedSystemFont(ofSize: NSFont.systemFontSize, weight: .regular) text.textContainerInset = NSSize(width: 16, height: 16) text.setAccessibilityLabel(AppText.string("Configuration File")) text.setAccessibilityIdentifier("config-file.contents") let scroll = NSScrollView() scroll.hasVerticalScroller = true scroll.documentView = text text.isVerticallyResizable = true text.isHorizontallyResizable = false text.autoresizingMask = [.width] text.textContainer?.widthTracksTextView = true let open = NSButton(title: AppText.string("Open in Editor"), target: self, action: #selector(openInEditor)) open.setAccessibilityIdentifier("config-file.open-in-editor") let problem = NSTextField(wrappingLabelWithString: reason) problem.isSelectable = true problem.setAccessibilityIdentifier("config-file.problem") let stack = NSStackView(views: [problem, scroll, open]) stack.orientation = .vertical stack.alignment = .leading stack.spacing = 12 stack.edgeInsets = NSEdgeInsets(top: 12, left: 12, bottom: 12, right: 12) scroll.translatesAutoresizingMaskIntoConstraints = false problem.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ problem.widthAnchor.constraint(equalTo: stack.widthAnchor, constant: -24), scroll.widthAnchor.constraint(equalTo: stack.widthAnchor, constant: -24), scroll.heightAnchor.constraint(greaterThanOrEqualToConstant: 280), ]) let window = self.window ?? NSWindow( contentRect: NSRect(x: 0, y: 0, width: 680, height: 480), styleMask: [.titled, .closable, .resizable], backing: .buffered, defer: false) window.title = AppText.string("Configuration File") window.isReleasedWhenClosed = false window.contentView = stack window.minSize = NSSize(width: 400, height: 360) if self.window == nil { window.center() } self.window = window stack.layoutSubtreeIfNeeded() text.frame = scroll.contentView.bounds text.minSize = NSSize(width: 0, height: scroll.contentSize.height) text.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude) text.textContainer?.containerSize = NSSize( width: scroll.contentSize.width, height: CGFloat.greatestFiniteMagnitude) let selected = line.map { Self.lineRange(in: contents, line: $0) } ?? NSRange(location: 0, length: 0) text.setSelectedRange(selected) NSApplication.shared.activate(ignoringOtherApps: true) window.makeKeyAndOrderFront(nil) window.makeFirstResponder(text) text.scrollRangeToVisible(selected) }
static func lineRange(in contents: String, line: Int) -> NSRange { precondition(line > 0) let text = contents as NSString var location = 0 for _ in 1..<line { guard location < text.length else { return NSRange(location: text.length, length: 0) } location = NSMaxRange(text.lineRange(for: NSRange(location: location, length: 0))) } guard location < text.length else { return NSRange(location: text.length, length: 0) } return text.lineRange(for: NSRange(location: location, length: 0)) }
func close() { window?.close() window = nil fileURL = nil }
@objc private func openInEditor() { guard let fileURL else { preconditionFailure("A configuration viewer needs its file") } guard NSWorkspace.shared.open(fileURL) else { onFailure(AppText.failure("The configuration file could not be opened in an editor.")) return } }}