Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Update preview pane to reflect locale change

AuthorDaveJarvis <email>
Date2020-12-27 22:06:29 GMT-0800
Commit190dc7d78b200fd2d5a8a960c8adb1791fcdb702
Parent804d981
src/main/java/com/keenwrite/MainPane.java
* Renders the actively selected plain text editor tab.
*/
- private final HtmlPreview mHtmlPreview = new HtmlPreview();
+ private final HtmlPreview mHtmlPreview;
/**
public MainPane( final Workspace workspace ) {
mWorkspace = workspace;
+ mHtmlPreview = new HtmlPreview(
+ workspace.localeProperty( KEY_UI_FONT_LOCALE ) );
open( bin( getRecentFiles() ) );
src/main/java/com/keenwrite/preferences/LocaleProperty.java
}
- public static String toLocale( final String displayName ) {
+ public static String toLanguageTag( final String displayName ) {
return sLocales.getOrDefault( displayName, LOCALE_DEFAULT ).toLanguageTag();
+ }
+
+ public Locale toLocale() {
+ return sLocales.getOrDefault( getValue(), LOCALE_DEFAULT );
}
src/main/java/com/keenwrite/preferences/Workspace.java
private static final Map<Class<?>, Function<String, Object>> MARSHALL =
Map.of(
- LocaleProperty.class, LocaleProperty::toLocale
+ LocaleProperty.class, LocaleProperty::toLanguageTag
);
}
- public ObjectProperty<String> localeProperty( final Key key ) {
+ public LocaleProperty localeProperty( final Key key ) {
return valuesProperty( key );
}
src/main/java/com/keenwrite/preview/HtmlPreview.java
package com.keenwrite.preview;
+import com.keenwrite.preferences.LocaleProperty;
import javafx.embed.swing.SwingNode;
import org.xhtmlrenderer.render.Box;
*/
private static final String HTML_HEAD =
- """
- <!DOCTYPE html>
- <html lang='%s'><head><title> </title><meta charset='utf-8'>
- <link rel='stylesheet' href='%s'>
- <link rel='stylesheet' href='%s'>
- <base href='%s'>
- </head><body>
- """;
+ """
+ <!DOCTYPE html>
+ <html lang='%s'><head><title> </title><meta charset='utf-8'>
+ <link rel='stylesheet' href='%s'>
+ <link rel='stylesheet' href='%s'>
+ <base href='%s'>
+ </head><body>
+ """;
private static final String HTML_TAIL = "</body></html>";
- private static final URL HTML_STYLE_PREVIEW =
- HtmlPreview.class.getResource( STYLESHEET_PREVIEW );
+ private static final URL HTML_STYLE_PREVIEW = toUrl( STYLESHEET_PREVIEW );
/**
* The buffer is reused so that previous memory allocations need not repeat.
*/
private final StringBuilder mHtmlDocument = new StringBuilder( 65536 );
private HtmlPanel mView;
private JScrollPane mScrollPane;
-
private String mBaseUriPath = "";
+ private final LocaleProperty mLocaleProperty;
+ private URL mLocaleUrl;
/**
* Creates a new preview pane that can scroll to the caret position within the
* document.
*/
- public HtmlPreview() {
+ public HtmlPreview( final LocaleProperty localeProperty ) {
+ mLocaleProperty = localeProperty;
+
+ mLocaleUrl = toUrl( localeProperty.toLocale() );
+ localeProperty.addListener( ( c, o, n ) -> {
+ if( n != null ) {
+ mLocaleUrl = toUrl( mLocaleProperty.toLocale() );
+ rerender();
+ }
+ } );
+
// Attempts to prevent a flash of black un-styled content upon load.
setStyle( "-fx-background-color: white;" );
public void render( final String html ) {
mView.render( decorate( html ), getBaseUri() );
+ }
+
+ private void rerender() {
+ render( mHtmlDocument.toString() );
+ }
+
+ private String decorate( final String html ) {
+ mHtmlDocument.setLength( 0 );
+ mHtmlDocument.append( html );
+ return head() + mHtmlDocument + tail();
+ }
+
+ private String head() {
+ return format(
+ HTML_HEAD,
+ getLocale().getLanguage(),
+ HTML_STYLE_PREVIEW,
+ mLocaleUrl,
+ mBaseUriPath
+ );
+ }
+
+ private String tail() {
+ return HTML_TAIL;
}
return new Point( x, y );
- }
-
- private String decorate( final String html ) {
- mHtmlDocument.setLength( 0 );
-
- final var locale = Locale.getDefault();
-
- final var head = format(
- HTML_HEAD,
- locale.getLanguage(),
- HTML_STYLE_PREVIEW,
- getLocaleStylesheet( locale ),
- mBaseUriPath
- );
-
- // Write the HTML body element followed by closing tags.
- return mHtmlDocument.append( head )
- .append( html )
- .append( HTML_TAIL )
- .toString();
}
/**
* Returns the ISO 639 alpha-2 or alpha-3 language code followed by a hyphen
- * followed by the ISO 3166 alpha-2 country code or UN M.49 numeric-3 area
- * code.
- * <p>
- * TODO: Override default locale user's workspace locale preference.
- * </p>
+ * followed by the ISO 15924 alpha-4 script code, followed by an ISO 3166
+ * alpha-2 country code or UN M.49 numeric-3 area code. For example, this
+ * could return "en-Latn-CA" for Canadian English written in the Latin
+ * character set.
*
* @return Unique identifier for language and country.
*/
- private static String getLocaleStylesheet( final Locale locale ) {
- return get(
+ private static URL toUrl( final Locale locale ) {
+ return toUrl(
+ get(
sSettings.getSetting( STYLESHEET_PREVIEW_LOCALE, "" ),
locale.getLanguage(),
+ locale.getScript(),
locale.getCountry()
+ )
);
+ }
+
+ private static URL toUrl( final String path ) {
+ return HtmlPreview.class.getResource( path );
+ }
+
+ private Locale getLocale() {
+ return mLocaleProperty.toLocale();
}
}
src/main/resources/com/keenwrite/settings.properties
# Preview styles are loaded statically through a class's classloader.
file.stylesheet.preview=webview.css
-# {0} - language code, {1} - country code
-file.stylesheet.preview.locale=webview_{0}-{1}.css
+# {0} language code, {1} script code, {2} country code
+file.stylesheet.preview.locale=webview_{0}-{1}-{2}.css
file.logo.16 =${application.package}/logo16.png
src/test/java/com/keenwrite/definition/TreeViewTest.java
import com.keenwrite.editors.definition.yaml.YamlTreeTransformer;
import com.keenwrite.editors.markdown.MarkdownEditor;
+import com.keenwrite.preferences.LocaleProperty;
import com.keenwrite.preview.HtmlPreview;
import com.panemu.tiwulfx.control.dock.DetachableTabPane;
import org.testfx.framework.junit5.Start;
+import static com.keenwrite.Constants.LOCALE_DEFAULT;
import static com.keenwrite.util.FontLoader.initFonts;
final var tabPane3 = new DetachableTabPane();
- tabPane3.addTab( "Preview", new HtmlPreview() );
+ final var localeProperty = new LocaleProperty( LOCALE_DEFAULT );
+ tabPane3.addTab( "Preview", new HtmlPreview( localeProperty ) );
editor.addTreeChangeHandler( mTreeHandler );
Delta80 lines added, 46 lines removed, 34-line increase