Support dark_theme_brightness for chat backgrounds.

This commit is contained in:
levlam 2023-04-10 18:45:26 +03:00
parent 13d0536db1
commit 00ed2f85b8
10 changed files with 64 additions and 31 deletions

View File

@ -518,6 +518,9 @@ background id:int64 is_default:Bool is_dark:Bool name:string document:document t
//@description Contains a list of backgrounds @backgrounds A list of backgrounds
backgrounds backgrounds:vector<background> = Backgrounds;
//@description Describes a background set for a specific chat @background The background @dark_theme_brightness Brightness of the background in dark themes, as a percentage; 1-100
chatBackground background:background dark_theme_brightness:int32 = ChatBackground;
//@description Describes a user profile photo
//@id Photo identifier; 0 for an empty photo. Can be used to find a photo in a list of user profile photos
@ -1443,7 +1446,7 @@ videoChat group_call_id:int32 has_participants:Bool default_participant_id:Messa
//@reply_markup_message_id Identifier of the message from which reply markup needs to be used; 0 if there is no default custom reply markup in the chat
//@draft_message A draft of a message in the chat; may be null
//@client_data Application-specific data associated with the chat. (For example, the chat scroll position or local chat notification settings can be stored here.) Persistent if the message database is used
chat id:int53 type:ChatType title:string photo:chatPhotoInfo permissions:chatPermissions last_message:message positions:vector<chatPosition> message_sender_id:MessageSender has_protected_content:Bool is_translatable:Bool is_marked_as_unread:Bool is_blocked:Bool has_scheduled_messages:Bool can_be_deleted_only_for_self:Bool can_be_deleted_for_all_users:Bool can_be_reported:Bool default_disable_notification:Bool unread_count:int32 last_read_inbox_message_id:int53 last_read_outbox_message_id:int53 unread_mention_count:int32 unread_reaction_count:int32 notification_settings:chatNotificationSettings available_reactions:ChatAvailableReactions message_auto_delete_time:int32 background:background theme_name:string action_bar:ChatActionBar video_chat:videoChat pending_join_requests:chatJoinRequestsInfo reply_markup_message_id:int53 draft_message:draftMessage client_data:string = Chat;
chat id:int53 type:ChatType title:string photo:chatPhotoInfo permissions:chatPermissions last_message:message positions:vector<chatPosition> message_sender_id:MessageSender has_protected_content:Bool is_translatable:Bool is_marked_as_unread:Bool is_blocked:Bool has_scheduled_messages:Bool can_be_deleted_only_for_self:Bool can_be_deleted_for_all_users:Bool can_be_reported:Bool default_disable_notification:Bool unread_count:int32 last_read_inbox_message_id:int53 last_read_outbox_message_id:int53 unread_mention_count:int32 unread_reaction_count:int32 notification_settings:chatNotificationSettings available_reactions:ChatAvailableReactions message_auto_delete_time:int32 background:chatBackground theme_name:string action_bar:ChatActionBar video_chat:videoChat pending_join_requests:chatJoinRequestsInfo reply_markup_message_id:int53 draft_message:draftMessage client_data:string = Chat;
//@description Represents a list of chats @total_count Approximate total number of chats found @chat_ids List of chat identifiers
chats total_count:int32 chat_ids:vector<int53> = Chats;
@ -2562,7 +2565,7 @@ messagePinMessage message_id:int53 = MessageContent;
messageScreenshotTaken = MessageContent;
//@description A new background was set in the chat @old_background_message_id Identifier of the message with a previously set same background; 0 if none. Can be an identifier of a deleted message @background The new background
messageChatSetBackground old_background_message_id:int53 background:background = MessageContent;
messageChatSetBackground old_background_message_id:int53 background:chatBackground = MessageContent;
//@description A theme in the chat has been changed @theme_name If non-empty, name of a new theme, set for the chat. Otherwise, chat theme was reset to the default one
messageChatSetTheme theme_name:string = MessageContent;
@ -5375,7 +5378,7 @@ updateChatPendingJoinRequests chat_id:int53 pending_join_requests:chatJoinReques
updateChatReplyMarkup chat_id:int53 reply_markup_message_id:int53 = Update;
//@description The chat background was changed @chat_id Chat identifier @background The new chat background; may be null if background was reset to default
updateChatBackground chat_id:int53 background:background = Update;
updateChatBackground chat_id:int53 background:chatBackground = Update;
//@description The chat theme was changed @chat_id Chat identifier @theme_name The new name of the chat theme; may be empty if theme was reset to default
updateChatTheme chat_id:int53 theme_name:string = Update;

