Skip to content

Commit

Permalink
feat: Config notification more.
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Nov 26, 2023
1 parent 21391e7 commit 4dcb2bc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
29 changes: 19 additions & 10 deletions src/main/java/viewtify/ui/toast/Toast.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,22 @@

public class Toast extends Preferences {

/** The margin for each notifications. */
private static final int MARGIN = 15;

/** The base transparent window. */
private static final LinkedList<Notification> notifications = new LinkedList();

public static final ToastSetting setting = Preferences.of(ToastSetting.class);

static {
setting.gap.observe().to(Toast::layoutNotifications);
setting.area.observe().to(Toast::layoutNotifications);
setting.screen.observe().to(Toast::layoutNotifications);
setting.opacity.observe().to(v -> {
for (Notification notification : notifications) {
notification.ui().getContent().get(0).setOpacity(v / 100d);
}
});
}

/**
* Show the specified node.
*/
Expand Down Expand Up @@ -120,33 +128,34 @@ private static void remove(Notification notification) {
*/
private static void layoutNotifications() {
Viewtify.inUI(() -> {
int gap = setting.gap.exact();
Rectangle2D rect = setting.screen.v.select();

// use viewtify notification
boolean isTopSide = setting.area.v.isTopSide();
double x = setting.area.v.isLeftSide() ? rect.getMinX() + MARGIN : rect.getMaxX() - setting.width.v - MARGIN;
double y = isTopSide ? rect.getMinY() + MARGIN : rect.getMaxY();
double x = setting.area.v.isLeftSide() ? rect.getMinX() + gap : rect.getMaxX() - setting.width.v - gap;
double y = isTopSide ? rect.getMinY() + gap : rect.getMaxY();

Iterator<Notification> iterator = isTopSide ? notifications.descendingIterator() : notifications.iterator();
while (iterator.hasNext()) {
Toast.Notification notify = iterator.next();
Popup popup = notify.ui();

if (popup.isShowing()) {
if (!isTopSide) y -= popup.getHeight() + MARGIN;
if (!isTopSide) y -= popup.getHeight() + gap;
popup.setX(x);
Animatable.play(setting.animation.v, notify.y, y);
} else {
popup.setOpacity(0);
popup.show(Window.getWindows().get(0));
if (!isTopSide) y -= popup.getHeight() + MARGIN;
if (!isTopSide) y -= popup.getHeight() + gap;
popup.setX(x);
popup.setY(y);

Animatable.play(setting.animation.v, popup.opacityProperty(), 1);
}

if (isTopSide) y += popup.getHeight() + MARGIN;
if (isTopSide) y += popup.getHeight() + gap;
}
});
}
Expand Down Expand Up @@ -199,7 +208,7 @@ private synchronized Popup ui() {
StyleHelper.of(box).style(Styles.popup);
box.setMaxWidth(setting.width.v);
box.setMinWidth(setting.width.v);
box.setOpacity(setting.opacity.v);
box.setOpacity(setting.opacity.v / 100d);

ui.setX(0);
ui.getContent().add(box);
Expand All @@ -221,7 +230,7 @@ private synchronized Popup ui() {
private static interface Styles extends StyleDSL {

Style popup = () -> {
padding.vertical(MARGIN, px).horizontal(MARGIN, px);
padding.vertical(15, px).horizontal(15, px);
background.color("derive(-fx-control-inner-background, 10%)");
border.radius(5, px).color("-fx-light-text-color");
};
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/viewtify/ui/toast/ToastSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package viewtify.ui.toast;

import javafx.util.Duration;

import viewtify.model.Preferences;
import viewtify.util.Corner;
import viewtify.util.ScreenSelector;
Expand All @@ -29,7 +30,7 @@ public class ToastSetting extends Preferences {
public final Preference<Duration> animation = initialize(Duration.millis(333)).requireMin(Duration.ONE);

/** The automatic hiding time. */
public final Preference<Duration> autoHide = initialize(Duration.seconds(15)).requireMin(Duration.ZERO);
public final Preference<Duration> autoHide = initialize(Duration.seconds(20)).requireMin(Duration.ZERO);

/** The notification area. */
public final Preference<Corner> area = initialize(Corner.BottomRight);
Expand All @@ -38,8 +39,11 @@ public class ToastSetting extends Preferences {
public final Preference<ScreenSelector> screen = initialize(ScreenSelector.InWindow);

/** The opacity of notification area. */
public final Preference<Double> opacity = initialize(0.9).requireBetween(0, 1);
public final Preference<Integer> opacity = initialize(90).requireBetween(0, 100);

/** The width of notification area. */
public final Preference<Integer> width = initialize(250).requireMin(50);

/** The gap of notification area. */
public final Preference<Integer> gap = initialize(15).requireBetween(0, 30);
}
17 changes: 17 additions & 0 deletions src/main/java/viewtify/ui/toast/ToastSettingView.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import java.util.stream.IntStream;

import javafx.util.Duration;

import viewtify.model.Preferences;
import viewtify.ui.UIButton;
import viewtify.ui.UICheckBox;
import viewtify.ui.UIComboBox;
import viewtify.ui.UISpinner;
Expand All @@ -38,6 +40,15 @@ public class ToastSettingView extends View {
/** The desktop configuration UI. */
private UISpinner<Integer> notificationMax;

/** The desktop configuration UI. */
private UISpinner<Integer> notificationGap;

/** The desktop configuration UI. */
private UISpinner<Integer> notificationOpacity;

/** Execute notification for test. */
private UIButton notificationTest;

/**
* {@inheritDoc}
*/
Expand All @@ -50,6 +61,9 @@ protected ViewDSL declareUI() {
form(en("Notification Location"), notificationArea);
form(en("Notification Duration"), notificationDuration);
form(en("Max number of Notifications"), notificationMax);
form(en("Notification area spacing"), notificationGap);
form(en("Notification area opacity"), notificationOpacity);
form(en("Test notification"), notificationTest);
});
}
};
Expand All @@ -70,5 +84,8 @@ protected void initialize() {
.sync(setting.autoHide)
.format(d -> String.valueOf((int) d.toSeconds()) + en("sec"))
.disableWhen(enableNotification.isNotSelected());
notificationGap.items(IntStream.range(0, 31)).sync(setting.gap).format(x -> x + "px");
notificationOpacity.items(IntStream.range(0, 101)).sync(setting.opacity).format(x -> x + "%");
notificationTest.text(en("Notify")).action(() -> Toast.show("Test"));
}
}

0 comments on commit 4dcb2bc

Please sign in to comment.