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

default callback handler that resolve files archived using packfolder #163

Closed mchampaneri closed 6 years ago

mchampaneri commented 6 years ago

There should be a defaultCallBackHandler function that resolves files from archive.

Function

func (sc *Sciter) DefaultCallBackHandler() {
    callback := &CallbackHandler{
        OnLoadData: func(params *ScnLoadData) int {
            if strings.HasPrefix(params.Uri(), "file://") {
                fileData := sc.GetArchiveItem(params.Uri()[7:])
                sc.DataReady(params.Uri()[7:], fileData)
            }
            return 0
        },
    }
    sc.SetCallback(callback)
}

Useage

func main() {

    rect := sciter.NewRect(100, 100, 300, 500)  
    win, _ := window.New(sciter.SW_MAIN|sciter.SW_CONTROLS|sciter.SW_ENABLE_DEBUG, rect)

    win.DefaultCallBackHandler() //  Will resolved file path during OnLoadData event
    win.OpenArchive(resources)

    htbytes := win.GetArchiveItem("notepad.htm")
    win.LoadHtml(string(htbytes), "")
    win.SetTitle("Demo")

    win.Show()
    win.Run()

    win.CloseArchive()
}
pravic commented 6 years ago

I think, this can be improved:

  // win.DefaultCallBackHandler()
  // win.OpenArchive(resources)

  // usage:
  win.SetResourceArchive(resources)
  win.LoadFile("this://app//notepad.htm")

  // win.SetTitle("Demo") - there is no point in this call if notepad.htm contains <title>
// in library
func (sc *Sciter) SetResourceArchive(data []byte) {
  sc.OpenArchive(data)
  sc.SetCallback(...)
}
pravic commented 6 years ago

Although I see your point with file:// (i.e. use archived items if available, fallback to files in filesystem otherwise - aka dev/production environment), but I am not sure about this, since Sciter itself does not use this approach.

Sciter uses a hard coded schema (this://app/) that relies on changing URLs in your app:


// dev/test environment, use local URLs:
// assume that 'index.htm` is in the current directory (common situation)
dir, _ := os.Getwd()
win.LoadFile(dir + "/index.htm")

// production environment
// use archived resources
win.LoadFile("this://app//index.htm")

Also keep in mind that HTML itself uses relative paths anyway:

<style src="main.css" />
<script type="text/tiscript" src="main.tis"/>
<body></body>

If you call LoadFile("~/sciter/index.htm"), main.css will be loaded from ~/sciter/main.css. If you call LoadFile("this://app/index.htm"), main.css will be loaded from this://app/main.css.

So you need to change only URL of the main file.

mchampaneri commented 6 years ago

@pravic

So you need to change only URL of the main file.

Actully , Its not working for me. When i just loaded main.html, It was not able to find style.css file using relative path.

I have to load every file from archive on their url using sc.DataReady(url, file-bytes)

pravic commented 6 years ago

Again, to make it work you need to load the main html using an absolute url. Either via LoadHtml or LoadFile. Or show your code, I might miss something.

mchampaneri commented 6 years ago

@pravic

I have uploaded zip file containing code.

It contains 4 resource files, 2 go files and packfolder.exe

During Callback function I have to load css file to their relative url using

    win.GetArchiveItem(params.Uri()[7:])
    win.DataReady(params.Uri()[7:], fileData)

Otherwise css files are not get found,

Callback function loads the files according to the html file I am loading from archive using GetArchiveItem

08-packfolderIntro.zip

pravic commented 6 years ago

win.LoadHtml(string(htbytes), "")

As I said, this is the problem (the empty URL). Either write win.LoadHtml(string(htbytes), "this://app/abc.html") or win.LoadFile("this://app/abc.html").

It works fine with CSS, check it.

pravic commented 6 years ago

25d8ed4 contains SetResourceArchive and a usage example (examples/restest2).

mchampaneri commented 6 years ago

@pravic

I understood what was the problem after reviewing your commit.

Thanks

pravic commented 6 years ago

Okay than. Thanks for bringing this up.