Remove hard-coded colors from MarkupBuffer

Instead, use colors set in the theme.
ft.font-size
Gonçalo Silva 2019-03-26 14:25:11 +00:00
parent 152db98f8a
commit bef52648c7
3 changed files with 53 additions and 66 deletions

View File

@ -16,6 +16,7 @@
import re
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Pango
@ -46,14 +47,10 @@ class MarkupBuffer():
self.normal_indent = self.text_buffer.create_tag('normal_indent', indent=100)
self.green_text = self.text_buffer.create_tag("greentext",
foreground="#00364C")
self.math_text = self.text_buffer.create_tag('math_text')
self.grayfont = self.text_buffer.create_tag('graytag',
foreground="gray")
self.blackfont = self.text_buffer.create_tag('blacktag',
foreground="#222")
self.unfocused_text = self.text_buffer.create_tag('graytag',
foreground="gray")
self.underline = self.text_buffer.create_tag("underline",
underline=Pango.Underline.SINGLE)
@ -101,7 +98,8 @@ class MarkupBuffer():
self.table_env.set_property('pixels-above-lines', 0)
self.table_env.set_property('pixels-below-lines', 0)
# self.ftag = self.TextBuffer.create_tag("pix_front", pixels_above_lines = 100)
self.update_style()
regex = {
"ITALIC": re.compile(r"(\*|_)(.*?)\1", re.UNICODE), # *asdasd* // _asdasd asd asd_
"STRONG": re.compile(r"(\*\*|__)(.*?)\1", re.UNICODE), # **as das** // __asdasd asd ad a__
@ -120,6 +118,13 @@ class MarkupBuffer():
"LINK": re.compile(r"\(http(.+?)\)")
}
def update_style(self):
(found, color) = self.parent.get_style_context().lookup_color('math_text_color')
if not found:
(_, color) = self.parent.get_style_context().lookup_color('foreground_color')
self.math_text.set_property("foreground", color.to_string())
def markup_buffer(self, mode=0):
buf = self.text_buffer
@ -178,13 +183,13 @@ class MarkupBuffer():
end_iter = buf.get_iter_at_offset(context_offset + match.end())
self.text_buffer.apply_tag(self.strikethrough, start_iter, end_iter)
self.text_buffer.remove_tag(self.green_text, context_start, context_end)
self.text_buffer.remove_tag(self.math_text, context_start, context_end)
matches = re.finditer(self.regex["MATH"], text)
for match in matches:
start_iter = buf.get_iter_at_offset(context_offset + match.start())
end_iter = buf.get_iter_at_offset(context_offset + match.end())
self.text_buffer.apply_tag(self.green_text, start_iter, end_iter)
self.text_buffer.apply_tag(self.math_text, start_iter, end_iter)
for margin in self.rev_leftmargin:
self.text_buffer.remove_tag(margin, context_start, context_end)
@ -266,15 +271,13 @@ class MarkupBuffer():
self.focusmode_highlight()
def focusmode_highlight(self):
self.text_buffer.apply_tag(
self.grayfont,
self.text_buffer.get_start_iter(),
self.text_buffer.get_end_iter())
start_document = self.text_buffer.get_start_iter()
end_document = self.text_buffer.get_end_iter()
self.text_buffer.remove_tag(
self.blackfont,
self.text_buffer.get_start_iter(),
self.text_buffer.get_end_iter())
self.unfocused_text,
start_document,
end_document)
cursor = self.text_buffer.get_mark("insert")
cursor_iter = self.text_buffer.get_iter_at_mark(cursor)
@ -293,9 +296,14 @@ class MarkupBuffer():
start_sentence = cursor_iter.copy()
start_sentence.backward_sentence_start()
# grey out everything before
self.text_buffer.apply_tag(
self.blackfont,
start_sentence, end_sentence)
self.unfocused_text,
self.text_buffer.get_start_iter(), start_sentence)
self.text_buffer.apply_tag(
self.unfocused_text,
end_sentence, self.text_buffer.get_end_iter())
def set_multiplier(self, multiplier):
self.multiplier = multiplier
@ -311,13 +319,3 @@ class MarkupBuffer():
new_margin = (lm - multiplier) + multiplier + multiplier * (i + 1)
self.leftmargin[i].set_property("left-margin", 0 if new_margin < 0 else new_margin)
self.leftmargin[i].set_property("indent", - (multiplier - 1) * (i + 1) - multiplier)
def dark_mode(self, active=False):
if active:
self.green_text.set_property("foreground", "#FA5B0F")
self.grayfont.set_property("foreground", "#666")
self.blackfont.set_property("foreground", "#CCC")
else:
self.green_text.set_property("foreground", "#00364C")
self.grayfont.set_property("foreground", "gray")
self.blackfont.set_property("foreground", "#222")

