From ff500a437d4c912d78fa3f9279ed26db39b69369 Mon Sep 17 00:00:00 2001 From: Dustin Falgout Date: Sun, 20 Nov 2016 20:21:11 -0600 Subject: [PATCH] finish implementing utility function for getting the current localized time as well as allowing the format and language to be overridden in the config file. --- data/lightdm-webkit2-greeter.conf | 6 ++++ src/gresource/js/GreeterConfig.js | 52 ++++++++++++++++++++++++++----- src/gresource/js/ThemeUtils.js | 30 +++++++++++++----- 3 files changed, 73 insertions(+), 15 deletions(-) diff --git a/data/lightdm-webkit2-greeter.conf b/data/lightdm-webkit2-greeter.conf index 704decf..bc4072b 100644 --- a/data/lightdm-webkit2-greeter.conf +++ b/data/lightdm-webkit2-greeter.conf @@ -3,13 +3,19 @@ # debug_mode = Greeter theme debug mode. # secure_mode = Don't allow themes to make remote http requests. # screensaver_timeout = Blank the screen after this many seconds of inactivity. +# time_format = A moment.js format string for use by theme's to display the time. +# time_language = Language to use when displaying the time or "auto" to use the system's language. # webkit_theme = Webkit theme to use. # +# NOTE: See moment.js documentation for format string options: http://momentjs.com/docs/#/displaying/format/ +# [greeter] debug_mode = false secure_mode = true screensaver_timeout = 300 +time_format = LT +time_language = auto webkit_theme = antergos # diff --git a/src/gresource/js/GreeterConfig.js b/src/gresource/js/GreeterConfig.js index 0184319..e1e8a0d 100644 --- a/src/gresource/js/GreeterConfig.js +++ b/src/gresource/js/GreeterConfig.js @@ -26,6 +26,10 @@ */ +let _branding = null, + _greeter = null; + + /** * Provides theme authors with a way to retrieve values from the greeter's config * file located at `/etc/lightdm/lightdm-webkit2-greeter.conf`. The greeter will @@ -45,7 +49,15 @@ class GreeterConfig { * * @readonly */ - get branding() {} + get branding() { + let props = ['background_images', 'logo', 'user_image']; + + if ( null === _branding ) { + props.forEach( prop => _branding[prop] = this.get_str( 'branding', prop ) ); + } + + return _branding; + } /** * Holds keys/values from the `greeter` section of the config file. @@ -54,11 +66,26 @@ class GreeterConfig { * {Boolean} greeter.debug_mode * {Boolean} greeter.secure_mode * {Number} greeter.screensaver_timeout + * {String} greeter.time_format + * {String} greeter.time_language * {String} greeter.webkit_theme * * @readonly */ - get greeter() {} + get greeter() { + let bools = ['debug_mode', 'secure_mode'], + strings = ['time_format', 'time_language', 'webkit_theme']; + + if ( null === _greeter ) { + _greeter = {}; + _greeter.screensaver_timeout = this.get_num( 'greeter', 'screensaver_timeout' ); + + bools.forEach( prop => _greeter[prop] = this.get_bool( 'greeter', prop ) ); + strings.forEach( prop => _greeter[prop] = this.get_str( 'greeter', prop ) ); + } + + return _greeter; + } /** * Returns the value of `key` from the `config_section` of the greeter's config file. @@ -70,7 +97,9 @@ class GreeterConfig { * * @returns {Boolean} Config value for `key`. */ - get_bool( config_section, key ) {} + get_bool( config_section, key ) { + return __GreeterConfig.get_bool( config_section, key ); + } /** * Returns the value of `key` from the `config_section` of the greeter's config file. @@ -82,7 +111,9 @@ class GreeterConfig { * * @returns {Number} Config value for `key`. */ - get_num( config_section, key ) {} + get_num( config_section, key ) { + return __GreeterConfig.get_num( config_section, key ); + } /** * Returns the value of `key` from the `config_section` of the greeter's config file. @@ -94,7 +125,9 @@ class GreeterConfig { * * @returns {String} Config value for `key`. */ - get_str( key ) {} + get_str( config_section, key ) { + return __GreeterConfig.get_str( config_section, key ); + } } @@ -102,8 +135,11 @@ class GreeterConfig { * @memberOf window * @type {LightDM.GreeterConfig} */ -window.greeter_config = __GreeterConfig; +window.greeter_config = new GreeterConfig(); + -/* -------->>> DEPRECATED! <<<-------- */ +/** + * @deprecated + * @type {LightDM.GreeterConfig} + */ window.config = window.greeter_config; -/* -------->>> DEPRECATED! <<<-------- */ diff --git a/src/gresource/js/ThemeUtils.js b/src/gresource/js/ThemeUtils.js index ddb8f14..406ddfa 100644 --- a/src/gresource/js/ThemeUtils.js +++ b/src/gresource/js/ThemeUtils.js @@ -30,7 +30,10 @@ if ( 'undefined' === typeof window.navigator.languages ) { } moment.locale( window.navigator.languages ); -let localized_invalid_date = moment('today', '!@#'); + +let localized_invalid_date = moment('today', '!@#'), + time_language = null, + time_format = null; @@ -45,9 +48,9 @@ class ThemeUtils { /** * Binds `this` to class, `context`, for all of the class's methods. * - * @arg {function(new:*): Object} context An ES6 class (not an instance) with at least one method. + * @arg {Object} context An ES6 class instance with at least one method. * - * @return {function(new:*): Object} `context` with `this` bound to it for all of its methods. + * @return {Object} `context` with `this` bound to it for all of its methods. */ bind_this( context ) { let excluded_methods = ['constructor']; @@ -98,8 +101,10 @@ class ThemeUtils { /** - * Get the current time in a localized format based on the `time_format` config file key. - * * When `time_format` has a valid value, time will be formatted + * Get the current time in a localized format. Time format and language are auto-detected + * by default, but can be set manually in the greeter config file. + * * `language` defaults to the system's language, but can be set manually in the config file. + * * When `time_format` config file option has a valid value, time will be formatted * according to that value. * * When `time_format` does not have a valid value, the time format will be `LT` * which is `1:00 PM` or `13:00` depending on the system's locale. @@ -107,8 +112,19 @@ class ThemeUtils { * @return {String} The current localized time. */ get_current_localized_time() { - let config_format = greeter_config.greeter.time_format; - let format = ( '' !== config_format ) ? config_format : 'LT'; + if ( null === time_language ) { + let config = greeter_config.greeter, + manual_language = ( '' !== config.time_language && 'auto' !== config.time_language ), + manual_time_format = ( '' !== config.time_format && 'auto' !== config.time_format ); + + time_language = manual_language ? config.time_language : window.navigator.language; + time_format = manual_time_format ? config.time_format : 'LT'; + + if ( manual_language ) { + moment.locale( time_language ); + } + } + let local_time = moment().format( format ); if ( local_time === localized_invalid_date ) {