From 0c66cb85e3bdb91cf2af207feca56dfd0ce9e499 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Mon, 6 Sep 2021 21:15:40 +0000 Subject: [PATCH] Python console: Adjust dark theme colors to look better Fixes https://gitlab.com/kicad/code/kicad/issues/8928 --- scripting/kicad_pyshell/kicad_pyeditor.py | 107 +++++++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/scripting/kicad_pyshell/kicad_pyeditor.py b/scripting/kicad_pyshell/kicad_pyeditor.py index ee1ab695d6..f50661d419 100644 --- a/scripting/kicad_pyshell/kicad_pyeditor.py +++ b/scripting/kicad_pyshell/kicad_pyeditor.py @@ -11,9 +11,10 @@ https://github.com/wxWidgets/Phoenix/tree/master/wx/py import wx -from wx.py import crust, version, dispatcher +from wx.py import crust, version, dispatcher, editwindow from wx.py.editor import Editor from wx.py.buffer import Buffer +from wx import stc def KiNewId(): try: @@ -1072,3 +1073,107 @@ class KiCadEditorNotebook(wx.Notebook): editor=window.editor) window.SetFocus() event.Skip() + + +def is_using_dark_background(): + """Backport of wxSystemAppearance::IsUsingDarkBackground() from wxWidgets 3.1.""" + bg = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW) + fg = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT) + + return get_luminance(fg) - get_luminance(bg) > 0.2 + + +def get_luminance(color): + """Backport of wxColour::GetLuminance() from wxWidgets 3.1.""" + return (0.299*color.Red() + 0.587*color.Green() + 0.114*color.Blue()) / 255.0 + + +editwindow_old_init = editwindow.EditWindow.__init__ + +calltip_dark_bg_color = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BACKGROUND).ChangeLightness(108) +calltip_dark_fg_color = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT).ChangeLightness(108) + +def editwindow_new_init(self, *args, **kwargs): + editwindow_old_init(self, *args, **kwargs) + + # Cannot be used until we switch to wxWidgets >=3.1.3. + #if wx.SystemSettings.GetAppearance().IsUsingDarkBackground(): + + # If a dark theme is detected, we set the default system colors and add our own Python + # styles (which are not otherwise supplied by the system). + if is_using_dark_background(): + faces = editwindow.FACES + + # Override the background in all styles by setting the default style, then copying this + # to all styles with StyleClearAll(). Iterating over all styles would be slower. + self.StyleSetForeground(stc.STC_STYLE_DEFAULT, + wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)) + self.StyleSetBackground(stc.STC_STYLE_DEFAULT, + wx.SystemSettings.GetColour(wx.SYS_COLOUR_BACKGROUND)) + self.StyleClearAll() + + self.SetSelForeground(True, wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)) + self.SetSelBackground(True, wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT)) + + # Override the colors set in EditWindow.setStyles(). We go in the same order as in that + # function. Most of the foreground colors are just inverses of the default. The background + # colors default system background color with slightly adjusted brightness. + + self.StyleSetSpec(stc.STC_STYLE_LINENUMBER, + "back:#3F3F3F,face:%(mono)s,size:%(lnsize)d".format(faces)) + self.StyleSetSpec(stc.STC_STYLE_CONTROLCHAR, "face:%(mono)s".format(faces)) + + self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT, "fore:#FFFF00") + self.StyleSetBackground(stc.STC_STYLE_BRACELIGHT, + wx.SystemSettings.GetColour(wx.SYS_COLOUR_BACKGROUND).ChangeLightness(120)) + + self.StyleSetSpec(stc.STC_STYLE_BRACEBAD, "fore:#FF0000") + self.StyleSetBackground(stc.STC_STYLE_BRACEBAD, + wx.SystemSettings.GetColour(wx.SYS_COLOUR_BACKGROUND).ChangeLightness(120)) + + # Python styles + self.StyleSetSpec(stc.STC_P_DEFAULT, "face:%(mono)s".format(faces)) + self.StyleSetSpec(stc.STC_P_COMMENTLINE, + "fore:#FF80FF,face:%(mono)s".format(faces)) + self.StyleSetSpec(stc.STC_P_NUMBER, "") + self.StyleSetSpec(stc.STC_P_STRING, "fore:#80FF80,face:%(mono)s".format(faces)) + self.StyleSetSpec(stc.STC_P_CHARACTER, + "fore:#80FF80,face:%(mono)s".format(faces)) + self.StyleSetSpec(stc.STC_P_WORD, "fore:#FFFF80,bold") + self.StyleSetSpec(stc.STC_P_TRIPLE, "fore:#80FFFF") + + self.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE, "fore:#FFFFCC") + self.StyleSetBackground(stc.STC_P_TRIPLEDOUBLE, + wx.SystemSettings.GetColour(wx.SYS_COLOUR_BACKGROUND).ChangeLightness(105)) + + self.StyleSetSpec(stc.STC_P_CLASSNAME, "fore:#FFFF00,bold") + self.StyleSetSpec(stc.STC_P_DEFNAME, "fore:#FF8080,bold") + self.StyleSetSpec(stc.STC_P_OPERATOR, "") + self.StyleSetSpec(stc.STC_P_IDENTIFIER, "") + self.StyleSetSpec(stc.STC_P_COMMENTBLOCK, "fore:#808080") + self.StyleSetSpec(stc.STC_P_STRINGEOL, + "fore:#FFFFFF,face:%(mono)s,back:#1F3F1F,eolfilled".format(faces)) + + # Yellow calltip background is too contrasting. + self.CallTipSetBackground(calltip_dark_bg_color) + self.CallTipSetForeground(calltip_dark_fg_color) + + # Caret is black by default, override this. + self.SetCaretForeground(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)) + + +# HACK: Monkey-patch EditWindow to support dark themes. +editwindow.EditWindow.__init__ = editwindow_new_init + + +calltip_old_init = crust.Calltip.__init__ + +def calltip_new_init(self, *args, **kwargs): + calltip_old_init(self, *args, **kwargs) + + if is_using_dark_background(): + self.SetBackgroundColour(calltip_dark_bg_color) + self.SetForegroundColour(calltip_dark_fg_color) + +# HACK: Monkey-patch Calltip to support dark themes. +crust.Calltip.__init__ = calltip_new_init