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

8313424: JavaFX controls in the title bar #1605

Open
wants to merge 59 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
3d19b87
WIP
mstr2 Oct 19, 2024
0ddd63d
doc change
mstr2 Oct 20, 2024
7969622
revert unintended change
mstr2 Oct 20, 2024
a289572
Merge branch 'master' into feature/extended-window
mstr2 Oct 23, 2024
ba02e8f
Improve HeaderBar documentation
mstr2 Oct 24, 2024
f973e8c
improve documentation
mstr2 Oct 25, 2024
f02e7e9
Windows: add system menu
mstr2 Oct 25, 2024
fef8cfc
Windows: custom context menu overrides system menu
mstr2 Oct 26, 2024
778e6c1
GTK: prevent resizing below window button size, fix crash
mstr2 Oct 26, 2024
3b468fe
GTK: add system menu
mstr2 Oct 26, 2024
0526edb
small code changes
mstr2 Oct 28, 2024
95736df
remove unused code
mstr2 Oct 28, 2024
d9c0fe2
Merge branch 'master' into feature/extended-window
mstr2 Oct 28, 2024
c0b588f
set minHeight to native height of title bar
mstr2 Oct 28, 2024
d7f88c3
better documentation
mstr2 Oct 28, 2024
9de4694
macOS: hide window title
mstr2 Oct 28, 2024
cd5d443
improve title text documentation
mstr2 Oct 28, 2024
bc48ae0
fix peer access outside of synchronizer
mstr2 Oct 28, 2024
804d0be
NPE
mstr2 Oct 28, 2024
f5e3121
fix header bar height flicker
mstr2 Oct 28, 2024
1c4ecc1
macOS: dynamically adapt toolbar style to headerbar height
mstr2 Oct 29, 2024
15dc3ff
Merge branch 'master' into feature/extended-window
mstr2 Oct 31, 2024
9b63892
Merge branch 'master' into feature/extended-window
mstr2 Nov 1, 2024
e7febc5
fix mirroring/unmirroring of X coord in win-glass
mstr2 Nov 5, 2024
d1c388b
stylistic changes
mstr2 Nov 5, 2024
8c9fbbd
use CsvSource in HeaderBarTest
mstr2 Nov 5, 2024
3660a29
EMPTY Dimension2D constant
mstr2 Nov 5, 2024
8974c14
HeaderBar changes
mstr2 Nov 5, 2024
ca9b325
refactor performWindowDrag
mstr2 Nov 6, 2024
8e77a22
HeaderBar javadoc change
mstr2 Nov 7, 2024
4336735
WindowControlsOverlay snapping
mstr2 Nov 7, 2024
a9178b7
add system menu documentation
mstr2 Nov 7, 2024
6a16536
Merge branch 'master' into feature/extended-window
mstr2 Nov 7, 2024
65f095e
Merge branch 'master' into feature/extended-window
mstr2 Nov 9, 2024
d5afc7d
Merge branch 'master' into feature/extended-window
mstr2 Nov 14, 2024
26b81b8
remove unneeded dll
mstr2 Nov 14, 2024
003e9d5
macOS: double-click action + fullscreen toolbar
mstr2 Nov 14, 2024
485a9d9
Merge branch 'master' into feature/extended-window
mstr2 Nov 14, 2024
743626f
Merge branch 'master' into feature/extended-window
mstr2 Dec 6, 2024
d9b82c8
Add HeaderBar.overlappingSystemInset property
mstr2 Dec 6, 2024
8649f91
StyleableBooleanProperty -> BooleanProperty
mstr2 Dec 6, 2024
af35dce
Merge branch 'master' into feature/extended-window
mstr2 Dec 10, 2024
6523073
fix system menu for non-resizeable extended window
mstr2 Dec 10, 2024
8d5d7b8
add StageStyle.EXTENDED_UTILITY
mstr2 Dec 10, 2024
eaafd9f
fix resizable states for GTK windows
mstr2 Dec 10, 2024
cbb3216
Merge branch 'master' into feature/extended-window
mstr2 Jan 15, 2025
63bd058
Refactoring, added HeaderBar.leadingSystemPadding/trailingSystemPadding
mstr2 Jan 25, 2025
8c33b3c
Merge branch 'master' into feature/extended-window
mstr2 Jan 25, 2025
49a81d3
added custom header buttons
mstr2 Jan 27, 2025
356a78e
fix line endings
mstr2 Jan 27, 2025
fcec7c8
update javadoc
mstr2 Jan 27, 2025
2ab1294
hide default window buttons on macOS
mstr2 Jan 27, 2025
8a73f5e
typo
mstr2 Jan 28, 2025
e874d21
add samples in MonkeyTester
mstr2 Jan 28, 2025
3efd782
small MonkeyTester refactor
mstr2 Jan 28, 2025
cadf1c2
added StageTester to MonkeyTester
mstr2 Feb 1, 2025
600c721
macOS bugfixes, default button behavior
mstr2 Feb 3, 2025
7cb591f
move StageTester to Tools menu
mstr2 Feb 4, 2025
c30eb60
add "maximized" pseudo-class for custom maximize button
mstr2 Feb 4, 2025
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 @@ -184,10 +184,12 @@ protected void onHeaderBarHeightChanged(double height) {
private void updateWindowOverlayMetrics(NSWindowToolbarStyle toolbarStyle) {
double minHeight = NSWindowToolbarStyle.SMALL.size.getHeight();
var empty = new Dimension2D(0, 0);
var size = isUtilityWindow() ? toolbarStyle.utilitySize : toolbarStyle.size;

WindowControlsMetrics metrics = isUsingNonClientOverlay()
? _isRightToLeftLayoutDirection()
? new WindowControlsMetrics(empty, toolbarStyle.size, minHeight)
: new WindowControlsMetrics(toolbarStyle.size, empty, minHeight)
? new WindowControlsMetrics(empty, size, minHeight)
: new WindowControlsMetrics(size, empty, minHeight)
: new WindowControlsMetrics(empty, empty, minHeight);

windowControlsMetrics.set(metrics);
Expand All @@ -200,10 +202,12 @@ private enum NSWindowToolbarStyle {

NSWindowToolbarStyle(double width, double height, int style) {
this.size = new Dimension2D(width, height);
this.utilitySize = new Dimension2D(height, height); // width intentionally set to height
this.style = style;
}

final Dimension2D size;
final Dimension2D utilitySize;
final int style;

static NSWindowToolbarStyle ofHeight(double height) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2025, 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.javafx.scene.layout;

import com.sun.javafx.PlatformUtil;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HeaderButtonType;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Subscription;
import java.util.Objects;
import java.util.Optional;

public final class HeaderButtonBehavior implements EventHandler<MouseEvent> {
Copy link
Contributor

Choose a reason for hiding this comment

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

it might be better to call this class other than "behavior", "handler" perhaps? mouse handler?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's a bit more than just a generic handler, it basically encapsulates the entire behavior of a window button (including setting its visibility and disabled states).

Copy link
Member

Choose a reason for hiding this comment

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

We generally use "Behavior" in the context of controls. Although not wrong, I also think another name might be better. Maybe "Controller"? Or "Manager"? Or ...


private final Node node;
private final HeaderButtonType type;
private final Subscription subscription;

public HeaderButtonBehavior(Node node, HeaderButtonType type) {
this.node = Objects.requireNonNull(node);
this.type = Objects.requireNonNull(type);

ObservableValue<Stage> stage = node.sceneProperty()
.flatMap(Scene::windowProperty)
.map(w -> w instanceof Stage s ? s : null);

subscription = Subscription.combine(
type == HeaderButtonType.MAXIMIZE
? stage.flatMap(Stage::resizableProperty).subscribe(this::onResizableChanged)
: Subscription.EMPTY,
stage.flatMap(Stage::fullScreenProperty).subscribe(this::onFullScreenChanged),
() -> node.removeEventHandler(MouseEvent.MOUSE_RELEASED, this)
);

node.addEventHandler(MouseEvent.MOUSE_RELEASED, this);
node.setFocusTraversable(false);
}

public void dispose() {
subscription.unsubscribe();
}

@Override
public void handle(MouseEvent event) {
if (!node.getLayoutBounds().contains(event.getX(), event.getY())) {
return;
}

switch (type) {
case CLOSE -> getStage().ifPresent(Stage::close);
case ICONIFY -> getStage().ifPresent(stage -> stage.setIconified(true));
case MAXIMIZE -> getStage().ifPresent(stage -> {
// On macOS, a non-modal window is put into full-screen mode when the maximize button is clicked,
// but enlarged to cover the desktop when the option key is pressed at the same time.
if (PlatformUtil.isMac() && stage.getModality() == Modality.NONE && !event.isAltDown()) {
stage.setFullScreen(!stage.isFullScreen());
} else {
stage.setMaximized(!stage.isMaximized());
}
});
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it might be better to add a default case here, in case this enum evolves.

Copy link
Member

Choose a reason for hiding this comment

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

Why? This isn't some external enum that might evolve out from under this code, but is part of the same feature. If the enum evolves then the resulting compiler error is a good thing as it alerts whoever added the new enum that they didn't finish implementing it.

A default makes sense when the enum might evolve separately from the use of the enum.

}
}

private Optional<Stage> getStage() {
Scene scene = node.getScene();
if (scene == null) {
return Optional.empty();
}

return scene.getWindow() instanceof Stage stage
? Optional.of(stage)
: Optional.empty();
}

private void onResizableChanged(Boolean resizable) {
if (!node.disableProperty().isBound()) {
node.setDisable(resizable == Boolean.FALSE);
}
}

private void onFullScreenChanged(Boolean fullScreen) {
if (!node.visibleProperty().isBound() && !node.managedProperty().isBound()) {
node.setVisible(fullScreen != Boolean.TRUE);
node.setManaged(fullScreen != Boolean.TRUE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3078,7 +3078,7 @@ public HeaderAreaType pickHeaderArea(double x, double y) {

if (HeaderBarBase.getHeaderButtonType(intersectedNode) instanceof HeaderButtonType type) {
return switch (type) {
case MINIMIZE -> HeaderAreaType.MINIMIZE;
case ICONIFY -> HeaderAreaType.MINIMIZE;
case MAXIMIZE -> HeaderAreaType.MAXIMIZE;
case CLOSE -> HeaderAreaType.CLOSE;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package javafx.scene.layout;

import com.sun.glass.ui.WindowControlsMetrics;
import com.sun.javafx.scene.layout.HeaderButtonBehavior;
import com.sun.javafx.stage.StageHelper;
import com.sun.javafx.tk.quantum.WindowStage;
import javafx.beans.property.ReadOnlyDoubleProperty;
Expand All @@ -36,6 +37,7 @@
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;
Expand Down Expand Up @@ -92,12 +94,26 @@ public static Boolean isDraggable(Node child) {

/**
* Specifies the {@code HeaderButtonType} of the child, indicating its semantic use in the header bar.
* <p>
* This property can be set on any {@link Node}. Specifying a header button type also provides the behavior
* associated with the button type. If the default behavior is not desired, applications can register an
* event filter on the child node that consumes the {@link MouseEvent#MOUSE_RELEASED} event.
*
* @param child the child node
* @param value the {@code HeaderButtonType}, or {@code null}
*/
public static void setHeaderButtonType(Node child, HeaderButtonType value) {
Pane.setConstraint(child, HEADER_BUTTON_TYPE, value);

if (child.getProperties().get(HeaderButtonBehavior.class) instanceof HeaderButtonBehavior behavior) {
behavior.dispose();
}

if (value != null) {
child.getProperties().put(HeaderButtonBehavior.class, new HeaderButtonBehavior(child, value));
} else {
child.getProperties().remove(HeaderButtonBehavior.class);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,32 @@
* Identifies the semantic type of a button in a custom {@link HeaderBar}, which enables integrations
* with the platform window manager. For example, hovering over a {@link #MAXIMIZE} button on Windows
* will summon snap layouts.
* <p>
* This property can be set on any {@link Node}. Applications are still required to provide their own
* click handlers to programmatically trigger the actions associated with header buttons, i.e. call
* appropriate stage methods like {@link Stage#setIconified(boolean)}, {@link Stage#setMaximized(boolean)}
* or {@link Stage#close()}.
*
* @since 25
* @see HeaderBarBase#setHeaderButtonType(Node, HeaderButtonType)
*/
public enum HeaderButtonType {

/**
* Identifies the minimize button.
* Identifies the iconify button.
*
* @see Stage#isIconified()
* @see Stage#setIconified(boolean)
*/
MINIMIZE,
ICONIFY,

/**
* Identifies the maximize button.
* <p>
* This button toggles the {@link Stage#isMaximized()} or {@link Stage#isFullScreen()} property,
* depending on platform-specific invocation semantics. For example, on macOS the button will
* put the window into full-screen mode by default, but maximize it to cover the desktop when
* the option key is pressed.
*
* @see Stage#isMaximized()
* @see Stage#setMaximized(boolean)
* @see Stage#isFullScreen()
* @see Stage#setFullScreen(boolean)
*/
MAXIMIZE,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ - (void)windowWillEnterFullScreen:(NSNotification *)notification
self->isWindowResizable = ((mask & NSWindowStyleMaskResizable) != 0);
[[self->view delegate] setResizableForFullscreen:YES];

// When we switch to full-screen mode, we always need the standard window buttons to be shown.
[[self->nsWindow standardWindowButton:NSWindowCloseButton] setHidden:NO];
[[self->nsWindow standardWindowButton:NSWindowMiniaturizeButton] setHidden:NO];
[[self->nsWindow standardWindowButton:NSWindowZoomButton] setHidden:NO];

if (nsWindow.toolbar != nil) {
nsWindow.toolbar.visible = NO;
}
Expand All @@ -210,6 +215,13 @@ - (void)windowDidEnterFullScreen:(NSNotification *)notification
- (void)windowWillExitFullScreen:(NSNotification *)notification
{
//NSLog(@"windowWillExitFullScreen");

// When we exit full-screen mode, hide the standard window buttons if they were previously hidden.
if (!self->isStandardButtonsVisible) {
[[self->nsWindow standardWindowButton:NSWindowCloseButton] setHidden:YES];
[[self->nsWindow standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES];
[[self->nsWindow standardWindowButton:NSWindowZoomButton] setHidden:YES];
}
}

- (void)windowDidExitFullScreen:(NSNotification *)notification
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2025, 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 @@ -50,6 +50,7 @@
BOOL isTransparent;
BOOL isDecorated;
BOOL isResizable;
BOOL isStandardButtonsVisible;
BOOL suppressWindowMoveEvent;
BOOL suppressWindowResizeEvent;

Expand Down
Loading