Per-user Ace theme settings

Per-user Ace theme settings

by Richard Lobb -
Number of replies: 2

I've been asked by a group of students to give them a 'dark' theme for the Ace editor. It does seem that dark themes are now fashionable for programming, though I'm not a huge fan myself.

I could - and perhaps should - have a CodeRunner admin setting for the Ace theme but that would be site-wide and I think this should ideally be a user preference.

The Ace editor has an obscure barely-documented keystroke Ctrl-, (i.e. CTRL + comma) that brings up a preferences menu with lots of fun options. But apart from being very obscure (I used Ace for over 10 years before accidentally typing that key combination) it's no use in CodeRunner, as the settings revert to default as soon as you click Check or navigate away, etc.

I have implemented a hack in the Ace-wrapper UI class that detects if the user has changed the theme and saves the new theme to browser local storage. When loading Ace I check for this key and switch to that theme if it's present. The theme then persists and affects all subsequent Ace editor loads. But apart from being a very obscure UI, it's also counter-intuitive that only the theme gets persisted and all the other Ace config settings revert to default. 

Ideally there would be a way for a plugin to have per-user settings but it seems from this old forum discussion that such a possibility was raised years ago but never went anywhere.

I really don't want to clutter every Ace window by adding a config-settings menu. 

Does anyone have any better ideas?

-- Richard

In reply to Richard Lobb

Re: Per-user Ace theme settings

by Tim Hunt -
Moodle plugins can have per-user settings. See set_user_preference in lib/moodlelib.php. The convention is to put the plugin name (qtype_coderunner) at the start of the setting name). See user_preference_allow_ajax_update for if you want to update use preferences from JavaScript code.

If you use user preferences, then technically you should add a bit in classes/privacy/provider.php https://docs.moodle.org/dev/Privacy_API#Indicating_that_you_store_site-wide_user_preferences.

I don't think Moodle has any support for dark mode in the web application yet. Most modern browsers support prefers-color-scheme CSS selector (https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme / https://caniuse.com/prefers-color-scheme) but I am not sure if that can be used to help with this situation.
In reply to Tim Hunt

Re: Per-user Ace theme settings

by Richard Lobb -
Many thanks Tim. That looks hugely helpful. Now I just have to figure out how to use it all.