Skip to content

Commit

Permalink
Improve handling of menus.
Browse files Browse the repository at this point in the history
  • Loading branch information
io7m committed Nov 23, 2023
1 parent 87fd647 commit 646ca4f
Show file tree
Hide file tree
Showing 25 changed files with 104 additions and 42 deletions.
2 changes: 1 addition & 1 deletion com.io7m.jsycamore.api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.io7m.jsycamore</groupId>
<artifactId>com.io7m.jsycamore</artifactId>
<version>0.1.0</version>
<version>0.2.0-SNAPSHOT</version>
</parent>
<artifactId>com.io7m.jsycamore.api</artifactId>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@ PAreaSizeI<SySpaceParentRelativeType> minimumSizeRequired(
*/

SyMenuType menu();

/**
* @return {@code true} if this menu has a descendant that is currently under the mouse cursor
*/

boolean isMouseOverMenuDescendant();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.io7m.jorchard.core.JOTreeNodeType;
import com.io7m.jsycamore.api.components.SyComponentType;

import java.util.List;

/**
* A menu.
*
Expand Down Expand Up @@ -77,4 +79,10 @@ SyMenuItemSubmenuType addSubmenu(

@Override
AttributeType<Boolean> expanded();

/**
* @return An immutable snapshot of the items within the menu
*/

List<SyMenuItemType> items();
}
2 changes: 1 addition & 1 deletion com.io7m.jsycamore.awt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>com.io7m.jsycamore</groupId>
<artifactId>com.io7m.jsycamore</artifactId>
<version>0.1.0</version>
<version>0.2.0-SNAPSHOT</version>
</parent>
<artifactId>com.io7m.jsycamore.awt</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion com.io7m.jsycamore.components.standard/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.io7m.jsycamore</groupId>
<artifactId>com.io7m.jsycamore</artifactId>
<version>0.1.0</version>
<version>0.2.0-SNAPSHOT</version>
</parent>
<artifactId>com.io7m.jsycamore.components.standard</artifactId>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@

