You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.3 KiB

2 years ago
2 years ago
2 years ago
  1. """
  2. Collection of Utility methods for creating often used, pre-styled wx Widgets
  3. """
  4. from functools import wraps
  5. import wx # type: ignore
  6. from contextlib import contextmanager
  7. from gooey.gui.three_to_four import Constants
  8. def callafter(f):
  9. """
  10. Wraps the supplied function in a wx.CallAfter
  11. for Thread-safe interop with WX.
  12. """
  13. @wraps(f)
  14. def inner(*args, **kwargs):
  15. wx.CallAfter(f, *args, **kwargs)
  16. return inner
  17. @contextmanager
  18. def transactUI(obj):
  19. """
  20. Coarse grain UI locking to avoid glitchy UI updates
  21. """
  22. obj.Freeze()
  23. try:
  24. yield
  25. finally:
  26. obj.Layout()
  27. obj.Thaw()
  28. styles = {
  29. 'h0': (wx.FONTFAMILY_DEFAULT, Constants.WX_FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False),
  30. 'h1': (wx.FONTFAMILY_DEFAULT, Constants.WX_FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False),
  31. 'h2': (wx.FONTFAMILY_DEFAULT, Constants.WX_FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False),
  32. 'bold': (wx.FONTFAMILY_DEFAULT, Constants.WX_FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False)
  33. }
  34. def make_bold(statictext):
  35. pointsize = statictext.GetFont().GetPointSize()
  36. font = wx.Font(pointsize, *styles['bold'])
  37. statictext.SetFont(font)
  38. def dark_grey(statictext):
  39. return withColor(statictext, (54, 54, 54))
  40. def withColor(statictext, hex):
  41. statictext.SetForegroundColour(hex)
  42. return statictext
  43. def h0(parent, label):
  44. text = wx.StaticText(parent, label=label)
  45. font_size = text.GetFont().GetPointSize()
  46. font = wx.Font(int(font_size * 1.4, *styles['h0']))
  47. text.SetFont(font)
  48. return text
  49. def h1(parent, label):
  50. return _header(parent, label, styles['h1'])
  51. def h2(parent, label):
  52. return _header(parent, label, styles['h2'])
  53. def _header(parent, label, styles):
  54. text = wx.StaticText(parent, label=label)
  55. font_size = text.GetFont().GetPointSize()
  56. font = wx.Font(int(font_size * 1.2), *styles)
  57. text.SetFont(font)
  58. return text
  59. def horizontal_rule(parent):
  60. return _rule(parent, wx.LI_HORIZONTAL)
  61. def vertical_rule(parent):
  62. return _rule(parent, wx.LI_VERTICAL)
  63. def _rule(parent, direction):
  64. line = wx.StaticLine(parent, -1, style=direction)
  65. line.SetSize((10, 10))
  66. return line