wails/v3/examples/screen/screens.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

137 lines
3.9 KiB
Go

package main
import (
"runtime"
"github.com/wailsapp/wails/v3/pkg/application"
)
type ScreenService struct {
screenManager application.ScreenManager
isExampleLayout bool
}
func (s *ScreenService) GetSystemScreens() []*application.Screen {
s.isExampleLayout = false
screens := application.Get().Screen.GetAll()
return screens
}
func (s *ScreenService) ProcessExampleScreens(rawScreens []interface{}) []*application.Screen {
s.isExampleLayout = true
parseRect := func(m map[string]interface{}) application.Rect {
return application.Rect{
X: int(m["X"].(float64)),
Y: int(m["Y"].(float64)),
Width: int(m["Width"].(float64)),
Height: int(m["Height"].(float64)),
}
}
// Prevent unbounded slice growth by limiting the number of screens
maxScreens := 32 // Reasonable limit for screen configurations
if len(rawScreens) > maxScreens {
rawScreens = rawScreens[:maxScreens]
}
screens := make([]*application.Screen, 0, len(rawScreens))
for _, s := range rawScreens {
s := s.(map[string]interface{})
bounds := parseRect(s["Bounds"].(map[string]interface{}))
screens = append(screens, &application.Screen{
ID: s["ID"].(string),
Name: s["Name"].(string),
X: bounds.X,
Y: bounds.Y,
Size: application.Size{Width: bounds.Width, Height: bounds.Height},
Bounds: bounds,
PhysicalBounds: parseRect(s["PhysicalBounds"].(map[string]interface{})),
WorkArea: parseRect(s["WorkArea"].(map[string]interface{})),
PhysicalWorkArea: parseRect(s["PhysicalWorkArea"].(map[string]interface{})),
IsPrimary: s["IsPrimary"].(bool),
ScaleFactor: float32(s["ScaleFactor"].(float64)),
Rotation: 0,
})
}
s.screenManager.LayoutScreens(screens)
return s.screenManager.GetAll()
}
func (s *ScreenService) transformPoint(point application.Point, toDIP bool) application.Point {
if s.isExampleLayout {
if toDIP {
return s.screenManager.PhysicalToDipPoint(point)
} else {
return s.screenManager.DipToPhysicalPoint(point)
}
} else {
// =======================
// TODO: remove this block when DPI is implemented in Linux & Mac
if runtime.GOOS != "windows" {
println("DPI not implemented yet!")
return point
}
// =======================
if toDIP {
return application.PhysicalToDipPoint(point)
} else {
return application.DipToPhysicalPoint(point)
}
}
}
func (s *ScreenService) TransformPoint(point map[string]interface{}, toDIP bool) (points [2]application.Point) {
pt := application.Point{
X: int(point["X"].(float64)),
Y: int(point["Y"].(float64)),
}
ptTransformed := s.transformPoint(pt, toDIP)
ptDblTransformed := s.transformPoint(ptTransformed, !toDIP)
// double-transform a limited number of times to catch any double-rounding issues
// Limit iterations to prevent potential performance issues
maxIterations := 3 // Reduced from 10 to limit computational overhead
for i := 0; i < maxIterations; i++ {
ptTransformed = s.transformPoint(ptDblTransformed, toDIP)
ptDblTransformed = s.transformPoint(ptTransformed, !toDIP)
}
points[0] = ptTransformed
points[1] = ptDblTransformed
return points
}
func (s *ScreenService) TransformRect(rect map[string]interface{}, toDIP bool) application.Rect {
r := application.Rect{
X: int(rect["X"].(float64)),
Y: int(rect["Y"].(float64)),
Width: int(rect["Width"].(float64)),
Height: int(rect["Height"].(float64)),
}
if s.isExampleLayout {
if toDIP {
return s.screenManager.PhysicalToDipRect(r)
} else {
return s.screenManager.DipToPhysicalRect(r)
}
} else {
// =======================
// TODO: remove this block when DPI is implemented in Linux & Mac
if runtime.GOOS != "windows" {
println("DPI not implemented yet!")
return r
}
// =======================
if toDIP {
return application.PhysicalToDipRect(r)
} else {
return application.DipToPhysicalRect(r)
}
}
}