TokamakUI / Tokamak

SwiftUI-compatible framework for building browser apps with WebAssembly and native apps for other platforms
Apache License 2.0
2.62k stars 111 forks source link

Canvas gets resized when view is redrawn #474

Open Jomy10 opened 2 years ago

Jomy10 commented 2 years ago

Describe the bug When using .onAppear to set a @State variable, or when setting it in init(), the view gets redrawn repeatedly and the Canvas present in the view gets resized every time the view is resized.

The canvas also get resized slightly every time the view is redrawn for any reason.

To Reproduce Steps to reproduce the behavior:

Example:

struct TokamakApp: App {
    var body: some Scene {
        WindowGroup("App") {
            ContentView()
        }
    }
}

struct ContentView: View {
    @State var myVar = ""

    var body: some View {
        HStack {
            Canvas(renderer: { gc, size in
                print(size)
            })
            Text(self.myVar)
        }.onAppear {
            self.myVar = "new val"
        }
    }
}

Expected behavior The Text's value should be set to the value once and nothing should happen to the Canvas.

Screenshots

Tokamak running on Safari: https://user-images.githubusercontent.com/62475953/158156032-3841027a-c929-4dae-b0a5-97d8ea4f44d2.mov

SwiftUI (macOS): https://user-images.githubusercontent.com/62475953/158156580-9969ac1a-b73e-4a79-98eb-aab18b0005de.mov

Desktop (please complete the following information):