How to observe focus event of NSTextField in macOS

Issue #589 becomeFirstResponder class FocusAwareTextField: NSTextField { var onFocusChange: (Bool) -> Void = { _ in } override func becomeFirstResponder() -> Bool { let textView = window?.fieldEditor(true, for: nil) as? NSTextView textView?.insertionPointColor = R.nsColor.action onFocusChange(true) return super.becomeFirstResponder() } } textField.delegate // NSTextFieldDelegate func controlTextDidEndEditing(_ obj: Notification) { onFocusChange(false) } NSTextField and NSText https://stackoverflow.com/questions/25692122/how-to-detect-when-nstextfield-has-the-focus-or-is-its-content-selected-cocoa When you clicked on search field, search field become first responder once, but NSText will be prepared sometime somewhere later, and the focus will be moved to the NSText....

January 29, 2020 · 1 min · 186 words · Khoa

How to change caret color of NSTextField in macOS

Issue #588 class FocusAwareTextField: NSTextField { var onFocus: () -> Void = {} var onUnfocus: () -> Void = {} override func becomeFirstResponder() -> Bool { onFocus() let textView = window?.fieldEditor(true, for: nil) as? NSTextView textView?.insertionPointColor = R.nsColor.action return super.becomeFirstResponder() } override func resignFirstResponder() -> Bool { onUnfocus() return super.resignFirstResponder() } }

January 29, 2020 · 1 min · 52 words · Khoa

How to make TextView in SwiftUI for macOS

Issue #587 Use NSTextVIew From https://github.com/twostraws/ControlRoom/blob/main/ControlRoom/NSViewWrappers/TextView.swift import SwiftUI /// A wrapper around NSTextView so we can get multiline text editing in SwiftUI. struct TextView: NSViewRepresentable { @Binding private var text: String private let isEditable: Bool init(text: Binding<String>, isEditable: Bool = true) { _text = text self.isEditable = isEditable } init(text: String) { self.init(text: Binding<String>.constant(text), isEditable: false) } func makeNSView(context: Context) -> NSScrollView { let text = NSTextView() text.backgroundColor = isEditable ?...

January 29, 2020 · 2 min · 370 words · Khoa

How to use Firebase Crashlytics in macOS app

Issue #585 New Firebase Crashlytics Follow the new Firebase Crashlytics guide Get started with Firebase Crashlytics using the Firebase Crashlytics SDK CocoaPods Specify FirebaseCore for community managed macOS version of Firebase platform :osx, '10.13' target 'MyMacApp' do # Comment the next line if you don't want to use dynamic frameworks use_frameworks! pod 'FirebaseCore' pod 'Firebase/Crashlytics' end Signing and capabilities Under Hardware runtime, check Disable library validation Under App sandbox, enable Outgoing connections (Client)...

January 28, 2020 · 1 min · 118 words · Khoa

How to handle radio group for NSButton

Issue #579 Use same action, or we can roll our own implementation An NSButton configured as a radio button (with the -buttonType set to NSRadioButton), will now operate in a radio button group for applications linked on 10.8 and later. To have the button work in a radio group, use the same -action for each NSButton instance, and have the same superview for each button. When these conditions are met, checking one button (by changing the -state to 1), will uncheck all other buttons (by setting their -state to 0)....

January 17, 2020 · 1 min · 111 words · Khoa

How to use Applications folder in macOS

Issue #573 There are 2 Applications folder /System/Applications: contains Notes, Books, Calculator, … /Applications: contains Safari, Xcode, Keynote, …

January 8, 2020 · 1 min · 19 words · Khoa

How to allow unnotarized app to run on macOS Catalina

Issue #520 Remove quarantine xattr -d com.apple.quarantine /Applications/Flipper.app

November 28, 2019 · 1 min · 8 words · Khoa

How to make Swift Package Manager package for multiple platforms

Issue #504 https://twitter.com/NeoNacho/status/1181245484867801088?s=20 There’s no way to have platform specific sources or targets today, so you’ll have to take a different approach. I would recommend wrapping all OS specific files in #if os and just having one target. For tests, you could do something similar, one test target, but conditional tests Every files are in Sources folder, so we can use platform and version checks. For example Omnia is a Swift Package Manager that supports iOS, tvOS, watchOS, macOS and Catalyst....

November 13, 2019 · 1 min · 119 words · Khoa

How to use Firebase in macOS

Issue #501 Use Catalyst Add to CocoaPods platform :ios, '13.0' target 'MyApp' do use_frameworks! pod 'FirebaseCore' pod 'Firebase/Firestore' end Troubleshooting Select a team for gRPC-C++-gRPCCertificates-Cpp FIRAnalyticsConnector: building for Mac Catalyst, but linking in object file built for iOS Simulator https://stackoverflow.com/questions/57666155/firanalyticsconnector-building-for-mac-catalyst-but-linking-in-object-file-bui The problem was related to the difference between Firebase/Core and FirebaseCore. The first is a subspec of the Firebase pod that depends on FirebaseAnalytics. The second is only the FirebaseCore pod....

November 12, 2019 · 1 min · 79 words · Khoa

How to use Apple certificate in Xcode 11

Issue #458 For push notification, we can now use just Production Certificate for 2 environments (production and sandbox) instead of Development and Production certificates. Now for code signing, with Xcode 11 https://developer.apple.com/documentation/xcode_release_notes/xcode_11_release_notes we can just use Apple Development and Apple Distribution certificate for multiple platforms Signing and capabilities settings are now combined within a new Signing & Capabilities tab in the Project Editor. The new tab enables using different app capabilities across multiple build configurations....

