From 5052c1994016f67652b9c450b2f444013a4e4461 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Fri, 10 Jul 2020 21:21:30 +0000 Subject: [PATCH] Bug 1637624 - Part 2: Use StaticPrefs for layout.word_select.* r=emilio Differential Revision: https://phabricator.services.mozilla.com/D83090 --- intl/lwbrk/WordBreaker.cpp | 8 ++--- layout/generic/nsIFrame.cpp | 45 ++++++++++++------------ layout/generic/nsTextFrame.cpp | 5 +-- modules/libpref/init/StaticPrefList.yaml | 22 ++++++++++++ modules/libpref/init/all.js | 11 ------ 5 files changed, 48 insertions(+), 43 deletions(-) diff --git a/intl/lwbrk/WordBreaker.cpp b/intl/lwbrk/WordBreaker.cpp index 40958431bfbc..46f634181c66 100644 --- a/intl/lwbrk/WordBreaker.cpp +++ b/intl/lwbrk/WordBreaker.cpp @@ -4,7 +4,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "mozilla/intl/WordBreaker.h" -#include "mozilla/Preferences.h" +#include "mozilla/StaticPrefs_layout.h" #include "nsComplexBreaker.h" #include "nsUnicodeProperties.h" @@ -74,10 +74,6 @@ static bool IsScriptioContinua(char16_t aChar) { /* static */ WordBreakClass WordBreaker::GetClass(char16_t c) { - // The pref is cached on first call; changes will require a browser restart. - static bool sStopAtUnderscore = - Preferences::GetBool("layout.word_select.stop_at_underscore", false); - // begin of the hack if (IS_ALPHABETICAL_SCRIPT(c)) { @@ -86,7 +82,7 @@ WordBreakClass WordBreaker::GetClass(char16_t c) { return kWbClassSpace; } if (ASCII_IS_ALPHA(c) || ASCII_IS_DIGIT(c) || - (c == '_' && !sStopAtUnderscore)) { + (c == '_' && !StaticPrefs::layout_word_select_stop_at_underscore())) { return kWbClassAlphaLetter; } return kWbClassPunct; diff --git a/layout/generic/nsIFrame.cpp b/layout/generic/nsIFrame.cpp index 238f89768a2b..0ec813fd6ce2 100644 --- a/layout/generic/nsIFrame.cpp +++ b/layout/generic/nsIFrame.cpp @@ -8303,6 +8303,27 @@ static bool IsMovingInFrameDirection(nsIFrame* frame, nsDirection aDirection, return aDirection == (isReverseDirection ? eDirPrevious : eDirNext); } +// Determines "are we looking for a boundary between whitespace and +// non-whitespace (in the direction we're moving in)". It is true when moving +// forward and looking for a beginning of a word, or when moving backwards and +// looking for an end of a word. +static bool ShouldWordSelectionEatSpace(const nsPeekOffsetStruct& aPos) { + if (aPos.mWordMovementType != eDefaultBehavior) { + // aPos->mWordMovementType possible values: + // eEndWord: eat the space if we're moving backwards + // eStartWord: eat the space if we're moving forwards + return (aPos.mWordMovementType == eEndWord) == + (aPos.mDirection == eDirPrevious); + } + // Use the hidden preference which is based on operating system + // behavior. This pref only affects whether moving forward by word + // should go to the end of this word or start of the next word. When + // going backwards, the start of the word is always used, on every + // operating system. + return aPos.mDirection == eDirNext && + StaticPrefs::layout_word_select_eat_space_to_next_word(); +} + nsresult nsIFrame::PeekOffset(nsPeekOffsetStruct* aPos) { if (!aPos) return NS_ERROR_NULL_POINTER; nsresult result = NS_ERROR_FAILURE; @@ -8394,27 +8415,7 @@ nsresult nsIFrame::PeekOffset(nsPeekOffsetStruct* aPos) { // Intentionally fall through the eSelectWord case. [[fallthrough]]; case eSelectWord: { - // wordSelectEatSpace means "are we looking for a boundary between - // whitespace and non-whitespace (in the direction we're moving in)". It - // is true when moving forward and looking for a beginning of a word, or - // when moving backwards and looking for an end of a word. - bool wordSelectEatSpace; - if (aPos->mWordMovementType != eDefaultBehavior) { - // aPos->mWordMovementType possible values: - // eEndWord: eat the space if we're moving backwards - // eStartWord: eat the space if we're moving forwards - wordSelectEatSpace = ((aPos->mWordMovementType == eEndWord) == - (aPos->mDirection == eDirPrevious)); - } else { - // Use the hidden preference which is based on operating system - // behavior. This pref only affects whether moving forward by word - // should go to the end of this word or start of the next word. When - // going backwards, the start of the word is always used, on every - // operating system. - wordSelectEatSpace = - aPos->mDirection == eDirNext && - Preferences::GetBool("layout.word_select.eat_space_to_next_word"); - } + bool wordSelectEatSpace = ShouldWordSelectionEatSpace(*aPos); // mSawBeforeType means "we already saw characters of the type // before the boundary we're looking for". Examples: @@ -8724,7 +8725,7 @@ bool nsIFrame::BreakWordBetweenPunctuation(const PeekWordState* aState, // We always stop between whitespace and punctuation return true; } - if (!Preferences::GetBool("layout.word_select.stop_at_punctuation")) { + if (!StaticPrefs::layout_word_select_stop_at_punctuation()) { // When this pref is false, we never stop at a punctuation boundary unless // it's followed by whitespace (in the relevant direction). return aWhitespaceAfter; diff --git a/layout/generic/nsTextFrame.cpp b/layout/generic/nsTextFrame.cpp index 29c87be42a28..232e3d54376e 100644 --- a/layout/generic/nsTextFrame.cpp +++ b/layout/generic/nsTextFrame.cpp @@ -7856,9 +7856,6 @@ bool ClusterIterator::IsWhitespace() const { bool ClusterIterator::IsPunctuation() const { NS_ASSERTION(mCharIndex >= 0, "No cluster selected"); - // The pref is cached on first call; changes will require a browser restart. - static bool sStopAtUnderscore = - Preferences::GetBool("layout.word_select.stop_at_underscore", false); // Return true for all Punctuation categories (Unicode general category P?), // and also for Symbol categories (S?) except for Modifier Symbol, which is // kept together with any adjacent letter/number. (Bug 1066756) @@ -7866,7 +7863,7 @@ bool ClusterIterator::IsPunctuation() const { uint8_t cat = unicode::GetGeneralCategory(ch); switch (cat) { case HB_UNICODE_GENERAL_CATEGORY_CONNECT_PUNCTUATION: /* Pc */ - if (ch == '_' && !sStopAtUnderscore) { + if (ch == '_' && !StaticPrefs::layout_word_select_stop_at_underscore()) { return false; } [[fallthrough]]; diff --git a/modules/libpref/init/StaticPrefList.yaml b/modules/libpref/init/StaticPrefList.yaml index a093c29bf918..7b6e35f11ece 100644 --- a/modules/libpref/init/StaticPrefList.yaml +++ b/modules/libpref/init/StaticPrefList.yaml @@ -6455,6 +6455,28 @@ value: 0 mirror: always +# Controls double click and Alt+Arrow word selection behavior. +- name: layout.word_select.eat_space_to_next_word + type: bool +#ifdef XP_WIN + value: true +#else + value: false +#endif + mirror: always + +- name: layout.word_select.stop_at_punctuation + type: bool + value: true + mirror: always + +# Whether underscore should be treated as a word-breaking character for +# word selection/arrow-key movement purposes. +- name: layout.word_select.stop_at_underscore + type: bool + value: false + mirror: always + #--------------------------------------------------------------------------- # Prefs starting with "mathml." #--------------------------------------------------------------------------- diff --git a/modules/libpref/init/all.js b/modules/libpref/init/all.js index 42610ca1a9e6..8bea40592edf 100644 --- a/modules/libpref/init/all.js +++ b/modules/libpref/init/all.js @@ -2409,14 +2409,6 @@ pref("bidi.numeral", 0); // expose it for bidi-associated system locales. pref("bidi.browser.ui", false); -// used for double-click word selection behavior. Win will override. -pref("layout.word_select.eat_space_to_next_word", false); -pref("layout.word_select.stop_at_punctuation", true); - -// Whether underscore should be treated as a word-breaking character for -// word selection/arrow-key movement purposes. -pref("layout.word_select.stop_at_underscore", false); - // Override DPI. A value of -1 means use the maximum of 96 and the system DPI. // A value of 0 means use the system DPI. A positive value is used as the DPI. // This sets the physical size of a device pixel and thus controls the @@ -2944,9 +2936,6 @@ pref("font.size.monospace.x-math", 13); // force_gdi_classic_for_families. pref("gfx.font_rendering.cleartype_params.force_gdi_classic_max_size", 15); - // override double-click word selection behavior. - pref("layout.word_select.eat_space_to_next_word", true); - // Locate plugins by the directories specified in the Windows registry for PLIDs // Which is currently HKLM\Software\MozillaPlugins\xxxPLIDxxx\Path pref("plugin.scan.plid.all", true);