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 = `