Freexian Collaborators: How we implemented a dark mode in Debusine (by Enrico Zini)
Having learnt that Bootstrap supports
color modes,
we decided to implement an option for users to
enable dark mode
in Debusine.
By default, the color mode is selected depending on the user browser
preferences. If explicitly selected, we use a cookie to store the theme
selection so that a user can choose different color modes in different
browsers.
The work is in merge request !2401
and minimizes JavaScript dependencies
like we do in other parts of debusine.
A view to select the theme
First is a simple view to configure the selected theme and store it in a
cookie. If auto is selected, then the cookie is deleted to delegate theme
selection to JavaScript:
class ThemeSelectionView(View):
"""Select and save the current theme."""
def post(
self, request: HttpRequest, *args: Any, **kwargs: Any # noqa: U100
) -> HttpResponse:
"""Set the selected theme."""
value = request.POST.get("theme", "auto")
next_url = request.POST.get("next", None)
if next_url is None:
next_url = reverse("homepage:homepage")
response = HttpResponseRedirect(next_url)
if value == "auto":
response.delete_cookie("theme")
else:
response.set_cookie(
"theme", value, httponly=False, max_age=dt.timedelta(days=3650)
)
return response
The main base view of Debusine
reads the value from the cookie and makes it available to the templates:
def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
ctx = super().get_context_data(**kwargs)
ctx["theme"] = self.request.COOKIES.get("theme", None)
# ...
return ctx
The base template will use this value to set data-bs-theme on the main
<html> element, and that s all that is needed to select the color mode in
Bootstrap:
<html lang="en" % if theme % data-bs-theme=" theme " % endif % >
The view uses HTTP POST as it changes state, so theme selection happens in a
form:
<form id="footer-theme" class="col-auto" method="post"
action=" % url "theme-selection" % ">
% csrf_token %
<input type="hidden" name="next" value=" request.get_full_path ">
Theme:
<button type="submit" name="theme" value="dark">dark</button>
<button type="submit" name="theme" value="light">light</button>
<button type="submit" name="theme" value="auto">auto</button>
</form>
Since we added the theme selection buttons in the footer, we use CSS to render
the buttons in the same way as the rest of the footer links.
Bootstrap has a set of CSS variables
that can be used to easily in sync with the site theme, and they are especially
useful now that the theme is configurable:
footer button
background: none;
border: none;
margin: 0;
padding: 0;
color: var(--bs-link-color);
Theme autoselection
Bootstrap would support theme autoselection via browser preferences, but
that requires rebuilding its Sass sources.
Alternatively, one can use JavaScript:
% if not theme %
<script blocking="render">
(function()
let theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
let [html] = document.getElementsByTagName("html");
html.setAttribute("data-bs-theme", theme);
)();
</script>
% endif %
This reads the color scheme preferences and sets the data-bs-theme attribute
on <html>.
The script is provided inline as it needs to use blocking="render" to avoid
flashing a light background at the beginning of page load until the attribute
is set.
Given that this is a render-blocking snippet, as an extra optimization it is
not added to the page if a theme has been set.
Bootstrap CSS fixes
We were making use of the bootstrap btn-light class in navbars to highlight
elements on hover, and that doesn t work well with theme selection.
Lacking a button class that does the right thing across themes, we came up with
a new CSS class that uses variables to define a button with hover highlight
that works preserving the underlying color:
:root[data-bs-theme=light]
--debusine-hover-layer: rgb(0 0 0 / 20%);
--debusine-hover-color-multiplier: 0.8;
--debusine-disabled-color-multiplier: 1.5;
:root[data-bs-theme=dark]
--debusine-hover-layer: rgb(255 255 255 / 20%);
--debusine-hover-color-multiplier: 1.2;
--debusine-disabled-color-multiplier: 0.5;
/* Button that preserves the underlying color scheme */
.btn-debusine
--bs-btn-hover-color: rgb(from var(--bs-btn-color) calc(r * var(--debusine-hover-color-multiplier)) calc(g * var(--debusine-hover-color-multiplier)) calc(b * var(--debusine-hover-color-multiplier)));
--bs-btn-hover-bg: var(--debusine-hover-layer);
--bs-btn-disabled-color: rgb(from var(--bs-btn-color) calc(r * var(--debusine-disabled-color-multiplier)) calc(g * var(--debusine-disabled-color-multiplier)) calc(b * var(--debusine-disabled-color-multiplier)));
--bs-btn-disabled-bg: var(--bs-btn-bg);
--bs-btn-disabled-border-color: var(--bs-btn-border-color);
Dark mode!
This was a nice integration exercise with many little tricks, like how to read
color scheme preferences from the browser, render form buttons as links, use
bootstrap variables, prevent a flashing background, handle cookies in Django.
And Debusine now has a dark mode!
class ThemeSelectionView(View):
"""Select and save the current theme."""
def post(
self, request: HttpRequest, *args: Any, **kwargs: Any # noqa: U100
) -> HttpResponse:
"""Set the selected theme."""
value = request.POST.get("theme", "auto")
next_url = request.POST.get("next", None)
if next_url is None:
next_url = reverse("homepage:homepage")
response = HttpResponseRedirect(next_url)
if value == "auto":
response.delete_cookie("theme")
else:
response.set_cookie(
"theme", value, httponly=False, max_age=dt.timedelta(days=3650)
)
return response
def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
ctx = super().get_context_data(**kwargs)
ctx["theme"] = self.request.COOKIES.get("theme", None)
# ...
return ctx
<html lang="en" % if theme % data-bs-theme=" theme " % endif % >
<form id="footer-theme" class="col-auto" method="post"
action=" % url "theme-selection" % ">
% csrf_token %
<input type="hidden" name="next" value=" request.get_full_path ">
Theme:
<button type="submit" name="theme" value="dark">dark</button>
<button type="submit" name="theme" value="light">light</button>
<button type="submit" name="theme" value="auto">auto</button>
</form>
footer button
background: none;
border: none;
margin: 0;
padding: 0;
color: var(--bs-link-color);
% if not theme %
<script blocking="render">
(function()
let theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
let [html] = document.getElementsByTagName("html");
html.setAttribute("data-bs-theme", theme);
)();
</script>
% endif %
data-bs-theme attribute
on <html>.
The script is provided inline as it needs to use blocking="render" to avoid
flashing a light background at the beginning of page load until the attribute
is set.
Given that this is a render-blocking snippet, as an extra optimization it is
not added to the page if a theme has been set.
Bootstrap CSS fixes
We were making use of the bootstrap btn-light class in navbars to highlight
elements on hover, and that doesn t work well with theme selection.
Lacking a button class that does the right thing across themes, we came up with
a new CSS class that uses variables to define a button with hover highlight
that works preserving the underlying color:
:root[data-bs-theme=light]
--debusine-hover-layer: rgb(0 0 0 / 20%);
--debusine-hover-color-multiplier: 0.8;
--debusine-disabled-color-multiplier: 1.5;
:root[data-bs-theme=dark]
--debusine-hover-layer: rgb(255 255 255 / 20%);
--debusine-hover-color-multiplier: 1.2;
--debusine-disabled-color-multiplier: 0.5;
/* Button that preserves the underlying color scheme */
.btn-debusine
--bs-btn-hover-color: rgb(from var(--bs-btn-color) calc(r * var(--debusine-hover-color-multiplier)) calc(g * var(--debusine-hover-color-multiplier)) calc(b * var(--debusine-hover-color-multiplier)));
--bs-btn-hover-bg: var(--debusine-hover-layer);
--bs-btn-disabled-color: rgb(from var(--bs-btn-color) calc(r * var(--debusine-disabled-color-multiplier)) calc(g * var(--debusine-disabled-color-multiplier)) calc(b * var(--debusine-disabled-color-multiplier)));
--bs-btn-disabled-bg: var(--bs-btn-bg);
--bs-btn-disabled-border-color: var(--bs-btn-border-color);
Dark mode!
This was a nice integration exercise with many little tricks, like how to read
color scheme preferences from the browser, render form buttons as links, use
bootstrap variables, prevent a flashing background, handle cookies in Django.
And Debusine now has a dark mode!
:root[data-bs-theme=light]
--debusine-hover-layer: rgb(0 0 0 / 20%);
--debusine-hover-color-multiplier: 0.8;
--debusine-disabled-color-multiplier: 1.5;
:root[data-bs-theme=dark]
--debusine-hover-layer: rgb(255 255 255 / 20%);
--debusine-hover-color-multiplier: 1.2;
--debusine-disabled-color-multiplier: 0.5;
/* Button that preserves the underlying color scheme */
.btn-debusine
--bs-btn-hover-color: rgb(from var(--bs-btn-color) calc(r * var(--debusine-hover-color-multiplier)) calc(g * var(--debusine-hover-color-multiplier)) calc(b * var(--debusine-hover-color-multiplier)));
--bs-btn-hover-bg: var(--debusine-hover-layer);
--bs-btn-disabled-color: rgb(from var(--bs-btn-color) calc(r * var(--debusine-disabled-color-multiplier)) calc(g * var(--debusine-disabled-color-multiplier)) calc(b * var(--debusine-disabled-color-multiplier)));
--bs-btn-disabled-bg: var(--bs-btn-bg);
--bs-btn-disabled-border-color: var(--bs-btn-border-color);
Welcome to post 55 in the
After cartridge pleating and honeycombing, I was still somewhat in the
mood for that kind of fabric manipulation, and directing my internet
searches in that vague direction, and I stumbled on this:
With the pieces being rectangles the width of the fabric, I was able to
have at least one side of selvedge on all seams, and took advantage of it
by finishing the seams by simply folding the allowances to one sides so
that the selvedge was on top, and hemstitching them down as I would have
done with a folded edge when felling.
Also, at first I wanted to make the smocking in white on white, but then
I thought about a few hanks of electric blue floss I had in my stash,
and decided to just go with it.
The initial seams were quickly made, then I started the smocking at the
neck, and at that time the project went on hold while I got ready to go
to DebConf. Then I came back and took some time to get back into a
sewing mood, but finally the smocking on the next was finished, and I
could go on with the main sewing, which, as I expected, went decently
fast for a handsewing project.
While doing the diagonal smocking on the collar I counted the stitches
to make each side the same length, which didn t completely work because
the gathers weren t that regular to start with, and started each line
from the two front opening going towards the center back, leaving a
triangle with a different size right in the middle. I think overall it
worked well enough.
Then there were a few more interruptions, but at last it was ready! just
as the weather turned cold-ish and puffy shirts were no longer in
season, but it will be there for me next spring.
I did manage to wear it a few times and I have to say that the neck
shaping is quite comfortable indeed: it doesn t pull in odd ways like
the classical historically accurate pirate shirt sometimes does, and the
heavy gathering at the neck makes it feel padded and soft.
I m not as happy with the cuffs: the way I did them with just
honeycombing means that they don t need a closure, and after washing and
a bit of steaming they lie nicely, but then they tend to relax in a
wider shape. The next time I think I ll leave a slit in the sleeves,
possibly make a different type of smocking (depending on whether I have
enough fabric) and then line them like the neck so that they are stable.
Because, yes, I think that there will be another time: I have a few more
project before that, and I want to spend maybe another year working from
my stash, but then I think I ll buy some soft linen and make at least
another one, maybe with white-on-white smocking so that it will be
easier to match with different garments.
I haven't been blogging as much as I used to. For a while, I've been hosting my
blog on a single-core DigitalOcean droplet, which cost me around $7 per month.
It also hosted my mail server. Most of the time, the droplet was idle, and I
wasn't actively using my personal email much. Since it was self-hosted, I didn't
have confidence that it would always be up, so I relied on Gmail as my personal
email for everything from banking to social media.
Now, I feel this cost is just a waste of money, even though it's not huge. So, I
decided to move my blog back to GitHub Pages, now published using GitHub
Workflows. I've stopped my mail server for the time being, and it won't be
reachable for a while. For any personal queries or comments, please reach me on
my
When I started looking at 
If you read the above title, you might wonder how the switch to wayland (yes, the
[1]: If you try to run
This week, some of the
gst-fallbacksrc
Carl's box's needs lead to looking at
DC25 network usage graphs.
Flow diagrams.
Streaming bandwidth graph.












