Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToolBar: use new Renderers #171

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.widgets.toolbar.*;

/**
Expand Down Expand Up @@ -55,43 +54,14 @@
*/
public class ToolBar extends Composite {

/**
* Renderer interface for the {@link ToolBar} widget. All renderers have to
* implement this to work with the ToolBar.
*/
public interface IToolBarRenderer {

/**
* Renders the handle.
*
* @param gc GC to render with.
* @param bounds Bounds of the rendering. x and y are always 0.
*/
void render(GC gc, Rectangle bounds);

/**
* Computes the size of the rendered ToolBar.
*
* @return The size as {@link Point}
*/
Point computeSize(Point size);

/**
* Returns the row count of the rendered widget.
*
* @return The row count.
*/
int rowCount();
}

/** The {@link ToolItem}s contained in the {@link ToolBar} */
private final java.util.List<ToolItem> items = new ArrayList<>();

@Deprecated
public int itemCount;

/** The renderer used to render to {@link ToolBar}. */
private final IToolBarRenderer renderer;
private final ToolBarRenderer renderer;

private final boolean flat;
private final boolean wrap;
Expand Down Expand Up @@ -148,7 +118,7 @@ public ToolBar(Composite parent, int style, boolean internal) {
super(parent, checkStyle(style));
this.style |= SWT.DOUBLE_BUFFERED;

renderer = new ToolBarRenderer(this);
renderer = new DefaultToolBarRenderer(this);

Listener listener = event -> {
if (isDisposed()) {
Expand Down Expand Up @@ -301,12 +271,12 @@ private void onPaint(Event event) {
return;
}

Rectangle bounds = getBounds();
if (bounds.width == 0 && bounds.height == 0) {
final Point size = getSize();
if (size.x == 0 || size.y == 0) {
return;
}

Drawing.drawWithGC(this, event.gc, gc -> renderer.render(gc, bounds));
Drawing.drawWithGC(this, event.gc, gc -> renderer.paint(gc, size.x, size.y));
}

/**
Expand Down Expand Up @@ -351,18 +321,7 @@ private void onPaint(Event event) {
@Override
public Point computeSize(int wHint, int hHint, boolean changed) {
checkWidget();
Point sizeHint = new Point(wHint, hHint);
Point size = renderer.computeSize(sizeHint);

return size;
}

Point computeSizeInPixels(int wHint, int hHint, boolean changed) {
checkWidget();
Point sizeHint = new Point(wHint, hHint);
Point size = renderer.computeSize(sizeHint);

return DPIUtil.autoScaleUp(size);
return renderer.computeSize(wHint, hHint);
}

void createItem(ToolItem item, int index) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*******************************************************************************
* Copyright (c) 2000, 2021 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.widgets;

import org.eclipse.swt.graphics.*;

/**
* Renderer interface for the {@link ToolBar} widget. All renderers have to
* implement this to work with the ToolBar.
*/
public abstract class ToolBarRenderer extends ControlRenderer {

/**
* Computes the size of the rendered ToolBar.
*
* @return The size as {@link Point}
*/
public abstract Point computeSize(int widthHint, int heightHint);

/**
* Returns the row count of the rendered widget.
*
* @return The row count.
*/
public abstract int rowCount();

protected ToolBarRenderer(ToolBar control) {
super(control);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.ToolBar.*;
import org.eclipse.swt.widgets.toolbar.ToolBarLayout.*;

/**
* Default renderer for the ToolBar.
*/
public class ToolBarRenderer implements IToolBarRenderer {
public class DefaultToolBarRenderer extends ToolBarRenderer {
public static final Color COLOR_SEPARATOR = new Color(Display.getDefault(), 160, 160, 160);

private final ToolBar bar;
private int rowCount = SWT.DEFAULT;

public ToolBarRenderer(ToolBar toolbar) {
public DefaultToolBarRenderer(ToolBar toolbar) {
super(toolbar);
this.bar = toolbar;
}

@Override
public void render(GC gc, Rectangle bounds) {
Point size = new Point(bounds.width, bounds.height);
public void paint(GC gc, int width, int height) {
Point size = new Point(width, height);
ToolBarLayout layout = computeLayout(size);
rowCount = layout.rows().size();

Expand All @@ -50,10 +50,10 @@ private void render(GC gc, Point size, List<Row> rows) {
gc.drawLine(0, 0, size.x, 0);
}

for (int i = 0; i < rows.size(); i++) {
Row row = rows.get(i);
for (Row row : rows) {
for (ItemRecord itemRecord : row.items) {
bar.getItem(itemRecord.index()).render(gc, itemRecord.bounds());
final ToolItem item = bar.getItem(itemRecord.index());
item.render(gc, itemRecord.bounds());
}
if (row.hasRowSeparator) {
drawHorizontalSeparator(gc, row);
Expand All @@ -72,8 +72,8 @@ private ToolBarLayout computeLayout(Point size) {
List<ItemRecord> itemRecords = new ArrayList<>();
for (int i = 0; i < bar.getItemCount(); i++) {
ToolItem item = bar.getItem(i);
Point preferedSize = item.getSize();
Rectangle initialBounds = new Rectangle(0, 0, preferedSize.x, preferedSize.y);
Point preferredSize = item.getSize();
Rectangle initialBounds = new Rectangle(0, 0, preferredSize.x, preferredSize.y);
ItemRecord itemRecord = new ItemRecord(i, initialBounds, item.isSeparator());
itemRecords.add(itemRecord);
}
Expand All @@ -86,8 +86,8 @@ private ToolBarLayout computeLayout(Point size) {


@Override
public Point computeSize(Point size) {
ToolBarLayout layout = computeLayout(size);
public Point computeSize(int widthHint, int heightHint) {
ToolBarLayout layout = computeLayout(new Point(widthHint, heightHint));

Point computedSize = layout.size();
if (bar.isBorder()) {
Expand All @@ -96,12 +96,12 @@ public Point computeSize(Point size) {
}

Point finalSize = new Point(computedSize.x, computedSize.y);
if (size.x > 0) {
finalSize.x = size.x;
if (widthHint > 0) {
finalSize.x = widthHint;
}

if (size.y > 0) {
finalSize.y = size.y;
if (heightHint > 0) {
finalSize.y = heightHint;
}

return finalSize;
Expand All @@ -111,8 +111,7 @@ public Point computeSize(Point size) {
@Override
public int rowCount() {
if (rowCount == SWT.DEFAULT) {
Rectangle bounds = bar.getBounds();
Point size = new Point(bounds.x, bounds.y);
Point size = getSize();
rowCount = computeLayout(size).rows().size();
}
return rowCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void setX(int x) {
/**
* Mirrors the layout vertically.
*/
void mirrow() {
void mirrorX() {
for (ItemRecord itemRecord : items) {
itemRecord.bounds.x = availableSpace.x - itemRecord.bounds.x - itemRecord.bounds.width;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private ToolBarLayout computeSingleHorizontalRow(Point offset) {
row.setY(offset.y);

if (rtl) {
row.mirrow();
row.mirrorX();
}

int width = offset.x + row.usedSpace.x;
Expand Down Expand Up @@ -114,7 +114,7 @@ private ToolBarLayout computeMultipleHorizontalRows(Point offset) {
}

if (rtl) {
row.mirrow();
row.mirrorX();
}
}

Expand Down
Loading