5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-20 19:09:29 +08:00

[examples] update label on click

This commit is contained in:
Travis McLane 2023-04-27 14:39:31 -05:00
parent 68cfd130d3
commit a9d834d715
7 changed files with 100 additions and 60 deletions

View File

@ -3,6 +3,7 @@ module github.com/wailsapp/wails/v3
go 1.19 go 1.19
require ( require (
github.com/ebitengine/purego v0.3.2
github.com/go-task/task/v3 v3.20.0 github.com/go-task/task/v3 v3.20.0
github.com/google/go-cmp v0.5.9 github.com/google/go-cmp v0.5.9
github.com/jackmordaunt/icns/v2 v2.2.1 github.com/jackmordaunt/icns/v2 v2.2.1
@ -72,3 +73,5 @@ require (
) )
replace github.com/wailsapp/wails/v2 => ../v2 replace github.com/wailsapp/wails/v2 => ../v2
replace github.com/ebitengine/purego v0.3.2 => github.com/TotallyGamerJet/purego v0.2.0-alpha.0.20230404174033-5655abccca7e

View File

@ -11,6 +11,8 @@ github.com/MarvinJWendt/testza v0.2.12/go.mod h1:JOIegYyV7rX+7VZ9r77L/eH6CfJHHzX
github.com/MarvinJWendt/testza v0.3.0/go.mod h1:eFcL4I0idjtIx8P9C6KkAuLgATNKpX4/2oUqKc6bF2c= github.com/MarvinJWendt/testza v0.3.0/go.mod h1:eFcL4I0idjtIx8P9C6KkAuLgATNKpX4/2oUqKc6bF2c=
github.com/MarvinJWendt/testza v0.4.2/go.mod h1:mSdhXiKH8sg/gQehJ63bINcCKp7RtYewEjXsvsVUPbE= github.com/MarvinJWendt/testza v0.4.2/go.mod h1:mSdhXiKH8sg/gQehJ63bINcCKp7RtYewEjXsvsVUPbE=
github.com/MarvinJWendt/testza v0.5.1 h1:a9Fqx6vQrHQ4CyiaLhktfTTelwGotmFWy8MNhyaohw8= github.com/MarvinJWendt/testza v0.5.1 h1:a9Fqx6vQrHQ4CyiaLhktfTTelwGotmFWy8MNhyaohw8=
github.com/TotallyGamerJet/purego v0.2.0-alpha.0.20230404174033-5655abccca7e h1:wQ7ot+e0mwJYkbomtIX9tU0dOV9lFTmwAgUGqvQTIUg=
github.com/TotallyGamerJet/purego v0.2.0-alpha.0.20230404174033-5655abccca7e/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk= github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw= github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U= github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=

View File

@ -363,6 +363,13 @@ func (a *App) Run() error {
} }
}() }()
a.impl.setApplicationMenu(a.ApplicationMenu)
// set the application menu
if runtime.GOOS == "darwin" {
a.impl.setIcon(a.options.Icon)
}
// run windows // run windows
for _, window := range a.windows { for _, window := range a.windows {
go window.run() go window.run()
@ -373,12 +380,6 @@ func (a *App) Run() error {
go systray.Run() go systray.Run()
} }
// set the application menu
if runtime.GOOS == "darwin" {
a.impl.setApplicationMenu(a.ApplicationMenu)
a.impl.setIcon(a.options.Icon)
}
err := a.impl.run() err := a.impl.run()
if err != nil { if err != nil {
return err return err

View File

@ -185,6 +185,7 @@ func (m *macosApp) run() error {
C.setApplicationShouldTerminateAfterLastWindowClosed(C.bool(m.parent.options.Mac.ApplicationShouldTerminateAfterLastWindowClosed)) C.setApplicationShouldTerminateAfterLastWindowClosed(C.bool(m.parent.options.Mac.ApplicationShouldTerminateAfterLastWindowClosed))
C.setActivationPolicy(C.int(m.parent.options.Mac.ActivationPolicy)) C.setActivationPolicy(C.int(m.parent.options.Mac.ActivationPolicy))
C.activateIgnoringOtherApps() C.activateIgnoringOtherApps()
globalApplication.activate()
}) })
// setup event listeners // setup event listeners
for eventID := range m.parent.applicationEventListeners { for eventID := range m.parent.applicationEventListeners {

View File

@ -4,19 +4,16 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strconv"
) )
func (m *MessageProcessor) callErrorCallback(window *WebviewWindow, message string, callID *string, err error) { func (m *MessageProcessor) callErrorCallback(window *WebviewWindow, message string, callID *string, err error) {
errorMsg := fmt.Sprintf(message, err) errorMsg := fmt.Sprintf(message, err)
m.Error(errorMsg) m.Error(errorMsg)
msg := "_wails.callErrorCallback('" + *callID + "', " + strconv.Quote(errorMsg) + ");" window.CallError(callID, errorMsg)
window.ExecJS(msg)
} }
func (m *MessageProcessor) callCallback(window *WebviewWindow, callID *string, result string, isJSON bool) { func (m *MessageProcessor) callCallback(window *WebviewWindow, callID *string, result string, isJSON bool) {
msg := fmt.Sprintf("_wails.callCallback('%s', %s, %v);", *callID, strconv.Quote(result), isJSON) window.CallResponse(callID, result)
window.ExecJS(msg)
} }
func (m *MessageProcessor) processCallMethod(method string, rw http.ResponseWriter, _ *http.Request, window *WebviewWindow, params QueryParams) { func (m *MessageProcessor) processCallMethod(method string, rw http.ResponseWriter, _ *http.Request, window *WebviewWindow, params QueryParams) {

View File

@ -5,19 +5,16 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"runtime" "runtime"
"strconv"
) )
func (m *MessageProcessor) dialogErrorCallback(window *WebviewWindow, message string, dialogID *string, err error) { func (m *MessageProcessor) dialogErrorCallback(window *WebviewWindow, message string, dialogID *string, err error) {
errorMsg := fmt.Sprintf(message, err) errorMsg := fmt.Sprintf(message, err)
m.Error(errorMsg) m.Error(errorMsg)
msg := "_wails.dialogErrorCallback('" + *dialogID + "', " + strconv.Quote(errorMsg) + ");" window.DialogError(dialogID, errorMsg)
window.ExecJS(msg)
} }
func (m *MessageProcessor) dialogCallback(window *WebviewWindow, dialogID *string, result string, isJSON bool) { func (m *MessageProcessor) dialogCallback(window *WebviewWindow, dialogID *string, result string, isJSON bool) {
msg := fmt.Sprintf("_wails.dialogCallback('%s', %s, %v);", *dialogID, strconv.Quote(result), isJSON) window.DialogResponse(dialogID, result)
window.ExecJS(msg)
} }
func (m *MessageProcessor) processDialogMethod(method string, rw http.ResponseWriter, r *http.Request, window *WebviewWindow, params QueryParams) { func (m *MessageProcessor) processDialogMethod(method string, rw http.ResponseWriter, r *http.Request, window *WebviewWindow, params QueryParams) {

View File

@ -1,12 +1,14 @@
package application package application
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/samber/lo"
"sync" "sync"
"time" "time"
"github.com/samber/lo"
"github.com/wailsapp/wails/v3/pkg/logger" "github.com/wailsapp/wails/v3/pkg/logger"
"github.com/wailsapp/wails/v3/pkg/events" "github.com/wailsapp/wails/v3/pkg/events"
@ -14,54 +16,57 @@ import (
type ( type (
webviewWindowImpl interface { webviewWindowImpl interface {
setTitle(title string)
setSize(width, height int)
setAlwaysOnTop(alwaysOnTop bool)
setURL(url string)
setResizable(resizable bool)
setMinSize(width, height int)
setMaxSize(width, height int)
execJS(js string)
setBackgroundColour(color RGBA)
run()
center() center()
size() (int, int)
width() int
height() int
position() (int, int)
destroy()
reload()
forceReload()
toggleDevTools()
zoomReset()
zoomIn()
zoomOut()
getZoom() float64
setZoom(zoom float64)
close() close()
zoom() destroy()
setHTML(html string) disableSizeConstraints()
setPosition(x int, y int) execJS(js string)
on(eventID uint) focus()
minimise() forceReload()
unminimise()
maximise()
unmaximise()
fullscreen() fullscreen()
unfullscreen() getScreen() (*Screen, error)
isMinimised() bool getZoom() float64
isMaximised() bool height() int
hide()
hide()
isFullscreen() bool isFullscreen() bool
isMaximised() bool
isMinimised() bool
isNormal() bool isNormal() bool
isVisible() bool isVisible() bool
setFullscreenButtonEnabled(enabled bool) maximise()
focus() minimise()
show()
hide()
getScreen() (*Screen, error)
setFrameless(bool)
openContextMenu(menu *Menu, data *ContextMenuData)
nativeWindowHandle() uintptr nativeWindowHandle() uintptr
on(eventID uint)
openContextMenu(menu *Menu, data *ContextMenuData)
position() (int, int)
reload()
restore() // FIXME: not used
run()
setAlwaysOnTop(alwaysOnTop bool)
setBackgroundColour(color RGBA)
setFrameless(bool)
setFullscreenButtonEnabled(enabled bool)
setHTML(html string)
setMaxSize(width, height int)
setMinSize(width, height int)
setPosition(x int, y int)
setResizable(resizable bool)
setSize(width, height int)
setTitle(title string)
setURL(url string)
setZoom(zoom float64)
show()
size() (int, int)
toggleDevTools()
unfullscreen()
unmaximise()
unminimise()
width() int
zoom()
zoomIn()
zoomOut()
zoomReset()
} }
) )
@ -131,6 +136,39 @@ func (w *WebviewWindow) addCancellationFunction(canceller func()) {
w.cancellers = append(w.cancellers, canceller) w.cancellers = append(w.cancellers, canceller)
} }
// formatJS ensures the 'data' provided marshals to valid json or panics
func (w *WebviewWindow) formatJS(f string, callID string, data string) string {
j, err := json.Marshal(data)
if err != nil {
panic(err)
}
return fmt.Sprintf(f, callID, j)
}
func (w *WebviewWindow) CallError(callID *string, result string) {
if w.impl != nil {
w.impl.execJS(w.formatJS("_wails.callErrorCallback('%s', %s);", *callID, result))
}
}
func (w *WebviewWindow) CallResponse(callID *string, result string) {
if w.impl != nil {
w.impl.execJS(w.formatJS("_wails.callCallback('%s', %s, true);", *callID, result))
}
}
func (w *WebviewWindow) DialogError(dialogID *string, result string) {
if w.impl != nil {
w.impl.execJS(w.formatJS("_wails.dialogErrorCallback('%s', %s);", *dialogID, result))
}
}
func (w *WebviewWindow) DialogResponse(dialogID *string, result string) {
if w.impl != nil {
w.impl.execJS(w.formatJS("_wails.dialogCallback('%s', %s, true);", *dialogID, result))
}
}
// SetTitle sets the title of the window // SetTitle sets the title of the window
func (w *WebviewWindow) SetTitle(title string) *WebviewWindow { func (w *WebviewWindow) SetTitle(title string) *WebviewWindow {
w.implLock.RLock() w.implLock.RLock()
@ -744,8 +782,9 @@ func (w *WebviewWindow) SetFrameless(frameless bool) *WebviewWindow {
} }
func (w *WebviewWindow) dispatchWailsEvent(event *WailsEvent) { func (w *WebviewWindow) dispatchWailsEvent(event *WailsEvent) {
msg := fmt.Sprintf("_wails.dispatchWailsEvent(%s);", event.ToJSON()) if w.impl != nil {
w.ExecJS(msg) w.impl.execJS(fmt.Sprintf("_wails.dispatchWailsEvent(%s);", event.ToJSON()))
}
} }
func (w *WebviewWindow) info(message string, args ...any) { func (w *WebviewWindow) info(message string, args ...any) {