~eliasnaur/gio

ref: ad4215a7ebb19bf5063e01bb8f3162fa95c63696 gio/ui/unit.go -rw-r--r-- 941 bytes
ad4215a7Elias Naur ui: use ints for unit conversions to pixels 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// SPDX-License-Identifier: Unlicense OR MIT

package ui

import "fmt"

// Value is a value with a unit.
type Value struct {
	V float32
	U Unit
}

// Unit represents a unit for a Value.
type Unit uint8

const (
	// UnitPx represent device pixels in the resolution of
	// the underlying display.
	UnitPx Unit = iota
	// UnitDp represents device independent pixels. 1 dp will
	// have the same apparent size across platforms and
	// display resolutions.
	UnitDp
	// UnitSp is like UnitDp but for font sizes.
	UnitSp
)

func Px(v float32) Value {
	return Value{V: v, U: UnitPx}
}

func Dp(v float32) Value {
	return Value{V: v, U: UnitDp}
}

func Sp(v float32) Value {
	return Value{V: v, U: UnitSp}
}

func (v Value) String() string {
	return fmt.Sprintf("%g%s", v.V, v.U)
}

func (u Unit) String() string {
	switch u {
	case UnitPx:
		return "px"
	case UnitDp:
		return "dp"
	case UnitSp:
		return "sp"
	default:
		panic("unknown unit")
	}
}