public final class SyMenu extends SyComponentAbstract implements SyMenuType
{
private static final int MENU_ICON_SIZE = 24;
private static final int MENU_SUBMENU_ICON_SIZE = 12;
private static final int MENU_SUBMENU_ICON_END_PADDING = 4;

private final SyPackVertical align;
private final AttributeType<Boolean> expanded;
private final JOTreeNodeType<SyMenuType> menuNode;
Expand All @@ -72,11 +76,22 @@ public SyMenu(
super(themeClasses);

this.items = List.of();

/*
* Menu items use four columns.
*
* 1. The menu icon (maybe invisible)
* 2. The flexible space for the menu item text
* 3. An icon used to indicate a submenu
* 4. Some padding.
*/

this.columns =
SyFormColumnsConfiguration.columns(
exact(24),
exact(MENU_ICON_SIZE),
flexible(),
exact(12)
exact(MENU_SUBMENU_ICON_SIZE),
exact(MENU_SUBMENU_ICON_END_PADDING)
);

final var attributes = SyComponentAttributes.get();
Expand Down Expand Up @@ -116,7 +131,13 @@ public PAreaSizeI<SySpaceParentRelativeType> layout(
var itemMaxWidth = 0;
var itemTotalY = 0;

final var menuItems = this.itemsNow();
/*
* Iterate over all the menu items and determine the total size on the Y
* axis needed for the menu, and the maximum width needed to display the
* longest menu item.
*/

final var menuItems = this.items();
for (final var menuItem : menuItems) {
final var itemSize =
menuItem.minimumSizeRequired(layoutContext);
Expand All @@ -125,6 +146,10 @@ public PAreaSizeI<SySpaceParentRelativeType> layout(
itemMaxWidth = Math.max(itemMaxWidth, itemSize.sizeX());
}

/*
* Add some padding.
*/

itemMaxWidth += 16;

final var subConstraints =
Expand Down Expand Up @@ -197,15 +222,16 @@ public AttributeType<Boolean> expanded()
}

@Override
protected SyEventConsumed onEvent(
final SyEventType event)
public List<SyMenuItemType> items()
{
return EVENT_NOT_CONSUMED;
return List.copyOf(this.items);
}

private List<SyMenuItemType> itemsNow()
@Override
protected SyEventConsumed onEvent(
final SyEventType event)
{
return this.items;
return EVENT_NOT_CONSUMED;
}

private <T extends SyMenuItemType> T addMenuItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public final class SyMenuItemAtom
private final SyTextView text;
private final Runnable action;
private final SyImageView icon;
private final SySpace space;
private final SyFormRow row;
private final SyFormColumnsConfiguration columns;
private final SyAlign textAlign;
Expand Down Expand Up @@ -90,8 +89,6 @@ public SyMenuItemAtom(
this.action =
Objects.requireNonNull(inAction, "action");

this.space = new SySpace();

this.row = new SyFormRow(this.columns);

this.icon = new SyImageView();
Expand All @@ -113,7 +110,8 @@ public SyMenuItemAtom(

this.row.childAdd(this.iconAlign);
this.row.childAdd(this.textAlign);
this.row.childAdd(this.space);
this.row.childAdd(new SySpace());
this.row.childAdd(new SySpace());

this.childAdd(this.row);
}
Expand Down Expand Up @@ -171,6 +169,12 @@ public SyMenuType menu()
return this.menu;
}

@Override
public boolean isMouseOverMenuDescendant()
{
return this.isMouseOver();
}

@Override
protected SyEventConsumed onEvent(
final SyEventType event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ public SyMenuType menu()
{
return this.menu;
}

@Override
public boolean isMouseOverMenuDescendant()
{
return this.isMouseOver();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.io7m.jsycamore.api.events.SyEventType;
import com.io7m.jsycamore.api.layout.SyLayoutContextType;
import com.io7m.jsycamore.api.menus.SyMenuItemSubmenuType;
import com.io7m.jsycamore.api.menus.SyMenuItemType;
import com.io7m.jsycamore.api.menus.SyMenuType;
import com.io7m.jsycamore.api.mouse.SyMouseEventOnOver;
import com.io7m.jsycamore.api.spaces.SySpaceParentRelativeType;
Expand All @@ -32,15 +33,14 @@
import com.io7m.jsycamore.components.standard.SyAlign;
import com.io7m.jsycamore.components.standard.SyComponentAbstract;
import com.io7m.jsycamore.components.standard.SyImageView;
import com.io7m.jsycamore.components.standard.SySpace;
import com.io7m.jsycamore.components.standard.SyTextView;
import com.io7m.jsycamore.components.standard.forms.SyFormColumnsConfiguration;
import com.io7m.jsycamore.components.standard.forms.SyFormRow;
import com.io7m.jtensors.core.parameterized.vectors.PVector2I;

import java.net.URI;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static com.io7m.jsycamore.api.events.SyEventConsumed.EVENT_CONSUMED;
import static com.io7m.jsycamore.api.events.SyEventConsumed.EVENT_NOT_CONSUMED;
Expand All @@ -62,7 +62,7 @@ public final class SyMenuItemSubmenu
private final SyFormColumnsConfiguration columns;
private final SyFormRow row;
private final SyImageView icon;
private final SyImageView iconSubmenu;
private final SyTextView iconSubmenu;
private final SyMenuType menuOpen;
private final SyMenuType menuOwner;
private final SyTextView text;
Expand Down Expand Up @@ -93,14 +93,12 @@ public SyMenuItemSubmenu(

this.row = new SyFormRow(this.columns);

this.iconSubmenu = new SyImageView();
this.iconSubmenu = new SyTextView(List.of(MENU_ITEM_TEXT));
this.iconSubmenu.setMouseQueryAccepting(false);
this.iconSubmenu.sizeUpperLimit().set(PAreaSizeI.of(8, 8));
this.iconSubmenu.imageURI()
.set(Optional.of(URI.create("jsycamore:icon:menu_submenu")));
this.iconSubmenu.text().set("→");

this.iconSubmenuAlign = new SyAlign();
this.iconSubmenuAlign.alignmentHorizontal().set(ALIGN_HORIZONTAL_LEFT);
this.iconSubmenuAlign.alignmentHorizontal().set(ALIGN_HORIZONTAL_CENTER);
this.iconSubmenuAlign.alignmentVertical().set(ALIGN_VERTICAL_CENTER);
this.iconSubmenuAlign.childAdd(this.iconSubmenu);

Expand All @@ -124,6 +122,7 @@ public SyMenuItemSubmenu(
this.row.childAdd(this.iconAlign);
this.row.childAdd(this.textAlign);
this.row.childAdd(this.iconSubmenuAlign);
this.row.childAdd(new SySpace());

this.childAdd(this.row);
}
Expand Down Expand Up @@ -181,6 +180,19 @@ public SyMenuType menu()
return this.menuOwner;
}

@Override
public boolean isMouseOverMenuDescendant()
{
if (this.isMouseOver()) {
return true;
}

return this.submenu()
.items()
.stream()
.anyMatch(SyMenuItemType::isMouseOverMenuDescendant);
}

@Override
public SyMenuType submenu()
{
Expand Down
2 changes: 1 addition & 1 deletion com.io7m.jsycamore.font.dejavu/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.io7m.jsycamore</groupId>
<artifactId>com.io7m.jsycamore</artifactId>
<version>0.1.0</version>
<version>0.2.0-SNAPSHOT</version>
</parent>
<artifactId>com.io7m.jsycamore.font.dejavu</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion com.io7m.jsycamore.font.york/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.io7m.jsycamore</groupId>
<artifactId>com.io7m.jsycamore</artifactId>
<version>0.1.0</version>
<version>0.2.0-SNAPSHOT</version>
</parent>
<artifactId>com.io7m.jsycamore.font.york</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion com.io7m.jsycamore.generation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>com.io7m.jsycamore</groupId>
<artifactId>com.io7m.jsycamore</artifactId>
<version>0.1.0</version>
<version>0.2.0-SNAPSHOT</version>
</parent>
<artifactId>com.io7m.jsycamore.generation</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion com.io7m.jsycamore.tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.io7m.jsycamore</groupId>
<artifactId>com.io7m.jsycamore</artifactId>
<version>0.1.0</version>
<version>0.2.0-SNAPSHOT</version>
</parent>
<artifactId>com.io7m.jsycamore.tests</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion com.io7m.jsycamore.theme.primal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.io7m.jsycamore</groupId>
<artifactId>com.io7m.jsycamore</artifactId>
<version>0.1.0</version>
<version>0.2.0-SNAPSHOT</version>
</parent>
<artifactId>com.io7m.jsycamore.theme.primal</artifactId>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ private static SyRenderNodeType imageOf(
case "icon:window_menu" -> {
yield iconOf(size, "window_menu.png");
}
case "icon:menu_submenu" -> {
yield iconOf(size, "menu_submenu.png");
}
case "icon:menu_submenu_selected" -> {
yield iconOf(size, "menu_submenu_selected.png");
}
default -> SyRenderNodeNoop.noop();
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public SyRenderNodeType render(
try {
final var values = this.theme().values();

if (component instanceof SyMenuBarItemType barItem) {
if (component instanceof final SyMenuBarItemType barItem) {
final var rectangle =
new SyShapeRectangle<SySpaceComponentRelativeType>(
PAreasI.cast(PAreasI.moveToOrigin(component.boundingArea()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,16 @@ public SyRenderNodeType render(
return SyRenderNodeNoop.noop();
}

if (component instanceof SyMenuItemType) {
if (component instanceof final SyMenuItemType menuItem) {
final var theme = this.theme();
final var values = theme.values();
if (component.isMouseOver()) {

/*
* The menu item should be highlighted if the cursor is over this
* item, or a descendant of this item.
*/

if (menuItem.isMouseOverMenuDescendant()) {
try {
return new SyRenderNodeShape(
Optional.of(values.edgeFlat(PRIMARY_EDGE)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public SyRenderNodeType render(
);
}

if (menuItem.isMouseOver()) {
if (menuItem.isMouseOverMenuDescendant()) {
return new SyRenderNodeText(
values.fillFlat(PRIMARY_BACKGROUND),
textSize,
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion com.io7m.jsycamore.theme.spi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.io7m.jsycamore</groupId>
<artifactId>com.io7m.jsycamore</artifactId>
<version>0.1.0</version>
<version>0.2.0-SNAPSHOT</version>
</parent>
<artifactId>com.io7m.jsycamore.theme.spi</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion com.io7m.jsycamore.vanilla/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.io7m.jsycamore</groupId>
<artifactId>com.io7m.jsycamore</artifactId>
<version>0.1.0</version>
<version>0.2.0-SNAPSHOT</version>
</parent>
<artifactId>com.io7m.jsycamore.vanilla</artifactId>

Expand Down
Loading

0 comments on commit 646ca4f

Please sign in to comment.