From 485df875600558b858446ff6882e06d7013f24eb Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sun, 30 Aug 2020 15:49:37 +1000 Subject: [PATCH] Update vanilla template --- cmd/templates/vanilla/counter.go | 61 +++++++++++++++++++++ cmd/templates/vanilla/frontend/src/main.css | 15 ++++- cmd/templates/vanilla/frontend/src/main.js | 31 ++++++++--- cmd/templates/vanilla/main.go.template | 6 +- 4 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 cmd/templates/vanilla/counter.go diff --git a/cmd/templates/vanilla/counter.go b/cmd/templates/vanilla/counter.go new file mode 100644 index 000000000..6118da150 --- /dev/null +++ b/cmd/templates/vanilla/counter.go @@ -0,0 +1,61 @@ +package main + +import ( + "math/rand" + + "github.com/wailsapp/wails" +) + +// Counter is what we use for counting +type Counter struct { + r *wails.Runtime + store *wails.Store +} + +// WailsInit is called when the component is being initialised +func (c *Counter) WailsInit(runtime *wails.Runtime) error { + c.r = runtime + c.store = runtime.Store.New("Counter") + c.store.Set(0) + return nil +} + +// RandomValue sets the counter to a random value +func (c *Counter) RandomValue() { + c.store.Set(rand.Intn(1000)) +} + +func (c *Counter) getInt(data interface{}) int { + + switch value := data.(type) { + case float64: + // All numbers sent by the frontend are float64 + // so we need to convert it back to an int + return int(value) + default: + return value.(int) + } +} + +// Increment will increment the counter +func (c *Counter) Increment() { + + increment := func(data interface{}) interface{} { + currentValue := c.getInt(data) + return currentValue + 1 + } + + // Update the store using the increment function + c.store.Update(increment) +} + +// Decrement will decrement the counter +func (c *Counter) Decrement() { + + decrement := func(data interface{}) interface{} { + currentValue := c.getInt(data) + return currentValue - 1 + } + // Update the store using the decrement function + c.store.Update(decrement) +} diff --git a/cmd/templates/vanilla/frontend/src/main.css b/cmd/templates/vanilla/frontend/src/main.css index a02c19ab7..14a1ae5ac 100644 --- a/cmd/templates/vanilla/frontend/src/main.css +++ b/cmd/templates/vanilla/frontend/src/main.css @@ -2,8 +2,9 @@ html, body { background-color: white; - width: 1024px; - height: 768px; + width: 100%; + height: 100%; + margin: 0; } .container { @@ -18,6 +19,16 @@ button { font-size: 1rem; } +#newCounter { + color: white; +} + +.result { + margin-top: 3rem; + text-align: center; + font-size: 2rem; +} + .logo { display: block; margin-left: auto; diff --git a/cmd/templates/vanilla/frontend/src/main.js b/cmd/templates/vanilla/frontend/src/main.js index 6fd9f5c54..90d423d86 100644 --- a/cmd/templates/vanilla/frontend/src/main.js +++ b/cmd/templates/vanilla/frontend/src/main.js @@ -4,6 +4,8 @@ const runtime = require('@wailsapp/runtime'); // Main entry point function start() { + var mystore = wails.Store.New('Counter'); + // Ensure the default app div is 100% wide/high var app = document.getElementById('app'); app.style.width = '100%'; @@ -13,17 +15,32 @@ function start() { app.innerHTML = `
- -
+ + +
+
Counter:
+
+ + +
`; - // Connect button to Go method - document.getElementById('button').onclick = function() { - window.backend.basic().then( function(result) { - document.getElementById('result').innerText = result; - }); + // Connect counter value button to Go method + document.getElementById('setvalue').onclick = function() { + let newValue = parseInt(document.getElementById('newCounter').value,10); + mystore.set(newValue); }; + + mystore.subscribe( (state) => { + document.getElementById('counter').innerText = state; + }); + + mystore.set(0); }; // We provide our entrypoint as a callback for runtime.Init diff --git a/cmd/templates/vanilla/main.go.template b/cmd/templates/vanilla/main.go.template index c9b28960a..b1a0e04bd 100644 --- a/cmd/templates/vanilla/main.go.template +++ b/cmd/templates/vanilla/main.go.template @@ -5,10 +5,6 @@ import ( "github.com/wailsapp/wails" ) -func basic() string { - return "Hello World!" -} - func main() { js := mewn.String("./frontend/build/main.js") @@ -22,6 +18,6 @@ func main() { CSS: css, Colour: "#131313", }) - app.Bind(basic) + app.Bind(&Counter{}) app.Run() }