Skip to content

Commit

Permalink
feat: rewrite inputfield and fix bqn errors
Browse files Browse the repository at this point in the history
  • Loading branch information
x86y committed Oct 16, 2023
1 parent e1052fa commit 75f9fda
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 20 deletions.
6 changes: 6 additions & 0 deletions Beacon.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
F31BCE7C2A33B3A200A6116D /* CustomInputField.swift in Sources */ = {isa = PBXBuildFile; fileRef = F31BCE7A2A33B3A200A6116D /* CustomInputField.swift */; };
F31BCE7E2A348B7500A6116D /* HelpView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F31BCE7D2A348B7500A6116D /* HelpView.swift */; };
F31BCE7F2A348B7500A6116D /* HelpView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F31BCE7D2A348B7500A6116D /* HelpView.swift */; };
F327CA1A2ADD0A2F00FD72D9 /* SwiftUIInput.swift in Sources */ = {isa = PBXBuildFile; fileRef = F327CA192ADD0A2F00FD72D9 /* SwiftUIInput.swift */; };
F327CA1B2ADD0A2F00FD72D9 /* SwiftUIInput.swift in Sources */ = {isa = PBXBuildFile; fileRef = F327CA192ADD0A2F00FD72D9 /* SwiftUIInput.swift */; };
F32D9E8429966FCD007BC97C /* Beacon-Bridging-Header.h in Sources */ = {isa = PBXBuildFile; fileRef = F312CF8729951C49008EC197 /* Beacon-Bridging-Header.h */; };
F32D9E8529966FCD007BC97C /* BeaconApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3B198CD299517DA00FE664F /* BeaconApp.swift */; };
F32D9E8629966FCD007BC97C /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3877C8B29952A5400E2FCB5 /* ContentView.swift */; };
Expand Down Expand Up @@ -100,6 +102,7 @@
F31BCE772A33B33E00A6116D /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
F31BCE7A2A33B3A200A6116D /* CustomInputField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomInputField.swift; sourceTree = "<group>"; };
F31BCE7D2A348B7500A6116D /* HelpView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HelpView.swift; sourceTree = "<group>"; };
F327CA192ADD0A2F00FD72D9 /* SwiftUIInput.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUIInput.swift; sourceTree = "<group>"; };
F32D9E9429966FCD007BC97C /* BeaconSim.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BeaconSim.app; sourceTree = BUILT_PRODUCTS_DIR; };
F32D9E9529966FCD007BC97C /* Beacon copy-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "Beacon copy-Info.plist"; path = "/Users/sp/mac/applefarm/Beacon/Beacon copy-Info.plist"; sourceTree = "<absolute>"; };
F3512E082A4CE18D00658F24 /* libk.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libk.a; path = Beacon/Libs/libk.a; sourceTree = "<group>"; };
Expand Down Expand Up @@ -173,6 +176,7 @@
isa = PBXGroup;
children = (
F31BCE7A2A33B3A200A6116D /* CustomInputField.swift */,
F327CA192ADD0A2F00FD72D9 /* SwiftUIInput.swift */,
);
path = Components;
sourceTree = "<group>";
Expand Down Expand Up @@ -450,6 +454,7 @@
F30A7B8B2A370B7800503966 /* BuffersView.swift in Sources */,
F31BCE7F2A348B7500A6116D /* HelpView.swift in Sources */,
F32D9E8529966FCD007BC97C /* BeaconApp.swift in Sources */,
F327CA1B2ADD0A2F00FD72D9 /* SwiftUIInput.swift in Sources */,
F31BCE7C2A33B3A200A6116D /* CustomInputField.swift in Sources */,
F32D9E8629966FCD007BC97C /* ContentView.swift in Sources */,
);
Expand All @@ -467,6 +472,7 @@
F30A7B8A2A370B7800503966 /* BuffersView.swift in Sources */,
F31BCE7E2A348B7500A6116D /* HelpView.swift in Sources */,
F3B198CE299517DA00FE664F /* BeaconApp.swift in Sources */,
F327CA1A2ADD0A2F00FD72D9 /* SwiftUIInput.swift in Sources */,
F31BCE7B2A33B3A200A6116D /* CustomInputField.swift in Sources */,
F32D9E972996710A007BC97C /* ContentView.swift in Sources */,
);
Expand Down
2 changes: 1 addition & 1 deletion Beacon/BeaconApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct BeaconApp: App {
TabView(selection: $selectedView) {
ContentView(viewModel: viewModel).tag(0)
.preferredColorScheme(appearanceSwitch)
Dashboard().tag(1)
//Dashboard().tag(1)
}.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
.onChange(of: selectedView) {
dismissKeyboard()
Expand Down
39 changes: 29 additions & 10 deletions Beacon/Components/CustomInputField.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
//
// CustomInputField.swift
// Beacon
//
//
//

import SwiftUI
import UIKit

class NewlineTextView: UITextView {
override var keyCommands: [UIKeyCommand]? {
return [
UIKeyCommand(input: "\r", modifierFlags: [.shift], action: #selector(shiftEnterPressed)),
]
}

@objc func shiftEnterPressed() {
let cursorPosition = selectedRange
text.insert("\n", at: text.index(text.startIndex, offsetBy: cursorPosition.location))
selectedRange = NSRange(location: cursorPosition.location + 1, length: 0)
}
}

struct CustomInputField: UIViewRepresentable {
@Binding var text: String
@Binding var helpOpen: Bool
Expand Down Expand Up @@ -45,20 +56,24 @@ struct CustomInputField: UIViewRepresentable {
}
}

func textViewDidChange(_ textView: UITextView) {
textView.invalidateIntrinsicContentSize()
}

func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
let textView = NewlineTextView()
textView.delegate = context.coordinator
textView.font = font
textView.keyboardType = keyboardType
textView.returnKeyType = .done
textView.autocorrectionType = .no
textView.translatesAutoresizingMaskIntoConstraints = false
textView.isScrollEnabled = true
textView.becomeFirstResponder()
textView.showsVerticalScrollIndicator = true
textView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
textView.textContainer.lineBreakMode = NSLineBreakMode.byWordWrapping
textView.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 500), for: NSLayoutConstraint.Axis.vertical)
textView.isScrollEnabled = false
textView.layer.cornerRadius = 16
textView.layer.borderWidth = 1
textView.layer.borderColor = UIColor.gray.cgColor
textView.textContainerInset = UIEdgeInsets(top: 8, left: 10, bottom: 8, right: 10)
textView.sizeToFit()

let stickyRightView = UIStackView()
stickyRightView.axis = .horizontal
Expand Down Expand Up @@ -134,6 +149,10 @@ struct CustomInputField: UIViewRepresentable {
parent = textView
}

@objc func onSubmit() {
parent.onSubmit?()
}

func textViewDidChange(_ textView: UITextView) {
parent.text = textView.text
}
Expand Down
92 changes: 92 additions & 0 deletions Beacon/Components/SwiftUIInput.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import SwiftUI

struct SwiftUIInputField: View {
@Binding var text: String
@Binding var helpOpen: Bool
@Binding var settingsOpen: Bool
@Binding var buffersOpen: Bool
var lang: Language
var onSubmit: (() -> Void)?
var font: Font
var letters: [String] {
lang == .bqn ? glyphs : ["'", "/", "\\", "%", "*", "+", "-", "=", "!", "\"", ":"]
}

var body: some View {
HStack {
ScrollView(.horizontal) {
HStack {
ForEach(letters, id: \.self) { letter in
Button(letter) {
text.append(letter)
}
.font(.custom("BQN386 Unicode", size: 16))
.padding(8)
.background(.gray.opacity(0.1))
.foregroundStyle(.primary)
.clipShape(RoundedRectangle(cornerRadius: 8))
}
}
}

HStack {
Button(action: { text = "" }) {
Image(systemName: "arrow.left.square.fill")
.resizable()
.frame(width: 24.0, height: 24.0)
}
Button(action: { helpOpen.toggle() }) {
Image(systemName: "questionmark.app.fill")
.resizable()
.frame(width: 24.0, height: 24.0)
}
Button(action: { buffersOpen.toggle() }) {
Image(systemName: "list.bullet.rectangle.portrait.fill")
.resizable()
.frame(width: 24.0, height: 24.0)
}
Button(action: { settingsOpen.toggle() }) {
Image(systemName: "command.square.fill")
.resizable()
.frame(width: 24.0, height: 24.0)
}
}
}

HStack {
TextField("Type some \(languageToString(l: lang)) code..", text: $text, axis: .vertical)
.onSubmit {
onSubmit?()
}
.lineLimit(1 ... 8)
.keyboardType(.asciiCapable)
.submitLabel(.done)
.disableAutocorrection(true)
.font(font)
.frame(minHeight: 48)
.padding(EdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8))
.overlay(
RoundedRectangle(cornerRadius: 18)
.stroke(.gray.opacity(0.25), lineWidth: 1)
).modifier(EvalButton(onSubmit: onSubmit))
}
}
}

