Skip to content

Commit

Permalink
Merge pull request #2387 from ControlSystemStudio/CSSTUDIO-1121
Browse files Browse the repository at this point in the history
fix: DisplayRuntime window doesn't use width set by display properties
  • Loading branch information
kasemir authored Sep 16, 2022
2 parents 8fd7bc7 + 4552396 commit 4077073
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import static org.csstudio.display.builder.runtime.WidgetRuntime.logger;


import java.awt.geom.Rectangle2D;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Objects;
Expand Down Expand Up @@ -506,4 +508,23 @@ public void onClosed()

navigation.dispose();
}

@Override
public Optional<Rectangle2D> getPositionAndSizeHint() {
return Optional.ofNullable(active_model).flatMap(displayModel -> {
Integer width = displayModel.propWidth().getValue();
Integer height = displayModel.propHeight().getValue();
if(width != null && width > 0 && height != null && height > 0) {
return Optional.of(new Rectangle2D.Double(
displayModel.propX().getValue(),
displayModel.propY().getValue(),
width,
height
));
} else {
return Optional.empty();
}
});
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.phoebus.framework.spi;

import java.awt.geom.Rectangle2D;
import java.util.Optional;
import org.phoebus.framework.persistence.Memento;

public interface AppInstance {
Expand Down Expand Up @@ -38,4 +40,15 @@ public default void restore(Memento memento) {
public default void save(Memento memento) {
// Default does nothing
}

/**
* Get possible window size and position for the application instance, if defined
* (for example, Display Runtime may want the window to be the size defined in the display)
*
* (Note: Use Rectangle2D to avoid loading AWT toolkit; we only need a data container)
*
* @return Empty optional if no dimension is specified by the application instance.
*/
public default Optional<Rectangle2D> getPositionAndSizeHint() { return Optional.empty(); }

}
58 changes: 49 additions & 9 deletions core/ui/src/main/java/org/phoebus/ui/docking/DockItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,19 +447,46 @@ private void handleDragDone(final DragEvent event)
// but event.getX(), getSceneX(), getScreenX() are all 0.0.
// --> Using MouseInfo, which is actually AWT code
final Stage other = item.detach();
final PointerInfo pi = MouseInfo.getPointerInfo();
if (pi != null)
{
final Point loc = pi.getLocation();
other.setX(loc.getX());
other.setY(loc.getY());
}

// Try to use the x/y location of the stage using the dimension hint if it
// has been specified; Otherwise, use the pointer location
AppInstance application = this.getApplication();
application.getPositionAndSizeHint().ifPresentOrElse(dimension -> {
// If the x and y position are default values (zero) then
// use the cursor position
if (isDefaultPosition(dimension.getX()) && isDefaultPosition(dimension.getY())) {
setLocationToMousePointer(other);
}
// Otherwise use the position hint
else {
other.setX(dimension.getX());
other.setY(dimension.getY());
}
}, () -> {
setLocationToMousePointer(other);
});

}
event.consume();
}

private void setLocationToMousePointer(Stage stage) {
final PointerInfo pi = MouseInfo.getPointerInfo();
if (pi != null)
{
final Point loc = pi.getLocation();
stage.setX(loc.getX());
stage.setY(loc.getY());
}
}

private boolean isDefaultPosition(double pos) {
return pos > -1.0 && pos < 1.0;
}

private Stage detach()
{

// For size of new stage, approximate the
// current size of the item, i.e. the size
// of its DockPane, adding some extra space
Expand All @@ -479,8 +506,21 @@ private Stage detach()
other.setTitle(UUID.randomUUID().toString());

DockStage.configureStage(other, this);
other.setWidth(old_parent.getWidth() + extra_width);
other.setHeight(old_parent.getHeight() + extra_height);

// Set the dimensions of the stage using the dimension hint from the
// application (e.g. the width and height of the Display widget if
// it's the display application). Otherwise, use the parent window dimensions.
AppInstance application = this.getApplication();
application.getPositionAndSizeHint().ifPresentOrElse(dimension -> {
// use the dimension hints suggested by the application
// such as possibly Display widget dimensions
other.setWidth(dimension.getWidth() + extra_width);
other.setHeight(dimension.getHeight() + extra_height);
}, () -> {
// use the parent window dimensions if no dimension hints
other.setWidth(old_parent.getWidth() + extra_width);
other.setHeight(old_parent.getHeight() + extra_height);
});

// Assert that styles used in old scene are still available
for (String css : old_scene.getStylesheets())
Expand Down

0 comments on commit 4077073

Please sign in to comment.