Skip to content

Libdoc: Support setting dark or light mode explicitly #4497

Closed

Description

@mardukbp

The HTML documentation generated by libdoc can be opened in an IDE, where many people use a dark theme. The contrast between the Robot Framework code on a dark background and the library documentation with a white background is unpleasant.

This problem can be solved if libdoc has a stylesheet parameter to specify the CSS file that should be used to style the documentation. And maybe libdoc should include a light and a dark stylesheet.

Activity

  1. pekkaklarck commented on Oct 6, 2022

    @pekkaklarck
    Member

    This is a good idea and injecting a custom style sheet wouldn't be too complicated. The problem is that we are planning to totally reimplement Libdoc's HTML output (#4304) and that would break all such external style sheets. A related issues that if we allow injecting styles, the whole HTML structure becomes part of the public API and we cannot change it without possibly breaking external styles. At this point it's probably best to wait until we get started with #4304 until we make a decision about that. I'll add a comment about this to that issue. Until this is resolved, you can pretty easily inject styles to the generated HTML file.

    Support for dark and light modes is a different thing because it could be just enabled externally and how it actually works would be an implementation detail. More importantly, I think Libdoc already supports the dark mode but I don't know how it's actually activated nor that is there a way to do that programmatically. Ping @Snooz82!

  2. mardukbp commented on Oct 7, 2022

    @mardukbp
    Author

    You are right. libdoc.css already supports dark mode, but only if configured in the OS. I think adding a button to manually toggle the dark mode would be a good idea.

  3. pekkaklarck commented on Oct 7, 2022

    @pekkaklarck
    Member

    Adding a button sounds good to me. Are you interested to provide a PR?

    For IDE interaction there probably should be an API to switch between modes as well. Do @d-biehl or @fabioz have opinions about this?

    If enhancements are added fast, they can still make it to RF 6.0. We need another release candidate and my current plan is to release it next Monday.

  4. Snooz82 commented on Oct 7, 2022

    @Snooz82
    Member

    It is not the os defining the color scheme.
    It is the browser. But by default the browser takes it from os.

    not sure if a switch makes sense. Your browser has a switch for that already.

  5. d-biehl commented on Oct 7, 2022

    @d-biehl
    Contributor

    I like the idea. And an API would be even better.

    @Snooz82, in Vscode and in most new apps meanwhile you can use the system settings or set your own mode. Then the system would be e.g. Dark and the app Light. In Vscode you can even define default themes for the respective system theme.

    An advanced idea, In Vscode there are predefined CSS constants for the color scheme that is in use and you can use these in your own sheets. Maybe it would be possible to change the CSS stylesheet of the libdoc via API?

  6. pekkaklarck commented on Oct 8, 2022

    @pekkaklarck
    Member

    As I commented above, we cannot really make CSS itself configurable until #4304 has been implemented. We'd most likely break all external styles at that point anyway. Even after that defining what parts of the HTML structure are considered public/stable can be tricky.

    A toggle to switch between the already supported dark and light modes would be fine because such a toggle can be preserved in the rewrite. I don't see a problem with a visible toggle and believe it would bring some benefits. I doubt it's common knowledge where in the browser to enable the dark mode and having a toggle would also make it explicit that a dark mode exists. The hover text could mention that the default is set by the browser and then users could figure out where to change that. I don't know how hard this would be to implement and since this is UI code also styles need to be thought a bit. If someone is interested to implement such a toggle, it would be great to have a PR latest on Monday when I plan to release RF 6.0 rc 2.

    Although a toggle for humans would be nice, I agree with @d-biehl that an API would be nicer as it would allow editor plugins to control the mode. An API would probably be easier to design than a visible toggle as well, so getting this into RF 6.0 ought be pretty easy. Are you @d-biehl interested to provide a PR?

  7. d-biehl commented on Oct 9, 2022

    @d-biehl
    Contributor

    Let me finish the other PR first. And then I'll take a look at it. But I don't think I'll make it until Monday. Tuesday or Wednesday more likely.

  8. pekkaklarck commented on Oct 10, 2022

    @pekkaklarck
    Member

    Would it be enough to add new --theme option to Libdoc with possible values DARK, LIGHT and NONE? DARK and LIGHT would force dark and light modes, respectively, and NONE as well as not using the option at all would mean using the default theme.

    This would be pretty easy to implement, I have a prototype ready, and ought to give external tools full control on the theme. Theme changes ought to be so rare that needing to regenerate HTML outputs, or serving outputs with previous theme, shouldn't be an issue. This wouldn't allow users changing the theme directly, but I don't consider that too important. Those interested in this can set OS or browser theme as nowadays. A toggle could also be added later if needed.

  9. Snooz82 commented on Oct 10, 2022

    @Snooz82
    Member

    I do not get it!

    the page shows the content theme based on the setting of the browser.
    Why forcing anything?

  10. pekkaklarck commented on Oct 10, 2022

    @pekkaklarck
    Member

    The original description has this:

    The HTML documentation generated by libdoc can be opened in an IDE, where many people use a dark theme. The contrast between the Robot Framework code on a dark background and the library documentation with a white background is unpleasant.

    Based on that my understanding is that the browser embedded to an IDE uses browser theme, not IDE theme, and it would be convenient to be override that. See also robotcodedev/robotcode#88 references above.

  11. pekkaklarck commented on Oct 10, 2022

    @pekkaklarck
    Member

    I got --theme option implemented using ideas from this SO answer. Below are changes to HTML and CSS files. Changes to Python files are pretty trivial.

    If the changes look fine, and we agree this is a good idea, I can add some tests for the Python side and commit this. We can also agree to not do this now or decide this isn't a good idea in general and just close this issue. I guess @d-biehl and @fabioz as IDE developers have best opinion is this useful or not.

    diff --git a/src/robot/htmldata/libdoc/libdoc.css b/src/robot/htmldata/libdoc/libdoc.css
    index 1c9aa8028..1eb8e41b2 100644
    --- a/src/robot/htmldata/libdoc/libdoc.css
    +++ b/src/robot/htmldata/libdoc/libdoc.css
    @@ -10,19 +10,17 @@
         --link-color: #0000ee;
     }
     
    -@media (prefers-color-scheme: dark) {
    -    :root {
    -        --background-color: #1c2227;
    -        --text-color: #e2e1d7;
    -        --border-color: #4e4e4e;
    -        --light-background-color: #002b36;
    -        --robot-highlight: yellow;
    -        --highlighted-color: var(--background-color);
    -        --highlighted-background-color: yellow;
    -        --less-important-text-color: #5b6a6f;
    -        --link-color: #52adff;
    -        color-scheme: dark;
    -    }
    +[data-theme="dark"] {
    +    --background-color: #1c2227;
    +    --text-color: #e2e1d7;
    +    --border-color: #4e4e4e;
    +    --light-background-color: #002b36;
    +    --robot-highlight: yellow;
    +    --highlighted-color: var(--background-color);
    +    --highlighted-background-color: yellow;
    +    --less-important-text-color: #5b6a6f;
    +    --link-color: #52adff;
    +    color-scheme: dark;
     }
     
     body {
    diff --git a/src/robot/htmldata/libdoc/libdoc.html b/src/robot/htmldata/libdoc/libdoc.html
    index c805b12c4..9c9bd32bf 100644
    --- a/src/robot/htmldata/libdoc/libdoc.html
    +++ b/src/robot/htmldata/libdoc/libdoc.html
    @@ -55,6 +55,7 @@
             parseTemplates();
             document.title = libdoc.name;
             storage.init('libdoc');
    +        setTheme();
             renderTemplate('base', libdoc, $('body'));
             if (libdoc.inits.length) {
                 libdoc.typedocs.map(function(type) {
    @@ -88,6 +89,20 @@
             createModal();
         });
     
    +    function setTheme(theme) {
    +        document.documentElement.setAttribute('data-theme', theme || getTheme());
    +    }
    +
    +    function getTheme() {
    +        if (libdoc['theme']) {
    +            return libdoc['theme'];
    +        } else if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
    +            return 'dark';
    +        } else {
    +            return 'light;'
    +        }
    +    }
    +
         function parseTemplates() {
             $('script[type="text/x-jquery-tmpl"]').map(function (idx, elem) {
                 $.template(elem.id, elem.text);

    Updated the code above so that setTheme can be called with a theme and it uses getTheme if a theme isn't given. That makes programmatically changing the team possible.

  12. pekkaklarck commented on Oct 10, 2022

    @pekkaklarck
    Member

    I enhanced the Javascript code for setting the theme a bit (see comment above), added tests and wrote documentation. Now the question is, is this a useful addition, does --theme work in the intended IDE usage, and are the HTML/CSS changes fine?

    Do you @Snooz82 have objections adding this? If not, can you check do the above changes look good? Does this implementation work for you @d-biehl?

  13. changed the title [-][ENHANCEMENT] Add a stylesheet parameter to libdoc[/-] [+]Libdoc: Support setting dark or light mode explicitly[/+] on Oct 10, 2022
  14. pekkaklarck commented on Oct 11, 2022

    @pekkaklarck
    Member

    I want to finally get RF 6.0 rc 2 out today and need to make a decision about this issue. Nobody has commented this lately, but I in my opinion the original request was valid and my implementation seems to work fine so I guess this can be included. I'm only a little concerned about the CSS stuff because I don't have that much experience from these newish features, based on what I've read, and based on my testing, the changes look good.

    I'll commit changes after I have eaten so you still have some time to convince me this isn't a good idea or that the implementation has problems. Fixes are obviously possible after this is in as well, but I won't be that interested to revert the whole feature at that point anymore.

  15. d-biehl commented on Oct 11, 2022

    @d-biehl
    Contributor

    Sorry, for the late response, the CSS code looks good to me. What I cannot imagine at the moment, how you implement the --theme, is it a new commandline parameter for libdoc and is it also available in direct call to LibraryDocumentation class?

  16. pekkaklarck commented on Oct 11, 2022

    @pekkaklarck
    Member

    It's a command line option and various APIs also got theme=None argument. For example, this usage works:

    from robot.libdocpkg import LibraryDocumentation
    libdoc = LibraryDocumentation('String')
    libdoc.save('out.html', theme='dark')

    If you have access to the browser JS environment, calling setTheme('dark') works as well. I'll commit changes so you can see and test yourself.

  17. added this to the v6.0 milestone on Oct 11, 2022
  18. pekkaklarck commented on Oct 11, 2022

    @pekkaklarck
    Member

    This ought to be done now but I keep this open for a little time to see if there's some more feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions