Add BackgroundType::get_link() method.

GitOrigin-RevId: be2522d94742608e61adbef223a9b161e87b6aa3
This commit is contained in:
levlam 2019-12-22 04:34:39 +03:00
parent e1f1253e21
commit 8be871bad7
3 changed files with 81 additions and 61 deletions

View File

@ -361,46 +361,18 @@ void BackgroundManager::get_backgrounds(Promise<Unit> &&promise) {
Result<string> BackgroundManager::get_background_url(const string &name, Result<string> BackgroundManager::get_background_url(const string &name,
td_api::object_ptr<td_api::BackgroundType> background_type) const { td_api::object_ptr<td_api::BackgroundType> background_type) const {
TRY_RESULT(type, get_background_type(background_type.get())); TRY_RESULT(type, get_background_type(background_type.get()));
auto url = PSTRING() << G()->shared_config().get_option_string("t_me_url", "https://t.me/") << "bg/";
vector<string> modes; auto link = type.get_link();
if (type.is_blurred) { if (type.is_server()) {
modes.emplace_back("blur"); url += name;
} if (!link.empty()) {
if (type.is_moving) { url += '?';
modes.emplace_back("motion"); url += link;
} }
string mode = implode(modes, '+'); } else {
url += link;
string url = PSTRING() << G()->shared_config().get_option_string("t_me_url", "https://t.me/") << "bg/";
switch (type.type) {
case BackgroundType::Type::Wallpaper:
url += name;
if (!mode.empty()) {
url += "?mode=";
url += mode;
}
return url;
case BackgroundType::Type::Pattern:
url += name;
url += "?intensity=";
url += to_string(type.intensity);
url += "&bg_color=";
url += type.get_color_hex_string();
if (!mode.empty()) {
url += "&mode=";
url += mode;
}
return url;
case BackgroundType::Type::Solid:
url += type.get_color_hex_string();
return url;
case BackgroundType::Type::Gradient:
url += type.gradient.get_colors_hex_string();
return url;
default:
UNREACHABLE();
return url;
} }
return url;
} }
void BackgroundManager::reload_background_from_server( void BackgroundManager::reload_background_from_server(
@ -612,6 +584,7 @@ BackgroundId BackgroundManager::set_background(const td_api::InputBackground *in
promise.set_value(Unit()); promise.set_value(Unit());
return background_id; return background_id;
} }
CHECK(type.is_server());
if (input_background == nullptr) { if (input_background == nullptr) {
promise.set_error(Status::Error(400, "Input background must be non-empty")); promise.set_error(Status::Error(400, "Input background must be non-empty"));
@ -812,7 +785,7 @@ void BackgroundManager::remove_background(BackgroundId background_id, Promise<Un
std::move(promise)); std::move(promise));
}); });
if (background->type.type == BackgroundType::Type::Solid || background->type.type == BackgroundType::Type::Gradient) { if (!background->type.is_server()) {
return query_promise.set_value(Unit()); return query_promise.set_value(Unit());
} }

View File

