- Add ui.Dp and ui.Px as distinct named types - Logic layer works exclusively in Dp (positions, sizes, regions) - Renderer converts Dp→Px at Gio interop boundaries - Capture gtx.Constraints once at start of Draw() for consistent positioning - main.go converts app.ConfigEvent pixels to Dp before passing to logic layer - Update documentation with new coordinate system architecture
66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
package ui
|
|
|
|
import "gioui.org/unit"
|
|
|
|
// Dp represents device-independent pixels. Use for all element positions,
|
|
// sizes, and spacing in the logic/layout layer.
|
|
type Dp unit.Dp
|
|
|
|
// Px represents physical device pixels. Use only when interfacing with
|
|
// Gio's layout.Context (gtx.Constraints, gtx.Dp(), etc.).
|
|
type Px int
|
|
|
|
// ToDp converts physical pixels to device-independent pixels using the
|
|
// given scale factor (pixels per DP). Typically from gtx.Metric.PxPerDp.
|
|
func ToDp(px Px, scale float32) Dp {
|
|
return Dp(float32(px) / scale)
|
|
}
|
|
|
|
// ToPx converts device-independent pixels to physical pixels using the
|
|
// given scale factor (pixels per DP).
|
|
func ToPx(dp Dp, scale float32) Px {
|
|
return Px(float32(dp) * scale)
|
|
}
|
|
|
|
// PxPerDp returns the scale factor: how many physical pixels per DP.
|
|
// Use this to convert between Dp and Px.
|
|
func PxPerDp(scale float32) float32 { return scale }
|
|
|
|
// --- Gio interop helpers ---
|
|
|
|
// DpToPx converts a gioui unit.Dp to our Px using the scale factor.
|
|
func DpToPx(d unit.Dp, scale float32) Px {
|
|
return Px(float32(d) * scale)
|
|
}
|
|
|
|
// PxToDp converts our Px to a gioui unit.Dp using the scale factor.
|
|
func PxToDp(p Px, scale float32) unit.Dp {
|
|
return unit.Dp(float32(p) / scale)
|
|
}
|
|
|
|
// RegionPx is a region in physical pixels. Used for Gio interop only.
|
|
type RegionPx struct {
|
|
X, Y Px
|
|
W, H Px
|
|
}
|
|
|
|
// ToDp converts a RegionPx to a Region (Dp) using the scale factor.
|
|
func (r RegionPx) ToDp(scale float32) Region {
|
|
return Region{
|
|
X: ToDp(r.X, scale),
|
|
Y: ToDp(r.Y, scale),
|
|
W: ToDp(r.W, scale),
|
|
H: ToDp(r.H, scale),
|
|
}
|
|
}
|
|
|
|
// FromDp converts a Region (Dp) to a RegionPx using the scale factor.
|
|
func FromDp(r Region, scale float32) RegionPx {
|
|
return RegionPx{
|
|
X: ToPx(r.X, scale),
|
|
Y: ToPx(r.Y, scale),
|
|
W: ToPx(r.W, scale),
|
|
H: ToPx(r.H, scale),
|
|
}
|
|
}
|