diff --git a/td/telegram/BackgroundManager.cpp b/td/telegram/BackgroundManager.cpp index c4bcce3d..72a990db 100644 --- a/td/telegram/BackgroundManager.cpp +++ b/td/telegram/BackgroundManager.cpp @@ -325,8 +325,7 @@ void BackgroundManager::start_up() { log_event_parse(logevent, logevent_string).ensure(); CHECK(logevent.background_.id.is_valid()); - bool needs_file_id = (logevent.background_.type.type != BackgroundType::Type::Solid && - logevent.background_.type.type != BackgroundType::Type::Gradient); + bool needs_file_id = logevent.background_.type.type != BackgroundType::Type::Fill; if (logevent.background_.file_id.is_valid() != needs_file_id) { LOG(ERROR) << "Failed to load " << logevent.background_.id << " of " << logevent.background_.type; G()->td_db()->get_binlog_pmc()->erase(get_background_database_key(for_dark_theme)); @@ -417,15 +416,17 @@ BackgroundId BackgroundManager::search_background(const string &name, Promise(hex_to_integer(name.substr(0, hyphen_pos))); int32 bottom_color = static_cast(hex_to_integer(name.substr(hyphen_pos + 1))); - background_id = add_gradient_background(GradientInfo(top_color, bottom_color)); + fill = BackgroundFill(top_color, bottom_color); } else { int32 color = static_cast(hex_to_integer(name)); - background_id = add_solid_background(color); + fill = BackgroundFill(color); } + auto background_id = add_fill_background(fill); promise.set_value(Unit()); return background_id; } @@ -463,8 +464,7 @@ void BackgroundManager::on_load_background_from_database(string name, string val LOG(INFO) << "Successfully loaded background " << name << " of size " << value.size() << " from database"; Background background; auto status = log_event_parse(background, value); - if (status.is_error() || background.type.type == BackgroundType::Type::Solid || - background.type.type == BackgroundType::Type::Gradient || !background.file_id.is_valid() || + if (status.is_error() || background.type.type == BackgroundType::Type::Fill || !background.file_id.is_valid() || !background.id.is_valid()) { LOG(ERROR) << "Can't load background " << name << ": " << status << ' ' << format::as_hex_dump<4>(Slice(value)); } else { @@ -508,44 +508,23 @@ Result BackgroundManager::prepare_input_file(const tl_object_ptr(color) + 1); +BackgroundId BackgroundManager::add_fill_background(const BackgroundFill &fill, bool is_default, bool is_dark) { + CHECK(0 <= fill.top_color && fill.top_color < 0x1000000); + CHECK(0 <= fill.bottom_color && fill.bottom_color < 0x1000000); + int64 id = fill.is_solid() ? static_cast(fill.top_color) + 1 + : (static_cast(fill.top_color) << 24) + fill.bottom_color + (1 << 24) + 1; + BackgroundId background_id(id); Background background; background.id = background_id; background.is_creator = true; background.is_default = is_default; background.is_dark = is_dark; - background.type = BackgroundType(color); - background.name = background.type.get_link(); - add_background(background); - - return background_id; -} - -BackgroundId BackgroundManager::add_gradient_background(const GradientInfo &gradient_info) { - return add_gradient_background( - gradient_info, false, (gradient_info.top_color & 0x808080) == 0 && (gradient_info.bottom_color & 0x808080) == 0); -} - -BackgroundId BackgroundManager::add_gradient_background(const GradientInfo &gradient_info, bool is_default, - bool is_dark) { - CHECK(0 <= gradient_info.top_color && gradient_info.top_color < 0x1000000); - CHECK(0 <= gradient_info.bottom_color && gradient_info.bottom_color < 0x1000000); - BackgroundId background_id((static_cast(gradient_info.top_color) << 24) + gradient_info.bottom_color + - (1 << 24) + 1); - - Background background; - background.id = background_id; - background.is_creator = true; - background.is_default = is_default; - background.is_dark = is_dark; - background.type = BackgroundType(gradient_info); + background.type = BackgroundType(fill); background.name = background.type.get_link(); add_background(background); @@ -568,16 +547,8 @@ BackgroundId BackgroundManager::set_background(const td_api::InputBackground *in } auto type = r_type.move_as_ok(); - if (type.type == BackgroundType::Type::Solid) { - auto background_id = add_solid_background(type.color); - if (set_background_id_[for_dark_theme] != background_id) { - set_background_id(background_id, type, for_dark_theme); - } - promise.set_value(Unit()); - return background_id; - } - if (type.type == BackgroundType::Type::Gradient) { - auto background_id = add_gradient_background(type.gradient); + if (type.type == BackgroundType::Type::Fill) { + auto background_id = add_fill_background(type.fill); if (set_background_id_[for_dark_theme] != background_id) { set_background_id(background_id, type, for_dark_theme); } @@ -895,7 +866,7 @@ void BackgroundManager::add_background(const Background &background) { file_id_to_background_id_.emplace(result->file_id, result->id); } else { // if file_source_id is valid, then this is a new background with result->file_id == FileId() - // then background.file_id == FileId(), then this is a solid or a gradient background, which can't have file_source_id + // then background.file_id == FileId(), then this is a fill background, which can't have file_source_id CHECK(!file_source_id.is_valid()); } } @@ -941,11 +912,11 @@ BackgroundId BackgroundManager::on_get_background(BackgroundId expected_backgrou auto is_default = (wallpaper->flags_ & telegram_api::wallPaperNoFile::DEFAULT_MASK) != 0; auto is_dark = (wallpaper->flags_ & telegram_api::wallPaperNoFile::DARK_MASK) != 0; + BackgroundFill fill = BackgroundFill(color); if ((settings->flags_ & telegram_api::wallPaperSettings::SECOND_BACKGROUND_COLOR_MASK) != 0) { - return add_gradient_background(GradientInfo(color, settings->second_background_color_), is_default, is_dark); - } else { - return add_solid_background(color, is_default, is_dark); + fill = BackgroundFill(color, settings->second_background_color_); } + return add_fill_background(fill, is_default, is_dark); } auto wallpaper = move_tl_object_as(wallpaper_ptr); diff --git a/td/telegram/BackgroundManager.h b/td/telegram/BackgroundManager.h index c8c32494..8e586b41 100644 --- a/td/telegram/BackgroundManager.h +++ b/td/telegram/BackgroundManager.h @@ -104,13 +104,9 @@ class BackgroundManager : public Actor { void send_update_selected_background(bool for_dark_theme) const; - BackgroundId add_solid_background(int32 color); + BackgroundId add_fill_background(const BackgroundFill &fill); - BackgroundId add_solid_background(int32 color, bool is_default, bool is_dark); - - BackgroundId add_gradient_background(const GradientInfo &gradient_info); - - BackgroundId add_gradient_background(const GradientInfo &gradient_info, bool is_default, bool is_dark); + BackgroundId add_fill_background(const BackgroundFill &fill, bool is_default, bool is_dark); void add_background(const Background &background); diff --git a/td/telegram/BackgroundType.cpp b/td/telegram/BackgroundType.cpp index 25b88a94..cd56938c 100644 --- a/td/telegram/BackgroundType.cpp +++ b/td/telegram/BackgroundType.cpp @@ -18,16 +18,16 @@ static string get_color_hex_string(int32 color) { return result; } -static GradientInfo get_gradient_info(const td_api::BackgroundFill *fill) { +static BackgroundFill get_background_fill(const td_api::BackgroundFill *fill) { CHECK(fill != nullptr); switch (fill->get_id()) { case td_api::backgroundFillSolid::ID: { auto solid = static_cast(fill); - return GradientInfo(solid->color_, solid->color_); + return BackgroundFill(solid->color_); } case td_api::backgroundFillGradient::ID: { auto gradient = static_cast(fill); - return GradientInfo(gradient->top_color_, gradient->bottom_color_); + return BackgroundFill(gradient->top_color_, gradient->bottom_color_); } default: UNREACHABLE(); @@ -35,7 +35,15 @@ static GradientInfo get_gradient_info(const td_api::BackgroundFill *fill) { } } -bool operator==(const GradientInfo &lhs, const GradientInfo &rhs) { +static string get_background_fill_color_hex_string(const BackgroundFill &fill) { + if (fill.is_solid()) { + return get_color_hex_string(fill.top_color); + } else { + return PSTRING() << get_color_hex_string(fill.top_color) << '-' << get_color_hex_string(fill.bottom_color); + } +} + +bool operator==(const BackgroundFill &lhs, const BackgroundFill &rhs) { return lhs.top_color == rhs.top_color && lhs.bottom_color == rhs.bottom_color; } @@ -59,18 +67,16 @@ string BackgroundType::get_link() const { return string(); } case BackgroundType::Type::Pattern: { - string link = PSTRING() << "intensity=" << intensity << "&bg_color=" << get_color_hex_string(color); + string link = PSTRING() << "intensity=" << intensity + << "&bg_color=" << get_background_fill_color_hex_string(fill); if (!mode.empty()) { link += "&mode="; link += mode; } return link; } - case BackgroundType::Type::Solid: - return get_color_hex_string(color); - case BackgroundType::Type::Gradient: - return PSTRING() << get_color_hex_string(gradient.top_color) << '-' - << get_color_hex_string(gradient.bottom_color); + case BackgroundType::Type::Fill: + return get_background_fill_color_hex_string(fill); default: UNREACHABLE(); return string(); @@ -79,7 +85,7 @@ string BackgroundType::get_link() const { bool operator==(const BackgroundType &lhs, const BackgroundType &rhs) { return lhs.type == rhs.type && lhs.is_blurred == rhs.is_blurred && lhs.is_moving == rhs.is_moving && - lhs.color == rhs.color && lhs.intensity == rhs.intensity && lhs.gradient == rhs.gradient; + lhs.intensity == rhs.intensity && lhs.fill == rhs.fill; } static StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType::Type &type) { @@ -88,10 +94,8 @@ static StringBuilder &operator<<(StringBuilder &string_builder, const Background return string_builder << "Wallpaper"; case BackgroundType::Type::Pattern: return string_builder << "Pattern"; - case BackgroundType::Type::Solid: - return string_builder << "Solid"; - case BackgroundType::Type::Gradient: - return string_builder << "Gradient"; + case BackgroundType::Type::Fill: + return string_builder << "Fill"; default: UNREACHABLE(); return string_builder; @@ -132,7 +136,7 @@ Result get_background_type(const td_api::BackgroundType *type) { if (fill->fill_ == nullptr) { return Status::Error(400, "Fill info must not be empty"); } - result = BackgroundType(get_gradient_info(fill->fill_.get())); + result = BackgroundType(get_background_fill(fill->fill_.get())); break; } default: @@ -141,13 +145,10 @@ Result get_background_type(const td_api::BackgroundType *type) { if (!is_valid_intensity(result.intensity)) { return Status::Error(400, "Wrong intensity value"); } - if (!is_valid_color(result.color)) { - return Status::Error(400, "Wrong color value"); + if (!is_valid_color(result.fill.top_color)) { + return Status::Error(400, result.fill.is_solid() ? Slice("Wrong color value") : ("Wrong top color value")); } - if (!is_valid_color(result.gradient.top_color)) { - return Status::Error(400, "Wrong top color value"); - } - if (!is_valid_color(result.gradient.bottom_color)) { + if (!is_valid_color(result.fill.bottom_color)) { return Status::Error(400, "Wrong bottom color value"); } return result; @@ -185,15 +186,11 @@ BackgroundType get_background_type(bool is_pattern, } } -static td_api::object_ptr get_background_fill_object(int32 color) { - return td_api::make_object(color); -} - -static td_api::object_ptr get_background_fill_object(const GradientInfo &gradient) { - if (gradient.is_solid()) { - return get_background_fill_object(gradient.top_color); +static td_api::object_ptr get_background_fill_object(const BackgroundFill &fill) { + if (fill.is_solid()) { + return td_api::make_object(fill.top_color); } - return td_api::make_object(gradient.top_color, gradient.bottom_color); + return td_api::make_object(fill.top_color, fill.bottom_color); } td_api::object_ptr get_background_type_object(const BackgroundType &type) { @@ -201,11 +198,9 @@ td_api::object_ptr get_background_type_object(const Back case BackgroundType::Type::Wallpaper: return td_api::make_object(type.is_blurred, type.is_moving); case BackgroundType::Type::Pattern: - return td_api::make_object(type.is_moving, type.color, type.intensity); - case BackgroundType::Type::Solid: - return td_api::make_object(get_background_fill_object(type.color)); - case BackgroundType::Type::Gradient: - return td_api::make_object(get_background_fill_object(type.gradient)); + return td_api::make_object(type.is_moving, type.fill.top_color, type.intensity); + case BackgroundType::Type::Fill: + return td_api::make_object(get_background_fill_object(type.fill)); default: UNREACHABLE(); return nullptr; @@ -220,15 +215,18 @@ telegram_api::object_ptr get_input_wallpaper_se if (type.is_moving) { flags |= telegram_api::wallPaperSettings::MOTION_MASK; } - if (type.color != 0) { + if (type.fill.top_color != 0 || type.fill.bottom_color != 0) { flags |= telegram_api::wallPaperSettings::BACKGROUND_COLOR_MASK; } + if (!type.fill.is_solid()) { + flags |= telegram_api::wallPaperSettings::SECOND_BACKGROUND_COLOR_MASK; + } if (type.intensity) { flags |= telegram_api::wallPaperSettings::INTENSITY_MASK; } if (type.is_server()) { - return telegram_api::make_object(flags, false /*ignored*/, false /*ignored*/, - type.color, 0, type.intensity, 0); + return telegram_api::make_object( + flags, false /*ignored*/, false /*ignored*/, type.fill.top_color, type.fill.bottom_color, type.intensity, 0); } UNREACHABLE(); diff --git a/td/telegram/BackgroundType.h b/td/telegram/BackgroundType.h index 6ed6968d..29f93d6c 100644 --- a/td/telegram/BackgroundType.h +++ b/td/telegram/BackgroundType.h @@ -15,12 +15,14 @@ namespace td { -struct GradientInfo { +struct BackgroundFill { int32 top_color = 0; int32 bottom_color = 0; - GradientInfo() = default; - GradientInfo(int32 top_color, int32 bottom_color) : top_color(top_color), bottom_color(bottom_color) { + BackgroundFill() = default; + explicit BackgroundFill(int32 solid_color) : top_color(solid_color), bottom_color(solid_color) { + } + BackgroundFill(int32 top_color, int32 bottom_color) : top_color(top_color), bottom_color(bottom_color) { } bool is_solid() const { @@ -28,28 +30,24 @@ struct GradientInfo { } }; -bool operator==(const GradientInfo &lhs, const GradientInfo &rhs); +bool operator==(const BackgroundFill &lhs, const BackgroundFill &rhs); struct BackgroundType { - enum class Type : int32 { Wallpaper, Pattern, Solid, Gradient }; - Type type = Type::Solid; + enum class Type : int32 { Wallpaper, Pattern, Fill }; + Type type = Type::Fill; bool is_blurred = false; bool is_moving = false; - int32 color = 0; int32 intensity = 0; - GradientInfo gradient; + BackgroundFill fill; BackgroundType() = default; BackgroundType(bool is_blurred, bool is_moving) : type(Type::Wallpaper), is_blurred(is_blurred), is_moving(is_moving) { } BackgroundType(bool is_moving, int32 color, int32 intensity) - : type(Type::Pattern), is_moving(is_moving), color(color), intensity(intensity) { + : type(Type::Pattern), is_moving(is_moving), fill(color), intensity(intensity) { } - explicit BackgroundType(int32 color) : type(Type::Solid), color(color) { - } - BackgroundType(GradientInfo gradient) - : type(gradient.is_solid() ? Type::Solid : Type::Gradient), color(gradient.top_color), gradient(gradient) { + BackgroundType(BackgroundFill fill) : type(Type::Fill), fill(fill) { } bool is_server() const { diff --git a/td/telegram/BackgroundType.hpp b/td/telegram/BackgroundType.hpp index 7813e151..5b9b589b 100644 --- a/td/telegram/BackgroundType.hpp +++ b/td/telegram/BackgroundType.hpp @@ -12,64 +12,54 @@ namespace td { -template -void store(const GradientInfo &gradient, StorerT &storer) { - BEGIN_STORE_FLAGS(); - END_STORE_FLAGS(); - store(gradient.top_color, storer); - store(gradient.bottom_color, storer); -} - -template -void parse(GradientInfo &gradient, ParserT &parser) { - BEGIN_PARSE_FLAGS(); - END_PARSE_FLAGS(); - parse(gradient.top_color, parser); - parse(gradient.bottom_color, parser); -} - template void store(const BackgroundType &type, StorerT &storer) { - bool has_color = type.color != 0; + bool has_fill = type.fill.top_color != 0 || type.fill.bottom_color != 0; bool has_intensity = type.intensity != 0; + bool is_gradient = !type.fill.is_solid(); BEGIN_STORE_FLAGS(); STORE_FLAG(type.is_blurred); STORE_FLAG(type.is_moving); - STORE_FLAG(has_color); + STORE_FLAG(has_fill); STORE_FLAG(has_intensity); + STORE_FLAG(is_gradient); END_STORE_FLAGS(); store(type.type, storer); - if (has_color) { - store(type.color, storer); + if (has_fill) { + store(type.fill.top_color, storer); + if (is_gradient) { + store(type.fill.bottom_color, storer); + } } if (has_intensity) { store(type.intensity, storer); } - if (type.type == BackgroundType::Type::Gradient) { - store(type.gradient, storer); - } } template void parse(BackgroundType &type, ParserT &parser) { - bool has_color; + bool has_fill; bool has_intensity; + bool is_gradient; BEGIN_PARSE_FLAGS(); PARSE_FLAG(type.is_blurred); PARSE_FLAG(type.is_moving); - PARSE_FLAG(has_color); + PARSE_FLAG(has_fill); PARSE_FLAG(has_intensity); + PARSE_FLAG(is_gradient); END_PARSE_FLAGS(); parse(type.type, parser); - if (has_color) { - parse(type.color, parser); + if (has_fill) { + parse(type.fill.top_color, parser); + if (is_gradient) { + parse(type.fill.bottom_color, parser); + } else { + type.fill.bottom_color = type.fill.top_color; + } } if (has_intensity) { parse(type.intensity, parser); } - if (type.type == BackgroundType::Type::Gradient) { - parse(type.gradient, parser); - } } } // namespace td