storage_page.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package Storage
  2. import (
  3. "fmt"
  4. "syscall"
  5. "github.com/cuu/gogame/color"
  6. "github.com/cuu/gogame/draw"
  7. "github.com/clockworkpi/LauncherGoDev/sysgo/UI"
  8. )
  9. const (
  10. B = 1
  11. KB = 1024 * B
  12. MB = 1024 * KB
  13. GB = 1024 * MB
  14. )
  15. type StoragePage struct {
  16. UI.Page
  17. BGpng UI.IconItemInterface
  18. BGwidth int // 96
  19. BGheight int // 73
  20. BGlabel UI.LabelInterface
  21. FreeLabel UI.LabelInterface
  22. BGmsg string // "%.1GB of %.1fGB Used"
  23. DskUsg [2]float64
  24. HighColor *color.Color //MySkinManager.GiveColor('High')
  25. }
  26. type DiskStatus struct {
  27. All uint64 `json:"all"`
  28. Used uint64 `json:"used"`
  29. Free uint64 `json:"free"`
  30. }
  31. func DiskUsage(path string) (disk DiskStatus) {
  32. fs := syscall.Statfs_t{}
  33. err := syscall.Statfs(path, &fs)
  34. if err != nil {
  35. return
  36. }
  37. disk.All = fs.Blocks * uint64(fs.Bsize)
  38. disk.Free = fs.Bfree * uint64(fs.Bsize)
  39. disk.Used = disk.All - disk.Free
  40. return
  41. }
  42. func NewStoragePage() *StoragePage {
  43. p := &StoragePage{}
  44. p.PageIconMargin = 20
  45. p.SelectedIconTopOffset = 20
  46. p.EasingDur = 10
  47. p.Align = UI.ALIGN["SLeft"]
  48. p.FootMsg = [5]string{"Nav", "", "", "Back", ""}
  49. p.HighColor = &color.Color{51, 166, 255, 255}
  50. p.BGwidth = 96
  51. p.BGheight = 73
  52. p.BGmsg = "%.1fGB of %.1fGB Used"
  53. return p
  54. }
  55. func (self *StoragePage) DiskUsage() (float64, float64) {
  56. disk := DiskUsage("/")
  57. all := float64(disk.All) / float64(GB)
  58. free := float64(disk.Free) / float64(GB)
  59. return free, all
  60. }
  61. func (self *StoragePage) Init() {
  62. self.DskUsg[0], self.DskUsg[1] = self.DiskUsage()
  63. self.CanvasHWND = self.Screen.CanvasHWND
  64. self.Width = self.Screen.Width
  65. self.Height = self.Screen.Height
  66. bgpng := UI.NewIconItem()
  67. bgpng.ImgSurf = UI.MyIconPool.GetImgSurf("icon_sd")
  68. bgpng.MyType = UI.ICON_TYPES["STAT"]
  69. bgpng.Parent = self
  70. bgpng.AddLabel(fmt.Sprintf(self.BGmsg, self.DskUsg[1]-self.DskUsg[0], self.DskUsg[1]), UI.Fonts["varela15"])
  71. bgpng.Adjust(0, 0, self.BGwidth, self.BGheight, 0)
  72. self.BGpng = bgpng
  73. self.BGlabel = UI.NewLabel()
  74. self.BGlabel.SetCanvasHWND(self.CanvasHWND)
  75. usage_percent := int((self.DskUsg[0] / self.DskUsg[1]) * 100.0)
  76. self.BGlabel.Init(fmt.Sprintf("%d%%", usage_percent), UI.Fonts["varela25"], nil)
  77. self.BGlabel.SetColor(self.HighColor)
  78. self.FreeLabel = UI.NewLabel()
  79. self.FreeLabel.SetCanvasHWND(self.CanvasHWND)
  80. self.FreeLabel.Init("Free", UI.Fonts["varela13"], nil)
  81. self.FreeLabel.SetColor(self.BGlabel.(*UI.Label).Color)
  82. }
  83. func (self *StoragePage) Draw() {
  84. self.ClearCanvas()
  85. self.BGpng.NewCoord(self.Width/2, self.Height/2-10)
  86. self.BGpng.Draw()
  87. self.BGlabel.NewCoord(self.Width/2-28, self.Height/2-30)
  88. self.BGlabel.Draw()
  89. x, _ := self.BGlabel.Coord()
  90. self.FreeLabel.NewCoord(x+10, self.Height/2)
  91. self.FreeLabel.Draw()
  92. usage_percent := (self.DskUsg[0] / self.DskUsg[1])
  93. if usage_percent < 0.1 {
  94. usage_percent = 0.1
  95. }
  96. rect_ := draw.MidRect(self.Width/2, self.Height-30, 170, 17, UI.Width, UI.Height)
  97. draw.AARoundRect(self.CanvasHWND, rect_, &color.Color{169, 169, 169, 255}, 5, 0, &color.Color{169, 169, 169, 255})
  98. rect2_ := draw.MidRect(self.Width/2, self.Height-30, int(170.0*(1.0-usage_percent)), 17, UI.Width, UI.Height)
  99. rect2_.X = rect_.X
  100. rect2_.Y = rect_.Y
  101. draw.AARoundRect(self.CanvasHWND, rect2_, &color.Color{131, 199, 219, 255}, 5, 0, &color.Color{131, 199, 219, 255})
  102. }