View File

@ -22,4 +22,13 @@ td_api::object_ptr<td_api::background> BackgroundInfo::get_background_object(con
return td->background_manager_->get_background_object(background_id_, false, &background_type_);
}
td_api::object_ptr<td_api::chatBackground> BackgroundInfo::get_chat_background_object(const Td *td) const {
auto background = get_background_object(td);
if (background == nullptr) {
return nullptr;
}
return td_api::make_object<td_api::chatBackground>(std::move(background),
background_type_.get_dark_theme_brightness());
}
} // namespace td

View File

@ -30,6 +30,8 @@ class BackgroundInfo {
td_api::object_ptr<td_api::background> get_background_object(const Td *td) const;
td_api::object_ptr<td_api::chatBackground> get_chat_background_object(const Td *td) const;
bool is_valid() const {
return background_id_.is_valid();
}

View File

@ -601,7 +601,7 @@ void BackgroundManager::set_background(const td_api::InputBackground *input_back
Promise<td_api::object_ptr<td_api::background>> &&promise) {
BackgroundType type;
if (background_type != nullptr) {
auto r_type = BackgroundType::get_background_type(background_type);
auto r_type = BackgroundType::get_background_type(background_type, 100);
if (r_type.is_error()) {
return promise.set_error(r_type.move_as_error());
}

View File

@ -335,15 +335,22 @@ StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &t
return string_builder << '[' << type.get_link() << ']';
}
Result<BackgroundType> BackgroundType::get_background_type(const td_api::BackgroundType *background_type) {
Result<BackgroundType> BackgroundType::get_background_type(const td_api::BackgroundType *background_type,
int32 dark_theme_brightness) {
if (background_type == nullptr) {
return Status::Error(400, "Type must be non-empty");
}
if (dark_theme_brightness < 1 || dark_theme_brightness > 100) {
return Status::Error(400, "Invalid dark them brightness specified");
}
if (dark_theme_brightness == 100) {
dark_theme_brightness = 0;
}
switch (background_type->get_id()) {
case td_api::backgroundTypeWallpaper::ID: {
auto wallpaper_type = static_cast<const td_api::backgroundTypeWallpaper *>(background_type);
return BackgroundType(wallpaper_type->is_blurred_, wallpaper_type->is_moving_);
return BackgroundType(wallpaper_type->is_blurred_, wallpaper_type->is_moving_, dark_theme_brightness);
}
case td_api::backgroundTypePattern::ID: {
auto pattern_type = static_cast<const td_api::backgroundTypePattern *>(background_type);
@ -357,7 +364,7 @@ Result<BackgroundType> BackgroundType::get_background_type(const td_api::Backgro
case td_api::backgroundTypeFill::ID: {
auto fill_type = static_cast<const td_api::backgroundTypeFill *>(background_type);
TRY_RESULT(background_fill, BackgroundFill::get_background_fill(fill_type->fill_.get()));
return BackgroundType(std::move(background_fill));
return BackgroundType(std::move(background_fill), dark_theme_brightness);
}
default:
UNREACHABLE();
@ -367,7 +374,7 @@ Result<BackgroundType> BackgroundType::get_background_type(const td_api::Backgro
Result<BackgroundType> BackgroundType::get_local_background_type(Slice name) {
TRY_RESULT(fill, BackgroundFill::get_background_fill(name));
return BackgroundType(fill);
return BackgroundType(std::move(fill), 0);
}
bool BackgroundType::is_background_name_local(Slice name) {
@ -376,26 +383,26 @@ bool BackgroundType::is_background_name_local(Slice name) {
BackgroundType::BackgroundType(bool is_fill, bool is_pattern,
telegram_api::object_ptr<telegram_api::wallPaperSettings> settings) {
if (settings != nullptr && (settings->flags_ & telegram_api::wallPaperSettings::INTENSITY_MASK) != 0) {
intensity_ = settings->intensity_;
if (!is_valid_intensity(intensity_, is_pattern)) {
LOG(ERROR) << "Receive " << to_string(settings);
intensity_ = is_pattern ? 50 : 0;
}
}
if (is_fill) {
type_ = Type::Fill;
CHECK(settings != nullptr);
fill_ = BackgroundFill(settings.get());
} else if (is_pattern) {
type_ = Type::Pattern;
if (settings) {
if (settings != nullptr) {
fill_ = BackgroundFill(settings.get());
is_moving_ = (settings->flags_ & telegram_api::wallPaperSettings::MOTION_MASK) != 0;
if ((settings->flags_ & telegram_api::wallPaperSettings::INTENSITY_MASK) != 0) {
intensity_ = settings->intensity_;
if (!is_valid_intensity(intensity_, true)) {
LOG(ERROR) << "Receive " << to_string(settings);
intensity_ = 50;
}
}
}
} else {
type_ = Type::Wallpaper;
if (settings) {
if (settings != nullptr) {
is_blurred_ = (settings->flags_ & telegram_api::wallPaperSettings::BLUR_MASK) != 0;
is_moving_ = (settings->flags_ & telegram_api::wallPaperSettings::MOTION_MASK) != 0;
}

View File

@ -75,13 +75,14 @@ class BackgroundType {
friend StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &type);
BackgroundType(bool is_blurred, bool is_moving)
: type_(Type::Wallpaper), is_blurred_(is_blurred), is_moving_(is_moving) {
BackgroundType(bool is_blurred, bool is_moving, int32 dark_theme_brightness)
: type_(Type::Wallpaper), is_blurred_(is_blurred), is_moving_(is_moving), intensity_(dark_theme_brightness) {
}
BackgroundType(bool is_moving, const BackgroundFill &fill, int32 intensity)
: type_(Type::Pattern), is_moving_(is_moving), intensity_(intensity), fill_(fill) {
BackgroundType(bool is_moving, BackgroundFill &&fill, int32 intensity)
: type_(Type::Pattern), is_moving_(is_moving), intensity_(intensity), fill_(std::move(fill)) {
}
explicit BackgroundType(BackgroundFill fill) : type_(Type::Fill), fill_(fill) {
BackgroundType(BackgroundFill &&fill, int32 dark_theme_brightness)
: type_(Type::Fill), fill_(std::move(fill)), intensity_(dark_theme_brightness) {
}
public:
@ -89,7 +90,8 @@ class BackgroundType {
BackgroundType(bool is_fill, bool is_pattern, telegram_api::object_ptr<telegram_api::wallPaperSettings> settings);
static Result<BackgroundType> get_background_type(const td_api::BackgroundType *background_type);
static Result<BackgroundType> get_background_type(const td_api::BackgroundType *background_type,
int32 dark_theme_brightness);
static Result<BackgroundType> get_local_background_type(Slice name);
@ -122,6 +124,16 @@ class BackgroundType {
return fill_.is_dark();
}
int32 get_dark_theme_brightness() const {
if (type_ == Type::Pattern) {
return 100;
}
if (intensity_ == 0) {
return 100;
}
return intensity_;
}
template <class StorerT>
void store(StorerT &storer) const;

View File

@ -2295,7 +2295,7 @@ void LinkManager::get_link_login_url(const string &url, bool allow_write_access,
Result<string> LinkManager::get_background_url(const string &name,
td_api::object_ptr<td_api::BackgroundType> background_type) {
TRY_RESULT(type, BackgroundType::get_background_type(background_type.get()));
TRY_RESULT(type, BackgroundType::get_background_type(background_type.get(), 100));
auto url = PSTRING() << get_t_me_url() << "bg/";
auto link = type.get_link();
if (type.has_file()) {

View File

@ -5880,7 +5880,7 @@ tl_object_ptr<td_api::MessageContent> get_message_content_object(const MessageCo
case MessageContentType::SetBackground: {
const auto *m = static_cast<const MessageSetBackground *>(content);
return td_api::make_object<td_api::messageChatSetBackground>(m->old_message_id.get(),
m->background_info.get_background_object(td));
m->background_info.get_chat_background_object(td));
}
default:
UNREACHABLE();

View File

@ -20608,7 +20608,7 @@ td_api::object_ptr<td_api::ChatActionBar> MessagesManager::get_chat_action_bar_o
return d->action_bar->get_chat_action_bar_object(dialog_type, false);
}
td_api::object_ptr<td_api::background> MessagesManager::get_dialog_background_object(const Dialog *d) const {
td_api::object_ptr<td_api::chatBackground> MessagesManager::get_chat_background_object(const Dialog *d) const {
CHECK(d != nullptr);
if (d->dialog_id.get_type() == DialogType::SecretChat) {
auto user_id = td_->contacts_manager_->get_secret_chat_user_id(d->dialog_id.get_secret_chat_id());
@ -20620,7 +20620,7 @@ td_api::object_ptr<td_api::background> MessagesManager::get_dialog_background_ob
return nullptr;
}
}
return d->background_info.get_background_object(td_);
return d->background_info.get_chat_background_object(td_);
}
string MessagesManager::get_dialog_theme_name(const Dialog *d) const {
@ -20688,7 +20688,7 @@ td_api::object_ptr<td_api::chat> MessagesManager::get_chat_object(const Dialog *
d->server_unread_count + d->local_unread_count, d->last_read_inbox_message_id.get(),
d->last_read_outbox_message_id.get(), d->unread_mention_count, d->unread_reaction_count,
get_chat_notification_settings_object(&d->notification_settings), std::move(available_reactions),
d->message_ttl.get_message_auto_delete_time_object(), get_dialog_background_object(d), get_dialog_theme_name(d),
d->message_ttl.get_message_auto_delete_time_object(), get_chat_background_object(d), get_dialog_theme_name(d),
get_chat_action_bar_object(d), get_video_chat_object(d), get_chat_join_requests_info_object(d),
d->reply_markup_message_id.get(), std::move(draft_message), d->client_data);
}
@ -30594,7 +30594,7 @@ void MessagesManager::send_update_secret_chats_with_user_background(const Dialog
if (secret_chat_d != nullptr && secret_chat_d->is_update_new_chat_sent) {
send_closure(
G()->td(), &Td::send_update,
td_api::make_object<td_api::updateChatBackground>(dialog_id.get(), get_dialog_background_object(user_d)));
td_api::make_object<td_api::updateChatBackground>(dialog_id.get(), get_chat_background_object(user_d)));
}
});
}
@ -30609,7 +30609,7 @@ void MessagesManager::send_update_chat_background(const Dialog *d) {
LOG_CHECK(d->is_update_new_chat_sent) << "Wrong " << d->dialog_id << " in send_update_chat_background";
on_dialog_updated(d->dialog_id, "send_update_chat_background");
send_closure(G()->td(), &Td::send_update,
td_api::make_object<td_api::updateChatBackground>(d->dialog_id.get(), get_dialog_background_object(d)));
td_api::make_object<td_api::updateChatBackground>(d->dialog_id.get(), get_chat_background_object(d)));
send_update_secret_chats_with_user_background(d);
}

View File

@ -2789,7 +2789,7 @@ class MessagesManager final : public Actor {
td_api::object_ptr<td_api::ChatActionBar> get_chat_action_bar_object(const Dialog *d) const;
td_api::object_ptr<td_api::background> get_dialog_background_object(const Dialog *d) const;
td_api::object_ptr<td_api::chatBackground> get_chat_background_object(const Dialog *d) const;
string get_dialog_theme_name(const Dialog *d) const;