Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

Commit

Permalink
Tackle .night_mode class in Overview and DeckBrowser #50
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski committed Sep 30, 2018
1 parent 7860895 commit c7bdbf6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 39 deletions.
31 changes: 31 additions & 0 deletions night_mode/css_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def inject_css_class(state: bool, html: str):
if state:
javascript = """
function add_night_mode_class(){
current_classes = document.body.className;
if(current_classes.indexOf("night_mode") == -1)
{
document.body.className += " night_mode";
}
}
// explanation of setTimeout use:
// callback defined in _showQuestion of reviewer.js would otherwise overwrite
// the newly set body class; in order to prevent that the function execution
// is being placed on the end of execution queue (hence time = 0)
setTimeout(add_night_mode_class, 0)
"""
else:
javascript = """
function remove_night_mode_class(){
current_classes = document.body.className;
if(current_classes.indexOf("night_mode") != -1)
{
document.body.className = current_classes.replace("night_mode","");
}
}
setTimeout(remove_night_mode_class, 0)
"""
# script on the beginning of the HTML so it will always be
# before any user-defined, potentially malformed HTML
html = f"<script>{javascript}</script>" + html
return html
32 changes: 2 additions & 30 deletions night_mode/night_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from .actions_and_settings import *
from .internals import alert
from .config import Config, ConfigValueGetter
from .css_class import inject_css_class
from .icons import Icons
from .menu import get_or_create_menu, Menu
from .stylers import Styler
Expand Down Expand Up @@ -216,36 +217,7 @@ def message_box(self):
return box

def night_class_injection(self, html, card, context):

if self.config.state_on.value:
javascript = """
function add_night_mode_class(){
current_classes = document.body.className;
if(current_classes.indexOf("night_mode") == -1)
{
document.body.className += " night_mode";
}
}
// explanation of setTimeout use:
// callback defined in _showQuestion of reviewer.js would otherwise overwrite
// the newly set body class; in order to prevent that the function execution
// is being placed on the end of execution queue (hence time = 0)
setTimeout(add_night_mode_class, 0)
"""
else:
javascript = """
function remove_night_mode_class(){
current_classes = document.body.className;
if(current_classes.indexOf("night_mode") != -1)
{
document.body.className = current_classes.replace("night_mode","");
}
}
setTimeout(remove_night_mode_class, 0)
"""
# script on the beginning of the HTML so it will always be
# before any user-defined, potentially malformed HTML
html = f"<script>{javascript}</script>" + html
html = inject_css_class(self.config.state_on.value, html)
return html

def background_bug_workaround(self, editor):
Expand Down
16 changes: 7 additions & 9 deletions night_mode/stylers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .gui import AddonDialog, iterate_widgets

from .config import ConfigValueGetter
from .css_class import inject_css_class
from .internals import percent_escaped, move_args_to_kwargs, from_utf8, PropertyDescriptor
from .internals import style_tag, wraps, appends_in_night_mode, replaces_in_night_mode, css
from .styles import SharedStyles, ButtonsStyle, ImageStyle, DeckStyle, LatexStyle, DialogStyle
Expand Down Expand Up @@ -231,10 +232,9 @@ class DeckBrowserStyler(Styler):
}

@appends_in_night_mode
@style_tag
@percent_escaped
def _body(self):
return self.deck.style + self.shared.body_colors
styles_html = style_tag(percent_escaped(self.deck.style + self.shared.body_colors))
return inject_css_class(True, styles_html)


class DeckBrowserBottomStyler(Styler):
Expand All @@ -245,10 +245,9 @@ class DeckBrowserBottomStyler(Styler):
}

@appends_in_night_mode
@style_tag
@percent_escaped
def _centerBody(self):
return self.deck.bottom
styles_html = style_tag(percent_escaped(self.deck.bottom))
return inject_css_class(True, styles_html)


class OverviewStyler(Styler):
Expand All @@ -260,10 +259,9 @@ class OverviewStyler(Styler):
}

@appends_in_night_mode
@style_tag
@percent_escaped
def _body(self):
return self.css
styles_html = style_tag(percent_escaped(self.css))
return inject_css_class(True, styles_html)

@css
def css(self):
Expand Down

0 comments on commit c7bdbf6

Please sign in to comment.