~eliasnaur/gio

00cb158247fe6c460d6bf774eb89bf565d830ba6 — Elias Naur 2 years ago acf345a
ui: add Config.Sp and Config.Dp convenience methods

And rename Pixels to Val.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
M ui/app/os_macos.go => ui/app/os_macos.go +2 -2
@@ 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))

M ui/app/os_wayland.go => ui/app/os_wayland.go +2 -2
@@ 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)

M ui/app/os_windows.go => ui/app/os_windows.go +2 -2
@@ 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)

M ui/gesture/gestures.go => ui/gesture/gestures.go +4 -4
@@ 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
				}

M ui/measure/measure.go => ui/measure/measure.go +2 -2
@@ 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,

M ui/text/editor.go => ui/text/editor.go +2 -2
@@ 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 {

M ui/ui.go => ui/ui.go +12 -2
@@ 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