about_applet.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/python3
  2. """
  3. Generic "About" applet provider.
  4. """
  5. import sys
  6. import cairo
  7. import yutani
  8. import text_region
  9. import toaru_fonts
  10. import yutani_mainloop
  11. class AboutAppletWindow(yutani.Window):
  12. base_width = 350
  13. base_height = 250
  14. text_offset = 110
  15. def __init__(self, decorator, title, logo, text, icon="star",on_close=None):
  16. super(AboutAppletWindow, self).__init__(self.base_width + decorator.width(), self.base_height + decorator.height(), title=title, flags=yutani.WindowFlag.FLAG_DISALLOW_RESIZE, icon=icon, doublebuffer=True)
  17. self.move(int((yutani.yutani_ctx._ptr.contents.display_width-self.width)/2),int((yutani.yutani_ctx._ptr.contents.display_height-self.height)/2))
  18. self.decorator = decorator
  19. if logo.endswith('.png'):
  20. logo = logo.replace('.png','.bmp') # Hope that works
  21. self.logo = yutani.Sprite.from_file(logo).get_cairo_surface()
  22. self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 13, 0xFF000000)
  23. self.tr = text_region.TextRegion(0,0,self.base_width-30,self.base_height-self.text_offset,font=self.font)
  24. self.tr.set_alignment(2)
  25. self.tr.set_richtext(text)
  26. self.on_close = on_close
  27. self.draw()
  28. def draw(self):
  29. surface = self.get_cairo_surface()
  30. WIDTH, HEIGHT = self.width - self.decorator.width(self), self.height - self.decorator.height(self)
  31. ctx = cairo.Context(surface)
  32. ctx.translate(self.decorator.left_width(self), self.decorator.top_height(self))
  33. ctx.rectangle(0,0,WIDTH,HEIGHT)
  34. ctx.set_source_rgb(204/255,204/255,204/255)
  35. ctx.fill()
  36. ctx.set_source_surface(self.logo,int((WIDTH-self.logo.get_width())/2),10+int((84-self.logo.get_height())/2))
  37. ctx.paint()
  38. self.tr.resize(WIDTH-30,HEIGHT-self.text_offset)
  39. self.tr.move(self.decorator.left_width(self) + 15,self.decorator.top_height(self)+self.text_offset)
  40. self.tr.draw(self)
  41. self.decorator.render(self)
  42. self.flip()
  43. def finish_resize(self, msg):
  44. """Accept a resize."""
  45. self.resize_accept(msg.width, msg.height)
  46. self.reinit()
  47. self.draw()
  48. self.resize_done()
  49. self.flip()
  50. def close_window(self):
  51. self.close()
  52. if self.on_close:
  53. self.on_close()
  54. def mouse_event(self, msg):
  55. if self.decorator.handle_event(msg) == yutani.Decor.EVENT_CLOSE:
  56. self.close_window()
  57. return
  58. x,y = msg.new_x - self.decorator.left_width(self), msg.new_y - self.decorator.top_height(self)
  59. w,h = self.width - self.decorator.width(self), self.height - self.decorator.height(self)
  60. def keyboard_event(self, msg):
  61. if msg.event.key == b"q":
  62. self.close_window()