How to solve the problem of RTL

How to solve the problem of RTL

by Gareth J Barnard -
Number of replies: 10
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Whilst this is still on my mind....

When I was in the process of fixing the RTL issues for Essential: https://moodle.org/mod/forum/discuss.php?d=264952#p1148492 - I realised that with theme designer mode off (TDM off) that the detection code in the theme's config.php was not switching to use different files.  This mechanism worked for swatch switching in Mutant Banjo (MB), but why not here?  So I realised that MB performed a theme cache purge.  However for RTL this is not practical as it affects everybody.

The reason it is an issue is because all of the CSS is compacted into one file called 'all'.  This is potentially great for HTTP requests and caching everything you need locally once, but perhaps not so great if you are a mobile user and bandwidth / local storage is an issue.  And in any event why should the file be so big when it contains css that you will never use?  Just like the instructions manuals you get with fourteen languages warning you that your iron will be hot when you turn it on.

So....

What about compartmentalising the CSS files into separate ones, perhaps all of the generic CSS and then specific language targeted CSS depending on the language being used?  To have two requests instead of one.  A small price to pay.

I did think that this could be done by a theme already through serving up the CSS files itself and bypassing the core caching process - even better with production minimised versions of the files as well as developer friendly copies.  But what about custom CSS?  That needs to be appended at the end and is placed in the cached 'all' file, so any theme served file would be after it.  Or so I thought as I came to the conclusion that core really needs theme configuration that has $THEME->ltrsheets and $THEME->rtlsheets instead of the combined $THEME-sheets.  And that mechanism serve up the correct cached 'all' file depending on the language.

However, what if the theme served its CSS before the 'all' sheet with the custom CSS from the settings?  Could this be a way? i.e before '$OUTPUT->standard_head_html()' in the layout files.  The downside of this is the need to separate any '$THEME->csspostprocess' type things with no language specific CSS as you don't want to create your own cache mechanism.  You just want to serve up pre-compiled files in the styles folder.

Thoughts?  Contributed theme prototype or should core really optimise this mechanism?

Cheers,

Gareth

Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: How to solve the problem of RTL

by David Scotson -
My thoughts are that the best approach for RTL is:

1. Moodle serving a single rtl.css or ltr.css file to the user, depending on the language used on the current page.

2. Moodle core and theme developers using rtl-flipping technology so that they only have to maintain a single theme file.

In the meantime, until that is all done, a potential workaround that lets us get most of the work done in parallel while waiting is to provide a setting in a theme that allows the admin to select ltr, rtl or both.

The ltr file is the main one that people work on. The rtl file is an automatically flipped version of that, possibly with some hand-crafted rtl-only tweaks appended.

The combined file is trickier, but my current approach would be to fuse the previous two files together and add [dir="ltr"] to the start of every line intended for ltr, and [dir="rtl"] to the start of every line intended for rtl. This basically means that the page would ignore half of the CSS based on which language direction it was using.

I believe the above should work, and I have a basic prototype, but I haven't had much time recently to test it out.

The immediate reaction to sending double Moodle's already quite large CSS to those sites that use both language directions is probably horror. But if you do some maths, with fairly rough numbers like 5% of Moodle users needing both RTL and LTR within a single theme and 10% of Moodle's current CSS being RTL specific you can see that:

current way = everyone gets a slightly larger CSS file = 1 x 1.1 = 110%
half way = most people get a slightly smaller CSS, some people get double = (.95 x 1) + (.05 x 2) = 105%
best way = everyone gets only the CSS they need = 1 x 1 = 100%

So overall the amount of CSS sent by Moodle could actually fall overall (depending on what the real numbers are) even though certain sites would be sending double. Of course, as mentioned above the real solution is to send only rtl for rtl pages and ltr for ltr pages. I'd guess this would be better even for sites that use both.

Generally, like most of the CSS it could just generally do with a tidy up e.g. part of the RTL CSS that is sent to every user is for the TinyMCE editor. And it's sent even if you're not using RTL, and even if you are using RTL but not using the TinyMCE editor. A few hundred extra lines of CSS here and there soon adds up.
Average of ratings: Useful (2)
In reply to David Scotson

Re: How to solve the problem of RTL

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Thank you David for a lengthy and informative reply.

Yes, I agree that the dir-ltr / dir-rtl will work especially if implemented as a body.dir-ltr / body.dir-rtl prefix.

Given the numbers I would still like to see a two file flip flop solution by default with a setting for combined if desired.

With the need to sometimes have specific direction only selectors I've started to use the 'noflip' preprocessing directive -> https://www.npmjs.org/package/css-flip - in Essential and Shoehorn.  Thus helps with maintainability and the need to group similar concepts in the same place.

And indeed LTR / RTL CSS is just one of many areas that needs to be addressed to reduce the current bloat of CSS and multiple declarations for the same thing.  I've not had the desire to return to MDL-43112 as its easy to implement and impossible to test and ensure that no pixel was harmed in its implementation.  There needs to be a leap of faith with that one and simply refactor it with icon being 16px by 16px and smallicon 12px by 12px, then tidy up as required if pragmatically needed.  After all a 1px move to the left or right is not the end of the world.

