Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zcohan committed Feb 8, 2021
1 parent 7b6b938 commit 7c76f66
Show file tree
Hide file tree
Showing 32 changed files with 2,734 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Examples/Mac/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// AppDelegate.swift
// SoulverTextKit
//
// Created by Zac Cohan on 9/1/21.
//

import Cocoa

@main
class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}


}

11 changes: 11 additions & 0 deletions Examples/Mac/Assets.xcassets/AccentColor.colorset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
58 changes: 58 additions & 0 deletions Examples/Mac/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"images" : [
{
"idiom" : "mac",
"scale" : "1x",
"size" : "16x16"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "16x16"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "32x32"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "32x32"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "128x128"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "128x128"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "256x256"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "256x256"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "512x512"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "512x512"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
6 changes: 6 additions & 0 deletions Examples/Mac/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
501 changes: 501 additions & 0 deletions Examples/Mac/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions Examples/Mac/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSMainStoryboardFile</key>
<string>Main</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>
89 changes: 89 additions & 0 deletions Examples/Mac/ViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// ViewController.swift
// SoulverTextKit
//
// Created by Zac Cohan on 9/1/21.
//

import Cocoa
import SoulverTextKit

class ViewController: NSViewController {

/// Grab a standard text view
@IBOutlet var textView: NSTextView!

/// Create one of these things
var paragraphCalculator: SoulverTextKit.ParagraphCalculator!

/// Choose what character distinguishes a calculating paragraph
let style = AnswerPosition.afterTab

override func viewDidLoad() {
super.viewDidLoad()

self.setupSoulverTextKit()

// Looks better
self.textView.textContainerInset = NSSize(width: 10.0, height: 15.0)

}

func setupSoulverTextKit() {

paragraphCalculator = ParagraphCalculator(answerPosition: self.style, textStorage: self.textView.textStorage!, textContainer: self.textView.textContainer!)

// Setup the text view to send us relevant delegate messages
self.textView.delegate = self
self.textView.layoutManager!.delegate = self

// Set some default expressions
self.textView.string = [
"123 + 456",
"10 USD in EUR",
"today + 3 weeks"
].expressionStringFor(style: self.style)

// let soulverTextKit know we changed the textView's text
paragraphCalculator.textDidChange()


}

}

extension ViewController : NSLayoutManagerDelegate, NSTextViewDelegate {

func textDidChange(_ notification: Notification) {

// Let us know when the text changes, so we can evaluate any changed paragraph if necessary
paragraphCalculator.textDidChange()
}

func layoutManager(_ layoutManager: NSLayoutManager, textContainer: NSTextContainer, didChangeGeometryFrom oldSize: NSSize) {

// Let us know when the text view changes size, so we can change update the formatting if necessary
paragraphCalculator.layoutDidChange()
}

func textView(_ textView: NSTextView, shouldChangeTextIn affectedCharRange: NSRange, replacementString: String?) -> Bool {

// Check with us to see if the user should be able to edit parts of the paragraph. For example, we don't allow the user to edit results on Soulver lines themselves

switch paragraphCalculator.shouldAllowReplacementFor(affectedCharRange: affectedCharRange, replacementString: replacementString) {
case .allow:
return true
case .deny:
NSSound.beep()
return false
case .setIntertionPoint(range: let range):
textView.setSelectedRange(range)
return false
}

}


}


Loading

0 comments on commit 7c76f66

Please sign in to comment.