Skip to content

Commit

Permalink
IDEA-127205 Specify Editor Tab Width
Browse files Browse the repository at this point in the history
  • Loading branch information
Vassiliy.Kudryashov authored and Vassiliy.Kudryashov committed Jun 4, 2015
1 parent 5e1a42a commit 2228d3e
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public class OnePixelDivider extends Divider {
public static final Color BACKGROUND = new JBColor(Gray.xC5, Gray.x51);

private boolean myVertical;
private Splitter mySplitter;
private Splittable mySplitter;
private boolean myResizeEnabled;
private boolean mySwitchOrientationEnabled;
protected Point myPoint;
private IdeGlassPane myGlassPane;
private final MouseAdapter myListener = new MyMouseAdapter();
private Disposable myDisposable;

public OnePixelDivider(boolean vertical, Splitter splitter) {
public OnePixelDivider(boolean vertical, Splittable splitter) {
super(new GridBagLayout());
mySplitter = splitter;
myResizeEnabled = true;
Expand All @@ -71,11 +71,18 @@ public void removeNotify() {
}
}

private boolean dragging = false;
private boolean myDragging = false;

private void setDragging(boolean dragging) {
if (myDragging != dragging) {
myDragging = dragging;
mySplitter.setDragging(dragging);
}
}
private class MyMouseAdapter extends MouseAdapter implements Weighted {
@Override
public void mousePressed(MouseEvent e) {
dragging = isInDragZone(e);
setDragging(isInDragZone(e));
_processMouseEvent(e);
}

Expand All @@ -92,7 +99,7 @@ boolean isInDragZone(MouseEvent e) {
@Override
public void mouseReleased(MouseEvent e) {
_processMouseEvent(e);
dragging = false;
setDragging(false);
}

@Override
Expand Down Expand Up @@ -163,44 +170,28 @@ public void setOrientation(boolean vertical) {
protected void processMouseMotionEvent(MouseEvent e) {
super.processMouseMotionEvent(e);
if (!myResizeEnabled) return;
if (MouseEvent.MOUSE_DRAGGED == e.getID() && dragging) {
myPoint = SwingUtilities.convertPoint(this, e.getPoint(), mySplitter);
if (MouseEvent.MOUSE_DRAGGED == e.getID() && myDragging) {
myPoint = SwingUtilities.convertPoint(this, e.getPoint(), mySplitter.asComponent());
float proportion;
final float firstMinProportion = getMinProportion(mySplitter.getFirstComponent());
final float secondMinProportion = getMinProportion(mySplitter.getSecondComponent());
final float firstMinProportion = mySplitter.getMinProportion(true);
final float secondMinProportion = mySplitter.getMinProportion(false);
if (isVertical()) {
if (getHeight() > 0) {
proportion = Math.min(1.0f, Math
.max(.0f, Math.min(Math.max(firstMinProportion, (float)myPoint.y / (float)mySplitter.getHeight()), 1 - secondMinProportion)));
.max(.0f, Math.min(Math.max(firstMinProportion, (float)myPoint.y / (float)mySplitter.asComponent().getHeight()), 1 - secondMinProportion)));
mySplitter.setProportion(proportion);
}
}
else {
if (getWidth() > 0) {
proportion = Math.min(1.0f, Math.max(.0f, Math.min(
Math.max(firstMinProportion, (float)myPoint.x / (float)mySplitter.getWidth()), 1 - secondMinProportion)));
Math.max(firstMinProportion, (float)myPoint.x / (float)mySplitter.asComponent().getWidth()), 1 - secondMinProportion)));
mySplitter.setProportion(proportion);
}
}
}
}

private float getMinProportion(JComponent component) {
if (component != null &&
mySplitter.getFirstComponent() != null &&
mySplitter.getFirstComponent().isVisible() &&
mySplitter.getSecondComponent() != null &&
mySplitter.getSecondComponent().isVisible()) {
if (isVertical()) {
return (float)component.getMinimumSize().height / (float)(mySplitter.getHeight() - 1);
} else {
return (float)component.getMinimumSize().width / (float)(mySplitter.getWidth() - 1);
}
}

return 0.0f;
}

@Override
protected void processMouseEvent(MouseEvent e) {
super.processMouseEvent(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class JBTabsImpl extends JComponent
QuickActionProvider {

public static final DataKey<JBTabsImpl> NAVIGATION_ACTIONS_KEY = DataKey.create("JBTabs");

@NonNls public static final Key<Integer> SIDE_TABS_SIZE_LIMIT_KEY = Key.create("SIDE_TABS_SIZE_LIMIT_KEY");
public static final Color MAC_AQUA_BG_COLOR = Gray._200;
private static final Comparator<TabInfo> ABC_COMPARATOR = new Comparator<TabInfo>() {
@Override
Expand Down Expand Up @@ -113,6 +113,7 @@ public int compare(TabInfo o1, TabInfo o2) {

private SingleRowLayout mySingleRowLayout;
private final TableLayout myTableLayout = new TableLayout(this);
private final TabsSideSplitter mySplitter = new TabsSideSplitter(this);


private TabLayout myLayout;
Expand Down Expand Up @@ -1492,7 +1493,6 @@ public boolean isEmpty() {
public void doLayout() {
try {
myHeaderFitSize = computeHeaderFitSize();

final Collection<TabLabel> labels = myInfo2Label.values();
for (TabLabel each : labels) {
each.setTabActionsAutoHide(myTabLabelActionsAutoHide);
Expand All @@ -1516,6 +1516,13 @@ public void doLayout() {
mySingleRowLayout.scroll(0);
myLastLayoutPass = mySingleRowLayout.layoutSingleRow(visible);
myTableLayout.myLastTableLayout = null;
OnePixelDivider divider = mySplitter.getDivider();
if (divider.getParent() == this) {
int location = getTabsPosition() == JBTabsPosition.left
? mySingleRowLayout.myLastSingRowLayout.tabRectangle.width
: getWidth() - mySingleRowLayout.myLastSingRowLayout.tabRectangle.width;
divider.setBounds(location, 0, 1, getHeight());
}
}
else {
myLastLayoutPass = myTableLayout.layoutTable(visible);
Expand Down Expand Up @@ -2359,6 +2366,11 @@ private Max computeMaxSize() {
max.myToolbar.width = Math.max(max.myToolbar.width, toolbar.getPreferredSize().width);
}
}
if (getTabsPosition() == JBTabsPosition.left || getTabsPosition() == JBTabsPosition.right) {
if (mySplitter.getSideTabsLimit() > 0) {
max.myLabel.width = Math.min(max.myLabel.width, mySplitter.getSideTabsLimit());
}
}

max.myToolbar.height++;

Expand Down Expand Up @@ -3270,6 +3282,12 @@ public JBTabsPresentation setTabLabelActionsMouseDeadzone(final TimedDeadzone.Le
@NotNull
public JBTabsPresentation setTabsPosition(final JBTabsPosition position) {
myPosition = position;
OnePixelDivider divider = mySplitter.getDivider();
if ((position == JBTabsPosition.left || position == JBTabsPosition.right) && divider.getParent() == null) {
add(divider);
} else if (divider.getParent() == this){
remove(divider);
}
relayout(true, false);
return this;
}
Expand All @@ -3290,7 +3308,7 @@ public JBTabsPresentation setTabDraggingEnabled(boolean enabled) {
}

public boolean isTabDraggingEnabled() {
return myTabDraggingEnabled;
return myTabDraggingEnabled && !mySplitter.isDragging();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,12 @@ public Font getFont() {

@Override
protected void doPaint(Graphics2D g) {
if (UISettings.getInstance().HIDE_TABS_IF_NEED || tabs.getTabsPosition() == JBTabsPosition.left || tabs.getTabsPosition() == JBTabsPosition.right) {
super.doPaint(g);
return;
}
Rectangle clip = getVisibleRect();
if (getPreferredSize().width <= clip.width) {
if (getPreferredSize().width <= clip.width + 2) {
super.doPaint(g);
return;
}
int dimSize = 30;
int dimSize = 10;
int dimStep = 2;
Composite oldComposite = g.getComposite();
Shape oldClip = g.getClip();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.ui.tabs.impl;

import com.intellij.openapi.ui.OnePixelDivider;
import com.intellij.openapi.ui.Splittable;
import com.intellij.ui.tabs.JBTabsPosition;
import com.intellij.ui.tabs.TabInfo;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;


class TabsSideSplitter implements Splittable, PropertyChangeListener {
private static final int MIN_TAB_WIDTH = JBUI.scale(75);

@NotNull private final JBTabsImpl myTabs;
private int mySideTabsLimit = 0;
private boolean myDragging;
private final OnePixelDivider myDivider;


TabsSideSplitter(@NotNull JBTabsImpl tabs) {
myTabs = tabs;
myTabs.addPropertyChangeListener(JBTabsImpl.SIDE_TABS_SIZE_LIMIT_KEY.toString(), this);
myDivider = new OnePixelDivider(false, this);
}

OnePixelDivider getDivider() {
return myDivider;
}

@Override
public float getMinProportion(boolean first) {
return Math.min(.5F, (float)MIN_TAB_WIDTH / Math.max(1, myTabs.getWidth()));
}

@Override
public void setProportion(float proportion) {
int width = myTabs.getWidth();
if (myTabs.getTabsPosition() == JBTabsPosition.left) {
setSideTabsLimit((int)Math.max(MIN_TAB_WIDTH, proportion * width));
}
else if (myTabs.getTabsPosition() == JBTabsPosition.right) {
setSideTabsLimit(width - (int)Math.max(MIN_TAB_WIDTH, proportion * width));
}
}

int getSideTabsLimit() {
return mySideTabsLimit;
}

void setSideTabsLimit(int sideTabsLimit) {
if (mySideTabsLimit != sideTabsLimit) {
mySideTabsLimit = sideTabsLimit;
UIUtil.putClientProperty(myTabs, JBTabsImpl.SIDE_TABS_SIZE_LIMIT_KEY, mySideTabsLimit);
myTabs.resetLayout(true);
myTabs.doLayout();
myTabs.repaint();
TabInfo info = myTabs.getSelectedInfo();
JComponent page = info != null ? info.getComponent() : null;
if (page != null) {
page.revalidate();
page.repaint();
}
}
}

@Override
public boolean getOrientation() {
return false;
}

@Override
public void setOrientation(boolean verticalSplit) {
//ignore
}

@Override
public void setDragging(boolean dragging) {
myDragging = dragging;
}

boolean isDragging() {
return myDragging;
}

@NotNull
@Override
public Component asComponent() {
return myTabs;
}

@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getSource() != myTabs) return;
Integer limit = UIUtil.getClientProperty(myTabs, JBTabsImpl.SIDE_TABS_SIZE_LIMIT_KEY);
if (limit == null) limit = 0;
setSideTabsLimit(limit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public ActionGroup get() {
@Override
@NotNull
public UiDecoration getDecoration() {
return new UiDecoration(null, new Insets(TabsUtil.TAB_VERTICAL_PADDING, 10, TabsUtil.TAB_VERTICAL_PADDING, 10));
return new UiDecoration(null, new Insets(TabsUtil.TAB_VERTICAL_PADDING, 8, TabsUtil.TAB_VERTICAL_PADDING, 8));
}
}).setTabLabelActionsMouseDeadzone(TimedDeadzone.NULL).setGhostsAlwaysVisible(true).setTabLabelActionsAutoHide(false)
.setActiveTabFillIn(EditorColorsManager.getInstance().getGlobalScheme().getDefaultBackground()).setPaintFocus(false).getJBTabs()
Expand Down Expand Up @@ -239,7 +239,8 @@ else if (eachAnchor == ToolWindowAnchor.RIGHT) {
myTabs.getPresentation().setPaintBorder(border.top, border.left, border.right, border.bottom).setTabSidePaintBorder(5);
}

public Component getComponent() {
@NotNull
public JComponent getComponent() {
return myTabs.getComponent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.intellij.ui.awt.RelativePoint;
import com.intellij.ui.docking.DockManager;
import com.intellij.ui.tabs.JBTabs;
import com.intellij.ui.tabs.impl.JBTabsImpl;
import com.intellij.util.Alarm;
import com.intellij.util.containers.ArrayListSet;
import com.intellij.util.containers.ContainerUtil;
Expand Down Expand Up @@ -196,6 +197,11 @@ private Element writePanel(final JPanel panel) {
}
else if (comp instanceof JBTabs) {
final Element res = new Element("leaf");
Integer limit = UIUtil.getClientProperty(((JBTabs)comp).getComponent(), JBTabsImpl.SIDE_TABS_SIZE_LIMIT_KEY);
if (limit != null) {
res.setAttribute(JBTabsImpl.SIDE_TABS_SIZE_LIMIT_KEY.toString(), String.valueOf(limit));
}

final EditorWindow window = findWindowWith(comp);
writeWindow(res, window);
return res;
Expand Down Expand Up @@ -843,6 +849,19 @@ public void run() {

for (int i = 0; i < fileElements.size(); i++) {
final Element file = fileElements.get(i);
if (i == 0) {
EditorTabbedContainer tabbedPane = window.getTabbedPane();
if (tabbedPane != null) {
try {
int limit =
Integer.parseInt(file.getParentElement().getAttributeValue(JBTabsImpl.SIDE_TABS_SIZE_LIMIT_KEY.toString(), "0"));
UIUtil.putClientProperty(tabbedPane.getComponent(), JBTabsImpl.SIDE_TABS_SIZE_LIMIT_KEY, limit);
}
catch (NumberFormatException e) {
//ignore
}
}
}
try {
final FileEditorManagerImpl fileEditorManager = getManager();
Element historyElement = file.getChild(HistoryEntry.TAG);
Expand Down
Loading

0 comments on commit 2228d3e

Please sign in to comment.