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

8271557: Undecorated interactive stage style #594

Closed
wants to merge 5 commits into from
Closed
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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -27,6 +27,7 @@
import com.sun.glass.events.KeyEvent;
import com.sun.glass.ui.CommonDialogs.ExtensionFilter;
import com.sun.glass.ui.CommonDialogs.FileChooserResult;
import javafx.stage.WindowRegionClassifier;

import java.io.File;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -567,7 +568,7 @@ public void menuAboutAction() {
* allowed to be of exactly one visual kind, and exactly one functional
* type.
*/
public abstract Window createWindow(Window owner, Screen screen, int styleMask);
public abstract Window createWindow(Window owner, Screen screen, WindowRegionClassifier classifier, int styleMask);

/**
* Create a window.
Expand All @@ -579,8 +580,8 @@ public void menuAboutAction() {
* allowed to be of exactly one visual kind, and exactly one functional
* type.
*/
public final Window createWindow(Screen screen, int styleMask) {
return createWindow(null, screen, styleMask);
public final Window createWindow(Screen screen, WindowRegionClassifier classifier, int styleMask) {
return createWindow(null, screen, classifier, styleMask);
}

public abstract Window createWindow(long parent);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.sun.glass.ui;

import com.sun.glass.events.MouseEvent;
import javafx.scene.Node;
import javafx.stage.WindowRegion;
import javafx.stage.WindowRegionClassifier;

import java.util.HashMap;
import java.util.Map;

public class MoveResizeHelper {

private static final Map<WindowRegion, Cursor> RESIZE_CURSORS = new HashMap<>();

static {
Application application = Application.GetApplication();
RESIZE_CURSORS.put(WindowRegion.TOP_LEFT, application.createCursor(Cursor.CURSOR_RESIZE_NORTHWEST));
RESIZE_CURSORS.put(WindowRegion.TOP_RIGHT, application.createCursor(Cursor.CURSOR_RESIZE_NORTHEAST));
RESIZE_CURSORS.put(WindowRegion.BOTTOM_LEFT, application.createCursor(Cursor.CURSOR_RESIZE_SOUTHWEST));
RESIZE_CURSORS.put(WindowRegion.BOTTOM_RIGHT, application.createCursor(Cursor.CURSOR_RESIZE_SOUTHEAST));
RESIZE_CURSORS.put(WindowRegion.LEFT, application.createCursor(Cursor.CURSOR_RESIZE_LEFTRIGHT));
RESIZE_CURSORS.put(WindowRegion.RIGHT, application.createCursor(Cursor.CURSOR_RESIZE_LEFTRIGHT));
RESIZE_CURSORS.put(WindowRegion.TOP, application.createCursor(Cursor.CURSOR_RESIZE_UPDOWN));
RESIZE_CURSORS.put(WindowRegion.BOTTOM, application.createCursor(Cursor.CURSOR_RESIZE_UPDOWN));
}

private final View view;
private final Window window;
private final WindowRegionClassifier regionClassifier;
private int mouseDownX, mouseDownY;
private int mouseDownWindowX, mouseDownWindowY;
private int mouseDownWindowWidth, mouseDownWindowHeight;
private WindowRegion currentWindowRegion;
private Cursor lastCursor;

public MoveResizeHelper(View view, Window window) {
this.view = view;
this.window = window;
this.regionClassifier = window.regionClassifier;
}

public final boolean handleMouseEvent(int type, int button, int x, int y, int xAbs, int yAbs) {
int wx = (int)(x / window.getPlatformScaleX());
int wy = (int)(y / window.getPlatformScaleY());

if (type != MouseEvent.DRAG) {
var eventHandler = view.getEventHandler();
Node pickedNode = eventHandler != null ? eventHandler.pickNode(wx, wy) : null;
currentWindowRegion = regionClassifier.classify(wx, wy, pickedNode);
updateCursor(currentWindowRegion);

if (currentWindowRegion == WindowRegion.CLIENT) {
return false;
}
}

switch (type) {
case MouseEvent.DRAG:
handleMouseDrag(button, xAbs, yAbs, currentWindowRegion);
break;
case MouseEvent.DOWN:
handleMouseDown(xAbs, yAbs);
break;
}

return false;
}

protected boolean shouldStartMoveDrag(int button, WindowRegion region) {
return button == MouseEvent.BUTTON_LEFT && region == WindowRegion.TITLE;
}

protected boolean shouldStartResizeDrag(int button, WindowRegion region) {
return button == MouseEvent.BUTTON_LEFT && RESIZE_CURSORS.get(region) != null;
}

private void handleMouseDrag(int button, int xAbs, int yAbs, WindowRegion region) {
if (shouldStartMoveDrag(button, region)) {
handleMoveWindow(xAbs, yAbs);
} else if (shouldStartResizeDrag(button, region)) {
handleResizeWindow(xAbs, yAbs, region);
}
}

private void handleMouseDown(int xAbs, int yAbs) {
mouseDownX = xAbs;
mouseDownY = yAbs;
mouseDownWindowX = window.getX();
mouseDownWindowY = window.getY();
mouseDownWindowWidth = window.getWidth();
mouseDownWindowHeight = window.getHeight();
}

private void handleMoveWindow(int xAbs, int yAbs) {
window.setPosition(mouseDownWindowX + xAbs - mouseDownX, mouseDownWindowY + yAbs - mouseDownY);
}
Comment on lines +119 to +121
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Linux this will be constrained by the window manager, so it's not possible to move the window outside of the desktop bounds, including panels.

This can be done by implementing a beginMoveDrag on glass that will call (on linux):
gdk_window_begin_move_drag


private void handleResizeWindow(int xAbs, int yAbs, WindowRegion region) {
int dx = xAbs - mouseDownX;
int dy = yAbs - mouseDownY;

switch (region) {
case LEFT:
adjustWindowPosition(dx, 0);
adjustWindowSize(-dx, 0);
break;
case RIGHT:
adjustWindowSize(dx, 0);
break;
case TOP:
adjustWindowPosition(0, dy);
adjustWindowSize(0, -dy);
break;
case BOTTOM:
adjustWindowSize(0, dy);
break;
case TOP_LEFT:
adjustWindowPosition(dx, dy);
adjustWindowSize(-dx, -dy);
break;
case TOP_RIGHT:
adjustWindowPosition(0, dy);
adjustWindowSize(dx, -dy);
break;
case BOTTOM_LEFT:
adjustWindowPosition(dx, 0);
adjustWindowSize(-dx, dy);
break;
case BOTTOM_RIGHT:
adjustWindowSize(dx, dy);
break;
}
}

private void adjustWindowPosition(int dx, int dy) {
int unclampedWidth = mouseDownWindowWidth - dx;
int unclampedHeight = mouseDownWindowHeight - dy;
int clampedWidth = dx != 0 ? clampWidth(unclampedWidth) : unclampedWidth;
int clampedHeight = dy != 0 ? clampHeight(unclampedHeight) : unclampedHeight;
int cx = unclampedWidth - clampedWidth;
int cy = unclampedHeight - clampedHeight;
window.setPosition(mouseDownWindowX + dx + cx, mouseDownWindowY + dy + cy);
}

private void adjustWindowSize(int dx, int dy) {
int width = dx != 0 ? clampWidth(mouseDownWindowWidth + dx) : mouseDownWindowWidth;
int height = dy != 0 ? clampHeight(mouseDownWindowHeight + dy) : mouseDownWindowHeight;
window.setSize(width, height);
}

private int clampWidth(int width) {
return Math.max(window.getMinimumWidth(), Math.min(window.getMaximumWidth(), width));
}

private int clampHeight(int height) {
return Math.max(window.getMinimumHeight(), Math.min(window.getMaximumHeight(), height));
}

private void updateCursor(WindowRegion region) {
Cursor newCursor = RESIZE_CURSORS.get(region);

if (lastCursor == null && newCursor != null) {
lastCursor = window.getCursor();
} else if (lastCursor != null && newCursor == null) {
window.setCursor(lastCursor);
lastCursor = null;
}

if (newCursor != null) {
window.setCursor(newCursor);
}
}

}
28 changes: 20 additions & 8 deletions modules/javafx.graphics/src/main/java/com/sun/glass/ui/View.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -26,6 +26,7 @@

import com.sun.glass.events.MouseEvent;
import com.sun.glass.events.ViewEvent;
import javafx.scene.Node;

import java.lang.annotation.Native;
import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -367,6 +368,10 @@ public void handleSwipeGestureEvent(View view, long time, int type,
public Accessible getSceneAccessible() {
return null;
}

public Node pickNode(double x, double y) {
return null;
}
}

public static long getMultiClickTime() {
Expand Down Expand Up @@ -394,6 +399,7 @@ protected void _finishInputMethodComposition(long ptr) {
*/
private volatile long ptr; // Native handle (NSView*, or internal structure pointer)
private Window window; // parent window
private MoveResizeHelper moveResizeHelper;
private EventHandler eventHandler;

private int width = -1; // not set
Expand Down Expand Up @@ -499,6 +505,16 @@ void setWindow(Window window) {
this.window = window;
_setParent(this.ptr, window == null ? 0L : window.getNativeHandle());
this.isValid = this.ptr != 0 && window != null;

if (isValid && window.regionClassifier != null && !window.isDecorated() && window.isInteractive()) {
this.moveResizeHelper = getMoveResizeHelper();
} else {
this.moveResizeHelper = null;
}
}

protected MoveResizeHelper getMoveResizeHelper() {
return new MoveResizeHelper(this, window);
}

// package private
Expand Down Expand Up @@ -908,13 +924,9 @@ protected void notifyMenu(int x, int y, int xAbs, int yAbs, boolean isKeyboardTr
protected void notifyMouse(int type, int button, int x, int y, int xAbs,
int yAbs, int modifiers, boolean isPopupTrigger,
boolean isSynthesized) {
// gznote: optimize - only call for undecorated Windows!
if (this.window != null) {
// handled by window (programmatical move/resize)
if (this.window.handleMouseEvent(type, button, x, y, xAbs, yAbs)) {
// The evnet has been processed by Glass
return;
}
// If we have a move-resize helper, we give it the first chance to handle the event.
if (moveResizeHelper != null && moveResizeHelper.handleMouseEvent(type, button, x, y, xAbs, yAbs)) {
return;
}

long now = System.nanoTime();
Expand Down
Loading