sfx2/source/dialog/StyleList.cxx |   37 ++++++++++++++++++++++++-------------
 sfx2/source/inc/StyleList.hxx    |    4 ++--
 2 files changed, 26 insertions(+), 15 deletions(-)

New commits:
commit 7c5de1a693da21b60378d6d270a6a4f8a7e2ba6a
Author:     Michael Weghorn <[email protected]>
AuthorDate: Wed Mar 4 21:56:11 2026 +0100
Commit:     Michael Weghorn <[email protected]>
CommitDate: Thu Mar 5 06:59:42 2026 +0100

    tdf#170820 a11y: Only use mouse position for StyleList menu if 
mouse-triggered
    
    The context menu can be triggered either via the mouse
    or via the keyboard. So far, the logic in StyleList
    was unconditionally trying to select the entry under
    the current mouse position before triggering the menu.
    
    That however doesn't make sense when the context menu
    was triggered using the keyboard, in which case the
    context menu should be triggered for the already
    selected entry.
    
    Therefore, make selecting the entry under the mouse
    position conditional on the context menu being a
    mouse-triggered event.
    
    In case of a keyboard-triggered menu, also use the
    center of the currently selected entry for the
    menu position instead of the current mouse position.
    
    Change-Id: I677e13859b0727cb7c94645551d8d74fd4b3fbe1
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/200988
    Tested-by: Jenkins
    Reviewed-by: Michael Weghorn <[email protected]>

diff --git a/sfx2/source/dialog/StyleList.cxx b/sfx2/source/dialog/StyleList.cxx
index 35e969be328e..0888068d4ae2 100644
--- a/sfx2/source/dialog/StyleList.cxx
+++ b/sfx2/source/dialog/StyleList.cxx
@@ -601,17 +601,28 @@ void StyleList::DropHdl(const OUString& rStyle, const 
OUString& rParent)
     m_bDontUpdate = false;
 }
 
-void StyleList::PrepareMenu(const Point& rPos)
+Point StyleList::PrepareMenu(const CommandEvent& rContextMenuEvent)
 {
     weld::TreeView* pTreeView = m_xTreeBox->get_visible() ? m_xTreeBox.get() : 
m_xFmtLb.get();
-    std::unique_ptr<weld::TreeIter> xIter = 
pTreeView->get_dest_row_at_pos(rPos, false);
-    if (xIter && !pTreeView->is_selected(*xIter))
+    if (rContextMenuEvent.IsMouseEvent())
     {
-        pTreeView->unselect_all();
-        pTreeView->set_cursor(*xIter);
-        pTreeView->select(*xIter);
+        const Point& rPos = rContextMenuEvent.GetMousePosPixel();
+        std::unique_ptr<weld::TreeIter> xIter = 
pTreeView->get_dest_row_at_pos(rPos, false);
+        if (xIter && !pTreeView->is_selected(*xIter))
+        {
+            pTreeView->unselect_all();
+            pTreeView->set_cursor(*xIter);
+            pTreeView->select(*xIter);
+        }
+        FmtSelectHdl(*pTreeView);
+        return rPos;
+    }
+    else
+    {
+        if (std::unique_ptr<weld::TreeIter> pSelected = 
pTreeView->get_selected())
+            return pTreeView->get_row_area(*pSelected).Center();
+        return {};
     }
-    FmtSelectHdl(*pTreeView);
 }
 
 /** Internal structure for the establishment of the hierarchical view */
@@ -1582,12 +1593,12 @@ IMPL_LINK_NOARG(StyleList, Clear, void*, void)
 
 IMPL_LINK(StyleList, OnPopupEnd, const OUString&, sCommand, void) { 
MenuSelect(sCommand); }
 
-void StyleList::ShowMenu(const CommandEvent& rCEvt)
+void StyleList::ShowMenu(const Point& rPos)
 {
     CreateContextMenu();
     weld::TreeView* pTreeView = m_xTreeBox->get_visible() ? m_xTreeBox.get() : 
m_xFmtLb.get();
     mxMenu->connect_activate(LINK(this, StyleList, OnPopupEnd));
-    mxMenu->popup_at_rect(pTreeView, 
tools::Rectangle(rCEvt.GetMousePosPixel(), Size(1, 1)));
+    mxMenu->popup_at_rect(pTreeView, tools::Rectangle(rPos, Size(1, 1)));
 }
 
 void StyleList::MenuSelect(const OUString& rIdent)
@@ -1990,7 +2001,7 @@ IMPL_LINK(StyleList, PopupFlatMenuHdl, const 
CommandEvent&, rCEvt, bool)
     if (rCEvt.GetCommand() != CommandEventId::ContextMenu)
         return false;
 
-    PrepareMenu(rCEvt.GetMousePosPixel());
+    const Point aPos = PrepareMenu(rCEvt);
 
     if (m_xFmtLb->count_selected_rows() <= 0)
     {
@@ -1998,7 +2009,7 @@ IMPL_LINK(StyleList, PopupFlatMenuHdl, const 
CommandEvent&, rCEvt, bool)
         m_pParentDialog->EnableDel(false, this);
     }
 
-    ShowMenu(rCEvt);
+    ShowMenu(aPos);
 
     return true;
 }
@@ -2008,9 +2019,9 @@ IMPL_LINK(StyleList, PopupTreeMenuHdl, const 
CommandEvent&, rCEvt, bool)
     if (rCEvt.GetCommand() != CommandEventId::ContextMenu)
         return false;
 
-    PrepareMenu(rCEvt.GetMousePosPixel());
+    const Point aPos = PrepareMenu(rCEvt);
 
-    ShowMenu(rCEvt);
+    ShowMenu(aPos);
 
     return true;
 }
diff --git a/sfx2/source/inc/StyleList.hxx b/sfx2/source/inc/StyleList.hxx
index 737bc99e3782..f562537834ae 100644
--- a/sfx2/source/inc/StyleList.hxx
+++ b/sfx2/source/inc/StyleList.hxx
@@ -152,8 +152,8 @@ private:
     void DropHdl(const OUString& rStyle, const OUString& rParent);
 
     void MenuSelect(const OUString& rIdent);
-    void PrepareMenu(const Point& rPos);
-    void ShowMenu(const CommandEvent& rCEvt);
+    Point PrepareMenu(const CommandEvent& rContextMenuEvent);
+    void ShowMenu(const Point& rPos);
     void CreateContextMenu();
 
     void Notify(SfxBroadcaster& rBC, const SfxHint& rHint);

Reply via email to