wails/v3/pkg/application/messageprocessor.go
Lea Anthony 3087ba0bdc
Refactor Manager API to use singular naming convention (#4367)
* Refactor Manager API to use singular naming convention

This is a RENAME-ONLY exercise that converts the Wails v3 Manager API from plural to singular naming for better consistency and clarity.

## Changes Applied

### API Transformations:
- `app.Windows.*` → `app.Window.*`
- `app.Events.*` → `app.Event.*`
- `app.ContextMenus.*` → `app.ContextMenu.*`
- `app.KeyBindings.*` → `app.KeyBinding.*`
- `app.Dialogs.*` → `app.Dialog.*`
- `app.Menus.*` → `app.Menu.*`
- `app.Screens.*` → `app.Screen.*`

### Files Updated:
- **Core Application**: 22 files in `v3/pkg/application/`
- **Examples**: 43+ files in `v3/examples/`
- **Documentation**: 13 files in `docs/src/content/docs/`
- **CLI Tests**: 1 file in `v3/internal/commands/`

### Critical Constraints Preserved:
-  Event string constants unchanged (e.g., "windows:WindowShow")
-  Platform event names preserved (events.Windows, events.Mac, etc.)
-  TypeScript API remains compatible
-  All functionality intact

### Verification:
-  All examples build successfully (`task test:examples` passes)
-  Application package compiles without errors
-  Documentation reflects new API patterns

## Benefits

- **Improved Clarity**: Singular names are more intuitive (`app.Window` vs `app.Windows`)
- **Better Consistency**: Aligns with Go naming conventions
- **Enhanced Developer Experience**: Clearer autocomplete and API discovery

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Fix generator testcases and add cross-platform test cleanup

- Update 28 generator testcase files to use singular API (app.Window.New() vs app.Windows.New())
- Add cross-platform cleanup system with Go script to remove test artifacts
- Add test:all task with comprehensive testing and automatic cleanup
- Fix cleanup to target files vs directories correctly (preserves source directories)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Fix remaining Windows CI failures by updating all plural API usage to singular

Fixed the last remaining instances of old plural Manager API usage:
- tests/window-visibility-test/main.go: Updated all app.Windows -> app.Window and app.Menus -> app.Menu
- internal/templates/_common/main.go.tmpl: Updated app.Windows -> app.Window and app.Events -> app.Event
- pkg/services/badge/badge_windows.go: Updated app.Windows -> app.Window (Windows-specific fix)

These fixes address the Windows CI failures where platform-specific files still used the old API.
The tests didn't catch this locally because Windows-specific files only compile on Windows.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-06-22 12:19:14 +10:00

185 lines
5.2 KiB
Go

package application
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"strconv"
"sync"
"math"
)
// TODO maybe we could use a new struct that has the targetWindow as an attribute so we could get rid of passing the targetWindow
// as parameter through every function call.
const (
callRequest = 0
clipboardRequest = 1
applicationRequest = 2
eventsRequest = 3
contextMenuRequest = 4
dialogRequest = 5
windowRequest = 6
screensRequest = 7
systemRequest = 8
browserRequest = 9
cancelCallRequesst = 10
)
type MessageProcessor struct {
logger *slog.Logger
runningCalls map[string]context.CancelFunc
l sync.Mutex
}
func NewMessageProcessor(logger *slog.Logger) *MessageProcessor {
return &MessageProcessor{
logger: logger,
runningCalls: map[string]context.CancelFunc{},
}
}
func (m *MessageProcessor) httpError(rw http.ResponseWriter, message string, err error) {
m.Error(message, "error", err)
rw.WriteHeader(http.StatusUnprocessableEntity)
_, err = rw.Write([]byte(err.Error()))
if err != nil {
m.Error("Unable to write error response:", "error", err)
}
}
func (m *MessageProcessor) getTargetWindow(r *http.Request) (Window, string) {
windowName := r.Header.Get(webViewRequestHeaderWindowName)
if windowName != "" {
window, _ := globalApplication.Window.GetByName(windowName)
return window, windowName
}
windowID := r.Header.Get(webViewRequestHeaderWindowId)
if windowID == "" {
return nil, windowID
}
wID, err := strconv.ParseUint(windowID, 10, 64)
if err != nil {
m.Error("Window ID not parsable:", "id", windowID, "error", err)
return nil, windowID
}
// Check if wID is within the valid range for uint
if wID > math.MaxUint32 {
m.Error("Window ID out of range for uint:", "id", wID)
return nil, windowID
}
targetWindow, _ := globalApplication.Window.GetByID(uint(wID))
if targetWindow == nil {
m.Error("Window ID not found:", "id", wID)
return nil, windowID
}
return targetWindow, windowID
}
func (m *MessageProcessor) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
object := r.URL.Query().Get("object")
if object == "" {
m.httpError(rw, "Invalid runtime call:", errors.New("missing object value"))
return
}
m.HandleRuntimeCallWithIDs(rw, r)
}
func (m *MessageProcessor) HandleRuntimeCallWithIDs(rw http.ResponseWriter, r *http.Request) {
defer func() {
if handlePanic() {
rw.WriteHeader(http.StatusInternalServerError)
}
}()
object, err := strconv.Atoi(r.URL.Query().Get("object"))
if err != nil {
m.httpError(rw, "Invalid runtime call:", fmt.Errorf("error decoding object value: %w", err))
return
}
method, err := strconv.Atoi(r.URL.Query().Get("method"))
if err != nil {
m.httpError(rw, "Invalid runtime call:", fmt.Errorf("error decoding method value: %w", err))
return
}
params := QueryParams(r.URL.Query())
targetWindow, nameOrID := m.getTargetWindow(r)
if targetWindow == nil {
m.httpError(rw, "Invalid runtime call:", fmt.Errorf("window '%s' not found", nameOrID))
return
}
switch object {
case windowRequest:
m.processWindowMethod(method, rw, r, targetWindow, params)
case clipboardRequest:
m.processClipboardMethod(method, rw, r, targetWindow, params)
case dialogRequest:
m.processDialogMethod(method, rw, r, targetWindow, params)
case eventsRequest:
m.processEventsMethod(method, rw, r, targetWindow, params)
case applicationRequest:
m.processApplicationMethod(method, rw, r, targetWindow, params)
case contextMenuRequest:
m.processContextMenuMethod(method, rw, r, targetWindow, params)
case screensRequest:
m.processScreensMethod(method, rw, r, targetWindow, params)
case callRequest:
m.processCallMethod(method, rw, r, targetWindow, params)
case systemRequest:
m.processSystemMethod(method, rw, r, targetWindow, params)
case browserRequest:
m.processBrowserMethod(method, rw, r, targetWindow, params)
case cancelCallRequesst:
m.processCallCancelMethod(method, rw, r, targetWindow, params)
default:
m.httpError(rw, "Invalid runtime call:", fmt.Errorf("unknown object %d", object))
}
}
func (m *MessageProcessor) Error(message string, args ...any) {
m.logger.Error(message, args...)
}
func (m *MessageProcessor) Info(message string, args ...any) {
m.logger.Info(message, args...)
}
func (m *MessageProcessor) json(rw http.ResponseWriter, data any) {
rw.Header().Set("Content-Type", "application/json")
// convert data to json
var jsonPayload = []byte("{}")
var err error
if data != nil {
jsonPayload, err = json.Marshal(data)
if err != nil {
m.Error("Unable to convert data to JSON. Please report this to the Wails team!", "error", err)
return
}
}
_, err = rw.Write(jsonPayload)
if err != nil {
m.Error("Unable to write json payload. Please report this to the Wails team!", "error", err)
return
}
m.ok(rw)
}
func (m *MessageProcessor) text(rw http.ResponseWriter, data string) {
_, err := rw.Write([]byte(data))
if err != nil {
m.Error("Unable to write json payload. Please report this to the Wails team!", "error", err)
return
}
rw.Header().Set("Content-Type", "text/plain")
rw.WriteHeader(http.StatusOK)
}
func (m *MessageProcessor) ok(rw http.ResponseWriter) {
rw.WriteHeader(http.StatusOK)
}