From 30dbaeb2b175c1e67f57aba748ced1a2280fb56d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 1 Oct 2020 19:12:33 +0200 Subject: Matrix: add room name and avatar of the current room at the top --- src/QuickMedia.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 8 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 709dfea..909bdd7 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -3502,6 +3502,20 @@ namespace QuickMedia { const float tab_vertical_offset = 10.0f; + sf::Text room_name_text("", *font, 18); + if(current_room_body_data) + room_name_text.setString(current_room_body_data->body_item->get_title()); + const float room_name_text_height = std::floor(room_name_text.getLocalBounds().height); + const float room_name_text_padding_y = 10.0f; + const float room_name_total_height = room_name_text_height + room_name_text_padding_y * 2.0f; + const float room_avatar_height = 32.0f; + + sf::Sprite room_avatar_sprite; + auto room_avatar_thumbnail_data = std::make_shared(); + AsyncImageLoader async_image_loader; + if(current_room_body_data) + async_image_loader.load_thumbnail(current_room_body_data->body_item->thumbnail_url, false, sf::Vector2i(), use_tor, room_avatar_thumbnail_data); + auto typing_async_func = [matrix](bool new_state, std::string room_id) { if(new_state) { matrix->on_start_typing(room_id); @@ -3755,7 +3769,7 @@ namespace QuickMedia { } //chat_input.on_event(event); chat_input.process_event(event); - } else if(tabs[selected_tab].type == ChatTabType::ROOMS && event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Enter) { + } else if(tabs[selected_tab].type == ChatTabType::ROOMS && event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Enter) { BodyItem *selected_item = tabs[selected_tab].body->get_selected(); if(selected_item) { current_room_id = selected_item->url; @@ -3773,8 +3787,11 @@ namespace QuickMedia { } auto room_body_item_it = body_items_by_room_id.find(current_room_id); - if(room_body_item_it != body_items_by_room_id.end()) + if(room_body_item_it != body_items_by_room_id.end()) { current_room_body_data = &room_body_item_it->second; + room_name_text.setString(current_room_body_data->body_item->get_title()); + room_avatar_thumbnail_data = std::make_shared(); + } } } } @@ -3782,8 +3799,10 @@ namespace QuickMedia { switch(new_page) { case Page::FILE_MANAGER: { new_page = Page::CHAT; - if(!file_manager) + if(!file_manager) { file_manager = new FileManager(); + file_manager->set_current_directory(get_home_dir().data); + } page_stack.push(Page::CHAT); current_page = Page::FILE_MANAGER; file_manager_page(); @@ -3839,7 +3858,28 @@ namespace QuickMedia { ++it; } - const float tab_shade_height = tab_spacer_height + std::floor(tab_vertical_offset) + tab_height + 10.0f; + async_image_loader.update(); + if(current_room_body_data && room_avatar_thumbnail_data->loading_state == LoadingState::NOT_LOADED) + async_image_loader.load_thumbnail(current_room_body_data->body_item->thumbnail_url, false, sf::Vector2i(), use_tor, room_avatar_thumbnail_data); + + if(room_avatar_thumbnail_data->loading_state == LoadingState::FINISHED_LOADING) { + if(!room_avatar_thumbnail_data->texture.loadFromImage(*room_avatar_thumbnail_data->image)) + fprintf(stderr, "Warning: failed to load texture for room avatar\n"); + room_avatar_thumbnail_data->image.reset(); + room_avatar_thumbnail_data->loading_state = LoadingState::APPLIED_TO_TEXTURE; + room_avatar_sprite.setTexture(room_avatar_thumbnail_data->texture, true); + + auto texture_size = room_avatar_sprite.getTexture()->getSize(); + if(texture_size.x > 0 && texture_size.y > 0) { + float width_ratio = (float)texture_size.x / (float)texture_size.y; + float height_scale = room_avatar_height / (float)texture_size.y; + float width_scale = height_scale * width_ratio; + room_avatar_sprite.setScale(width_scale, height_scale); + } + } + + const float room_name_padding_y = (selected_tab == MESSAGES_TAB_INDEX ? room_name_total_height : 0.0f); + const float tab_shade_height = tab_spacer_height + std::floor(tab_vertical_offset) + tab_height + 10.0f + room_name_padding_y; const float chat_height = chat_input.get_height(); if(std::abs(chat_height - prev_chat_height) > 1.0f) { @@ -3883,7 +3923,7 @@ namespace QuickMedia { if(!sync_running && sync_timer.getElapsedTime().asMilliseconds() >= sync_min_time_ms) { fprintf(stderr, "Time since last sync: %d ms\n", sync_timer.getElapsedTime().asMilliseconds()); // TODO: Ignore matrix->sync() call the first time, its already called above for the first time - sync_min_time_ms = 3000; + sync_min_time_ms = 50; sync_running = true; sync_timer.restart(); sync_future_room_id = current_room_id; @@ -3949,11 +3989,25 @@ namespace QuickMedia { url_selection_body.draw(window, body_pos, body_size); else tabs[selected_tab].body->draw(window, body_pos, body_size); - const float tab_y = tab_spacer_height + std::floor(tab_vertical_offset + tab_height * 0.5f - (tab_text_size + 5.0f) * 0.5f); + const float tab_y = tab_spacer_height + std::floor(tab_vertical_offset + tab_height * 0.5f - (tab_text_size + 5.0f) * 0.5f) + room_name_padding_y; tab_shade.setSize(sf::Vector2f(window_size.x, tab_shade_height)); window.draw(tab_shade); + if(tabs[selected_tab].type == ChatTabType::MESSAGES) { + float room_name_text_offset_x = 0.0f; + if(room_avatar_sprite.getTexture() && room_avatar_sprite.getTexture()->getNativeHandle() != 0) { + auto room_avatar_texture_size = room_avatar_sprite.getTexture()->getSize(); + room_avatar_texture_size.x *= room_avatar_sprite.getScale().x; + room_avatar_texture_size.y *= room_avatar_sprite.getScale().y; + room_avatar_sprite.setPosition(body_pos.x, room_name_total_height * 0.5f - room_avatar_texture_size.y * 0.5f + 5.0f); + window.draw(room_avatar_sprite); + room_name_text_offset_x += room_avatar_texture_size.x + 10.0f; + } + room_name_text.setPosition(body_pos.x + room_name_text_offset_x, room_name_text_padding_y); + window.draw(room_name_text); + } + gradient_points[0].position.x = 0.0f; gradient_points[0].position.y = tab_shade_height; @@ -3969,7 +4023,7 @@ namespace QuickMedia { int i = 0; for(ChatTab &tab : tabs) { if(i == selected_tab) { - tab_background.setPosition(std::floor(i * width_per_tab + tab_margin_x), tab_spacer_height + std::floor(tab_vertical_offset)); + tab_background.setPosition(std::floor(i * width_per_tab + tab_margin_x), tab_spacer_height + std::floor(tab_vertical_offset) + room_name_padding_y); window.draw(tab_background); } const float center = (i * width_per_tab) + (width_per_tab * 0.5f); @@ -4025,7 +4079,7 @@ namespace QuickMedia { current_room_body_data->body_item->title_color = sf::Color::White; current_room_body_data->last_message_read = true; } - } else { + } else if(current_room_body_data && !current_room_body_data->last_message_read) { window.draw(more_messages_below_rect); } } -- cgit v1.2.3