all: make unit.Converter concrete and rename to Metric

An interface for scaling dp and sp is overkill, at least for all
current uses. Make it a concrete struct type, and rename it to the
shorter and more precise Metric.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-06-17 11:47:14 +02:00
parent 1603a6f3ee
commit 596e321610
15 changed files with 86 additions and 85 deletions
+3 -3
View File
@@ -396,9 +396,9 @@ func (w *window) draw(sync bool) {
Y: int(height),
},
Insets: w.insets,
Config: &config{
pxPerDp: ppdp,
pxPerSp: w.fontScale * ppdp,
Metric: unit.Metric{
PxPerDp: ppdp,
PxPerSp: w.fontScale * ppdp,
},
},
Sync: sync,
+3 -3
View File
@@ -119,9 +119,9 @@ func (w *window) draw(sync bool) {
Bottom: unit.Px(float32(params.bottom)),
Left: unit.Px(float32(params.left)),
},
Config: &config{
pxPerDp: float32(params.dpi) * inchPrDp,
pxPerSp: float32(params.sdpi) * inchPrDp,
Metric: unit.Metric{
PxPerDp: float32(params.dpi) * inchPrDp,
PxPerSp: float32(params.sdpi) * inchPrDp,
},
},
Sync: sync,
+7 -6
View File
@@ -15,6 +15,7 @@ import (
"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/io/system"
"gioui.org/unit"
)
type window struct {
@@ -407,7 +408,7 @@ func (w *window) ShowTextInput(show bool) {
func (w *window) draw(sync bool) {
width, height, scale, cfg := w.config()
if cfg == (config{}) || width == 0 || height == 0 {
if cfg == (unit.Metric{}) || width == 0 || height == 0 {
return
}
w.mu.Lock()
@@ -420,13 +421,13 @@ func (w *window) draw(sync bool) {
X: width,
Y: height,
},
Config: &cfg,
Metric: cfg,
},
Sync: sync,
})
}
func (w *window) config() (int, int, float32, config) {
func (w *window) config() (int, int, float32, unit.Metric) {
rect := w.cnv.Call("getBoundingClientRect")
width, height := rect.Get("width").Float(), rect.Get("height").Float()
scale := w.window.Get("devicePixelRatio").Float()
@@ -438,9 +439,9 @@ func (w *window) config() (int, int, float32, config) {
w.cnv.Set("width", iw)
w.cnv.Set("height", ih)
}
return iw, ih, float32(scale), config{
pxPerDp: float32(scale),
pxPerSp: float32(scale),
return iw, ih, float32(scale), unit.Metric{
PxPerDp: float32(scale),
PxPerSp: float32(scale),
}
}
+6 -5
View File
@@ -17,6 +17,7 @@ import (
"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/io/system"
"gioui.org/unit"
_ "gioui.org/app/internal/cocoainit"
)
@@ -236,16 +237,16 @@ func (w *window) draw() {
X: width,
Y: height,
},
Config: &cfg,
Metric: cfg,
},
Sync: true,
})
}
func configFor(scale float32) config {
return config{
pxPerDp: scale,
pxPerSp: scale,
func configFor(scale float32) unit.Metric {
return unit.Metric{
PxPerDp: scale,
PxPerSp: scale,
}
}
+8 -7
View File
@@ -25,6 +25,7 @@ import (
"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/io/system"
"gioui.org/unit"
syscall "golang.org/x/sys/unix"
)
@@ -873,7 +874,7 @@ func (w *window) flushFling() {
w.fling.yExtrapolation = fling.Extrapolation{}
vel := float32(math.Sqrt(float64(estx.Velocity*estx.Velocity + esty.Velocity*esty.Velocity)))
_, _, c := w.config()
if !w.fling.anim.Start(&c, time.Now(), vel) {
if !w.fling.anim.Start(c, time.Now(), vel) {
return
}
invDist := 1 / vel
@@ -1359,11 +1360,11 @@ func (w *window) updateOutputs() {
}
}
func (w *window) config() (int, int, config) {
func (w *window) config() (int, int, unit.Metric) {
width, height := w.width*w.scale, w.height*w.scale
return width, height, config{
pxPerDp: w.ppdp * float32(w.scale),
pxPerSp: w.ppsp * float32(w.scale),
return width, height, unit.Metric{
PxPerDp: w.ppdp * float32(w.scale),
PxPerSp: w.ppsp * float32(w.scale),
}
}
@@ -1377,7 +1378,7 @@ func (w *window) draw(sync bool) {
return
}
width, height, cfg := w.config()
if cfg == (config{}) {
if cfg == (unit.Metric{}) {
return
}
if anim && w.lastFrameCallback == nil {
@@ -1392,7 +1393,7 @@ func (w *window) draw(sync bool) {
X: width,
Y: height,
},
Config: &cfg,
Metric: cfg,
},
Sync: sync,
})
+6 -5
View File
@@ -19,6 +19,7 @@ import (
syscall "golang.org/x/sys/windows"
"gioui.org/app/internal/windows"
"gioui.org/unit"
"gioui.org/f32"
"gioui.org/io/key"
@@ -377,7 +378,7 @@ func (w *window) draw(sync bool) {
X: w.width,
Y: w.height,
},
Config: &cfg,
Metric: cfg,
},
Sync: sync,
})
@@ -586,12 +587,12 @@ func convertKeyCode(code uintptr) (string, bool) {
return r, true
}
func configForDC() config {
func configForDC() unit.Metric {
dpi := windows.GetSystemDPI()
const inchPrDp = 1.0 / 96.0
ppdp := float32(dpi) * inchPrDp
return config{
pxPerDp: ppdp,
pxPerSp: ppdp,
return unit.Metric{
PxPerDp: ppdp,
PxPerSp: ppdp,
}
}
+4 -3
View File
@@ -37,6 +37,7 @@ import (
"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/io/system"
"gioui.org/unit"
"gioui.org/app/internal/xkb"
syscall "golang.org/x/sys/unix"
@@ -64,7 +65,7 @@ type x11Window struct {
atom C.Atom
}
stage system.Stage
cfg config
cfg unit.Metric
width int
height int
notify struct {
@@ -193,7 +194,7 @@ loop:
X: w.width,
Y: w.height,
},
Config: &w.cfg,
Metric: w.cfg,
},
Sync: syn,
})
@@ -474,7 +475,7 @@ func newX11Window(gioWin Callbacks, opts *Options) error {
}
ppsp := x11DetectUIScale(dpy)
cfg := config{pxPerDp: ppsp, pxPerSp: ppsp}
cfg := unit.Metric{PxPerDp: ppsp, PxPerSp: ppsp}
swa := C.XSetWindowAttributes{
event_mask: C.ExposureMask | C.FocusChangeMask | // update
C.KeyPressMask | C.KeyReleaseMask | // keyboard
-24
View File
@@ -6,7 +6,6 @@ package window
import (
"errors"
"math"
"gioui.org/gpu/backend"
"gioui.org/io/event"
@@ -71,29 +70,6 @@ type windowAndOptions struct {
opts *Options
}
// config implements the system.Config interface.
type config struct {
// Device pixels per dp.
pxPerDp float32
// Device pixels per sp.
pxPerSp float32
}
func (c *config) Px(v unit.Value) int {
var r float32
switch v.U {
case unit.UnitPx:
r = v.V
case unit.UnitDp:
r = c.pxPerDp * v.V
case unit.UnitSp:
r = c.pxPerSp * v.V
default:
panic("unknown unit")
}
return int(math.Round(float64(r)))
}
func newWindowRendezvous() *windowRendezvous {
wr := &windowRendezvous{
in: make(chan windowAndOptions),