In reply to Gareth J Barnard

Re: How to solve the problem of RTL

by David Scotson -
I tried using body.dir-rtl but one complication is that Moodle puts a lot of classes on the body tag so you need to treat those differently, and can't just prepend the same string as other lines, which is a bit of a pain.

If you use the dir=RTL marker that Moodle puts on the HTML tag instead then you only have to worry about CSS targeting the HTML tag.

Doing the former took many lines of shell script, with the new system you take the ltr and rtl CSS and make a .less file by wrapping them like so:

[dir=ltr] {
All LTR CSS
}
[dir=rtl] {
All RTL CSS
}

And then compile it. Initial testing seemed positive.
Average of ratings: Useful (1)
In reply to David Scotson

Re: How to solve the problem of RTL

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

I think I have a working solution that breaks apart the css-flip targeted CSS into two files and serves them separately from the core cached css depending on language direction.  Any CSS that needs pre-processing is placed into the core cached css file.  This all appears to work with TDM off.  The only disadvantage is an extra HTTP request.  However, once retrieved, 304's are given.

If you would like to test on a test server (use at your own risk and definitely not for production), then please see: https://github.com/gjb2048/theme_bootstrap/tree/master_rtl

The changes are: https://github.com/gjb2048/theme_bootstrap/compare/master_rtl

I've not had much of a chance to compare the output, so this branch may change without notice.

In reply to Gareth J Barnard

Re: How to solve the problem of RTL

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

OK...following my rant on the problems of the Essential Theme.

The real problem with RTL is...

  1. it needs lots of browser support with Bi-Directional options which are slow in being developed
  2. it needs lots of theme support with CSS specific selectors like .dir-rtl placed in the correct order relating to the body class in Moodle (not everyone knows this).
  3. care needs to be taken when using CSS properties like paddings and margins, background-position, floats etc.
  4. also care needs to be taken when using directional properties like direction: rtl as these can cause problems with punctuation forcing commas and full-stops, brackets etc,, to the wrong part of the sentence

These are only a few of the many problems with RTL in general, but would perhaps be an a great idea if something good could be done with Moodle for the RTL communities around the world. It has been a long time coming but we still need the support for all this in Browsers which is well supported in IE strangely enough. Did you know for instance that you can actually change the direction of the page in IE.  I think you can also do this in Mozilla too.

Splitting CSS up is one why or making a theme RTL specific is another.

Cleaning up Boostrap CSS might be better as there are lots of things in bootstrap.css that is never used.

If it were me I would advocate that Moodle design their own version of Bootstrap and call is MoodleStrap. In other words something that matches what Moodle is all about, that of a Modular Object-Oriented Dynamic Learning Environment

In reply to Mary Evans

Re: How to solve the problem of RTL

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

There are lots of problems to solve and this is just one of them, so small achievable steps.  Ok, the current issue is that of separation and the inclusion of both dir-ltr and dir-rtl when in reality for the most part only one is needed at any given instance of a page load.  Therefore, this solution for the most part solves it, hence fixes 2.

The solution here builds upon the work already being done in the use of 'grunt-css-flip' (https://www.npmjs.org/package/grunt-css-flip) which is based upon 'css-flip' (https://github.com/twitter/css-flip) which takes care of points 3 and 4.

With regards cleaning up Bootstrap CSS, then the un-minimised version of bootstrap.css is 130kb.  When combined with Moodle CSS that jumps to approx 456kb, a difference of 326kb in the direction of what in essence is base css.  Therefore logically, the priority must be the latter rather than the former.

In reply to Gareth J Barnard

Re: How to solve the problem of RTL

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

I would suspect that the grunt-CSS-flip is OK some times, but not necessarily every time as some elements may not require it. The actual requirements in RTL is not to make a mirror image of everything, but to make the page conform to the precepts and directional standards required by those languages.

Also, since there is a big RTL community who work closely together fixing Moodle, it may be better to set up a Moodle Tracker to start the ball rolling, this way those in the know, like Nadav Kavalerchik could help too.

Cheers

Mary

In reply to Mary Evans

Re: How to solve the problem of RTL

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

For elements that do not need it there is a /*noflip*/ pre-processing directive.

I want to undertake a proof of concept in Bootstrap first as its easier and faster in a contributed theme.

Nadav Kavalerchik has already been helping with the BS3 version.

In reply to Gareth J Barnard

Re: How to solve the problem of RTL

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

I know Nadav has been helping Bas, but I was assuming this was a Moodle initiative rather than a theme thing, as we have to conform to what Moodle dictate don't we? Or are you planning on making this a theme thing only?

In reply to Mary Evans

Re: How to solve the problem of RTL

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Not a contributed theme thing only.  However there have been lots of examples in the past where concepts have been proven in contributed code before being adopted in core.  This is just following that precedence and could be said 'Moodle dictate' if such a mandate exists.

I have also updated the branch and eliminated some styles and logic.  It seems to be fine so far.