Add "Restore defaults" to editor font options

This commit is contained in:
Rustam Vishnyakov
2017-07-13 21:11:39 +03:00
parent 008f6bdc3e
commit b537576498
4 changed files with 68 additions and 12 deletions
@@ -22,6 +22,7 @@ import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.editor.colors.EditorFontCache;
import com.intellij.openapi.editor.colors.FontPreferences;
import com.intellij.openapi.editor.colors.ModifiableFontPreferences;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -64,6 +65,10 @@ public class AppEditorFontOptions implements PersistentStateComponent<AppEditorF
SECONDARY_FONT_FAMILY = fontFamilies.get(1);
}
}
private static PersistentFontPreferences getDefaultState() {
return new PersistentFontPreferences();
}
}
@@ -79,17 +84,25 @@ public class AppEditorFontOptions implements PersistentStateComponent<AppEditorF
@Override
public void loadState(PersistentFontPreferences state) {
myFontPreferences.clear();
int fontSize = UISettings.restoreFontSize(state.FONT_SIZE, state.FONT_SCALE);
myFontPreferences.register(state.FONT_FAMILY, fontSize);
myFontPreferences.setLineSpacing(state.LINE_SPACING);
myFontPreferences.setUseLigatures(state.USE_LIGATURES);
if (state.SECONDARY_FONT_FAMILY != null) {
myFontPreferences.register(state.SECONDARY_FONT_FAMILY, fontSize);
}
copyState(state, myFontPreferences);
myFontPreferences.setChangeListener(() -> EditorFontCache.getInstance().reset());
}
private static void copyState(PersistentFontPreferences state, @NotNull ModifiableFontPreferences fontPreferences) {
fontPreferences.clear();
int fontSize = UISettings.restoreFontSize(state.FONT_SIZE, state.FONT_SCALE);
fontPreferences.register(state.FONT_FAMILY, fontSize);
fontPreferences.setLineSpacing(state.LINE_SPACING);
fontPreferences.setUseLigatures(state.USE_LIGATURES);
if (state.SECONDARY_FONT_FAMILY != null) {
fontPreferences.register(state.SECONDARY_FONT_FAMILY, fontSize);
}
}
public static void initDefaults(@NotNull ModifiableFontPreferences fontPreferences) {
copyState(PersistentFontPreferences.getDefaultState(), fontPreferences);
}
@NotNull
public FontPreferences getFontPreferences() {
return myFontPreferences;
@@ -77,10 +77,7 @@ public class AppEditorFontConfigurable implements SearchableConfigurable, NoScro
@Override
public void reset() {
getStoredPreferences().copyTo(getUIFontPreferences());
AppEditorFontOptionsPanel optionsPanel = getFontPanel().getOptionsPanel();
optionsPanel.updateWarning();
optionsPanel.updateOptionsList();
getFontPanel().updatePreview();
getFontPanel().getOptionsPanel().updateOnChangedFont();
}
@NotNull
@@ -17,12 +17,16 @@ package com.intellij.application.options.editor.fonts;
import com.intellij.application.options.colors.AbstractFontOptionsPanel;
import com.intellij.application.options.colors.ColorAndFontOptions;
import com.intellij.application.options.colors.ColorAndFontSettingsListener;
import com.intellij.icons.AllIcons;
import com.intellij.ide.DataManager;
import com.intellij.openapi.application.ApplicationBundle;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.FontPreferences;
import com.intellij.openapi.editor.colors.ModifiableFontPreferences;
import com.intellij.openapi.editor.colors.impl.AppEditorFontOptions;
import com.intellij.openapi.editor.colors.impl.FontPreferencesImpl;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ex.Settings;
import com.intellij.ui.HoverHyperlinkLabel;
@@ -34,14 +38,20 @@ import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AppEditorFontOptionsPanel extends AbstractFontOptionsPanel {
private final EditorColorsScheme myScheme;
private JPanel myWarningPanel;
private JLabel myEditorFontLabel;
private JButton myRestoreButton;
private FontPreferences myDefaultPreferences;
protected AppEditorFontOptionsPanel(EditorColorsScheme scheme) {
myScheme = scheme;
myDefaultPreferences = new FontPreferencesImpl();
AppEditorFontOptions.initDefaults((ModifiableFontPreferences)myDefaultPreferences);
updateOptionsList();
}
@@ -57,9 +67,44 @@ public class AppEditorFontOptionsPanel extends AbstractFontOptionsPanel {
topPanel.add(myWarningPanel, c);
c.gridy = 1;
topPanel.add(createFontSettingsPanel(), c);
c.insets = JBUI.insets(5, 0, 0, 0);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = 2;
topPanel.add(new JSeparator(), c);
c.gridy = 3;
c.fill = GridBagConstraints.NONE;
myRestoreButton = new JButton(ApplicationBundle.message("settings.editor.font.restored.defaults"));
myRestoreButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
restoreDefaults();
}
});
addListener(new ColorAndFontSettingsListener.Abstract() {
@Override
public void fontChanged() {
updateWarning();
updateRestoreButtonState();
}
});
topPanel.add(myRestoreButton, c);
return topPanel;
}
private void restoreDefaults() {
AppEditorFontOptions.initDefaults((ModifiableFontPreferences)getFontPreferences());
updateOnChangedFont();
}
public void updateOnChangedFont() {
updateOptionsList();
fireFontChanged();
}
private void updateRestoreButtonState() {
myRestoreButton.setEnabled(!myDefaultPreferences.equals(getFontPreferences()));
}
private JPanel createMessagePanel() {
JPanel messagePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
messagePanel.add(new JLabel(AllIcons.General.BalloonWarning));
@@ -540,6 +540,7 @@ error.scheme.must.have.a.name=Scheme must have a name
error.illegal.scheme.name=Illegal scheme name
error.a.scheme.with.this.name.already.exists.or.was.deleted.without.applying.the.changes=A scheme with this name already exists or was deleted without applying the changes
title.select.font=Select Font
settings.editor.font.restored.defaults=Restore Defaults
settings.editor.font.overwrite=Use color scheme font instead of the
settings.editor.console.font.overwrite=Use console font instead of the
action.apply.editor.font.settings=Apply editor font settings