[meld] Break out style handling helpers into new module
- From: Kai Willadsen <kaiw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [meld] Break out style handling helpers into new module
- Date: Fri, 5 Jul 2019 01:24:27 +0000 (UTC)
commit de10bd839b4494d11c26a9a3dcb5177efdb597ba
Author: Kai Willadsen <kai willadsen gmail com>
Date: Sun Jun 30 09:01:23 2019 +1000
Break out style handling helpers into new module
meld/actiongutter.py | 2 +-
meld/chunkmap.py | 2 +-
meld/gutterrendererchunk.py | 2 +-
meld/linkmap.py | 2 +-
meld/misc.py | 96 ----------------------------------
meld/sourceview.py | 2 +-
meld/style.py | 122 ++++++++++++++++++++++++++++++++++++++++++++
meld/tree.py | 2 +-
po/POTFILES.in | 1 +
9 files changed, 129 insertions(+), 102 deletions(-)
---
diff --git a/meld/actiongutter.py b/meld/actiongutter.py
index deb4b868..3d7740d2 100644
--- a/meld/actiongutter.py
+++ b/meld/actiongutter.py
@@ -23,8 +23,8 @@ from gi.repository import Gtk
from meld.conf import _
from meld.const import ActionMode, ChunkAction
-from meld.misc import get_common_theme
from meld.settings import meldsettings
+from meld.style import get_common_theme
from meld.ui.gtkcompat import get_style
diff --git a/meld/chunkmap.py b/meld/chunkmap.py
index 4f489a82..c33d855e 100644
--- a/meld/chunkmap.py
+++ b/meld/chunkmap.py
@@ -22,8 +22,8 @@ from gi.repository import Gdk
from gi.repository import GObject
from gi.repository import Gtk
-from meld.misc import get_common_theme
from meld.settings import meldsettings
+from meld.style import get_common_theme
from meld.tree import STATE_ERROR, STATE_MODIFIED, STATE_NEW
log = logging.getLogger(__name__)
diff --git a/meld/gutterrendererchunk.py b/meld/gutterrendererchunk.py
index c2a62acd..58d60500 100644
--- a/meld/gutterrendererchunk.py
+++ b/meld/gutterrendererchunk.py
@@ -19,8 +19,8 @@ from gi.repository import Gdk
from gi.repository import GtkSource
from gi.repository import Pango
-from meld.misc import get_common_theme
from meld.settings import meldsettings
+from meld.style import get_common_theme
def get_background_rgba(renderer):
diff --git a/meld/linkmap.py b/meld/linkmap.py
index f8446206..6996da00 100644
--- a/meld/linkmap.py
+++ b/meld/linkmap.py
@@ -20,8 +20,8 @@ import math
from gi.repository import Gdk
from gi.repository import Gtk
-from meld.misc import get_common_theme
from meld.settings import meldsettings
+from meld.style import get_common_theme
# Rounded rectangle corner radius for culled changes display
diff --git a/meld/misc.py b/meld/misc.py
index e91bddb8..c727f57f 100644
--- a/meld/misc.py
+++ b/meld/misc.py
@@ -30,7 +30,6 @@ from typing import (
Callable,
Generator,
List,
- Mapping,
Optional,
Pattern,
Sequence,
@@ -39,10 +38,8 @@ from typing import (
Union,
)
-from gi.repository import Gdk
from gi.repository import GLib
from gi.repository import Gtk
-from gi.repository import GtkSource
from meld.conf import _
@@ -163,99 +160,6 @@ def user_critical(
return wrap
-MELD_STYLE_SCHEME = "meld-base"
-MELD_STYLE_SCHEME_DARK = "meld-dark"
-
-
-base_style_scheme: Optional[GtkSource.StyleScheme] = None
-
-
-def get_base_style_scheme() -> GtkSource.StyleScheme:
-
- global base_style_scheme
-
- if base_style_scheme:
- return base_style_scheme
-
- env_theme = GLib.getenv('GTK_THEME')
- if env_theme:
- use_dark = env_theme.endswith(':dark')
- else:
- gtk_settings = Gtk.Settings.get_default()
- use_dark = gtk_settings.props.gtk_application_prefer_dark_theme
-
- # As of 3.28, the global dark theme switch is going away.
- if not use_dark:
- from meld.sourceview import MeldSourceView
- stylecontext = MeldSourceView().get_style_context()
- background_set, rgba = (
- stylecontext.lookup_color('theme_bg_color'))
-
- # This heuristic is absolutely dire. I made it up. There's
- # literally no basis to this.
- if background_set and rgba.red + rgba.green + rgba.blue < 1.0:
- use_dark = True
-
- base_scheme_name = (
- MELD_STYLE_SCHEME_DARK if use_dark else MELD_STYLE_SCHEME)
-
- manager = GtkSource.StyleSchemeManager.get_default()
- base_style_scheme = manager.get_scheme(base_scheme_name)
-
- return base_style_scheme
-
-
-def colour_lookup_with_fallback(name: str, attribute: str) -> Gdk.RGBA:
- from meld.settings import meldsettings
- source_style = meldsettings.style_scheme
-
- style = source_style.get_style(name) if source_style else None
- style_attr = getattr(style.props, attribute) if style else None
- if not style or not style_attr:
- base_style = get_base_style_scheme()
- try:
- style = base_style.get_style(name)
- style_attr = getattr(style.props, attribute)
- except AttributeError:
- pass
-
- if not style_attr:
- import sys
- print(_(
- "Couldn’t find colour scheme details for %s-%s; "
- "this is a bad install") % (name, attribute), file=sys.stderr)
- sys.exit(1)
-
- colour = Gdk.RGBA()
- colour.parse(style_attr)
- return colour
-
-
-ColourMap = Mapping[str, Gdk.RGBA]
-
-
-def get_common_theme() -> Tuple[ColourMap, ColourMap]:
- lookup = colour_lookup_with_fallback
- fill_colours = {
- "insert": lookup("meld:insert", "background"),
- "delete": lookup("meld:insert", "background"),
- "conflict": lookup("meld:conflict", "background"),
- "replace": lookup("meld:replace", "background"),
- "error": lookup("meld:error", "background"),
- "focus-highlight": lookup("meld:current-line-highlight", "foreground"),
- "current-chunk-highlight": lookup(
- "meld:current-chunk-highlight", "background")
- }
- line_colours = {
- "insert": lookup("meld:insert", "line-background"),
- "delete": lookup("meld:insert", "line-background"),
- "conflict": lookup("meld:conflict", "line-background"),
- "replace": lookup("meld:replace", "line-background"),
- "error": lookup("meld:error", "line-background"),
- }
- return fill_colours, line_colours
-
-
def all_same(iterable: Sequence) -> bool:
"""Return True if all elements of the list are equal"""
sample, has_no_sample = None, True
diff --git a/meld/sourceview.py b/meld/sourceview.py
index 152b31b7..c7735d23 100644
--- a/meld/sourceview.py
+++ b/meld/sourceview.py
@@ -25,8 +25,8 @@ from gi.repository import Gtk
from gi.repository import GtkSource
from meld.meldbuffer import MeldBuffer
-from meld.misc import colour_lookup_with_fallback, get_common_theme
from meld.settings import bind_settings, meldsettings, settings
+from meld.style import colour_lookup_with_fallback, get_common_theme
log = logging.getLogger(__name__)
diff --git a/meld/style.py b/meld/style.py
new file mode 100644
index 00000000..fc90dd2e
--- /dev/null
+++ b/meld/style.py
@@ -0,0 +1,122 @@
+# Copyright (C) 2002-2006 Stephen Kennedy <stevek gnome org>
+# Copyright (C) 2009 Vincent Legoll <vincent legoll gmail com>
+# Copyright (C) 2012-2019 Kai Willadsen <kai willadsen gmail com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or (at
+# your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+from typing import (
+ Mapping,
+ Optional,
+ Tuple,
+)
+
+from gi.repository import Gdk
+from gi.repository import GLib
+from gi.repository import Gtk
+from gi.repository import GtkSource
+
+from meld.conf import _
+
+MELD_STYLE_SCHEME = "meld-base"
+MELD_STYLE_SCHEME_DARK = "meld-dark"
+
+
+base_style_scheme: Optional[GtkSource.StyleScheme] = None
+
+
+def get_base_style_scheme() -> GtkSource.StyleScheme:
+
+ global base_style_scheme
+
+ if base_style_scheme:
+ return base_style_scheme
+
+ env_theme = GLib.getenv('GTK_THEME')
+ if env_theme:
+ use_dark = env_theme.endswith(':dark')
+ else:
+ gtk_settings = Gtk.Settings.get_default()
+ use_dark = gtk_settings.props.gtk_application_prefer_dark_theme
+
+ # As of 3.28, the global dark theme switch is going away.
+ if not use_dark:
+ from meld.sourceview import MeldSourceView
+ stylecontext = MeldSourceView().get_style_context()
+ background_set, rgba = (
+ stylecontext.lookup_color('theme_bg_color'))
+
+ # This heuristic is absolutely dire. I made it up. There's
+ # literally no basis to this.
+ if background_set and rgba.red + rgba.green + rgba.blue < 1.0:
+ use_dark = True
+
+ base_scheme_name = (
+ MELD_STYLE_SCHEME_DARK if use_dark else MELD_STYLE_SCHEME)
+
+ manager = GtkSource.StyleSchemeManager.get_default()
+ base_style_scheme = manager.get_scheme(base_scheme_name)
+
+ return base_style_scheme
+
+
+def colour_lookup_with_fallback(name: str, attribute: str) -> Gdk.RGBA:
+ from meld.settings import meldsettings
+ source_style = meldsettings.style_scheme
+
+ style = source_style.get_style(name) if source_style else None
+ style_attr = getattr(style.props, attribute) if style else None
+ if not style or not style_attr:
+ base_style = get_base_style_scheme()
+ try:
+ style = base_style.get_style(name)
+ style_attr = getattr(style.props, attribute)
+ except AttributeError:
+ pass
+
+ if not style_attr:
+ import sys
+ print(_(
+ "Couldn’t find colour scheme details for %s-%s; "
+ "this is a bad install") % (name, attribute), file=sys.stderr)
+ sys.exit(1)
+
+ colour = Gdk.RGBA()
+ colour.parse(style_attr)
+ return colour
+
+
+ColourMap = Mapping[str, Gdk.RGBA]
+
+
+def get_common_theme() -> Tuple[ColourMap, ColourMap]:
+ lookup = colour_lookup_with_fallback
+ fill_colours = {
+ "insert": lookup("meld:insert", "background"),
+ "delete": lookup("meld:insert", "background"),
+ "conflict": lookup("meld:conflict", "background"),
+ "replace": lookup("meld:replace", "background"),
+ "error": lookup("meld:error", "background"),
+ "focus-highlight": lookup("meld:current-line-highlight", "foreground"),
+ "current-chunk-highlight": lookup(
+ "meld:current-chunk-highlight", "background")
+ }
+ line_colours = {
+ "insert": lookup("meld:insert", "line-background"),
+ "delete": lookup("meld:insert", "line-background"),
+ "conflict": lookup("meld:conflict", "line-background"),
+ "replace": lookup("meld:replace", "line-background"),
+ "error": lookup("meld:error", "line-background"),
+ }
+ return fill_colours, line_colours
diff --git a/meld/tree.py b/meld/tree.py
index f4d853fe..fcee1601 100644
--- a/meld/tree.py
+++ b/meld/tree.py
@@ -22,7 +22,7 @@ from gi.repository import GLib
from gi.repository import GObject
from gi.repository import Pango
-from meld.misc import colour_lookup_with_fallback
+from meld.style import colour_lookup_with_fallback
from meld.treehelpers import SearchableTreeStore
from meld.vc._vc import ( # noqa: F401
CONFLICT_BASE, CONFLICT_LOCAL, CONFLICT_MERGED, CONFLICT_OTHER,
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 83c5a543..649d1d73 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -48,6 +48,7 @@ meld/newdifftab.py
meld/patchdialog.py
meld/preferences.py
meld/recent.py
+meld/style.py
meld/ui/bufferselectors.py
meld/ui/findbar.py
meld/ui/historyentry.py
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]