Skip to content

Commit

Permalink
fix login button session ready behaviour and size
Browse files Browse the repository at this point in the history
  • Loading branch information
francescotescari committed Mar 25, 2021
1 parent 0132db8 commit bac4554
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 19 deletions.
46 changes: 44 additions & 2 deletions src/main/java/com/xiaomitool/v2/gui/GuiObjects.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.xiaomitool.v2.gui;

import com.xiaomitool.v2.utility.MultiMap;
import javafx.scene.Node;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Function;

public class GuiObjects extends HashMap<String, Node> {
public static final String LOGIN_LINK = "login_link";
Expand All @@ -14,10 +18,48 @@ public class GuiObjects extends HashMap<String, Node> {
private static final GuiObjects instance = new GuiObjects();

public static Node getNode(String name) {
return instance.get(name);
synchronized (instance) {
return instance.get(name);
}
}

public static void set(String name, Node node) {
instance.put(name, node);
List<Function<Node, Boolean>> callbacks;
synchronized (instance) {
instance.put(name, node);
callbacks = ON_SET_CALLBACKS.get(name);
}
if (callbacks != null) {
List<Function<Node, Boolean>> toRemove = new ArrayList<>();
callbacks.forEach(fn -> {
if (fn.apply(node)) {
toRemove.add(fn);
}
});
synchronized (instance) {
toRemove.forEach(fn -> {
ON_SET_CALLBACKS.removeSingle(name, fn);
});
}
}

}

private static final MultiMap<String, Function<Node, Boolean>> ON_SET_CALLBACKS = new MultiMap<>();

public static void setOnSetCallback(String name, Function<Node, Boolean> callback) {
synchronized (instance) {
ON_SET_CALLBACKS.putSingle(name, callback);
}
}

public static void runOnReady(String name, Function<Node, Boolean> function) {
Node node = getNode(name);
if (node != null) {
function.apply(node);
} else {
setOnSetCallback(name, function);
}
}

}
36 changes: 23 additions & 13 deletions src/main/java/com/xiaomitool/v2/gui/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,30 @@ public static void setLoginNumber(String userId) {
if (userId == null) {
userId = "";
}
Label uidLabel = (Label) GuiObjects.getNode(GuiObjects.LOGIN_NUMBER);
if (uidLabel == null) {
Log.error("Cannot set userId label: null object");
return;
}
uidLabel.setText(userId);
final String uid = userId;

GuiObjects.runOnReady(GuiObjects.LOGIN_NUMBER, node -> {
Label uidLabel = (Label) node;
if (uidLabel == null) {
Log.error("Cannot set userId label: null object");
} else {
uidLabel.setText(uid);
}
return true;
});

loggedIn = !userId.isEmpty();
String text = loggedIn ? "Logout" : "Login";
Hyperlink link = (Hyperlink) GuiObjects.getNode(GuiObjects.LOGIN_LINK);
if (link == null) {
Log.error("Cannot set login hyperLink: null object");
return;
}
link.setText(text);
final String text = loggedIn ? LRes.LOGOUT.toString() : LRes.LOGIN.toString();
GuiObjects.runOnReady(GuiObjects.LOGIN_LINK, node -> {
Hyperlink link = (Hyperlink) node;
if (link == null) {
Log.error("Cannot set login hyperLink: null object");
} else {
link.setText(text);
}
return true;
});

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public static MainWindowController getInstance() {

@FXML
protected void initialize() {
saveNodes();
initHeaderDrag(HEADER);
setSettingsImage(IMG_SETTINGS);
setCloseImage(IMG_CLOSE, true);
Expand All @@ -59,6 +58,7 @@ protected void initialize() {
initDisclaimer();
initTranslateClick();
initText();
saveNodes();
instance = this;
}

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/xiaomitool/v2/gui/fxml/MainFrame.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@
</Label>
<HBox alignment="CENTER_RIGHT" prefHeight="40.0" prefWidth="431.0">
<children>
<Label fx:id="LOGIN_NUMBER" alignment="CENTER_RIGHT" prefHeight="21.0"
prefWidth="127.0">
<Label fx:id="LOGIN_NUMBER" alignment="CENTER_RIGHT" prefHeight="21.0">
<font>
<Font size="15.0"/>
</font>
</Label>
<Hyperlink fx:id="LOGIN_LINK" alignment="CENTER_RIGHT" prefHeight="29.0"
prefWidth="300.0" style="-fx-border-color: transparent;" text="Login">
style="-fx-border-color: transparent;" text="Login">
<font>
<Font size="15.0"/>
</font>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/xiaomitool/v2/language/LRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum LRes {
SETTINGS_RESET("Reset default settings"),
CHOOSE("Choose"),
LOGIN("Login"),
LOGOUT("Logout"),
TRANSLATED_BY("Translated by"),
TRANSLATOR(""),
TRANSLATOR_URL("https://www.xiaomitool.com"),
Expand Down

0 comments on commit bac4554

Please sign in to comment.