View File

@ -264,11 +264,7 @@ class UberwriterWindow(Gtk.ApplicationWindow):
"""Adjusts both the window and the CSD for the current theme.
"""
theme = Theme.get_current()
if theme.is_dark:
self.markup_buffer.dark_mode(True)
else:
self.markup_buffer.dark_mode(False)
self.markup_buffer.update_style()
# Reload preview if it exists, otherwise redraw contents of window (self)
if self.preview_webview:

View File

@ -16,13 +16,14 @@ from gettext import gettext as _
import gi
from uberwriter.Theme import Theme
gi.require_version('Gtk', '3.0') # pylint: disable=wrong-import-position
from gi.repository import GLib, Gio, Gtk, Gdk, GdkPixbuf
from uberwriter import UberwriterWindow
from uberwriter.Settings import Settings
from uberwriter_lib import set_up_logging
from uberwriter_lib import helpers
from uberwriter_lib.PreferencesDialog import PreferencesDialog
from . helpers import get_builder, get_media_path
@ -36,30 +37,6 @@ class Application(Gtk.Application):
self.window = None
self.settings = Settings.new()
def init(self):
"""Init main application"""
# set theme variant (dark/light)
dark = self.settings.get_value("dark-mode")
Gtk.Settings.get_default().set_property("gtk-application-prefer-dark-theme", dark)
# set css for current theme
self.style_provider = Gtk.CssProvider()
themes = {
"Arc": "arc_style.css",
"Arc-Dark": "arc_style.css",
"Arc-Darker": "arc_style.css",
}
theme = Gtk.Settings.get_default().get_property("gtk-theme-name")
self.style_provider.load_from_path(helpers.get_media_path(themes.get(theme,"adwaita_style.css")))
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(), self.style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
def do_startup(self, *args, **kwargs):
Gtk.Application.do_startup(self)
@ -176,7 +153,7 @@ class Application(Gtk.Application):
self.set_accels_for_action("app.save", ["<Ctl>s"])
self.set_accels_for_action("app.save_as", ["<Ctl><shift>s"])
self.init()
self.apply_current_theme()
def do_activate(self, *args, **kwargs):
# We only allow a single window and raise any existing ones
@ -209,6 +186,23 @@ class Application(Gtk.Application):
self.activate()
return 0
def apply_current_theme(self):
# get current theme
theme = Theme.get_current()
# set theme variant (dark/light)
Gtk.Settings.get_default().set_property(
"gtk-application-prefer-dark-theme",
GLib.Variant("b", theme.is_dark))
# set theme css
style_provider = Gtk.CssProvider()
style_provider.load_from_path(theme.gtk_css_path)
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(), style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
def on_about(self, _action, _param):
builder = get_builder('About')
about_dialog = builder.get_object("AboutDialog")
@ -234,14 +228,13 @@ class Application(Gtk.Application):
def on_dark_mode(self, action, value):
action.set_state(value)
self.settings.set_value("dark-mode",
GLib.Variant("b", value))
self.window.toggle_dark_mode(value)
self.settings.set_value("dark-mode", GLib.Variant("b", value))
# this changes the headerbar theme accordingly
self.dark_setting = Gtk.Settings.get_default()
self.dark_setting.set_property(
"gtk-application-prefer-dark-theme", value)
self.apply_current_theme()
# adjust window for theme
self.window.apply_current_theme()
def on_focus_mode(self, action, value):
action.set_state(value)