rensbreur / SwiftTUI

SwiftUI for terminal applications
MIT License
1.23k stars 47 forks source link

Updating Binding var from subView does not trigger redraw of parent/sibling #35

Closed ebeneezer closed 6 months ago

ebeneezer commented 6 months ago

I have a workSpaceView that fills the terminal like so:

`import SwiftTUI

struct workSpaceView: View {

@Binding var workSpaceSize : String

var body: some View {

    GeometryReader {proxy in
        workSpaceSize = proxy.description
        return ForEach(1 ... proxy.height.intValue,id : \.self) { row in
    //      Text(String(repeating: " ", count: proxy.width.intValue))
            Text("\(workSpaceSize)")
        }

    }.foregroundColor(workSpaceFG).background(workSpaceBG).frame( minWidth: 1, maxWidth: .infinity, minHeight: 1, maxHeight: .infinity, alignment: .topLeading)
 }

}`

and a main scrren that consists of a menu bar and the workSpaceView. This should become a simple textfile editor ...

`struct mainScr: View {

@State var workSpaceSize = "!x!"

var body: some View {

    VStack {
        menuBarView(workSpaceSize: workSpaceSize)
        workSpaceView(workSpaceSize: $workSpaceSize)
    }

}

}`

`import SwiftTUI

struct menuBarView: View {

@State var workSpaceSize: String

var body: some View {

    HStack {
        menuView()
        Spacer()
        statusView(workSpaceSize: workSpaceSize)
    }.frame(alignment: .leading).foregroundColor(menuBarFG).background(menuBarBG)

}

}`

When I resize the terminal the GeometryReader updates correctly ond writes its Size in every line on the screen (just for debugging) but the bound menuBarView does never get the new value of workSpaceSize and does not redraw.

Its like mainScreen / \ workSpaceView menuBarView

The value is changed inside workSpaceView goes up through the Binding into mainScreen and triggers redraw of menuBarView.

But it does not.

rensbreur commented 6 months ago

Updating state or binding from within a view's body is indeed not supported in SwiftUI or SwiftTUI. In this case you might want to see if you can achieve what you want by putting the GeometryReader in the root view.

ebeneezer commented 6 months ago

Dear Rens,

Thank you so much for your reply.

I am returning to Swift coding after being absend for 3 years. Still trying to get along with this SwiftUI stuff. SwiftUI is far from being straight forward in my optinion.

Anyways as i have retired now i wanted to code a text mode programmers editor and found your superb piece of work with SwiftTUI. Its a rewrite of American Cybernetics MultiEdit from the 90s for DOS under MacOS. So SwiftTUI seems a bullseye hit to me.

As the docs and examples for SwiftTUI are somewhat sparse i am trying to transpone SwiftUI docs towards SwiftTUI to understand what is going on. Indeed i found your comment in the sources of SwifTUI stating that there are no hierarchical view updates for Binding chained views.

I will do as adviced but am asking if you see any chance to implement upwards hierarchical updating for Binding views?

When i am fluently again with the new concepts i maybe try to make a pull request from your work and will tryto help. But alas i do need more time to exercise the new language mechanics..

Regards and please continue SwiftTUIs development.

Michael

Rens Breur @.***> schrieb am Do., 23. Mai 2024, 18:54:

Updating state or binding from within a view's body is indeed not supported in SwiftUI or SwiftTUI. In this case you might want to see if you can achieve what you want by putting the GeometryReader in the root view.

— Reply to this email directly, view it on GitHub https://github.com/rensbreur/SwiftTUI/issues/35#issuecomment-2127638097, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHSRFRM6ZRMQST34BWNGKTZDYNNHAVCNFSM6AAAAABIDLHT2CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRXGYZTQMBZG4 . You are receiving this because you authored the thread.Message ID: @.***>

ebeneezer commented 6 months ago

I close this issue as its a feature (for now) not an issue.