October 11, 2019 · 1 min · 142 words · Khoa

How to style NSTextView and NSTextField in macOS

Issue #443 let textField = NSTextField() textField.focusRingType = .none let textView = NSTextView() textView.insertionPointColor = R.color.caret textView.isRichText = false textView.importsGraphics = false textView.isEditable = true textView.isSelectable = true textView.drawsBackground = false textView.allowsUndo = true

October 5, 2019 · 1 min · 34 words · Khoa

How to center NSWindow in screen

Issue #442 On macOS, coordinate origin is bottom left let window = NSWindow(contentRect: rect, styleMask: .borderless, backing: .buffered, defer: false) window.center() let frame = window.frame window.setFrameOrigin(CGPoint(x: frame.origin.x, y: 100))

October 4, 2019 · 1 min · 29 words · Khoa

How to log Error in Swift

Issue #439 Use localizedDescription We need to provide NSLocalizedDescriptionKey inside user info dictionary, otherwise the outputt string may not be what we want. NSError https://developer.apple.com/documentation/foundation/nserror/1414418-localizeddescription A string containing the localized description of the error. The object in the user info dictionary for the key NSLocalizedDescriptionKey. If the user info dictionary doesn’t contain a value for NSLocalizedDescriptionKey, a default string is constructed from the domain and code. let error = NSError(domain: "com....

October 3, 2019 · 1 min · 164 words · Khoa

How to handle NSTextField change in macOS

Issue #438 Storyboard In Storyboard, NSTextField has an Action option that specify whether Send on Send on Enter only` should be the default behaviour. Code In code, NSTextFieldDelegate notifies whenever text field value changes, and target action notifies when Enter key is pressed import Cocoa class ViewController: NSViewController, NSTextFieldDelegate { @IBOutlet weak var textField: NSTextField! override func viewDidLoad() { super.viewDidLoad() textField.delegate = self } func controlTextDidChange(_ obj: Notification) { let textField = obj....

October 2, 2019 · 1 min · 116 words · Khoa

How to add section header to NSCollectionView in macOS

Issue #437 Normal Use Omnia for itemId extension HeaderCell.swift final class HeaderCell: NSView, NSCollectionViewSectionHeaderView { let label: NSTextField = withObject(NSTextField(labelWithString: "")) { $0.textColor = R.color.header $0.font = R.font.header $0.alignment = .left $0.lineBreakMode = .byTruncatingTail } override init(frame frameRect: NSRect) { super.init(frame: frameRect) addSubviews([label]) activate( label.anchor.centerY, label.anchor.left.constant(8) ) } required init?(coder: NSCoder) { fatalError() } } ViewController.swift collectionView.register( HeaderCell.self, forSupplementaryViewOfKind: NSCollectionView.elementKindSectionHeader, withIdentifier: HeaderCell.itemId ) func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView { if kind == NSCollectionView....

October 2, 2019 · 2 min · 284 words · Khoa

How to show log in Apple Script

Issue #436 Open Script Editor, use log command and look for 4 tabs in bottom panel Result, Messages, Events and Replies log "hello world"

October 1, 2019 · 1 min · 24 words · Khoa

How to show context menu from NSButton in macOS

Issue #435 Use NSMenu and popUp func showQuitMenu() { let menu = NSMenu() let aboutItem = NSMenuItem( title: "About", action: #selector(onAboutTouched(_:)), keyEquivalent: "" ) let quitItem = NSMenuItem( title: "Quit Hacker Pad", action: #selector(onQuitTouched(_:)), keyEquivalent: "" ) aboutItem.target = self quitItem.target = self menu.addItem(aboutItem) menu.addItem(quitItem) menu.popUp( positioning: aboutItem, at: bottomView.quitButton.frame.origin, in: bottomView ) } Use Omnia let menuHandler = MenuHandler() menuHandler.add(title: "About", action: { NSWorkspace.shared.open(URL(string: "https://onmyway133.github.io/")!) }) menuHandler.add(title: "Quit Hacker Pad", action: { NSApp....

October 1, 2019 · 1 min · 79 words · Khoa

How to make checked NSButton in AppKit

Issue #433 Use Omnia for convenient style and isOn property let checkButton = NSButton(checkboxWithTitle: "", target: nil, action: nil) checkButton.stylePlain(title: "Autosave", color: R.color.text, font: R.font.text) checkButton.isOn = true

September 30, 2019 · 1 min · 28 words · Khoa

How to save files in sandboxed macOS app

Issue #432 Read Container Directories and File System Access When you adopt App Sandbox, your application has access to the following locations: The app container directory. Upon first launch, the operating system creates a special directory for use by your app—and only by your app—called a container. Each user on a system gets an individual container for your app, within their home directory; your app has unfettered read/write access to the container for the user who ran it....

September 28, 2019 · 1 min · 92 words · Khoa

How to use marked in WKWebView in AppKit

Issue #429 Use https://github.com/markedjs/marked <!doctype html> <html> <head> <meta charset="utf-8"/> <title>Marked in the browser</title> </head> <body> <div id="content"></div> <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script> <script> document.getElementById('content').innerHTML = marked(`# Marked in the browser\n\nRendered by **marked**.`); </script> </body> </html> Should use back tick instead of ' to deal with multiline string, and escape back tick let markdown = markdown.replacingOccurrences(of: "`", with: "\\`") marked(`\(markdown)`); webView.loadHTMLString(string, baseURL: nil) For WKWebView to load even local content, need to enable Outgoing connections, and maybe Incoming connections in Sandbox

September 26, 2019 · 1 min · 78 words · Khoa