tacos_tacos_tacos
tacos_tacos_tacos

Reputation: 10585

Zoom Editor Font Size through Keyboard Shortcuts in Eclipse

Is it possible to achieve in Eclipse (through the use of a plugin or otherwise) tab-specific font-size changes via a keyboard shortcut? This would make the behavior similar to how Visual Studio for instance handles it.

The existing questions for this topic were either way outdated or not exactly what I am asking for.

More detail

In Visual Studio, the sequence:

  1. Hold down Ctrl key
  2. Scroll mousewheel up or down

... achieves zoom in and zoom out, respectively.

Moreover, you can do this on each opened file (each tab in the IDE) so that you can open one file, say, in effectively "size 72" font and another in the default font and another in "size 36" font (I know, this is not the correct term, but you know what I mean) and it just works.

In Eclipse, the action above has no effect and there is no evident way to achieve either tab-specific zoom or hotkey+scroll-driven zoom, let alone both.

Upvotes: 4

Views: 2208

Answers (1)

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 21005

The upcoming Eclipse milestone Neon M4 provides some of the features that you are asking for. See the new and noteworthy for details.

Providing a zoom functionality from the outside requires decent knowledge of the Eclipse workbench and editors (or you'll gain it along the way). I see three work items here:

1. Changing the Editor Font Size

In Eclipse, the tabs in the center of the workbench window are called editors. The actual UI and behavior of an editor is defined by the plug-in that contributes an editor for a certain content type. Hence there are plenty of editor implementations in the wild.

An editor does not necessarily have to display textual content either. For example, an editor for XML files could choose to display the file contents as a tree of elements instead of plain text.

However most text editors are derived from a common base class (AbstractTextEditor).

There is a Basic Text Font preference setting (Window > Preferences > General > Appearance > Colors and Fonts) that most text editors use as the base font. If you programmatically increase and decrease the size of this setting all well-implemented text editors will adjust their display accordingly. But it would of course affect all text editors.

If each text editor should be zoomed individually, you'd have to delve into the depths of AbstractTextEditor. My first attempt would be to get hold of the StyledText that the editor uses to edit the text and manipulate its font size directly. There is a protected getSourceViewer() method that returns the StyledText through getTextWidget().

It may well be that your font size changes get overridden by other code that But it still is the simplest thing that could possibly work.

2. Capturing Ctrl+Wheel

Again you would need the StyledText of each editor to register listeners that can react when Ctrl+Wheel occurs.

If would start with registering an IPartListener that will get notified whenever a part is activated and deactivated. Since an editor is a part, you'll be informed when an editor gains and loses the input focus. If the editor is an instance of AbstractTextEditor, register or deregister the mouse wheel listener.

3. Restoring the Font Size when Editors are Reopened

To make the zoom level persistent you would also need an IPartListener that is notified when an editor part is closed (save font size here) and when it is opened (set stored font size here).

And since you don't know the specifics of all the text editor implementations, it may well happen that your zoom command interferes with one of them,

Upvotes: 1

Related Questions