# Suggestion: Make night mode toggleable in the card type editor

**URL:** <https://forums.ankiweb.net/t/suggestion-make-night-mode-toggleable-in-the-card-type-editor/321>\
**Category:** Suggestions\
**Created:** [June 19, 2020, 11:00am UTC](https://forums.ankiweb.net/t/suggestion-make-night-mode-toggleable-in-the-card-type-editor/321 "2020-06-19T11:00:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nwwt](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@nwwt](https://forums.ankiweb.net/u/nwwt)\
**Post date:** [June 19, 2020, 11:00am UTC](https://forums.ankiweb.net/t/suggestion-make-night-mode-toggleable-in-the-card-type-editor/321/1 "2020-06-19T11:00:29Z")

</div>

As the title says. Would make designing for both modes a bit more straightforward.

---

<div class="post-metadata">

**Author:** ![guillempalausalva](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/guillempalausalva/32/34258_2.png) [@guillempalausalva](https://forums.ankiweb.net/u/guillempalausalva)\
**Post date:** [June 19, 2020, 11:41am UTC](https://forums.ankiweb.net/t/suggestion-make-night-mode-toggleable-in-the-card-type-editor/321/2 "2020-06-19T11:41:20Z")

</div>

That would be nice. I don’t know if that involves much change in to code. Just to rephrase you point little bit: the idea of having a button in card template editor to toogle the preview windows to a nightmode preview, so not changing the whole Anki IU.

That woud prevent restariing Anki each time for the sole purpose of changing the nightmode on and off in order too see the styling. That scenario would be more useful for the users who do review almost exclusively on mobile versions with nightmode on rather than in the desktop. Just my thought.

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [June 19, 2020, 10:56pm UTC](https://forums.ankiweb.net/t/suggestion-make-night-mode-toggleable-in-the-card-type-editor/321/3 "2020-06-19T22:56:58Z")

</div>

Thanks for the suggestion, I’ll look into it.

---

<div class="post-metadata">

**Author:** ![nwwt](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@nwwt](https://forums.ankiweb.net/u/nwwt)\
**Post date:** [June 20, 2020, 10:40am UTC](https://forums.ankiweb.net/t/suggestion-make-night-mode-toggleable-in-the-card-type-editor/321/4 "2020-06-20T10:40:57Z")

</div>

Come to think of it, maybe likewise make the .mobile class toggleable, though that would need an added warning how other factors (different rendering engines, resolution, apps’ own CSS, …) may cause unexpected differences.

---

<div class="post-metadata">

**Author:** ![ijgnd](https://avatars.discourse-cdn.com/v4/letter/i/bbe5ce/32.png) [@ijgnd](https://forums.ankiweb.net/u/ijgnd)\
**Post date:** [June 20, 2020, 3:04pm UTC](https://forums.ankiweb.net/t/suggestion-make-night-mode-toggleable-in-the-card-type-editor/321/5 "2020-06-20T15:04:03Z")

</div>

a built-in solution sounds useful.

&nbsp;

the reddit user [/u/yumenogotoshi](https://www.reddit.com/user/yumenogotoshi/) has posted an add-on (that’s not on ankiweb) that toggles the nightmode in webviews based on a tag, see

source code available at: [https://gist.github.com/hikaru-y/737e60d5ff58a124ffe7e2a78a385831](https://gist.github.com/hikaru-y/737e60d5ff58a124ffe7e2a78a385831)

in response to [a reddit questions of mine here](https://www.reddit.com/r/Anki/comments/gehk60/how_to_disable_night_mode_temporarily_for_a/).

in the reviewer you can toggle and reload the webview with my updated version of [Tag Toggler (Fork for 2.1)](https://ankiweb.net/shared/info/1429363228)

---

<div class="post-metadata">

**Author:** ![mmdj2](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/mmdj2/32/124_2.png) [@mmdj2](https://forums.ankiweb.net/u/mmdj2)\
**Post date:** [June 20, 2020, 3:26pm UTC](https://forums.ankiweb.net/t/suggestion-make-night-mode-toggleable-in-the-card-type-editor/321/6 "2020-06-20T15:26:08Z")

</div>

am i not able to access this or it’s just disabled?

 ![Annotation 2020-06-20 194732](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/a394305cba61d120d996ab4ec29521148a529166.png)

---

<div class="post-metadata">

**Author:** ![guillempalausalva](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/guillempalausalva/32/34258_2.png) [@guillempalausalva](https://forums.ankiweb.net/u/guillempalausalva)\
**Post date:** [June 20, 2020, 3:41pm UTC](https://forums.ankiweb.net/t/suggestion-make-night-mode-toggleable-in-the-card-type-editor/321/7 "2020-06-20T15:41:30Z")

</div>

I can access it. I guess it depends on the region and you are in a" US sanctioned region". Here is the code:

```
from typing import Optional, Any
import anki
import aqt

TARGET_TAG = 'disable_night_mode'
TMP_NORMAL_CLASS = 'tmp_normal_mode'
night_mode_classes = "'nightMode', 'night_mode'"
ADD_NM = f'document.body.classList.add({night_mode_classes});'
REMOVE_NM = f'document.body.classList.remove({night_mode_classes});'
ON_SHOWN = '''
<script>
    onShownHook.push(() => {{
        {operation}
    }});
</script>
'''

def get_normal_mode_window_color():
    ''' Based on: aqt.webview.AnkiWebView._getWindowColor() '''
    qcolor: aqt.qt.QColor
    if anki.utils.isMac:
        # standard palette does not return correct window color on macOS
        qcolor = aqt.qt.QColor('#ececec')
    else:
        qcolor = aqt.mw.web.style().standardPalette().color(
            aqt.qt.QPalette.Window)
    return qcolor.name()

EDITOR_TMP_NORMAL_MODE_STYLE = f'''
.{TMP_NORMAL_CLASS},
.{TMP_NORMAL_CLASS} body,
.{TMP_NORMAL_CLASS} #topbutsOuter {{
    background-color: {get_normal_mode_window_color()};
}}
'''

def toggle_editor_night_mode(tags_text, editor):
    js: str
    if TARGET_TAG in tags_text.split():
        js = REMOVE_NM
        js += f'document.documentElement.classList.add("{TMP_NORMAL_CLASS}");'
    else:
        js = ADD_NM
        js += f'document.documentElement.classList.remove("{TMP_NORMAL_CLASS}");'

    editor.web.eval(js)

def on_editor_did_init(editor: aqt.editor.Editor):
    editor.tags.textChanged.connect(
        lambda text: toggle_editor_night_mode(text, editor))

def on_webview_will_set_content(
        web_content: aqt.webview.WebContent, context: Optional[Any]):
    if isinstance(context, aqt.editor.Editor):
        web_content.head += f'<style>{EDITOR_TMP_NORMAL_MODE_STYLE}</style>'

def on_card_will_show(text: str, card: anki.cards.Card, kind: str):
    js_to_append: str
    if TARGET_TAG in card.note().tags:
        js_to_append = ON_SHOWN.format(operation=REMOVE_NM)
    else:
        js_to_append = ON_SHOWN.format(operation=ADD_NM)

    return text + js_to_append

if aqt.mw.pm.night_mode():
    aqt.gui_hooks.editor_did_init.append(on_editor_did_init)
    aqt.gui_hooks.webview_will_set_content.append(on_webview_will_set_content)
    aqt.gui_hooks.card_will_show.append(on_card_will_show)
```

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [June 21, 2020, 11:49pm UTC](https://forums.ankiweb.net/t/suggestion-make-night-mode-toggleable-in-the-card-type-editor/321/8 "2020-06-21T23:49:44Z")

</div>

Added to

> <https://github.com/ankitects/help-wanted/issues/12>
>
> Suggested on:
> https://forums.ankiweb.net/t/suggestion-make-night-mode-toggleable-in-the-card-type-editor/321/2
> Due to limited space, the "fill empty fields" button should probably be turned into an options button that reveals two...
