title_bar.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. package UI
  2. import (
  3. "fmt"
  4. "os"
  5. "log"
  6. "strconv"
  7. "bufio"
  8. "strings"
  9. gotime "time"
  10. "github.com/veandco/go-sdl2/sdl"
  11. "github.com/veandco/go-sdl2/ttf"
  12. "github.com/cuu/gogame/surface"
  13. "github.com/cuu/gogame/rect"
  14. "github.com/cuu/gogame/font"
  15. "github.com/cuu/gogame/draw"
  16. "github.com/itchyny/volume-go"
  17. "github.com/vjeantet/jodaTime"
  18. "github.com/cuu/LauncherGoDev/sysgo/DBUS"
  19. "github.com/cuu/LauncherGoDev/sysgo"
  20. )
  21. var TitleBar_BarHeight = 24
  22. type TitleBarIconItem struct {
  23. MultiIconItem
  24. Parent *TitleBar
  25. }
  26. func NewTitleBarIconItem() *TitleBarIconItem {
  27. m := &TitleBarIconItem{}
  28. m.IconIndex = 0
  29. m.IconWidth = 18
  30. m.IconHeight = 18
  31. m.Align = ALIGN["VCenter"]
  32. return m
  33. }
  34. func (self *TitleBarIconItem) Adjust(x,y,w,h,at int) {
  35. self.PosX = x
  36. self.PosY = y
  37. self.Width = w
  38. self.Height = h
  39. self.AnimationTime = at
  40. if self.Label != nil {
  41. self.Label.SetCanvasHWND(self.Parent.CanvasHWND)
  42. }
  43. self.CreateImgSurf()
  44. // self.AdjustLinkPage()
  45. }
  46. func (self *TitleBarIconItem) Draw() {
  47. parent_x,parent_y := self.Parent.PosX,self.Parent.PosY
  48. if self.Label != nil {
  49. // lab_x,lab_y := self.Label.Coord()
  50. lab_w,lab_h:= self.Label.Size()
  51. if self.Align == ALIGN["VCenter"] {
  52. self.Label.NewCoord( self.PosX - lab_w/2 + parent_x, self.PosY + self.Height/2+6 + parent_y)
  53. }else if self.Align == ALIGN["HLeft"] {
  54. self.Label.NewCoord( self.PosX + self.Width/2+3 + parent_x, self.PosY - lab_h/2 + parent_y )
  55. }
  56. self.Label.Draw()
  57. }
  58. if self.ImgSurf != nil {
  59. portion := rect.Rect(0,self.IconIndex*self.IconHeight,self.IconWidth,self.IconHeight)
  60. surface.Blit(self.Parent.CanvasHWND,
  61. self.ImgSurf,draw.MidRect(self.PosX + parent_x, self.PosY + parent_y,
  62. self.Width,self.Height, Width, Height),&portion)
  63. }
  64. }
  65. type TitleBar struct {
  66. Widget
  67. BarHeight int
  68. LOffset int
  69. ROffset int
  70. Icons map[string]IconItemInterface
  71. IconWidth int
  72. IconHeight int
  73. BorderWidth int
  74. CanvasHWND *sdl.Surface
  75. HWND *sdl.Surface
  76. Title string
  77. InLowBackLight int
  78. InAirPlaneMode bool
  79. SkinManager *SkinManager //set by MainScreen
  80. DBusManager DBUS.DBusInterface
  81. icon_base_path string /// SkinMap("gameshell/titlebar_icons/")
  82. TitleFont *ttf.Font
  83. TimeFont *ttf.Font
  84. }
  85. func NewTitleBar() *TitleBar {
  86. t := &TitleBar{}
  87. t.BorderWidth = 1
  88. t.BarHeight = TitleBar_BarHeight
  89. t.Height = t.BarHeight + t.BorderWidth
  90. t.Width = Width
  91. t.IconWidth = 18
  92. t.IconHeight = 18
  93. t.LOffset = 3
  94. t.ROffset = 3
  95. t.Icons = make(map[string]IconItemInterface)
  96. t.icon_base_path = SkinMap("sysgo/gameshell/titlebar_icons/")
  97. t.TitleFont = Fonts["varela16"]
  98. t.TimeFont = Fonts["varela12"]
  99. return t
  100. }
  101. func (t *TitleBar) RoundRobinCheck() {
  102. for {
  103. if self.InLowBackLight < 0 {
  104. self.CheckBatteryStat()
  105. ///self.CheckBluetooth()
  106. self.UpdateWifiStrength()
  107. }else if self.InLowBackLight >= 0 {
  108. self.InLowBackLight +=1
  109. if self.InLowBackLight > 10 {
  110. self.CheckBatteryStat()
  111. self.UpdateWifiStrength()
  112. self.InLowBackLight = 0 // reset
  113. }
  114. }
  115. gotime.Sleep(3000 * gotime.Millisecond)
  116. }
  117. }
  118. func (t *TitleBar) UpdateWifiStrength() {
  119. self.Draw(self.Title)
  120. }
  121. func (t *TitleBar) GetWifiStrength(stren int) int {
  122. segs := [][]int{ []int{-2,-1}, []int{0,25}, []int{25,50}, []int{50,75},[]int{75,100}}
  123. stren_number := stren
  124. ge := 0
  125. if stren_number == 0 {
  126. return ge
  127. }
  128. for i,v := range segs {
  129. if stren_number >= v[0] && stren_number <= v[1] {
  130. ge = i
  131. break
  132. }
  133. }
  134. return ge
  135. }
  136. func (self *TitleBar) SyncSoundVolume() {
  137. vol, err := volume.GetVolume()
  138. if err != nil {
  139. log.Fatalf("TitleBar SyncSoundVolume get volume failed: %+v", err)
  140. vol = 0
  141. }
  142. fmt.Printf("TitleBar SyncSoundVolume current volume: %d\n", vol)
  143. snd_segs := [][]int{ []int{0,10}, []int{10,30}, []int{30,70},[]int{70,100} }
  144. ge := 0
  145. for i,v := range snd_segs {
  146. if vol >= v[0] && vol <= v[1] {
  147. ge = i
  148. break
  149. }
  150. }
  151. self.Icons["soundvolume"].SetIconIndex(ge)
  152. self.Icons["sound"] = self.Icons["soundvolume"]
  153. //
  154. }
  155. // for outside widget to update sound icon
  156. func (t *TitleBar) SetSoundVolume(vol int) {
  157. snd_segs := [][]int{ []int{0,10}, []int{10,30}, []int{30,70},[]int{70,100} }
  158. ge := 0
  159. for i,v := range snd_segs {
  160. if vol >= v[0] && vol <= v[1] {
  161. ge = i
  162. break
  163. }
  164. }
  165. self.Icons["soundvolume"].SetIconIndex(ge)
  166. self.Icons["sound"] = self.Icons["soundvolume"]
  167. }
  168. func (self *TitleBar) CheckBatteryStat() {
  169. bat_segs:= [][]int{[]int{0,6},[]int{7,15},[]int{16,20},[]int{21,30},[]int{31,50},[]int{51,60},[]int{61,80},[]int{81,90},[]int{91,100}}
  170. file, err := os.Open( sysgo.Battery )
  171. if err != nil {
  172. fmt.Println("Could not open file ", sysgo.Battery)
  173. self.Icons["battery"] = self.Icons["battery_unknown"]
  174. return
  175. }
  176. defer file.Close()
  177. bat_uevent := make(map[string]string)
  178. scanner := bufio.NewScanner(file)
  179. scanner.Split(bufio.ScanLines)
  180. for scanner.Scan() {
  181. line := scanner.Text()
  182. line = strings.Trim(line," ")
  183. pis := strings.Split(line,"=")
  184. if len(pis) > 1 {
  185. bat_uevent[ pis[0] ] = pis[1]
  186. }
  187. }
  188. cur_cap := 0
  189. if val, ok := bat_uevent["POWER_SUPPLY_CAPACITY"]; ok {
  190. cur_cap,_ = strconv.Atoi(val)
  191. }else {
  192. cur_cap = 0
  193. }
  194. cap_ge := 0
  195. for i,v := range bat_segs {
  196. if cur_cap >= v[0] && cur_cap <= v[1] {
  197. cap_ge = i
  198. break
  199. }
  200. }
  201. if val, ok := bat_uevent["POWER_SUPPLY_STATUS"]; ok {
  202. if val == "Charging" {
  203. self.Icons["battery_charging"].SetIconIndex(cap_ge)
  204. self.Icons["battery"] = self.Icons["battery_charging"]
  205. }else {
  206. self.Icons["battery_charging"].SetIconIndex(cap_ge)
  207. self.Icons["battery"] = self.Icons["battery_discharging"]
  208. }
  209. }
  210. }
  211. func (self *TitleBar) SetBatteryStat( bat int) {
  212. }
  213. func (self *TitleBar) Init(main_screen *MainScreen) {
  214. start_x := 0
  215. self.CanvasHWND = surface.Surface(self.Width,self.Height)
  216. self.HWND = main_screen.HWND
  217. self.SkinManager = main_screen.SkinManager
  218. self.DBusManager = main_screen.DBusManager
  219. icon_wifi_status := NewTitleBarIconItem()
  220. icon_wifi_status.MyType = ICON_TYPES["STAT"]
  221. icon_wifi_status.ImageName = self.icon_base_path+"wifi.png"
  222. icon_wifi_status.Parent = self
  223. icon_wifi_status.Adjust(start_x+self.IconWidth+5,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
  224. self.Icons["wifistatus"] = icon_wifi_status
  225. battery_charging := NewTitleBarIconItem()
  226. battery_charging.MyType = ICON_TYPES["STAT"]
  227. battery_charging.Parent = self
  228. battery_charging.ImageName = self.icon_base_path+"withcharging.png"
  229. battery_charging.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
  230. self.Icons["battery_charging"] = battery_charging
  231. battery_discharging := NewTitleBarIconItem()
  232. battery_discharging.MyType = ICON_TYPES["STAT"]
  233. battery_discharging.Parent = self
  234. battery_discharging.ImageName = self.icon_base_path+"without_charging.png"
  235. battery_discharging.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
  236. self.Icons["battery_discharging"] = battery_discharging
  237. battery_unknown := NewTitleBarIconItem()
  238. battery_unknown.MyType = ICON_TYPES["STAT"]
  239. battery_unknown.Parent = self
  240. battery_unknown.ImageName = self.icon_base_path+"battery_unknown.png"
  241. battery_unknown.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
  242. self.Icons["battery_unknown"] = battery_unknown
  243. self.CheckBatteryStat()
  244. sound_volume := NewTitleBarIconItem()
  245. sound_volume.MyType = ICON_TYPES["STAT"]
  246. sound_volume.Parent = self
  247. sound_volume.ImageName = self.icon_base_path+"soundvolume.png"
  248. sound_volume.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
  249. self.Icons["soundvolume"] = sound_volume
  250. self.SyncSoundVolume()
  251. round_corners := NewTitleBarIconItem()
  252. round_corners.IconWidth = 10
  253. round_corners.IconHeight = 10
  254. round_corners.MyType = ICON_TYPES["STAT"]
  255. round_corners.Parent = self
  256. round_corners.ImgSurf = MyIconPool.GetImgSurf("roundcorners")
  257. round_corners.Adjust(0,0,10,10,0)
  258. self.Icons["round_corners"] = round_corners
  259. if self.DBusManager.IsWifiConnectedNow() {
  260. print("wifi is connected")
  261. print( self.DBusManager.WifiStrength())
  262. }else {
  263. cmd := "sudo rfkill list | grep yes | cut -d \" \" -f3" //make sure sudo rfkill needs no password
  264. out, err := exec.Command("bash", "-c", cmd).Output()
  265. if err != nil {
  266. fmt.Printf("Failed to execute command: %s\n", cmd)
  267. }else {
  268. outs := strings.Split(string(out),"\n")
  269. if len(outs) > 0 && outs[0] == "yes" {
  270. self.InAirPlaneMode = true
  271. }else{
  272. self.InAirPlaneMode = false
  273. }
  274. }
  275. }
  276. }
  277. func (self *TitleBar) ClearCanvas() {
  278. surface.Fill(self.CanvasHWND, self.SkinManager.GiveColor("TitleBg"))
  279. self.Icons["round_corners"].NewCoord(5,5)
  280. self.Icons["round_corners"].SetIconIndex(0)
  281. self.Icons["round_corners"].Draw()
  282. self.Icons["round_corners"].NewCoord(self.Width-5, 5)
  283. self.Icons["round_corners"].SetIconIndex(1)
  284. self.Icons["round_corners"].Draw()
  285. }
  286. func (self *TitleBar) Draw(title string) {
  287. self.ClearCanvas()
  288. self.Title = title
  289. cur_time := jodaTime.Format("HH:mm", gotime.Now())
  290. time_text_w, time_text_h := font.Size(self.TimeFont, cur_time)
  291. title_text_w, title_text_h := font.Size(self.TitleFont, self.Title)
  292. title_text_surf := font.Render(self.TitleFont, self.Title, true, self.SkinManager.GiveColor("Text"),nil)
  293. surface.Blit(self.CanvasHWND,title_text_surf, draw.MidRect(title_text_w/2+self.LOffset,title_text_h/2+(self.BarHeight-title_text_h)/2,title_text_w,title_text_h,Width,Height),nil)
  294. time_text_surf := font.Render(self.TimeFont, cur_time,true,self.SkinManager.GiveColor("Text"),nil)
  295. surface.Blit(self.CanvasHWND, time_text_surf, draw.MidRect(Width-time_text_w/2-self.ROffset, time_text_h/2+(self.BarHeight-time_text_h)/2, time_text_w,time_text_h,Width,Height),nil)
  296. start_x := Width - time_text_w - self.ROffset - self.IconWidth*3 // close to the time_text
  297. self.Icons["sound"].NewCoord( start_x, self.IconHeight/2+ (self.BarHeight-self.IconHeight)/2)
  298. self.Icons["battery"].NewCoord(start_x+self.IconWidth+self.IconWidth+8, self.IconHeight/2+(self.BarHeight-self.IconHeight)/2)
  299. if self.DBusManager.IsWifiConnectedNow() == true {
  300. ge := self.GetWifiStrength( self.DBusManager.WifiStrength() )
  301. fmt.Println("wifi ge: ",ge)
  302. if ge > 0 {
  303. self.Icons["wifistatus"].SetIconIndex(ge)
  304. self.Icons["wifistatus"].NewCoord(start_x+self.IconWidth+5, self.IconHeight/2+(self.BarHeight-self.IconHeight)/2 )
  305. self.Icons["wifistatus"].Draw()
  306. }else {
  307. self.Icons["wifistatus"].SetIconIndex(0)
  308. self.Icons["wifistatus"].Draw()
  309. }
  310. }else {
  311. self.Icons["wifistatus"].SetIconIndex(0)
  312. self.Icons["wifistatus"].NewCoord(start_x+self.IconWidth+5, self.IconHeight/2+(self.BarHeight-self.IconHeight)/2)
  313. self.Icons["wifistatus"].Draw()
  314. }
  315. self.Icons["sound"].Draw()
  316. self.Icons["battery"].Draw()
  317. draw.Line(self.CanvasHWND,self.SkinManager.GiveColor("Line"), 0,self.BarHeight,self.Width,self.BarHeight, self.BorderWidth)
  318. if self.HWND != nil {
  319. rect_ := rect.Rect(self.PosX,self.PosY, self.Width,self.Height)
  320. surface.Blit(self.HWND, self.CanvasHWND, &rect_, nil)
  321. }
  322. }