From 9602603135f456d906192112288dcd84429c8fee Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 30 Sep 2020 22:05:41 +0200 Subject: Matrix: implement message editing --- src/QuickMedia.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 11 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index c3e61fe..ecb44d5 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -3346,20 +3346,21 @@ namespace QuickMedia { enum class ChatState { NAVIGATING, TYPING_MESSAGE, - REPLYING + REPLYING, + EDITING }; Page new_page = Page::CHAT; ChatState chat_state = ChatState::NAVIGATING; - std::shared_ptr replying_to_message; + std::shared_ptr currently_operating_on_item; sf::Text replying_to_text("Replying to:", *font, 18); sf::Sprite logo_sprite(plugin_logo); Entry chat_input("Press ctrl+m to begin writing a message...", font.get(), cjk_font.get()); chat_input.set_editable(false); - chat_input.on_submit_callback = [matrix, &chat_input, &tabs, &selected_tab, ¤t_room_id, &new_page, &chat_state, &replying_to_message](const sf::String &text) mutable { + chat_input.on_submit_callback = [matrix, &chat_input, &tabs, &selected_tab, ¤t_room_id, &new_page, &chat_state, ¤tly_operating_on_item](const sf::String &text) mutable { if(tabs[selected_tab].type == ChatTabType::MESSAGES) { if(text.isEmpty()) return false; @@ -3397,15 +3398,26 @@ namespace QuickMedia { } } else if(chat_state == ChatState::REPLYING) { // TODO: Make asynchronous - if(matrix->post_reply(current_room_id, text, replying_to_message->userdata) == PluginResult::OK) { + if(matrix->post_reply(current_room_id, text, currently_operating_on_item->userdata) == PluginResult::OK) { chat_input.set_editable(false); chat_state = ChatState::NAVIGATING; - replying_to_message = nullptr; + currently_operating_on_item = nullptr; return true; } else { show_notification("QuickMedia", "Failed to post matrix reply", Urgency::CRITICAL); return false; } + } else if(chat_state == ChatState::EDITING) { + // TODO: Make asynchronous + if(matrix->post_edit(current_room_id, text, currently_operating_on_item->userdata) == PluginResult::OK) { + chat_input.set_editable(false); + chat_state = ChatState::NAVIGATING; + currently_operating_on_item = nullptr; + return true; + } else { + show_notification("QuickMedia", "Failed to post matrix edit", Urgency::CRITICAL); + return false; + } } } return false; @@ -3545,12 +3557,34 @@ namespace QuickMedia { std::shared_ptr selected = tabs[selected_tab].body->get_selected_shared(); if(selected) { chat_state = ChatState::REPLYING; - replying_to_message = selected; + currently_operating_on_item = selected; chat_input.set_editable(true); + replying_to_text.setString("Replying to:"); } else { // TODO: Show inline notification show_notification("QuickMedia", "No message selected for replying"); } + } else if(tabs[selected_tab].type == ChatTabType::MESSAGES && event.key.control && event.key.code == sf::Keyboard::E) { + std::shared_ptr selected = tabs[selected_tab].body->get_selected_shared(); + if(selected) { + if(!selected->url.empty()) { // cant edit messages that are image/video posts + // TODO: Show inline notification + show_notification("QuickMedia", "You can only edit messages with no file attached to it"); + } else if(!matrix->was_message_posted_by_me(current_room_id, selected->userdata)) { + // TODO: Show inline notification + show_notification("QuickMedia", "You can't edit a message that was posted by somebody else"); + } else { + chat_state = ChatState::EDITING; + currently_operating_on_item = selected; + chat_input.set_editable(true); + chat_input.set_text(selected->get_description()); // TODO: Description? it may change in the future, in which case this should be edited + chat_input.move_caret_to_end(); + replying_to_text.setString("Editing message:"); + } + } else { + // TODO: Show inline notification + show_notification("QuickMedia", "No message selected for editing"); + } } } @@ -3559,7 +3593,7 @@ namespace QuickMedia { chat_state = ChatState::TYPING_MESSAGE; } - if((chat_state == ChatState::TYPING_MESSAGE || chat_state == ChatState::REPLYING) && tabs[selected_tab].type == ChatTabType::MESSAGES) { + if((chat_state == ChatState::TYPING_MESSAGE || chat_state == ChatState::REPLYING || chat_state == ChatState::EDITING) && tabs[selected_tab].type == ChatTabType::MESSAGES) { if(event.type == sf::Event::TextEntered) { //chat_input.onTextEntered(event.text.unicode); // TODO: Also show typing event when ctrl+v pasting? @@ -3573,8 +3607,9 @@ namespace QuickMedia { } } else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) { chat_input.set_editable(false); + chat_input.set_text(""); chat_state = ChatState::NAVIGATING; - replying_to_message = nullptr; + currently_operating_on_item = nullptr; } //chat_input.on_event(event); chat_input.process_event(event); @@ -3799,11 +3834,11 @@ namespace QuickMedia { window.draw(gradient_points, 4, sf::Quads); // Note: sf::Quads doesn't work with egl } - if(chat_state == ChatState::REPLYING) { + if(chat_state == ChatState::REPLYING || chat_state == ChatState::EDITING) { const float margin = 5.0f; const float replying_to_text_height = replying_to_text.getLocalBounds().height + margin; - const float item_height = std::min(body_size.y - replying_to_text_height - margin, tabs[MESSAGES_TAB_INDEX].body->get_item_height(replying_to_message.get()) + margin); + const float item_height = std::min(body_size.y - replying_to_text_height - margin, tabs[MESSAGES_TAB_INDEX].body->get_item_height(currently_operating_on_item.get()) + margin); sf::RectangleShape overlay(sf::Vector2f(window_size.x, window_size.y - tab_shade_height - chat_input_height_full)); overlay.setPosition(0.0f, tab_shade_height); @@ -3821,7 +3856,7 @@ namespace QuickMedia { replying_to_text.setPosition(body_item_pos.x, body_item_pos.y - replying_to_text_height); window.draw(replying_to_text); - tabs[MESSAGES_TAB_INDEX].body->draw_item(window, replying_to_message.get(), body_item_pos, body_item_size); + tabs[MESSAGES_TAB_INDEX].body->draw_item(window, currently_operating_on_item.get(), body_item_pos, body_item_size); } if(tabs[selected_tab].type == ChatTabType::MESSAGES && !tabs[selected_tab].body->is_last_item_fully_visible()) { -- cgit v1.2.3