html_container.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package ethui
  2. import (
  3. "errors"
  4. "github.com/ethereum/eth-go/ethchain"
  5. "github.com/ethereum/eth-go/ethpub"
  6. "github.com/ethereum/eth-go/ethutil"
  7. "github.com/go-qml/qml"
  8. "math/big"
  9. "path/filepath"
  10. )
  11. type HtmlApplication struct {
  12. win *qml.Window
  13. webView qml.Object
  14. engine *qml.Engine
  15. lib *UiLib
  16. path string
  17. }
  18. func NewHtmlApplication(path string, lib *UiLib) *HtmlApplication {
  19. engine := qml.NewEngine()
  20. return &HtmlApplication{engine: engine, lib: lib, path: path}
  21. }
  22. func (app *HtmlApplication) Create() error {
  23. component, err := app.engine.LoadFile(app.lib.AssetPath("qml/webapp.qml"))
  24. if err != nil {
  25. return err
  26. }
  27. if filepath.Ext(app.path) == "eth" {
  28. return errors.New("Ethereum package not yet supported")
  29. // TODO
  30. ethutil.OpenPackage(app.path)
  31. }
  32. win := component.CreateWindow(nil)
  33. win.Set("url", app.path)
  34. webView := win.ObjectByName("webView")
  35. app.win = win
  36. app.webView = webView
  37. return nil
  38. }
  39. func (app *HtmlApplication) Engine() *qml.Engine {
  40. return app.engine
  41. }
  42. func (app *HtmlApplication) Window() *qml.Window {
  43. return app.win
  44. }
  45. func (app *HtmlApplication) NewBlock(block *ethchain.Block) {
  46. b := &ethpub.PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Hex(block.Hash())}
  47. app.webView.Call("onNewBlockCb", b)
  48. }
  49. func (app *HtmlApplication) ObjectChanged(stateObject *ethchain.StateObject) {
  50. app.webView.Call("onObjectChangeCb", ethpub.NewPStateObject(stateObject))
  51. }
  52. func (app *HtmlApplication) StorageChanged(stateObject *ethchain.StateObject, addr []byte, value *big.Int) {
  53. app.webView.Call("onStorageChangeCb", nil)
  54. }
  55. func (app *HtmlApplication) Destroy() {
  56. app.engine.Destroy()
  57. }