如何在Eclipse中为MToolItem应用CSS聚焦样式?
Great question! Getting focus styles working for MToolItem in Eclipse can be trickier than MToolBar because of how SWT renders these components and the specific CSS classes it uses under the hood. Let's walk through the most actionable fixes:
1. Target Eclipse's Specific CSS Class for MToolItem
Eclipse applies unique, component-specific CSS classes to its workbench elements. Instead of relying on the generic ToolItem selector, use the actual class Eclipse assigns to MToolItem:
.org-eclipse-ui-workbench-MToolItem:focus { background-color: #ffffff; /* Optional: Add an outline to make focus more visually distinct */ outline: 1px solid #cccccc; }
2. Boost Selector Specificity to Override Theme Styles
If your custom CSS is being overridden by Eclipse's default theme rules, increase the selector specificity by combining it with the parent MToolBar:
ToolBar > .org-eclipse-ui-workbench-MToolItem:focus { background-color: #ffffff; }
This ensures your style takes priority over more generic theme declarations.
3. Ensure the MToolItem is Configured to Accept Focus
By default, some MToolItem instances aren't set to receive focus. In your Java code, enable focus for the item to make the :focus pseudo-class trigger:
MToolItem item = new MToolItem(toolBar, SWT.PUSH); item.setFocusable(true); // Critical step for CSS focus styles to work
4. Use Eclipse's CSS Spy to Inspect the Component
If you're still hitting roadblocks, use Eclipse's built-in tool to inspect the exact structure and classes of your MToolItem:
- Go to
Window > Show View > Other... - Search for and open
CSS Spy(under theGeneralcategory) - Hover over the
MToolItemin your UI to view its full CSS class list and hierarchy
This will let you craft a perfectly targeted selector that works with your specific Eclipse version and theme.
内容的提问来源于stack exchange,提问作者PeterK

