Skip to content

Commit ba5ab1a

Browse files
committed
Dialog should float
- New WindowView which can be partially atop of background. - DialogView now extends WindowView - Change dialog in Catalog sample and Dialog scenario - Relates spring-projects#825 - Relates spring-projects#855
1 parent 88c1f56 commit ba5ab1a

File tree

5 files changed

+98
-6
lines changed

5 files changed

+98
-6
lines changed

spring-shell-core/src/main/java/org/springframework/shell/component/view/control/DialogView.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* @author Janne Valkealahti
3636
*/
37-
public class DialogView extends BoxView {
37+
public class DialogView extends WindowView {
3838

3939
private View content;
4040
private List<ButtonView> buttons;
@@ -92,7 +92,6 @@ public void setRect(int x, int y, int width, int height) {
9292
super.setRect(x, y, width, height);
9393

9494
Rectangle rect = getInnerRect();
95-
rect = new Rectangle(rect.x() + rect.width() / 4, rect.y() + rect.height() / 4, rect.width() / 2, rect.height() / 2);
9695
rect = new Rectangle(rect.x() + 1, rect.y() + 1, rect.width() - 2, rect.height() - 2);
9796
if (content != null) {
9897
content.setRect(rect.x(), rect.y(), rect.width(), rect.height() - 3);
@@ -111,7 +110,6 @@ public void setRect(int x, int y, int width, int height) {
111110
@Override
112111
protected void drawInternal(Screen screen) {
113112
Rectangle rect = getInnerRect();
114-
rect = new Rectangle(rect.x() + rect.width() / 4, rect.y() + rect.height() / 4, rect.width() / 2, rect.height() / 2);
115113
Writer writer = screen.writerBuilder().layer(getLayer()).build();
116114
writer.border(rect.x(), rect.y(), rect.width(), rect.height());
117115
if (content != null) {
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.component.view.control;
17+
18+
import org.springframework.shell.component.view.geom.Rectangle;
19+
import org.springframework.shell.component.view.screen.Screen;
20+
import org.springframework.shell.style.StyleSettings;
21+
22+
23+
/**
24+
* {@code WindowView} is a {@link View} defining area within view itself.
25+
*
26+
* @author Janne Valkealahti
27+
*/
28+
public class WindowView extends AbstractView {
29+
30+
private int backgroundColor = -1;
31+
private int minWidth = 30;
32+
private int maxWidth = 60;
33+
private int minHeight = 8;
34+
private int maxHeight = 12;
35+
36+
/**
37+
* Sets a background color. If color is set to {@code null} it indicates that
38+
* background should be set to be {@code empty} causing possible layer to be
39+
* non-transparent.
40+
*
41+
* @param backgroundColor the background color
42+
*/
43+
public void setBackgroundColor(int backgroundColor) {
44+
this.backgroundColor = backgroundColor;
45+
}
46+
47+
@Override
48+
protected void drawInternal(Screen screen) {
49+
Rectangle rect = getInnerRect();
50+
int bgColor = resolveThemeBackground(StyleSettings.TAG_BACKGROUND, backgroundColor, -1);
51+
screen.writerBuilder().layer(getLayer()).build().background(rect, bgColor);
52+
}
53+
54+
/**
55+
* Gets an inner rectangle of this view.
56+
*
57+
* @return an inner rectangle of this view
58+
*/
59+
protected Rectangle getInnerRect() {
60+
Rectangle rect = getRect();
61+
62+
int w;
63+
int x;
64+
if (rect.width() <= minWidth) {
65+
w = rect.width();
66+
x = rect.x();
67+
}
68+
else if (rect.width() >= maxWidth) {
69+
w = maxWidth;
70+
x = (rect.width() - w) / 2;
71+
}
72+
else {
73+
w = minWidth;
74+
x = (rect.width() - w) / 2;
75+
}
76+
77+
int h;
78+
int y;
79+
if (rect.height() <= minHeight) {
80+
h = rect.height();
81+
y = rect.y();
82+
}
83+
else if (rect.height() >= maxHeight) {
84+
h = maxHeight;
85+
y = (rect.height() - h) / 2;
86+
}
87+
else {
88+
h = rect.height();
89+
y = (rect.height() - h) / 2;
90+
}
91+
92+
rect = new Rectangle(x, y, w, h);
93+
return rect;
94+
}
95+
96+
}

spring-shell-core/src/test/java/org/springframework/shell/component/view/control/DialogViewTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void setup() {
6262

6363
@Test
6464
void handlesMouseClick() {
65-
MouseEvent click = mouseClick(3, 3);
65+
MouseEvent click = mouseClick(1, 6);
6666

6767
Flux<ButtonViewSelectEvent> actions1 = eventLoop
6868
.viewEvents(ButtonViewSelectEvent.class);

spring-shell-samples/spring-shell-sample-catalog/src/main/java/org/springframework/shell/samples/catalog/Catalog.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ private DialogView buildAboutDialog() {
295295
dialog.setEventLoop(eventLoop);
296296
dialog.setViewService(ui);
297297

298-
dialog.setTransparent(false);
299298
return dialog;
300299
}
301300

spring-shell-samples/spring-shell-sample-catalog/src/main/java/org/springframework/shell/samples/catalog/scenario/other/DialogScenario.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ private DialogView buildDialog() {
5151
});
5252
DialogView dialog = new DialogView(content, button);
5353
configure(dialog);
54-
dialog.setTransparent(false);
5554
dialog.setViewService(getViewService());
5655
return dialog;
5756
}

0 commit comments

Comments
 (0)