Move context menu from ChatScene globally to ChatControl specific
Additionally fixed a small bug in UserCreationProcessor and when deleting a contact offline
This commit is contained in:
@ -1,12 +1,13 @@
|
||||
package envoy.client.ui.control;
|
||||
|
||||
import javafx.geometry.*;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.layout.*;
|
||||
|
||||
import envoy.client.data.*;
|
||||
import envoy.client.util.IconUtil;
|
||||
import envoy.client.util.*;
|
||||
import envoy.data.User;
|
||||
|
||||
/**
|
||||
* Displays a chat using a contact control for the recipient and a label for the
|
||||
@ -16,7 +17,7 @@ import envoy.client.util.IconUtil;
|
||||
* @author Leon Hofmeister
|
||||
* @since Envoy Client v0.1-beta
|
||||
*/
|
||||
public final class ChatControl extends HBox {
|
||||
public final class ChatControl extends Label {
|
||||
|
||||
private static final Image userIcon = IconUtil.loadIconThemeSensitive("user_icon", 32),
|
||||
groupIcon = IconUtil.loadIconThemeSensitive("group_icon", 32);
|
||||
@ -31,25 +32,35 @@ public final class ChatControl extends HBox {
|
||||
setAlignment(Pos.CENTER_LEFT);
|
||||
setPadding(new Insets(0, 0, 3, 0));
|
||||
|
||||
final var menu = new ContextMenu();
|
||||
final var removeMI = new MenuItem();
|
||||
removeMI
|
||||
.setText(chat.isDisabled() ? "Delete " : chat.getRecipient() instanceof User ? "Block " : "Leave group " + chat.getRecipient().getName());
|
||||
removeMI.setOnAction(chat.isDisabled() ? e -> UserUtil.deleteContact(chat.getRecipient()) : e -> UserUtil.blockContact(chat.getRecipient()));
|
||||
menu.getItems().add(removeMI);
|
||||
setContextMenu(menu);
|
||||
|
||||
final var display = new HBox();
|
||||
|
||||
// Profile picture
|
||||
final var contactProfilePic = new ProfilePicImageView(chat instanceof GroupChat ? groupIcon : userIcon, 32);
|
||||
getChildren().add(contactProfilePic);
|
||||
display.getChildren().add(contactProfilePic);
|
||||
|
||||
// Spacing
|
||||
final var leftSpacing = new Region();
|
||||
leftSpacing.setPrefSize(8, 0);
|
||||
leftSpacing.setMinSize(8, 0);
|
||||
leftSpacing.setMaxSize(8, 0);
|
||||
getChildren().add(leftSpacing);
|
||||
display.getChildren().add(leftSpacing);
|
||||
|
||||
// Contact control
|
||||
getChildren().add(new ContactControl(chat.getRecipient()));
|
||||
display.getChildren().add(new ContactControl(chat.getRecipient()));
|
||||
|
||||
// Unread messages
|
||||
if (chat.getUnreadAmount() != 0) {
|
||||
final var spacing = new Region();
|
||||
setHgrow(spacing, Priority.ALWAYS);
|
||||
getChildren().add(spacing);
|
||||
HBox.setHgrow(spacing, Priority.ALWAYS);
|
||||
display.getChildren().add(spacing);
|
||||
final var unreadMessagesLabel = new Label(Integer.toString(chat.getUnreadAmount()));
|
||||
unreadMessagesLabel.setMinSize(15, 15);
|
||||
final var vbox = new VBox();
|
||||
@ -57,9 +68,11 @@ public final class ChatControl extends HBox {
|
||||
unreadMessagesLabel.setAlignment(Pos.CENTER);
|
||||
unreadMessagesLabel.getStyleClass().add("unread-messages-amount");
|
||||
vbox.getChildren().add(unreadMessagesLabel);
|
||||
getChildren().add(vbox);
|
||||
display.getChildren().add(vbox);
|
||||
}
|
||||
|
||||
setGraphic(display);
|
||||
|
||||
// Set background depending on whether it is disabled or not
|
||||
getStyleClass().add(chat.isDisabled() ? "disabled-chat" : "list-element");
|
||||
}
|
||||
|
@ -91,9 +91,6 @@ public final class ChatScene implements EventListener, Restorable {
|
||||
@FXML
|
||||
private Label topBarStatusLabel;
|
||||
|
||||
@FXML
|
||||
private MenuItem deleteContactMenuItem;
|
||||
|
||||
@FXML
|
||||
private ImageView attachmentView;
|
||||
|
||||
@ -278,12 +275,13 @@ public final class ChatScene implements EventListener, Restorable {
|
||||
@Event
|
||||
private void onGroupResize(GroupResize resize) {
|
||||
final var chatFound = localDB.getChat(resize.getGroupID());
|
||||
if (chatFound.isEmpty()) return;
|
||||
Platform.runLater(() -> {
|
||||
chatList.refresh();
|
||||
|
||||
// Update the top-bar status label if all conditions apply
|
||||
if (currentChat != null && currentChat.getRecipient().equals(chatFound.get().getRecipient()))
|
||||
chatFound.ifPresent(chat -> topBarStatusLabel.setText(chat.getRecipient().getContacts().size() + " members"));
|
||||
topBarStatusLabel.setText(chatFound.get().getRecipient().getContacts().size() + " members");
|
||||
});
|
||||
}
|
||||
|
||||
@ -348,7 +346,6 @@ public final class ChatScene implements EventListener, Restorable {
|
||||
final var scrollIndex = messageList.getItems().size() - currentChat.getUnreadAmount();
|
||||
messageList.scrollTo(scrollIndex);
|
||||
logger.log(Level.FINEST, "Loading chat with " + user + " at index " + scrollIndex);
|
||||
deleteContactMenuItem.setText("Delete " + user.getName());
|
||||
|
||||
// Read the current chat
|
||||
currentChat.read(writeProxy);
|
||||
@ -746,18 +743,6 @@ public final class ChatScene implements EventListener, Restorable {
|
||||
}
|
||||
}
|
||||
|
||||
// Context menu actions
|
||||
|
||||
@FXML
|
||||
private void blockOrDeleteContact() {
|
||||
final var selectedChat = chatList.getSelectionModel().getSelectedItem();
|
||||
|
||||
if (selectedChat == null) return;
|
||||
// If a contact has already been blocked deletes this chat else only blocks him
|
||||
if (selectedChat.isDisabled()) UserUtil.deleteContact(selectedChat.getRecipient());
|
||||
else UserUtil.blockContact(selectedChat.getRecipient());
|
||||
}
|
||||
|
||||
/**
|
||||
* Redesigns the UI when the {@link Chat} of the given contact has been marked
|
||||
* as disabled.
|
||||
|
@ -102,7 +102,7 @@ public final class UserUtil {
|
||||
* @since Envoy Client v0.3-beta
|
||||
*/
|
||||
public static void deleteContact(Contact delete) {
|
||||
if (!context.getClient().isOnline() || delete == null) return;
|
||||
if (delete == null) return;
|
||||
else {
|
||||
final var alert = new Alert(AlertType.CONFIRMATION);
|
||||
alert.setContentText("Are you sure you want to delete " + delete.getName()
|
||||
|
@ -126,15 +126,6 @@
|
||||
<ListView id="chat-list" fx:id="chatList"
|
||||
focusTraversable="false" onMouseClicked="#chatListClicked"
|
||||
prefWidth="316.0" VBox.vgrow="ALWAYS">
|
||||
<contextMenu>
|
||||
<ContextMenu anchorLocation="CONTENT_TOP_LEFT">
|
||||
<items>
|
||||
<MenuItem fx:id="deleteContactMenuItem"
|
||||
mnemonicParsing="false" onAction="#blockOrDeleteContact"
|
||||
text="Delete" />
|
||||
</items>
|
||||
</ContextMenu>
|
||||
</contextMenu>
|
||||
<padding>
|
||||
<Insets bottom="5.0" left="5.0" right="2.0" top="5.0" />
|
||||
</padding>
|
||||
|
Reference in New Issue
Block a user