Skip to content

Commit

Permalink
Let user configure syntax highlighting colors
Browse files Browse the repository at this point in the history
  • Loading branch information
zas committed May 26, 2024
1 parent 1676cf1 commit a2906f2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
23 changes: 23 additions & 0 deletions picard/ui/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class UnknownColorException(Exception):
'tagstatus_added': N_("Tag added"),
'tagstatus_changed': N_("Tag changed"),
'tagstatus_removed': N_("Tag removed"),
'syntax_hl_error': N_("Error syntax highlight"),
'syntax_hl_escape': N_("Escape syntax highlight"),
'syntax_hl_func': N_("Function syntax highlight"),
'syntax_hl_noop': N_("Noop syntax highlight"),
'syntax_hl_special': N_("Special syntax highlight"),
'syntax_hl_unicode': N_("Unicode syntax highlight"),
'syntax_hl_var': N_("Variable syntax highlight"),
}


Expand Down Expand Up @@ -97,6 +104,22 @@ def register_color(themes, name, value):
register_color(_LIGHT, 'first_cover_hl', 'darkgoldenrod')
register_color(_DARK, 'first_cover_hl', 'orange')

# syntax highlighting colors
register_color(_LIGHT, 'syntax_hl_error', 'blue')
register_color(_LIGHT, 'syntax_hl_escape', 'darkRed')
register_color(_LIGHT, 'syntax_hl_func', 'blue')
register_color(_LIGHT, 'syntax_hl_noop', 'darkGray')
register_color(_LIGHT, 'syntax_hl_special', 'blue')
register_color(_LIGHT, 'syntax_hl_unicode', 'darkRed')
register_color(_LIGHT, 'syntax_hl_var', 'darkCyan')
register_color(_DARK, 'syntax_hl_error', '#FF57A0')
register_color(_DARK, 'syntax_hl_escape', '#4BEF1F')
register_color(_DARK, 'syntax_hl_func', '#FF57A0')
register_color(_DARK, 'syntax_hl_noop', '#04E7D5')
register_color(_DARK, 'syntax_hl_special', '#FF57A0')
register_color(_DARK, 'syntax_hl_unicode', '#4BEF1F')
register_color(_DARK, 'syntax_hl_var', '#FCBB51')


class InterfaceColors:

Expand Down
23 changes: 0 additions & 23 deletions picard/ui/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


from collections import namedtuple
from enum import Enum

from PyQt6 import (
Expand Down Expand Up @@ -73,24 +72,6 @@ def _missing_(cls, value):
elif not IS_HAIKU:
AVAILABLE_UI_THEMES.extend([UiTheme.SYSTEM])

SyntaxTheme = namedtuple('SyntaxTheme', 'func var escape special noop')

light_syntax_theme = SyntaxTheme(
func=QtGui.QColor(QtCore.Qt.GlobalColor.blue),
var=QtGui.QColor(QtCore.Qt.GlobalColor.darkCyan),
escape=QtGui.QColor(QtCore.Qt.GlobalColor.darkRed),
special=QtGui.QColor(QtCore.Qt.GlobalColor.blue),
noop=QtGui.QColor(QtCore.Qt.GlobalColor.darkGray),
)

dark_syntax_theme = SyntaxTheme(
func=QtGui.QColor(255, 87, 160, 255), # magenta
var=QtGui.QColor(252, 187, 81, 255), # orange
escape=QtGui.QColor(75, 239, 31, 255), # green
special=QtGui.QColor(255, 87, 160, 255), # magenta
noop=QtGui.QColor(4, 231, 213, 255), # cyan
)


class MacOverrideStyle(QtWidgets.QProxyStyle):
"""Override the default style to fix some platform specific issues"""
Expand Down Expand Up @@ -144,10 +125,6 @@ def is_dark_theme(self):
def accent_color(self): # pylint: disable=no-self-use
return None

@property
def syntax_theme(self):
return dark_syntax_theme if self.is_dark_theme else light_syntax_theme

# pylint: disable=no-self-use
def update_palette(self, palette, dark_theme, accent_color):
if accent_color:
Expand Down
4 changes: 2 additions & 2 deletions picard/ui/widgets/scriptdocumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from picard.script import script_function_documentation_all

from picard.ui import FONT_FAMILY_MONOSPACE
from picard.ui.theme import theme
from picard.ui.colors import interface_colors


DOCUMENTATION_HTML_TEMPLATE = '''
Expand Down Expand Up @@ -97,7 +97,7 @@ def process_html(html, function):

html = DOCUMENTATION_HTML_TEMPLATE % {
'html': "<dl>%s</dl>" % funcdoc,
'script_function_fg': theme.syntax_theme.func.name(),
'script_function_fg': interface_colors.get_qcolor('syntax_hl_func').name(),
'monospace_font': FONT_FAMILY_MONOSPACE,
'dir': text_direction,
'inline_start': 'right' if text_direction == 'rtl' else 'left'
Expand Down
19 changes: 9 additions & 10 deletions picard/ui/widgets/scripttextedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
)

from picard.ui import FONT_FAMILY_MONOSPACE
from picard.ui.theme import theme
from picard.ui.colors import interface_colors


EXTRA_VARIABLES = (
Expand Down Expand Up @@ -114,7 +114,7 @@ class HighlightFormat(QtGui.QTextCharFormat):
def __init__(self, fg_color=None, italic=False, bold=False):
super().__init__()
if fg_color is not None:
self.setForeground(fg_color)
self.setForeground(interface_colors.get_qcolor(fg_color))
if italic:
self.setFontItalic(True)
if bold:
Expand All @@ -125,16 +125,15 @@ class TaggerScriptSyntaxHighlighter(QtGui.QSyntaxHighlighter):

def __init__(self, document):
super().__init__(document)
syntax_theme = theme.syntax_theme

self.textcharformats = {
'escape': HighlightFormat(fg_color=syntax_theme.escape),
'func': HighlightFormat(fg_color=syntax_theme.func, bold=True),
'noop': HighlightFormat(fg_color=syntax_theme.noop, bold=True, italic=True),
'special': HighlightFormat(fg_color=syntax_theme.special),
'unicode': HighlightFormat(fg_color=syntax_theme.escape, italic=True),
'unknown_func': HighlightFormat(fg_color=syntax_theme.special, italic=True),
'var': HighlightFormat(fg_color=syntax_theme.var),
'escape': HighlightFormat(fg_color='syntax_hl_escape'),
'func': HighlightFormat(fg_color='syntax_hl_func', bold=True),
'noop': HighlightFormat(fg_color='syntax_hl_noop', bold=True, italic=True),
'special': HighlightFormat(fg_color='syntax_hl_special'),
'unicode': HighlightFormat(fg_color='syntax_hl_unicode', italic=True),
'unknown_func': HighlightFormat(fg_color='syntax_hl_error', italic=True),
'var': HighlightFormat(fg_color='syntax_hl_var'),
}

self.rules = list(self.func_rules())
Expand Down

0 comments on commit a2906f2

Please sign in to comment.