aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-05-20 19:06:14 +0200
committerdec05eba <dec05eba@protonmail.com>2021-05-20 19:06:14 +0200
commit5b11322c33ef61d3aa8d6bc9c9bc0355f35afa52 (patch)
tree1a36d304bfc9350d008787aa8c284adb7d92bc2d
parent3609429b3d8fccd99994dde015b6516376e6835b (diff)
Restrict image size to body size
-rw-r--r--src/Body.cpp20
-rw-r--r--src/plugins/Matrix.cpp29
2 files changed, 32 insertions, 17 deletions
diff --git a/src/Body.cpp b/src/Body.cpp
index 72974fa..1b1d35a 100644
--- a/src/Body.cpp
+++ b/src/Body.cpp
@@ -500,6 +500,16 @@ namespace QuickMedia {
if(frame_time > 2.0f)
frame_time = 2.0f;
+ if(selected_item >= (int)items.size())
+ selected_item = (int)items.size() - 1;
+
+ if(selected_item < 0)
+ selected_item = 0;
+
+ if(prev_selected_item < 0 || prev_selected_item >= (int)items.size()) {
+ prev_selected_item = selected_item;
+ }
+
body_pos = pos;
bool body_size_changed = std::abs(size.x - body_size.x) > 0.1f || std::abs(size.y - body_size.y) > 0.1f;
if(body_size_changed) {
@@ -613,10 +623,6 @@ namespace QuickMedia {
}
}
- if(prev_selected_item < 0 || prev_selected_item >= (int)items.size()) {
- prev_selected_item = selected_item;
- }
-
const int prev_body_item_index = get_previous_visible_item(selected_item);
bool selected_merge_with_previous = !items.empty() && body_item_merge_handler && body_item_merge_handler(prev_body_item_index == -1 ? nullptr : items[prev_body_item_index].get(), items[selected_item].get());
@@ -1202,6 +1208,8 @@ namespace QuickMedia {
if(draw_thumbnails && !merge_with_previous && !item->thumbnail_url.empty())
item_thumbnail = AsyncImageLoader::get_instance().get_thumbnail(item->thumbnail_url, item->thumbnail_is_local, thumbnail_size);
+ thumbnail_size = clamp_to_size_x(thumbnail_size, sf::Vector2i(size.x, thumbnail_size.y));
+
if(body_item_render_callback && include_embedded_item)
body_item_render_callback(item);
@@ -1393,11 +1401,13 @@ namespace QuickMedia {
item->last_drawn_time = elapsed_time_sec;
sf::Vector2i content_size = get_item_thumbnail_size(item);
+
float image_height = 0.0f;
float text_offset_x = padding_x;
if(draw_thumbnails && load_texture && !item->thumbnail_url.empty() && !merge_with_previous) {
- image_height = content_size.y;
std::shared_ptr<ThumbnailData> item_thumbnail = AsyncImageLoader::get_instance().get_thumbnail(item->thumbnail_url, item->thumbnail_is_local, content_size);
+ content_size = clamp_to_size_x(content_size, sf::Vector2i(width, content_size.y));
+ image_height = content_size.y;
if(item_thumbnail && item_thumbnail->loading_state == LoadingState::FINISHED_LOADING && item_thumbnail->image->getSize().x > 0 && item_thumbnail->image->getSize().y > 0) {
if(!item_thumbnail->texture.loadFromImage(*item_thumbnail->image))
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp
index afa8738..e015ada 100644
--- a/src/plugins/Matrix.cpp
+++ b/src/plugins/Matrix.cpp
@@ -1631,14 +1631,20 @@ namespace QuickMedia {
static std::string thumbnail_url_extract_media_id(const std::string &media_url) {
size_t start = 0;
size_t end = media_url.size();
- if(strncmp(media_url.c_str(), "mxc://", 6) == 0)
- start = 6;
+ if(strncmp(media_url.c_str(), "mxc://", 6) != 0)
+ return "";
+
+ start = 6;
if(media_url.size() >= start + 5 && strncmp(media_url.c_str() + media_url.size() - 5, "#auto", 5) == 0)
end = media_url.size() - 5;
+
return media_url.substr(start, end - start);
}
static std::string get_thumbnail_url(const std::string &homeserver, const std::string &mxc_id) {
+ if(mxc_id.empty())
+ return "";
+
std::string size = std::to_string(int(32 * get_ui_scale()));
return homeserver + "/_matrix/media/r0/thumbnail/" + mxc_id + "?width=" + size + "&height=" + size + "&method=crop";
}
@@ -1749,10 +1755,11 @@ namespace QuickMedia {
const rapidjson::Value &thumbnail_url_json = GetMember(info_json, "thumbnail_url");
if(thumbnail_url_json.IsString()) {
std::string thumbnail_str = thumbnail_url_json.GetString();
- if(strncmp(thumbnail_str.c_str(), "mxc://", 6) == 0) {
- thumbnail_str.erase(thumbnail_str.begin(), thumbnail_str.begin() + 6);
- return homeserver + "/_matrix/media/r0/download/" + std::move(thumbnail_str);
- }
+ if(strncmp(thumbnail_str.c_str(), "mxc://", 6) != 0)
+ return "";
+
+ thumbnail_str.erase(thumbnail_str.begin(), thumbnail_str.begin() + 6);
+ return homeserver + "/_matrix/media/r0/download/" + std::move(thumbnail_str);
}
}
@@ -2287,11 +2294,10 @@ namespace QuickMedia {
continue;
const rapidjson::Value &url_json = GetMember(content_json, "url");
- if(!url_json.IsString())
+ if(!url_json.IsString() || strncmp(url_json.GetString(), "mxc://", 6) != 0)
continue;
- std::string url_json_str = url_json.GetString() + 6;
- room_data->set_avatar_url(get_thumbnail_url(homeserver, thumbnail_url_extract_media_id(url_json_str)));
+ room_data->set_avatar_url(get_thumbnail_url(homeserver, thumbnail_url_extract_media_id(url_json.GetString())));
room_data->avatar_is_fallback = false;
} else if(strcmp(type_json.GetString(), "m.room.avatar") == 0) {
const rapidjson::Value &content_json = GetMember(event_item_json, "content");
@@ -2299,11 +2305,10 @@ namespace QuickMedia {
continue;
const rapidjson::Value &url_json = GetMember(content_json, "url");
- if(!url_json.IsString())
+ if(!url_json.IsString() || strncmp(url_json.GetString(), "mxc://", 6) != 0)
continue;
- std::string url_json_str = url_json.GetString() + 6;
- room_data->set_avatar_url(get_thumbnail_url(homeserver, thumbnail_url_extract_media_id(url_json_str)));
+ room_data->set_avatar_url(get_thumbnail_url(homeserver, thumbnail_url_extract_media_id(url_json.GetString())));
room_data->avatar_is_fallback = false;
} else if(strcmp(type_json.GetString(), "m.room.topic") == 0) {
const rapidjson::Value &content_json = GetMember(event_item_json, "content");