[LyX/2.4.1-devel] Smarter menu length calculation

Juergen Spitzmueller spitz at lyx.org
Wed Apr 3 07:15:55 UTC 2024


commit c92d14e8c978372d24fccbc0a0fe8284f4f8601c
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Tue Apr 2 14:41:54 2024 +0200

    Smarter menu length calculation
    
    It is possible I have missed some shortcut conflicts, so please report
    if you find any.
    
    (cherry picked from commit f3a4602c4c1eca9bc79e7ba0b58395b79eafe9db)
---
 lib/ui/stdcontext.inc      |  6 +++---
 lib/ui/stdmenus.inc        |  4 ++--
 src/frontends/qt/Menus.cpp | 43 +++++++++++++++++++++++++++++++++----------
 3 files changed, 38 insertions(+), 15 deletions(-)

diff --git a/lib/ui/stdcontext.inc b/lib/ui/stdcontext.inc
index 6d4fc75703..90a4cda1aa 100644
--- a/lib/ui/stdcontext.inc
+++ b/lib/ui/stdcontext.inc
@@ -234,10 +234,10 @@ Menuset
 	Menu "context-note"
 		Item "LyX Note|N" "inset-modify note Note Note"
 		Item "Comment|m" "inset-modify note Note Comment"
-		Item "Greyed Out|G" "inset-modify note Note Greyedout"
+		Item "Greyed Out|y" "inset-modify note Note Greyedout"
 		Separator
 		Item "Open All Notes|A" "inset-forall Note inset-toggle open"
-		Item "Close All Notes|l" "inset-forall Note inset-toggle close"
+		Item "Close All Notes|o" "inset-forall Note inset-toggle close"
 	End
 
 #
@@ -383,7 +383,7 @@ Menuset
 		Item "Jump Back to Saved Bookmark|B" "bookmark-goto 0"
 		OptItem "Forward Search|F" "forward-search"
 		Separator
-		Item "Move Paragraph Up|o" "paragraph-move-up"
+		Item "Move Paragraph Up|h" "paragraph-move-up"
 		Item "Move Paragraph Down|v" "paragraph-move-down"
 		Separator
 		EnvironmentSeparatorsContext
diff --git a/lib/ui/stdmenus.inc b/lib/ui/stdmenus.inc
index 7d7750cc03..14c927dc06 100644
--- a/lib/ui/stdmenus.inc
+++ b/lib/ui/stdmenus.inc
@@ -117,7 +117,7 @@ Menuset
 		Item "Find & Replace (Quick)...|F" "dialog-show findreplace"
 		Item "Find & Replace (Advanced)..." "dialog-show findreplaceadv"
 		Separator
-		Item "Move Paragraph Up|o" "paragraph-move-up"
+		Item "Move Paragraph Up|h" "paragraph-move-up"
 		Item "Move Paragraph Down|v" "paragraph-move-down"
 		Separator
 		Item "Paragraph Settings...|P" "layout-paragraph"
@@ -545,7 +545,7 @@ Menuset
 	Menu "insert_note"
 		Item "LyX Note|N" "note-insert Note"
 		Item "Comment|C" "note-insert Comment"
-		Item "Greyed Out|G" "note-insert Greyedout"
+		Item "Greyed Out|y" "note-insert Greyedout"
 	End
 
 	Menu "insert_branches"
diff --git a/src/frontends/qt/Menus.cpp b/src/frontends/qt/Menus.cpp
index a3fc5a7ce1..6c4b08cf48 100644
--- a/src/frontends/qt/Menus.cpp
+++ b/src/frontends/qt/Menus.cpp
@@ -342,6 +342,8 @@ public:
 		const;
 	///
 	bool hasFunc(FuncRequest const &) const;
+	/// The real size of the menu considering hidden entries
+	int realSize() const;
 	/// Add the menu item unconditionally
 	void add(MenuItem const & item) { items_.push_back(item); }
 	/// Checks the associated FuncRequest status before adding the
@@ -727,6 +729,23 @@ bool MenuDefinition::hasFunc(FuncRequest const & func) const
 }
 
 
