sciter-sdk / go-sciter

Golang bindings of Sciter: the Embeddable HTML/CSS/script engine for modern UI development
https://sciter.com
2.57k stars 268 forks source link

How to access DOM after calling window.Run? #270

Closed jilieryuyi closed 3 years ago

jilieryuyi commented 3 years ago

求教,如题!

jilieryuyi commented 3 years ago

Update(true) 没有更新界面

pravic commented 3 years ago

English?

jilieryuyi commented 3 years ago

atfer call "Run" api, win.Show() win.Run() use root.Append(element), how to reflesh window? Update(true) not work!!

jilieryuyi commented 3 years ago

use golang Append element

pravic commented 3 years ago

Could you show your code?

Also, check examples: https://github.com/sciter-sdk/go-sciter/tree/master/examples

jilieryuyi commented 3 years ago
package main

import (
    "fmt"
    "github.com/sciter-sdk/go-sciter"
    "github.com/sciter-sdk/go-sciter/window"
    "log"
    "time"
)

var (
    win *window.Window
)

func tick() {
    go func() {
        startTime := time.Now()
        tick := time.NewTicker(time.Second)
        defer tick.Stop()
        for {
            select {
            case <- tick.C:
                root, err := win.GetRootElement()
                if root == nil {
                    fmt.Println(err)
                    continue
                }
                t, err := root.SelectFirst("#timer")
                if t == nil {
                    fmt.Println(err)
                    continue
                }
                t.SetText(fmt.Sprintf("%v", time.Since(startTime)))
                t.Update(true)
            }
        }
    }()
}

func main() {
    defer func() {
        if err := recover(); err != nil {
            fmt.Println(err)
        }
    }()

    var err error
    sciter.SetOption(sciter.SCITER_SET_SCRIPT_RUNTIME_FEATURES, sciter.ALLOW_SYSINFO)
    win, err = window.New(
        sciter.SW_TITLEBAR|
            sciter.SW_RESIZEABLE|
            sciter.SW_CONTROLS|
            sciter.SW_MAIN|
            sciter.SW_ENABLE_DEBUG, &sciter.Rect{
            Left:   0,
            Top:    0,
            Right:  500,
            Bottom: 600,
        })
    if err != nil {
        log.Fatal("Create Window Error: ", err)
    }
    win.SetTitle("some test")
    win.LoadFile("index.html")
    // UI will be blocked here. UI can't be displayed normally. Can I use the goroutine here?
    go tick()
    win.Show()
    win.Run()
}

why ui block?

pravic commented 3 years ago

Because Run() function is synchronous and it is a loop essentially.

Use events instead. Also, you can access the DOM after LoadFile() but before Run(): https://github.com/sciter-sdk/go-sciter/blob/master/examples/insert/insert.go#L19

jilieryuyi commented 3 years ago

Do you have any examples of using events?

jilieryuyi commented 3 years ago

Because Run() function is synchronous and it is a loop essentially.

Use events instead. Also, you can access the DOM after LoadFile() but before Run(): https://github.com/sciter-sdk/go-sciter/blob/master/examples/insert/insert.go#L19

Do you have any examples of using events?

jilieryuyi commented 3 years ago

Because Run() function is synchronous and it is a loop essentially.

Use events instead. Also, you can access the DOM after LoadFile() but before Run(): https://github.com/sciter-sdk/go-sciter/blob/master/examples/insert/insert.go#L19

how to use Task in UI multi-goroutine?

jilieryuyi commented 3 years ago

https://github.com/sciter-sdk/go-sciter/issues/133 I found this