Bug 1437393 - Font lists in preferences are no longer grouped by font type, port asynchronous handling like Bug 1399206. r=frg

This commit is contained in:
Ian Neal 2024-05-04 19:34:13 +01:00
Родитель 422e88bffc
Коммит 1aec3aaff3
2 изменённых файлов: 180 добавлений и 206 удалений

Просмотреть файл

@ -3,218 +3,191 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var gAllFonts = null;
var gFontEnumerator = null;
var gDisabled = false;
function GetFontEnumerator()
{
if (!gFontEnumerator)
{
gFontEnumerator = Cc["@mozilla.org/gfx/fontenumerator;1"]
.createInstance(Ci.nsIFontEnumerator);
}
return gFontEnumerator;
}
function BuildFontList(aLanguage, aFontType, aMenuList, aPreference)
{
var defaultFont = null;
// Load Font Lists
var fonts = GetFontEnumerator().EnumerateFonts(aLanguage, aFontType);
if (fonts.length)
{
defaultFont = GetFontEnumerator().getDefaultFont(aLanguage, aFontType);
}
else
{
fonts = GetFontEnumerator().EnumerateFonts(aLanguage, "");
if (fonts.length)
defaultFont = GetFontEnumerator().getDefaultFont(aLanguage, "");
}
if (!gAllFonts)
gAllFonts = GetFontEnumerator().EnumerateAllFonts();
// Reset the list
while (aMenuList.hasChildNodes())
aMenuList.lastChild.remove();
// Build the UI for the Default Font and Fonts for this CSS type.
var popup = document.createElement("menupopup");
var separator;
if (fonts.length > 0)
{
const prefutilitiesBundle = document.getElementById("bundle_prefutilities");
let label = defaultFont ?
prefutilitiesBundle.getFormattedString("labelDefaultFont2", [defaultFont]) :
prefutilitiesBundle.getString("labelDefaultFontUnnamed");
let menuitem = document.createElement("menuitem");
menuitem.setAttribute("label", label);
menuitem.setAttribute("value", ""); // Default Font has a blank value
popup.appendChild(menuitem);
separator = document.createElement("menuseparator");
popup.appendChild(separator);
for (let font of fonts)
{
menuitem = document.createElement("menuitem");
menuitem.setAttribute("value", font);
menuitem.setAttribute("label", font);
popup.appendChild(menuitem);
var gFontsDialog = {
_disabled: false,
_enumerator: null,
get enumerator() {
if (!this._enumerator) {
this._enumerator = Cc["@mozilla.org/gfx/fontenumerator;1"]
.createInstance(Ci.nsIFontEnumerator);
}
}
return this._enumerator;
},
// Build the UI for the remaining fonts.
if (gAllFonts.length > fonts.length)
{
// Both lists are sorted, and the Fonts-By-Type list is a subset of the
// All-Fonts list, so walk both lists side-by-side, skipping values we've
// already created menu items for.
_allFonts: null,
async buildFontList(aLanguage, aFontType, aMenuList) {
// Reset the list
while (aMenuList.hasChildNodes())
aMenuList.firstChild.remove();
let defaultFont = null;
// Load Font Lists
let fonts = await this.enumerator.EnumerateFontsAsync(aLanguage, aFontType);
if (fonts.length > 0) {
defaultFont = this.enumerator.getDefaultFont(aLanguage, aFontType);
} else {
fonts = await this.enumerator.EnumerateFontsAsync(aLanguage, "");
if (fonts.length > 0)
defaultFont = this.enumerator.getDefaultFont(aLanguage, "");
}
if (!this._allFonts)
this._allFonts = await this.enumerator.EnumerateAllFontsAsync({});
// Build the UI for the Default Font and Fonts for this CSS type.
const popup = document.createElement("menupopup");
let separator;
let menuitem;
if (fonts.length > 0) {
const bundlePrefs = document.getElementById("bundle_prefutilities");
let defaultLabel = defaultFont ?
bundlePrefs.getFormattedString("labelDefaultFont2", [defaultFont]) :
bundlePrefs.getString("labelDefaultFontUnnamed");
menuitem = document.createElement("menuitem");
menuitem.setAttribute("label", defaultLabel);
menuitem.setAttribute("value", ""); // Default Font has a blank value
popup.appendChild(menuitem);
if (fonts.length)
{
separator = document.createElement("menuseparator");
popup.appendChild(separator);
}
for (i = 0; i < gAllFonts.length; ++i)
{
if (fonts.lastIndexOf(gAllFonts[i], 0) == 0)
{
fonts.shift(); //Remove matched font from array
}
else
{
for (let font of fonts) {
menuitem = document.createElement("menuitem");
menuitem.setAttribute("value", gAllFonts[i]);
menuitem.setAttribute("label", gAllFonts[i]);
menuitem.setAttribute("value", font);
menuitem.setAttribute("label", font);
popup.appendChild(menuitem);
}
}
}
aMenuList.appendChild(popup);
// Fully populated so re-enable menulist before setting preference,
// unless panel is locked.
if (!gDisabled)
aMenuList.disabled = false;
aMenuList.setAttribute("preference", aPreference.id);
aPreference.setElementValue(aMenuList);
}
// Build the UI for the remaining fonts.
if (this._allFonts.length > fonts.length) {
// Both lists are sorted, and the Fonts-By-Type list is a subset of the
// All-Fonts list, so walk both lists side-by-side, skipping values we've
// already created menu items for.
function ReadFontLanguageGroup()
{
var prefs = [{format: "default", type: "string", element: "defaultFontType", fonttype: "" },
{format: "name.", type: "unichar", element: "serif", fonttype: "serif" },
{format: "name.", type: "unichar", element: "sans-serif", fonttype: "sans-serif"},
{format: "name.", type: "unichar", element: "monospace", fonttype: "monospace" },
{format: "name.", type: "unichar", element: "cursive", fonttype: "cursive" },
{format: "name.", type: "unichar", element: "fantasy", fonttype: "fantasy" },
{format: "name-list.", type: "unichar", element: null, fonttype: "serif" },
{format: "name-list.", type: "unichar", element: null, fonttype: "sans-serif"},
{format: "name-list.", type: "unichar", element: null, fonttype: "monospace" },
{format: "name-list.", type: "unichar", element: null, fonttype: "cursive" },
{format: "name-list.", type: "unichar", element: null, fonttype: "fantasy" },
{format: "size.variable", type: "int", element: "sizeVar", fonttype: "" },
{format: "size.fixed", type: "int", element: "sizeMono", fonttype: "" },
{format: "minimum-size", type: "int", element: "minSize", fonttype: "" }];
gDisabled = document.getElementById("browser.display.languageList").locked;
var fontLanguage = document.getElementById("font.language.group");
if (gDisabled)
fontLanguage.disabled = true;
var languageGroup = fontLanguage.value;
var preferences = document.getElementById("fonts_preferences");
for (var i = 0; i < prefs.length; ++i)
{
var name = "font."+ prefs[i].format + prefs[i].fonttype + "." + languageGroup;
var preference = document.getElementById(name);
if (!preference)
{
preference = document.createElement("preference");
preference.id = name;
preference.setAttribute("name", name);
preference.setAttribute("type", prefs[i].type);
preferences.appendChild(preference);
}
if (!prefs[i].element)
continue;
var element = document.getElementById(prefs[i].element);
if (element)
{
if (prefs[i].fonttype)
{
// Set an empty label so it does not jump when items are added.
element.setAttribute("label", "");
// Disable menulist for the moment.
element.disabled = true;
// Lazily populate font lists, each gets re-enabled at the end.
window.setTimeout(BuildFontList, 0, languageGroup,
prefs[i].fonttype, element, preference);
if (fonts.length > 0) {
separator = document.createElement("menuseparator");
popup.appendChild(separator);
}
else
{
// Unless the panel is locked, make sure these elements are not
// disabled just in case they were in the last language group.
element.disabled = gDisabled;
element.setAttribute("preference", preference.id);
preference.setElementValue(element);
for (let font of this._allFonts) {
if (fonts.lastIndexOf(font, 0) == 0) {
fonts.shift(); //Remove matched font from array
} else {
menuitem = document.createElement("menuitem");
menuitem.setAttribute("value", font);
menuitem.setAttribute("label", font);
popup.appendChild(menuitem);
}
}
}
}
}
aMenuList.appendChild(popup);
},
function ReadFontSelection(aElement)
{
// Determine the appropriate value to select, for the following cases:
// - there is no setting
// - the font selected by the user is no longer present (e.g. deleted from
// fonts folder)
var preference = document.getElementById(aElement.getAttribute("preference"));
if (preference.value)
{
var fontItems = aElement.getElementsByAttribute("value", preference.value);
readFontSelection(aElement) {
// Determine the appropriate value to select, for the following cases:
// - there is no setting
// - the font selected by the user is no longer present (e.g. deleted from
// fonts folder)
const preference = document.getElementById(aElement.getAttribute("preference"));
if (preference.value) {
let fontItems = aElement.getElementsByAttribute("value", preference.value);
// There is a setting that actually is in the list. Respect it.
if (fontItems.length)
return undefined;
}
// There is a setting that actually is in the list. Respect it.
if (fontItems.length)
return undefined;
}
var defaultValue = aElement.firstChild.firstChild.getAttribute("value");
var languagePref = document.getElementById("font.language.group");
preference = document.getElementById("font.name-list." + aElement.id + "." + languagePref.value);
if (!preference || !preference.hasUserValue)
return defaultValue;
// Otherwise, use "default" font of current system which is computed
// with "font.name-list.*". If "font.name.*" is empty string, it means
// "default". So, return empty string in this case.
return "";
},
var fontNames = preference.value.split(",");
_selectLanguageGroupPromise: Promise.resolve(),
for (var i = 0; i < fontNames.length; ++i)
{
fontItems = aElement.getElementsByAttribute("value", fontNames[i].trim());
if (fontItems.length)
return fontItems[0].getAttribute("value");
}
return defaultValue;
}
function ReadFontPref(aElement, aDefaultValue)
{
// Check to see if preference value exists,
// if not return given default value.
var preference = document.getElementById(aElement.getAttribute("preference"));
return preference.value || aDefaultValue;
}
_selectLanguageGroup(aLanguageGroup) {
this._selectLanguageGroupPromise = (async () => {
// Avoid overlapping language group selections by awaiting the resolution
// of the previous one. We do this because this function is re-entrant,
// as inserting <preference> elements into the DOM sometimes triggers a
// call back into this function. And since this function is also
// asynchronous, that call can enter this function before the previous
// run has completed, which would corrupt the font menulists. Awaiting
// the previous call's resolution avoids that fate.
await this._selectLanguageGroupPromise;
function ReadUseDocumentFonts()
{
var preference = document.getElementById("browser.display.use_document_fonts");
return preference.value == 1;
}
var prefs = [{format: "default", type: "string", element: "defaultFontType", fonttype: "" },
{format: "name.", type: "fontname", element: "serif", fonttype: "serif" },
{format: "name.", type: "fontname", element: "sans-serif", fonttype: "sans-serif"},
{format: "name.", type: "fontname", element: "monospace", fonttype: "monospace" },
{format: "name.", type: "fontname", element: "cursive", fonttype: "cursive" },
{format: "name.", type: "fontname", element: "fantasy", fonttype: "fantasy" },
{format: "name-list.", type: "unichar", element: null, fonttype: "serif" },
{format: "name-list.", type: "unichar", element: null, fonttype: "sans-serif"},
{format: "name-list.", type: "unichar", element: null, fonttype: "monospace" },
{format: "name-list.", type: "unichar", element: null, fonttype: "cursive" },
{format: "name-list.", type: "unichar", element: null, fonttype: "fantasy" },
{format: "size.variable", type: "int", element: "sizeVar", fonttype: "" },
{format: "size.fixed", type: "int", element: "sizeMono", fonttype: "" },
{format: "minimum-size", type: "int", element: "minSize", fonttype: "" }];
var preferences = document.getElementById("fonts_preferences");
for (var i = 0; i < prefs.length; ++i) {
var name = "font."+ prefs[i].format + prefs[i].fonttype + "." + aLanguageGroup;
var preference = document.getElementById(name);
if (!preference) {
preference = document.createElement("preference");
preference.id = name;
preference.setAttribute("name", name);
preference.setAttribute("type", prefs[i].type);
preferences.appendChild(preference);
}
function WriteUseDocumentFonts(aUseDocumentFonts)
{
return aUseDocumentFonts.checked ? 1 : 0;
}
if (!prefs[i].element)
continue;
var element = document.getElementById(prefs[i].element);
if (element) {
element.setAttribute("preference", preference.id);
if (prefs[i].fonttype) {
// Set an empty label so it does not jump when items are added.
element.setAttribute("label", "");
await this.buildFontList(aLanguageGroup, prefs[i].fonttype, element);
}
// Unless the panel is locked, make sure these elements are not
// disabled just in case they were in the last language group.
element.disabled = this._disabled;
preference.setElementValue(element);
}
}
})().catch(Cu.reportError);
},
readFontLanguageGroup() {
this._disabled = document.getElementById("browser.display.languageList").locked;
var languagePref = document.getElementById("font.language.group");
if (this._disabled)
languagePref.disabled = true;
this._selectLanguageGroup(languagePref.value);
return undefined;
},
readFontPref(aElement, aDefaultValue) {
// Check to see if preference value exists,
// if not return given default value.
var preference = document.getElementById(aElement.getAttribute("preference"));
return preference.value || aDefaultValue;
},
readUseDocumentFonts() {
var preference = document.getElementById("browser.display.use_document_fonts");
return preference.value == 1;
},
writeUseDocumentFonts(aUseDocumentFonts) {
return aUseDocumentFonts.checked ? 1 : 0;
},
};

Просмотреть файл

@ -6,8 +6,7 @@
<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<prefpane id="fonts_pane"
label="&pref.fonts.title;"
script="chrome://communicator/content/pref/pref-fonts.js">
label="&pref.fonts.title;">
<preferences id="fonts_preferences">
<preference id="font.language.group"
name="font.language.group"
@ -20,13 +19,15 @@
type="wstring"/>
</preferences>
<script src="chrome://communicator/content/pref/pref-fonts.js"/>
<groupbox>
<caption align="center">
<label value="&language.label;"
accesskey="&language.accesskey;"
control="selectLangs"/>
<menulist id="selectLangs" preference="font.language.group"
onsyncfrompreference="document.getElementById('fonts_pane').ReadFontLanguageGroup();">
onsyncfrompreference="return gFontsDialog.readFontLanguageGroup();">
<menupopup>
<menuitem value="ar" label="&font.langGroup.arabic;"/>
<menuitem value="x-armn" label="&font.langGroup.armenian;"/>
@ -86,7 +87,7 @@
control="defaultFontType"/>
</hbox>
<menulist id="defaultFontType" flex="1" style="width: 0px;"
onsyncfrompreference="return document.getElementById('fonts_pane').ReadFontSelection(this);">
onsyncfrompreference="return gFontsDialog.readFontSelection(this);">
<menupopup>
<menuitem value="serif"
label="&useDefaultFontSerif.label;"/>
@ -95,7 +96,7 @@
</menupopup>
</menulist>
<menulist id="sizeVar" class="small-margin"
onsyncfrompreference="return document.getElementById('fonts_pane').ReadFontPref(this, 16);">
onsyncfrompreference="return gFontsDialog.readFontPref(this, 16);">
<menupopup>
<menuitem value="8" label="8"/>
<menuitem value="9" label="9"/>
@ -136,7 +137,7 @@
control="serif"/>
</hbox>
<menulist id="serif" class="prefpanel-font-list"
onsyncfrompreference="return document.getElementById('fonts_pane').ReadFontSelection(this);"/>
onsyncfrompreference="return gFontsDialog.readFontSelection(this);"/>
<spacer/>
</row>
<row align="center">
@ -146,7 +147,7 @@
control="sans-serif"/>
</hbox>
<menulist id="sans-serif" class="prefpanel-font-list"
onsyncfrompreference="return document.getElementById('fonts_pane').ReadFontSelection(this);"/>
onsyncfrompreference="return gFontsDialog.readFontSelection(this);"/>
<spacer/>
</row>
<row align="center">
@ -156,7 +157,7 @@
control="cursive"/>
</hbox>
<menulist id="cursive" class="prefpanel-font-list"
onsyncfrompreference="return document.getElementById('fonts_pane').ReadFontSelection(this);"/>
onsyncfrompreference="return gFontsDialog.readFontSelection(this);"/>
<spacer/>
</row>
<row align="center">
@ -166,7 +167,7 @@
control="fantasy"/>
</hbox>
<menulist id="fantasy" class="prefpanel-font-list"
onsyncfrompreference="return document.getElementById('fonts_pane').ReadFontSelection(this);"/>
onsyncfrompreference="return gFontsDialog.readFontSelection(this);"/>
<spacer/>
</row>
<row>
@ -179,9 +180,9 @@
control="monospace"/>
</hbox>
<menulist id="monospace" class="prefpanel-font-list"
onsyncfrompreference="return document.getElementById('fonts_pane').ReadFontSelection(this);"/>
onsyncfrompreference="return gFontsDialog.readFontSelection(this);"/>
<menulist id="sizeMono"
onsyncfrompreference="return document.getElementById('fonts_pane').ReadFontPref(this, 12);">
onsyncfrompreference="return gFontsDialog.readFontPref(this, 12);">
<menupopup>
<menuitem value="8" label="8"/>
<menuitem value="9" label="9"/>
@ -223,7 +224,7 @@
control="minSize"/>
</hbox>
<menulist id="minSize"
onsyncfrompreference="return document.getElementById('fonts_pane').ReadFontPref(this, 0);">
onsyncfrompreference="return gFontsDialog.readFontPref(this, 0);">
<menupopup>
<menuitem value="0" label="&minSize.none;"/>
<menuitem value="9" label="9"/>
@ -253,8 +254,8 @@
label="&useDocumentFonts.label;"
accesskey="&useDocumentFonts.accesskey;"
preference="browser.display.use_document_fonts"
onsyncfrompreference="return document.getElementById('fonts_pane').ReadUseDocumentFonts();"
onsynctopreference="return document.getElementById('fonts_pane').WriteUseDocumentFonts(this);"/>
onsyncfrompreference="return gFontsDialog.readUseDocumentFonts();"
onsynctopreference="return gFontsDialog.writeUseDocumentFonts(this);"/>
</prefpane>
</overlay>