@ -22,33 +22,76 @@ string GradientInfo::get_colors_hex_string() const {
return PSTRING() << get_color_hex_string(top_color) << '-' << get_color_hex_string(bottom_color); return PSTRING() << get_color_hex_string(top_color) << '-' << get_color_hex_string(bottom_color);
} }
bool operator==(const GradientInfo &lhs, const GradientInfo &rhs) {
return lhs.top_color == rhs.top_color && lhs.bottom_color == rhs.bottom_color;
}
string BackgroundType::get_color_hex_string() const { string BackgroundType::get_color_hex_string() const {
return td::get_color_hex_string(color); return td::get_color_hex_string(color);
} }
bool operator==(const BackgroundType &lhs, const BackgroundType &rhs) { string BackgroundType::get_link() const {
return lhs.type == rhs.type && lhs.is_blurred == rhs.is_blurred && lhs.is_moving == rhs.is_moving && string mode;
lhs.color == rhs.color && lhs.intensity == rhs.intensity; if (is_blurred) {
mode = "blur";
}
if (is_moving) {
if (!mode.empty()) {
mode += '+';
}
mode += "motion";
}
switch (type) {
case BackgroundType::Type::Wallpaper: {
if (!mode.empty()) {
return PSTRING() << "mode=" << mode;
}
return string();
}
case BackgroundType::Type::Pattern: {
string link = PSTRING() << "intensity=" << intensity << "&bg_color=" << get_color_hex_string();
if (!mode.empty()) {
link += "&mode=";
link += mode;
}
return link;
}
case BackgroundType::Type::Solid:
return get_color_hex_string();
case BackgroundType::Type::Gradient:
return gradient.get_colors_hex_string();
default:
UNREACHABLE();
return string();
}
} }
StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &type) { bool operator==(const BackgroundType &lhs, const BackgroundType &rhs) {
switch (type.type) { 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;
}
static StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType::Type &type) {
switch (type) {
case BackgroundType::Type::Wallpaper: case BackgroundType::Type::Wallpaper:
return string_builder << "type Wallpaper[" << (type.is_blurred ? "blurred" : "") << ' ' return string_builder << "Wallpaper";
<< (type.is_moving ? "moving" : "") << ']';
case BackgroundType::Type::Pattern: case BackgroundType::Type::Pattern:
return string_builder << "type Pattern[" << (type.is_moving ? "moving" : "") << ' ' << type.get_color_hex_string() return string_builder << "Pattern";
<< ' ' << type.intensity << ']';
case BackgroundType::Type::Solid: case BackgroundType::Type::Solid:
return string_builder << "type Solid[" << type.get_color_hex_string() << ']'; return string_builder << "Solid";
case BackgroundType::Type::Gradient: case BackgroundType::Type::Gradient:
return string_builder << "type Gradient[" << type.gradient.get_colors_hex_string() << ']'; return string_builder << "Gradient";
default: default:
UNREACHABLE(); UNREACHABLE();
return string_builder; return string_builder;
} }
} }
StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &type) {
return string_builder << "type " << type.type << '[' << type.get_link() << ']';
}
static bool is_valid_color(int32 color) { static bool is_valid_color(int32 color) {
return 0 <= color && color <= 0xFFFFFF; return 0 <= color && color <= 0xFFFFFF;
} }
@ -171,17 +214,13 @@ telegram_api::object_ptr<telegram_api::wallPaperSettings> get_input_wallpaper_se
if (type.intensity) { if (type.intensity) {
flags |= telegram_api::wallPaperSettings::INTENSITY_MASK; flags |= telegram_api::wallPaperSettings::INTENSITY_MASK;
} }
switch (type.type) { if (type.is_server()) {
case BackgroundType::Type::Wallpaper: return telegram_api::make_object<telegram_api::wallPaperSettings>(flags, false /*ignored*/, false /*ignored*/,
case BackgroundType::Type::Pattern: type.color, 0, type.intensity, 0);
return telegram_api::make_object<telegram_api::wallPaperSettings>(flags, false /*ignored*/, false /*ignored*/,
type.color, 0, type.intensity, 0);
case BackgroundType::Type::Solid:
case BackgroundType::Type::Gradient:
default:
UNREACHABLE();
return nullptr;
} }
UNREACHABLE();
return nullptr;
} }
} // namespace td } // namespace td

View File

@ -28,6 +28,8 @@ struct GradientInfo {
string get_colors_hex_string() const; string get_colors_hex_string() const;
}; };
bool operator==(const GradientInfo &lhs, const GradientInfo &rhs);
struct BackgroundType { struct BackgroundType {
enum class Type : int32 { Wallpaper, Pattern, Solid, Gradient }; enum class Type : int32 { Wallpaper, Pattern, Solid, Gradient };
Type type = Type::Solid; Type type = Type::Solid;
@ -49,6 +51,12 @@ struct BackgroundType {
BackgroundType(GradientInfo gradient) : type(Type::Gradient), gradient(gradient) { BackgroundType(GradientInfo gradient) : type(Type::Gradient), gradient(gradient) {
} }
bool is_server() const {
return type == Type::Wallpaper || type == Type::Pattern;
}
string get_link() const;
string get_color_hex_string() const; string get_color_hex_string() const;
}; };