ui: add Config.Sp and Config.Dp convenience methods

And rename Pixels to Val.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-05-16 19:32:47 +02:00
parent acf345a0f8
commit 00cb158247
7 changed files with 26 additions and 16 deletions
+2 -2
View File
@@ -207,8 +207,8 @@ func Main() {
} }
cfg := getConfig() cfg := getConfig()
opts := singleWindow.opts opts := singleWindow.opts
w := cfg.Pixels(opts.Width) w := cfg.Val(opts.Width)
h := cfg.Pixels(opts.Height) h := cfg.Val(opts.Height)
title := C.CString(opts.Title) title := C.CString(opts.Title)
defer C.free(unsafe.Pointer(title)) defer C.free(unsafe.Pointer(title))
C.gio_main(view, title, C.CGFloat(w), C.CGFloat(h)) C.gio_main(view, title, C.CGFloat(w), C.CGFloat(h))
+2 -2
View File
@@ -222,8 +222,8 @@ func createNativeWindow(opts *WindowOptions) (*window, error) {
C.free(unsafe.Pointer(title)) C.free(unsafe.Pointer(title))
_, _, cfg := w.config() _, _, cfg := w.config()
w.width = int(cfg.Pixels(opts.Width) + .5) w.width = int(cfg.Val(opts.Width) + .5)
w.height = int(cfg.Pixels(opts.Height) + .5) w.height = int(cfg.Val(opts.Height) + .5)
if conn.decor != nil { if conn.decor != nil {
// Request server side decorations. // Request server side decorations.
w.decor = C.zxdg_decoration_manager_v1_get_toplevel_decoration(conn.decor, w.topLvl) w.decor = C.zxdg_decoration_manager_v1_get_toplevel_decoration(conn.decor, w.topLvl)
+2 -2
View File
@@ -217,8 +217,8 @@ func createNativeWindow(opts *WindowOptions) (*window, error) {
} }
defer unregisterClass(cls, hInst) defer unregisterClass(cls, hInst)
wr := rect{ wr := rect{
right: int32(cfg.Pixels(opts.Width) + .5), right: int32(cfg.Val(opts.Width) + .5),
bottom: int32(cfg.Pixels(opts.Height) + .5), bottom: int32(cfg.Val(opts.Height) + .5),
} }
dwStyle := uint32(_WS_OVERLAPPEDWINDOW) dwStyle := uint32(_WS_OVERLAPPEDWINDOW)
dwExStyle := uint32(_WS_EX_APPWINDOW | _WS_EX_WINDOWEDGE) dwExStyle := uint32(_WS_EX_APPWINDOW | _WS_EX_WINDOWEDGE)
+4 -4
View File
@@ -157,9 +157,9 @@ func (s *Scroll) Scroll(cfg *ui.Config, q pointer.Events, axis Axis) int {
break break
} }
fling := s.estimator.Estimate() fling := s.estimator.Estimate()
if slop, d := cfg.Pixels(touchSlop), fling.Distance; d >= slop || -slop >= d { if slop, d := cfg.Val(touchSlop), fling.Distance; d >= slop || -slop >= d {
if min, v := cfg.Pixels(minFlingVelocity), fling.Velocity; v >= min || -min >= v { if min, v := cfg.Val(minFlingVelocity), fling.Velocity; v >= min || -min >= v {
max := cfg.Pixels(maxFlingVelocity) max := cfg.Val(maxFlingVelocity)
if v > max { if v > max {
v = max v = max
} else if 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))) v := int(math.Round(float64(val)))
dist := s.last - v dist := s.last - v
if e.Priority < pointer.Grabbed { if e.Priority < pointer.Grabbed {
slop := cfg.Pixels(touchSlop) slop := cfg.Val(touchSlop)
if dist := float32(dist); dist >= slop || -slop >= dist { if dist := float32(dist); dist >= slop || -slop >= dist {
s.grab = true s.grab = true
} }
+2 -2
View File
@@ -103,7 +103,7 @@ func (f *Faces) init() {
} }
func (f *textFace) Layout(str string, singleLine bool, maxWidth int) *text.Layout { 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{ lk := layoutKey{
f: f.font.Font, f: f.font.Font,
ppem: ppem, 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 { 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{ pk := pathKey{
f: f.font.Font, f: f.font.Font,
ppem: ppem, ppem: ppem,
+2 -2
View File
@@ -124,12 +124,12 @@ func (e *Editor) Update(c *ui.Config, pq pointer.Events, kq key.Events) {
} }
func (e *Editor) caretWidth() fixed.Int26_6 { 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) return fixed.Int26_6(oneDp * 64)
} }
func (e *Editor) W(ops *ui.Ops, cs layout.Constraints) layout.Dimens { 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 e.padLeft, e.padRight = twoDp, twoDp
maxWidth := cs.Width.Max maxWidth := cs.Width.Max
if e.SingleLine { if e.SingleLine {
+12 -2
View File
@@ -22,8 +22,18 @@ type Config struct {
Now time.Time Now time.Time
} }
// Pixels converts a value to unitless device pixels. // Dp converts a value in dp units to pixels.
func (c *Config) Pixels(v Value) float32 { 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 { switch v.U {
case UnitPx: case UnitPx:
return v.V return v.V