Skip to content

Commit

Permalink
feat: propagate showingCustomKeybiard state
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Oct 9, 2022
1 parent 3951286 commit 5e1840c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 41 deletions.
76 changes: 41 additions & 35 deletions PlantUML/PlantUMLEditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct PlantUMLEditorView: View {

@FocusState private var focusedItem: Focusable?

@State private var isEditorVisible = true
@State private var isEditorVisible = true
@State private var isPreviewVisible = true
@State private var isScaleToFit = true

@State private var isScaleToFit = true
@State private var showingKeyboard = false

var body: some View {
GeometryReader { geometry in
HStack {
Expand Down Expand Up @@ -67,7 +68,44 @@ struct PlantUMLEditorView: View {
}
}

// MARK: Editor View
func EditorView() -> some View {

List() {
ForEach( diagram.items ) { item in

HStack {
if editMode?.wrappedValue != .active {
Image(systemName: "plus")
.contextMenu {
AddBelowButton( theItem: item )
AddAboveButton( theItem: item )
AddCloneButton( theItem: item )
}
}

PlantUMLTextFieldWithCustomKeyboard( item: item,
showingKeyboard: $showingKeyboard,
onChange: updateItem,
onAddNew: addNewItem )
.focused($focusedItem, equals: .row(id: item.id))
}

}
.onMove(perform: move)
.onDelete( perform: delete)

}
.onChange( of: showingKeyboard ) {
print( "showingKeyboard: \($0)")
}
.font(.footnote)
.listStyle(SidebarListStyle())


}


func SaveButton() -> some View {

Button( action: saveToDocument ) {
Expand Down Expand Up @@ -153,38 +191,6 @@ struct PlantUMLEditorView: View {
}
}

// MARK: Editor View
func EditorView() -> some View {

List() {
ForEach( diagram.items ) { item in

HStack {
if editMode?.wrappedValue != .active {
Image(systemName: "plus")
.contextMenu {
AddBelowButton( theItem: item )
AddAboveButton( theItem: item )
AddCloneButton( theItem: item )
}
}

PlantUMLTextFieldWithCustomKeyboard( item: item,
onChange: updateItem,
onAddNew: addNewItem )
.focused($focusedItem, equals: .row(id: item.id))
}

}
.onMove(perform: move)
.onDelete( perform: delete)

}
.font(.footnote)
.listStyle(SidebarListStyle())

}


}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ public struct PlantUMLTextFieldWithCustomKeyboard : UIViewRepresentable {
public typealias UIViewType = UITextField
private let textField = UITextField()


public var item:SyntaxStructure
public var onChange:ChangeHandler
public var onAddNew:AddNewActionHandler

@Binding private var showingKeyboard: Bool

public init( item:SyntaxStructure,
showingKeyboard: Binding<Bool>,
onChange:@escaping ChangeHandler,
onAddNew:@escaping AddNewActionHandler
) {
self.item = item
self._showingKeyboard = showingKeyboard
self.onChange = onChange
self.onAddNew = onAddNew
}
Expand All @@ -47,6 +50,7 @@ public struct PlantUMLTextFieldWithCustomKeyboard : UIViewRepresentable {
textField.font = UIFont.monospacedSystemFont(ofSize: 15, weight: .regular)
textField.returnKeyType = .done
textField.text = item.rawValue

return textField
}

Expand All @@ -72,7 +76,6 @@ extension PlantUMLTextFieldWithCustomKeyboard {

public class Coordinator: NSObject, UITextFieldDelegate, CustomKeyboardPresenter {


private var keyboardRect:CGRect = .zero
private let owner : PlantUMLTextFieldWithCustomKeyboard
private var showCustomKeyboard:Bool
Expand Down Expand Up @@ -100,7 +103,6 @@ extension PlantUMLTextFieldWithCustomKeyboard {

}


public init(textfield : PlantUMLTextFieldWithCustomKeyboard) {
self.owner = textfield
self.showCustomKeyboard = false
Expand All @@ -112,7 +114,6 @@ extension PlantUMLTextFieldWithCustomKeyboard {

updateAccesoryView()


}

func updateAccesoryView() {
Expand All @@ -129,10 +130,11 @@ extension PlantUMLTextFieldWithCustomKeyboard {
]
bar.sizeToFit()
owner.textField.inputAccessoryView = bar

}

}

public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

if let text = textField.text, let range = Range(range, in: text) {
Expand Down Expand Up @@ -164,7 +166,6 @@ extension PlantUMLTextFieldWithCustomKeyboard {
self.owner.onAddNew(owner.item, .BELOW, "")
}


@objc public func addAbove() {
self.owner.onAddNew(owner.item, .ABOVE, "")
}
Expand All @@ -181,6 +182,7 @@ extension PlantUMLTextFieldWithCustomKeyboard {
owner.textField.inputView = nil
}
owner.textField.reloadInputViews()
self.owner.showingKeyboard = showCustomKeyboard

}

Expand Down

0 comments on commit 5e1840c

Please sign in to comment.