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

General housekeeping cleanup #306

Merged
merged 10 commits into from
Dec 21, 2017
Prev Previous commit
Next Next commit
Preview size of dropped widgets from the gallery
Also improves performance of previewing tiles from dropped sources by caching the size of the widget rather than creating a new widget/layout EVERY time the mouse is moved while dragging
Fixes #115
  • Loading branch information
SamCarlberg committed Nov 21, 2017
commit 72df8d478431398557e4d18eea783c32617a9032
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ public class WidgetPaneController {

private final Map<Node, Boolean> tilesAlreadySetup = new WeakHashMap<>();

/**
* Memoizes the size of a tile that would be added when dropping a source or widget. Memoizing prevents calling
* potentially expensive component initialization code every time the mouse moves when previewing the location of a
* tile for a source or widget.
*/
private TileSize tilePreviewSize = null;

@FXML
private void initialize() {

Expand All @@ -90,11 +97,12 @@ private void initialize() {
pane.setOnDragOver(event -> {
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
GridPoint point = pane.pointAt(event.getX(), event.getY());
boolean isWidget = event.getDragboard().hasContent(DataFormats.widgetTile);
boolean isWidgetTile = event.getDragboard().hasContent(DataFormats.widgetTile);
boolean isSource = event.getDragboard().hasContent(DataFormats.source);
boolean isWidget = event.getDragboard().hasContent(DataFormats.widgetType);

// preview the location of the widget if one is being dragged
if (isWidget) {
if (isWidgetTile) {
pane.setHighlight(true);
pane.setHighlightPoint(point);
DataFormats.WidgetData data = (DataFormats.WidgetData) event.getDragboard().getContent(DataFormats.widgetTile);
Expand All @@ -111,11 +119,37 @@ private void initialize() {
Optional<String> componentName = Components.getDefault().pickComponentNameFor(source.getDataType());
Optional<DataSource<?>> dummySource = DummySource.forTypes(source.getDataType());
if (componentName.isPresent() && dummySource.isPresent()) {
Components.getDefault().createComponent(componentName.get(), dummySource.get()).ifPresent(c -> {
if (tilePreviewSize == null) {
Components.getDefault().createComponent(componentName.get(), dummySource.get())
.map(pane::sizeOfWidget)
.ifPresent(size -> tilePreviewSize = size);
}
if (tilePreviewSize == null) {
pane.setHighlight(false);
} else {
pane.setHighlight(true);
pane.setHighlightPoint(point);
pane.setHighlightSize(pane.sizeOfWidget(c));
});
pane.setHighlightSize(tilePreviewSize);
}
}
} else if (isWidget) {
if (!pane.isOpen(point, new TileSize(1, 1), n -> false)) {
// Dragged a widget onto a tile, can't drop
pane.setHighlight(false);
return;
}
String componentType = (String) event.getDragboard().getContent(DataFormats.widgetType);
if (tilePreviewSize == null) {
Components.getDefault().createComponent(componentType)
.map(pane::sizeOfWidget)
.ifPresent(size -> tilePreviewSize = size);
}
if (tilePreviewSize == null) {
pane.setHighlight(false);
} else {
pane.setHighlight(true);
pane.setHighlightPoint(point);
pane.setHighlightSize(tilePreviewSize);
}
}

Expand All @@ -130,17 +164,20 @@ private void initialize() {
pane.setOnDragDropped(event -> {
Dragboard dragboard = event.getDragboard();
GridPoint point = pane.pointAt(event.getX(), event.getY());
// Dropping a source from the sources tree
if (dragboard.hasContent(DataFormats.source)) {
SourceEntry entry = (SourceEntry) dragboard.getContent(DataFormats.source);
dropSource(entry.get(), point);
}

// Dropping a tile onto the pane after moving it around
if (dragboard.hasContent(DataFormats.widgetTile)) {
DataFormats.WidgetData data = (DataFormats.WidgetData) dragboard.getContent(DataFormats.widgetTile);
pane.tileMatching(tile -> tile.getId().equals(data.getId()))
.ifPresent(tile -> moveTile(tile, point.subtract(data.getDragPoint())));
}

// Dropping a widget from the gallery
if (dragboard.hasContent(DataFormats.widgetType)) {
String componentType = (String) dragboard.getContent(DataFormats.widgetType);
Components.getDefault().createComponent(componentType).ifPresent(c -> {
Expand All @@ -153,6 +190,7 @@ private void initialize() {
}

cleanupWidgetDrag();
tilePreviewSize = null;
event.consume();
});

Expand Down Expand Up @@ -316,6 +354,7 @@ private void setupTile(Tile tile) {

tile.setOnDragDropped(event -> {
Dragboard dragboard = event.getDragboard();
// Dragging a source onto a tile
if (dragboard.hasContent(DataFormats.source) && tile.getContent() instanceof Sourced) {
SourceEntry entry = (SourceEntry) dragboard.getContent(DataFormats.source);
((Sourced) tile.getContent()).addSource(entry.get());
Expand All @@ -324,6 +363,7 @@ private void setupTile(Tile tile) {
return;
}

// Moving a layout tile around
if (dragboard.hasContent(DataFormats.widgetTile) && tile instanceof LayoutTile) {
DataFormats.WidgetData data = (DataFormats.WidgetData) event.getDragboard().getContent(DataFormats.widgetTile);

Expand All @@ -340,6 +380,7 @@ private void setupTile(Tile tile) {
return;
}

// Dragging a widget from the gallery
if (dragboard.hasContent(DataFormats.widgetType) && tile instanceof LayoutTile) {
String widgetType = (String) dragboard.getContent(DataFormats.widgetType);

Expand All @@ -351,6 +392,7 @@ private void setupTile(Tile tile) {
return;
}

// Dragging a source from the sources tree
if (dragboard.hasContent(DataFormats.source) && tile instanceof LayoutTile) {
SourceEntry entry = (SourceEntry) dragboard.getContent(DataFormats.source);

Expand Down