+int MenuDefinition::realSize() const
+{
+	int res = 0;
+	for (auto const & it : *this) {
+		if (it.kind() == MenuItem::Submenu)
+			++res;
+		else if (it.kind() == MenuItem::Command) {
+			FuncStatus status = lyx::getStatus(*it.func());
+			// count only items that are actually displayed
+			if (!status.unknown() && (status.enabled() || !it.optional()))
+				++res;
+		}
+	}
+	return res;
+}
+
+
 void MenuDefinition::catSub(docstring const & name)
 {
 	add(MenuItem(MenuItem::Submenu,
@@ -867,13 +886,13 @@ void MenuDefinition::expandSpellingSuggestions(BufferView const * bv)
 				if (i > 0)
 					add(MenuItem(MenuItem::Separator));
 				docstring const arg = wl.word() + " " + from_ascii(wl.lang()->lang());
-				add(MenuItem(MenuItem::Command, qt_("Add to personal dictionary|n"),
+				add(MenuItem(MenuItem::Command, qt_("Add to personal dictionary|r"),
 						FuncRequest(LFUN_SPELLING_ADD, arg)));
 				add(MenuItem(MenuItem::Command, qt_("Ignore this occurrence|g"),
 						FuncRequest(LFUN_FONT_NO_SPELLCHECK, arg)));
-				add(MenuItem(MenuItem::Command, qt_("Ignore all for this session|I"),
+				add(MenuItem(MenuItem::Command, qt_("Ignore all for this session|l"),
 						FuncRequest(LFUN_SPELLING_IGNORE, arg)));
-				add(MenuItem(MenuItem::Command, qt_("Ignore all in this document|d"),
+				add(MenuItem(MenuItem::Command, qt_("Ignore all in this document|u"),
 						FuncRequest(LFUN_SPELLING_ADD_LOCAL, arg)));
 			}
 		}
@@ -918,7 +937,7 @@ void MenuDefinition::expandLanguageSelector(Buffer const * buf)
 		buf->masterBuffer()->getLanguages();
 
 	if (languages_buffer.size() < 2) {
-		add(MenuItem(MenuItem::Command, qt_("Switch Language...|L"),
+		add(MenuItem(MenuItem::Command, qt_("Switch Language...|w"),
 			     FuncRequest(LFUN_DIALOG_SHOW, "character")));
 		return;
 	}
@@ -2707,6 +2726,9 @@ void Menus::updateMenu(Menu * qmenu)
 
 	docstring identifier = qstring_to_ucs4(qmenu->d->name);
 	MenuDefinition fromLyxMenu(qmenu->d->name);
+	BufferView * bv = 0;
+	if (qmenu->d->view)
+		bv = qmenu->d->view->currentBufferView();
 	while (!identifier.empty()) {
 		docstring menu_name;
 		identifier = split(identifier, menu_name, ';');
@@ -2718,10 +2740,14 @@ void Menus::updateMenu(Menu * qmenu)
 		}
 
 		MenuDefinition cat_menu = d->getMenu(toqstr(menu_name));
-		//FIXME: 50 is a wild guess. We should take into account here
-		//the expansion of menu items, disabled optional items etc.
+		// We take into account here the expansion of menu items,
+		// disabled optional items etc.
+		MenuDefinition to_menu;
+		d->expand(fromLyxMenu, to_menu, bv);
+		MenuDefinition to_cat_menu;
+		d->expand(cat_menu, to_cat_menu, bv);
 		bool const in_sub_menu = !fromLyxMenu.empty()
-			&& fromLyxMenu.size() + cat_menu.size() > 50 ;
+			&& to_menu.realSize() + to_cat_menu.realSize() > 50;
 		if (in_sub_menu)
 			fromLyxMenu.catSub(menu_name);
 		else
@@ -2734,9 +2760,6 @@ void Menus::updateMenu(Menu * qmenu)
 		return;
 	}
 
-	BufferView * bv = 0;
-	if (qmenu->d->view)
-		bv = qmenu->d->view->currentBufferView();
 	d->expand(fromLyxMenu, *qmenu->d->top_level_menu, bv);
 	qmenu->d->populate(qmenu, *qmenu->d->top_level_menu);
 }


More information about the lyx-cvs mailing list