diff --git a/spring-shell-core/src/main/java/org/springframework/shell/component/view/control/DialogView.java b/spring-shell-core/src/main/java/org/springframework/shell/component/view/control/DialogView.java index dcc8255b..3dc6a069 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/component/view/control/DialogView.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/component/view/control/DialogView.java @@ -34,7 +34,7 @@ import org.springframework.shell.component.view.screen.Screen.Writer; * * @author Janne Valkealahti */ -public class DialogView extends BoxView { +public class DialogView extends WindowView { private View content; private List buttons; @@ -92,7 +92,6 @@ public class DialogView extends BoxView { super.setRect(x, y, width, height); Rectangle rect = getInnerRect(); - rect = new Rectangle(rect.x() + rect.width() / 4, rect.y() + rect.height() / 4, rect.width() / 2, rect.height() / 2); rect = new Rectangle(rect.x() + 1, rect.y() + 1, rect.width() - 2, rect.height() - 2); if (content != null) { content.setRect(rect.x(), rect.y(), rect.width(), rect.height() - 3); @@ -111,7 +110,6 @@ public class DialogView extends BoxView { @Override protected void drawInternal(Screen screen) { Rectangle rect = getInnerRect(); - rect = new Rectangle(rect.x() + rect.width() / 4, rect.y() + rect.height() / 4, rect.width() / 2, rect.height() / 2); Writer writer = screen.writerBuilder().layer(getLayer()).build(); writer.border(rect.x(), rect.y(), rect.width(), rect.height()); if (content != null) { diff --git a/spring-shell-core/src/main/java/org/springframework/shell/component/view/control/WindowView.java b/spring-shell-core/src/main/java/org/springframework/shell/component/view/control/WindowView.java new file mode 100644 index 00000000..26d5bc7d --- /dev/null +++ b/spring-shell-core/src/main/java/org/springframework/shell/component/view/control/WindowView.java @@ -0,0 +1,96 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.shell.component.view.control; + +import org.springframework.shell.component.view.geom.Rectangle; +import org.springframework.shell.component.view.screen.Screen; +import org.springframework.shell.style.StyleSettings; + + +/** + * {@code WindowView} is a {@link View} defining area within view itself. + * + * @author Janne Valkealahti + */ +public class WindowView extends AbstractView { + + private int backgroundColor = -1; + private int minWidth = 30; + private int maxWidth = 60; + private int minHeight = 8; + private int maxHeight = 12; + + /** + * Sets a background color. If color is set to {@code null} it indicates that + * background should be set to be {@code empty} causing possible layer to be + * non-transparent. + * + * @param backgroundColor the background color + */ + public void setBackgroundColor(int backgroundColor) { + this.backgroundColor = backgroundColor; + } + + @Override + protected void drawInternal(Screen screen) { + Rectangle rect = getInnerRect(); + int bgColor = resolveThemeBackground(StyleSettings.TAG_BACKGROUND, backgroundColor, -1); + screen.writerBuilder().layer(getLayer()).build().background(rect, bgColor); + } + + /** + * Gets an inner rectangle of this view. + * + * @return an inner rectangle of this view + */ + protected Rectangle getInnerRect() { + Rectangle rect = getRect(); + + int w; + int x; + if (rect.width() <= minWidth) { + w = rect.width(); + x = rect.x(); + } + else if (rect.width() >= maxWidth) { + w = maxWidth; + x = (rect.width() - w) / 2; + } + else { + w = minWidth; + x = (rect.width() - w) / 2; + } + + int h; + int y; + if (rect.height() <= minHeight) { + h = rect.height(); + y = rect.y(); + } + else if (rect.height() >= maxHeight) { + h = maxHeight; + y = (rect.height() - h) / 2; + } + else { + h = rect.height(); + y = (rect.height() - h) / 2; + } + + rect = new Rectangle(x, y, w, h); + return rect; + } + +} diff --git a/spring-shell-core/src/test/java/org/springframework/shell/component/view/control/DialogViewTests.java b/spring-shell-core/src/test/java/org/springframework/shell/component/view/control/DialogViewTests.java index 8e50148a..c21b57ab 100644 --- a/spring-shell-core/src/test/java/org/springframework/shell/component/view/control/DialogViewTests.java +++ b/spring-shell-core/src/test/java/org/springframework/shell/component/view/control/DialogViewTests.java @@ -62,7 +62,7 @@ class DialogViewTests extends AbstractViewTests { @Test void handlesMouseClick() { - MouseEvent click = mouseClick(3, 3); + MouseEvent click = mouseClick(1, 6); Flux actions1 = eventLoop .viewEvents(ButtonViewSelectEvent.class); diff --git a/spring-shell-samples/spring-shell-sample-catalog/src/main/java/org/springframework/shell/samples/catalog/Catalog.java b/spring-shell-samples/spring-shell-sample-catalog/src/main/java/org/springframework/shell/samples/catalog/Catalog.java index 9fcc5462..3a4da430 100644 --- a/spring-shell-samples/spring-shell-sample-catalog/src/main/java/org/springframework/shell/samples/catalog/Catalog.java +++ b/spring-shell-samples/spring-shell-sample-catalog/src/main/java/org/springframework/shell/samples/catalog/Catalog.java @@ -295,7 +295,6 @@ public class Catalog { dialog.setEventLoop(eventLoop); dialog.setViewService(ui); - dialog.setTransparent(false); return dialog; } diff --git a/spring-shell-samples/spring-shell-sample-catalog/src/main/java/org/springframework/shell/samples/catalog/scenario/other/DialogScenario.java b/spring-shell-samples/spring-shell-sample-catalog/src/main/java/org/springframework/shell/samples/catalog/scenario/other/DialogScenario.java index d18026b5..d53b463c 100644 --- a/spring-shell-samples/spring-shell-sample-catalog/src/main/java/org/springframework/shell/samples/catalog/scenario/other/DialogScenario.java +++ b/spring-shell-samples/spring-shell-sample-catalog/src/main/java/org/springframework/shell/samples/catalog/scenario/other/DialogScenario.java @@ -51,7 +51,6 @@ public class DialogScenario extends AbstractScenario { }); DialogView dialog = new DialogView(content, button); configure(dialog); - dialog.setTransparent(false); dialog.setViewService(getViewService()); return dialog; }