Skip to content

Commit

Permalink
Switched to NSTextField SwiftUI wrapper using MacEditorTextView imple…
Browse files Browse the repository at this point in the history
…mentation
  • Loading branch information
antingle committed Apr 30, 2022
1 parent 9911346 commit 3d05200
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 221 deletions.
4 changes: 0 additions & 4 deletions Calculator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
92F4477328089FF100EC45D5 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 92F4477228089FF100EC45D5 /* Preview Assets.xcassets */; };
92F447852808C25800EC45D5 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92F447842808C25800EC45D5 /* AppDelegate.swift */; };
92F447872808C3B000EC45D5 /* HelperFunctions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92F447862808C3B000EC45D5 /* HelperFunctions.swift */; };
92F657B9280BB000003DB042 /* TextFieldAppKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92F657B8280BB000003DB042 /* TextFieldAppKit.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -36,7 +35,6 @@
92F447832808C1B600EC45D5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
92F447842808C25800EC45D5 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
92F447862808C3B000EC45D5 /* HelperFunctions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HelperFunctions.swift; sourceTree = "<group>"; };
92F657B8280BB000003DB042 /* TextFieldAppKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextFieldAppKit.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -78,7 +76,6 @@
92F4476D28089FEE00EC45D5 /* ContentView.swift */,
928D96C9280AA5E400B6CE2E /* SettingsView.swift */,
928D96C7280AA58D00B6CE2E /* CalculatorView.swift */,
92F657B8280BB000003DB042 /* TextFieldAppKit.swift */,
928D96C5280A8F9300B6CE2E /* ButtonView.swift */,
92F447862808C3B000EC45D5 /* HelperFunctions.swift */,
92F4476F28089FF100EC45D5 /* Assets.xcassets */,
Expand Down Expand Up @@ -176,7 +173,6 @@
files = (
92F447872808C3B000EC45D5 /* HelperFunctions.swift in Sources */,
928D96CA280AA5E400B6CE2E /* SettingsView.swift in Sources */,
92F657B9280BB000003DB042 /* TextFieldAppKit.swift in Sources */,
928D96C8280AA58D00B6CE2E /* CalculatorView.swift in Sources */,
928D96C6280A8F9300B6CE2E /* ButtonView.swift in Sources */,
92F4476E28089FEE00EC45D5 /* ContentView.swift in Sources */,
Expand Down
77 changes: 26 additions & 51 deletions Calculator/CalculatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,61 +56,36 @@ struct CalculatorView: View {
})
}).rotationEffect(Angle(degrees: 180)).scaleEffect(x: -1.0, y: 1.0, anchor: .center)

MacEditorTextView(text: $expression, placeholderText: "Calculate", isEditable: true, onEditingChanged: {}, onCommit: {do {
solution = try evaluateExpression(expression)

// insert item into history
withAnimation(.spring()) {
let historyItem = History(expression: expression, solution: solution.removeZerosFromEnd())
history.insert(historyItem, at: 0)

MacEditorTextView(text: $expression, placeholderText: "Calculate",
// Run when submmitting the text (hitting return)
onCommit: {
do {
solution = try evaluateExpression(expression)

// insert item into history
withAnimation(.spring()) {
let historyItem = History(expression: expression, solution: solution.removeZerosFromEnd())
history.insert(historyItem, at: 0)

}
expression = ""
solution = 0
}
expression = ""
}
catch {
solution = 0
}}, onTextChange: {newExpression in
do {
solution = try evaluateExpression(newExpression)
}
catch {
solution = 0
} })
// TextFieldAppKit("Calculate", text: $expression, nsFont: .systemFont(ofSize: 14))
// .onCommit {
// do {
// solution = try evaluateExpression(expression)
//
// // insert item into history
// withAnimation(.spring()) {
// let historyItem = History(expression: expression, solution: solution.removeZerosFromEnd())
// history.insert(historyItem, at: 0)
//
// }
// expression = ""
// }
// catch {
// solution = 0
// }
// }
// .onChange(of: expression) { newExpression in
// do {
// solution = try evaluateExpression(newExpression)
// }
// catch {
// solution = 0
// }
// }
//
// Expressions are typed in here
// TextField("Calculate", text: $expression)
// .textFieldStyle(.plain)
// .onSubmit {
//
// }
//
// }

}
},
// Run on every update of the text
onTextChange: { newExpression in
do {
solution = try evaluateExpression(newExpression)
}
catch {
solution = 0
}
})
.frame(height: 27) // MARK TODO: Make height-adjustable

Text(solution == 0 ? " " : solution.removeZerosFromEnd())
.font(.caption)
Expand Down
5 changes: 4 additions & 1 deletion Calculator/MacEditorTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* https://twitter.com/tholanda
*
* MIT license
*
* Edited by Anthony Ingle
*/

import Combine
Expand Down Expand Up @@ -116,6 +118,7 @@ extension MacEditorTextView {
self.parent.onCommit()
return true
}
// keeping commented for future use
// } else if (commandSelector == #selector(NSResponder.deleteForward(_:))) {
// // Do something against DELETE key
// return true
Expand Down Expand Up @@ -166,7 +169,7 @@ final class CustomTextView: NSView {
let scrollView = NSScrollView()
scrollView.drawsBackground = false
scrollView.borderType = .noBorder
scrollView.hasVerticalScroller = true
scrollView.hasVerticalScroller = false
scrollView.hasHorizontalRuler = false
scrollView.autoresizingMask = [.width, .height]
scrollView.translatesAutoresizingMaskIntoConstraints = false
Expand Down
165 changes: 0 additions & 165 deletions Calculator/TextFieldAppKit.swift

This file was deleted.

0 comments on commit 3d05200

Please sign in to comment.