-
Notifications
You must be signed in to change notification settings - Fork 1
/
screen_size.go
99 lines (86 loc) · 3.38 KB
/
screen_size.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"regexp"
"strconv"
)
var sizeRegex = regexp.MustCompile(`^([0-9]{2,6})x([0-9]{2,6})$`)
type screenInfo struct {
ScrIsProcessed bool `json:"i"`
ScrScreenOrientation bool `json:"o"`
ScrScreenOrientationIsPortrait bool `json:"ip"`
ScrScreenOrientationIsSecondary bool `json:"is"`
ScrScreen string `json:"s"`
ScrScreenWidth uint16 `json:"sw"`
ScrScreenHeight uint16 `json:"sh"`
ScrViewport string `json:"v"`
ScrViewportWidth uint16 `json:"vw"`
ScrViewportHeight uint16 `json:"vh"`
ScrResolution string `json:"r"`
ScrResolutionWidth uint16 `json:"rw"`
ScrResolutionHeight uint16 `json:"rh"`
ScrDevicePixelRatio float64 `json:"dp"`
ScrColorDepth uint8 `json:"c"`
}
func parseScreenSize(
screenOrientation string,
screenSize string,
viewportSize string,
devicePixelRatio string,
colorDepth string,
) screenInfo {
result := screenInfo{
ScrIsProcessed: true,
ScrScreenOrientation: false,
}
if devicePixelRatioFloat, err := strconv.ParseFloat(devicePixelRatio, 64); err == nil {
result.ScrDevicePixelRatio = devicePixelRatioFloat
}
if colorDepthUInt8, err := strconv.ParseInt(colorDepth, 10, 16); err == nil {
result.ScrColorDepth = uint8(colorDepthUInt8)
}
switch screenOrientation {
case "portrait-primary":
result.ScrScreenOrientation = true
result.ScrScreenOrientationIsPortrait = true
result.ScrScreenOrientationIsSecondary = false
case "portrait-secondary":
result.ScrScreenOrientation = true
result.ScrScreenOrientationIsPortrait = true
result.ScrScreenOrientationIsSecondary = true
case "landscape-primary":
result.ScrScreenOrientation = true
result.ScrScreenOrientationIsPortrait = false
result.ScrScreenOrientationIsSecondary = false
case "landscape-secondary":
result.ScrScreenOrientation = true
result.ScrScreenOrientationIsPortrait = false
result.ScrScreenOrientationIsSecondary = true
}
if sizeRegex.MatchString(viewportSize) {
matched := sizeRegex.FindStringSubmatch(viewportSize)
viewportWidth, viewportWidthErr := strconv.ParseUint(matched[1], 10, 16)
viewportHeight, viewportHeightErr := strconv.ParseUint(matched[2], 10, 16)
if viewportWidthErr == nil && viewportHeightErr == nil {
result.ScrViewportWidth = uint16(viewportWidth)
result.ScrViewportHeight = uint16(viewportHeight)
result.ScrViewport = fmt.Sprintf("%dx%d", result.ScrViewportWidth, result.ScrViewportHeight)
}
}
if sizeRegex.MatchString(screenSize) {
matched := sizeRegex.FindStringSubmatch(screenSize)
screenWidth, screenWidthErr := strconv.ParseUint(matched[1], 10, 16)
screenHeight, screenHeightErr := strconv.ParseUint(matched[2], 10, 16)
if screenWidthErr == nil && screenHeightErr == nil {
result.ScrScreenWidth = uint16(screenWidth)
result.ScrScreenHeight = uint16(screenHeight)
result.ScrScreen = fmt.Sprintf("%dx%d", screenWidth, screenHeight)
if result.ScrDevicePixelRatio != 0 {
result.ScrResolutionWidth = uint16(float64(screenWidth) * result.ScrDevicePixelRatio)
result.ScrResolutionHeight = uint16(float64(screenHeight) * result.ScrDevicePixelRatio)
result.ScrResolution = fmt.Sprintf("%dx%d", result.ScrResolutionWidth, result.ScrResolutionHeight)
}
}
}
return result
}