diff --git a/ui/app/os_macos.go b/ui/app/os_macos.go index 006c50fb..0fb55414 100644 --- a/ui/app/os_macos.go +++ b/ui/app/os_macos.go @@ -207,8 +207,8 @@ func Main() { } cfg := getConfig() opts := singleWindow.opts - w := cfg.Pixels(opts.Width) - h := cfg.Pixels(opts.Height) + w := cfg.Val(opts.Width) + h := cfg.Val(opts.Height) title := C.CString(opts.Title) defer C.free(unsafe.Pointer(title)) C.gio_main(view, title, C.CGFloat(w), C.CGFloat(h)) diff --git a/ui/app/os_wayland.go b/ui/app/os_wayland.go index 7e4f38cd..4e1a0c5a 100644 --- a/ui/app/os_wayland.go +++ b/ui/app/os_wayland.go @@ -222,8 +222,8 @@ func createNativeWindow(opts *WindowOptions) (*window, error) { C.free(unsafe.Pointer(title)) _, _, cfg := w.config() - w.width = int(cfg.Pixels(opts.Width) + .5) - w.height = int(cfg.Pixels(opts.Height) + .5) + w.width = int(cfg.Val(opts.Width) + .5) + w.height = int(cfg.Val(opts.Height) + .5) if conn.decor != nil { // Request server side decorations. w.decor = C.zxdg_decoration_manager_v1_get_toplevel_decoration(conn.decor, w.topLvl) diff --git a/ui/app/os_windows.go b/ui/app/os_windows.go index 53740209..75e85dfa 100644 --- a/ui/app/os_windows.go +++ b/ui/app/os_windows.go @@ -217,8 +217,8 @@ func createNativeWindow(opts *WindowOptions) (*window, error) { } defer unregisterClass(cls, hInst) wr := rect{ - right: int32(cfg.Pixels(opts.Width) + .5), - bottom: int32(cfg.Pixels(opts.Height) + .5), + right: int32(cfg.Val(opts.Width) + .5), + bottom: int32(cfg.Val(opts.Height) + .5), } dwStyle := uint32(_WS_OVERLAPPEDWINDOW) dwExStyle := uint32(_WS_EX_APPWINDOW | _WS_EX_WINDOWEDGE) diff --git a/ui/gesture/gestures.go b/ui/gesture/gestures.go index dfbf868d..a456f5be 100644 --- a/ui/gesture/gestures.go +++ b/ui/gesture/gestures.go @@ -157,9 +157,9 @@ func (s *Scroll) Scroll(cfg *ui.Config, q pointer.Events, axis Axis) int { break } fling := s.estimator.Estimate() - if slop, d := cfg.Pixels(touchSlop), fling.Distance; d >= slop || -slop >= d { - if min, v := cfg.Pixels(minFlingVelocity), fling.Velocity; v >= min || -min >= v { - max := cfg.Pixels(maxFlingVelocity) + if slop, d := cfg.Val(touchSlop), fling.Distance; d >= slop || -slop >= d { + if min, v := cfg.Val(minFlingVelocity), fling.Velocity; v >= min || -min >= v { + max := cfg.Val(maxFlingVelocity) if v > max { v = max } else if v < -max { @@ -192,7 +192,7 @@ func (s *Scroll) Scroll(cfg *ui.Config, q pointer.Events, axis Axis) int { v := int(math.Round(float64(val))) dist := s.last - v if e.Priority < pointer.Grabbed { - slop := cfg.Pixels(touchSlop) + slop := cfg.Val(touchSlop) if dist := float32(dist); dist >= slop || -slop >= dist { s.grab = true } diff --git a/ui/measure/measure.go b/ui/measure/measure.go index a99da8c8..2ff2840e 100644 --- a/ui/measure/measure.go +++ b/ui/measure/measure.go @@ -103,7 +103,7 @@ func (f *Faces) init() { } func (f *textFace) Layout(str string, singleLine bool, maxWidth int) *text.Layout { - ppem := fixed.Int26_6(f.faces.Cfg.Pixels(f.size)*64 + .5) + ppem := fixed.Int26_6(f.faces.Cfg.Val(f.size)*64 + .5) lk := layoutKey{ f: f.font.Font, ppem: ppem, @@ -122,7 +122,7 @@ func (f *textFace) Layout(str string, singleLine bool, maxWidth int) *text.Layou } func (f *textFace) Path(str text.String) *draw.Path { - ppem := fixed.Int26_6(f.faces.Cfg.Pixels(f.size)*64 + .5) + ppem := fixed.Int26_6(f.faces.Cfg.Val(f.size)*64 + .5) pk := pathKey{ f: f.font.Font, ppem: ppem, diff --git a/ui/text/editor.go b/ui/text/editor.go index 40592538..1ca18be5 100644 --- a/ui/text/editor.go +++ b/ui/text/editor.go @@ -124,12 +124,12 @@ func (e *Editor) Update(c *ui.Config, pq pointer.Events, kq key.Events) { } func (e *Editor) caretWidth() fixed.Int26_6 { - oneDp := int(e.cfg.Pixels(ui.Dp(1)) + .5) + oneDp := int(e.cfg.Val(ui.Dp(1)) + .5) return fixed.Int26_6(oneDp * 64) } func (e *Editor) W(ops *ui.Ops, cs layout.Constraints) layout.Dimens { - twoDp := int(e.cfg.Pixels(ui.Dp(2)) + 0.5) + twoDp := int(e.cfg.Val(ui.Dp(2)) + 0.5) e.padLeft, e.padRight = twoDp, twoDp maxWidth := cs.Width.Max if e.SingleLine { diff --git a/ui/ui.go b/ui/ui.go index 11926edf..7a0592aa 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -22,8 +22,18 @@ type Config struct { Now time.Time } -// Pixels converts a value to unitless device pixels. -func (c *Config) Pixels(v Value) float32 { +// Dp converts a value in dp units to pixels. +func (c *Config) Dp(dp float32) float32 { + return c.PxPerDp * dp +} + +// Sp converts a value in sp units to pixels. +func (c *Config) Sp(sp float32) float32 { + return c.PxPerSp * sp +} + +// Val converts a value to pixels. +func (c *Config) Val(v Value) float32 { switch v.U { case UnitPx: return v.V