struct EvalButton: ViewModifier {
var onSubmit: (() -> Void)?

public func body(content: Content) -> some View {
ZStack(alignment: .trailing) {
content
Button(action: {
onSubmit?()
}) {
Image(systemName: "restart.circle")
.resizable()
.frame(width: 24.0, height: 24.0)
}
.padding(8)
}
}
}
2 changes: 2 additions & 0 deletions Beacon/Utilities/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func e(input: String) -> String {
let filename = i[1].replacingOccurrences(of: "\"", with: "")
input = "\(vars) •Import \"\(Bundle.main.resourcePath!)/bqn-libs/\(filename)\""
}
input = input.replacingOccurrences(of: "\"", with: #""""#)
input = "•Out ((•ReBQN{repl⇐\"loose\"})⎊{𝕊: \"Error: \"∾•CurrentError@}) \"\(input)\""
return runCmd(cbqnCmd, input)
}

Expand Down
8 changes: 8 additions & 0 deletions Beacon/Views/ConfigView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ enum Language: Int {
case k = 1
}

func languageToString(l: Language) -> String {
if l == .bqn {
return "BQN"
} else {
return "K"
}
}

enum Behavior: Int {
case inlineEdit = 0
case duplicate = 1
Expand Down
15 changes: 6 additions & 9 deletions Beacon/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,12 @@ struct ContentView: View {
}.padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10))
.padding(.bottom, 5)

CustomInputField(text: $input,
helpB: $showHelp,
settingsB: $showSettings,
buffersB: $showBuffers,
onSubmit: { onMySubmit(input: self.input) },
font: UIFont(name: "BQN386 Unicode", size: 20)!,
keyboardType: .asciiCapable,
lang: self.lang)
.frame(height: 24)
SwiftUIInputField(text: $input,
helpOpen: $showHelp,
settingsOpen: $showSettings,
buffersOpen: $showBuffers,
lang: self.lang, onSubmit: { onMySubmit(input: self.input) },
font: Font.custom("BQN386 Unicode", size: 20))
.padding(.bottom, 4)
.focused($isFocused)
.onTapGesture {
Expand Down

0 comments on commit 75f9fda

Please sign in to comment.