From a1af39bc8f42aca15c0877d3f7e9f066224ac2f0 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 22 Dec 2019 20:11:54 +0300 Subject: [PATCH] Better scheme for BackgroundType. GitOrigin-RevId: 01f15daff83986078b9fff55a5e3a70bab2a800b --- td/generate/scheme/td_api.tl | 16 ++++++----- td/generate/scheme/td_api.tlo | Bin 166024 -> 166024 bytes td/telegram/BackgroundType.cpp | 47 +++++++++++++++++++++++---------- td/telegram/BackgroundType.h | 7 ++++- td/telegram/cli.cpp | 21 +++++++++------ 5 files changed, 61 insertions(+), 30 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 40708e92a..211fa4f85 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -2096,8 +2096,13 @@ deviceTokenTizenPush reg_id:string = DeviceToken; pushReceiverId id:int64 = PushReceiverId; -//@description Describes a gradient background @top_color A top color of the background in the RGB24 format @bottom_color A bottom color of the background in the RGB24 format -gradientInfo top_color:int32 bottom_color:int32 = GradientInfo; +//@class BackgroundFill @description Describes a fill of a background + +//@description Describes a solid fill of a background @color A color of the background in the RGB24 format +backgroundFillSolid color:int32 = BackgroundFill; + +//@description Describes a gradient fill of a background @top_color A top color of the background in the RGB24 format @bottom_color A bottom color of the background in the RGB24 format +backgroundFillGradient top_color:int32 bottom_color:int32 = BackgroundFill; //@class BackgroundType @description Describes a type of a background @@ -2113,11 +2118,8 @@ backgroundTypeWallpaper is_blurred:Bool is_moving:Bool = BackgroundType; //@intensity Intensity of the pattern when it is shown above the main background color, 0-100 backgroundTypePattern is_moving:Bool color:int32 intensity:int32 = BackgroundType; -//@description A solid background @color A color of the background in the RGB24 format -backgroundTypeSolid color:int32 = BackgroundType; - -//@description A gradient background @gradient Gradient description -backgroundTypeGradient gradient:gradientInfo = BackgroundType; +//@description A filled background @fill Background fill description +backgroundTypeFill fill:BackgroundFill = BackgroundType; //@description Describes a chat background diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index 3e465e7038c6ab931c3e4bdad40d67a571a9bb03..d4b76d750dc52fdfaa18f9b96d3c2b7f57a64f61 100644 GIT binary patch delta 337 zcmeB}$kj2CYl8xlXy;CWqkK+@$=T^e`K5U&Zkahb3`~;)S;hH1^20tL$xPN4mfh^b zbb@=c1K$B{R*)>i^oQO|@-nh}Y;A;-P>cx9&&f;y89lkc+Q0zBV1RSsYQCi(3%xR|CuT(sRFnJGdQ01k9=_W%F@ delta 333 zcmeB}$kj2CYl8w4zd(Ar0iRQ1a&~%AeraCHWP3*0%@s^1xCL$Ib?o4AFG@_wOwB9t z%uCCk9LOrY*@5qXHY-RE!}N#VO!AX?tYuh0+{rrD5fDa)wFZQ-#kxWVtdao&uF7%6 z{N+;lC!7SgEu^v_H8?*fGX-pfgOx!Bx`s|URaP-n4KOz`fYjbRCgj5iG++AJeNB#Z zsB57Xa@e*V1o;9a4+2HbY&sw@kiN}5Hb257zget_id()) { + case td_api::backgroundFillSolid::ID: { + auto solid = static_cast(fill); + return GradientInfo(solid->color_, solid->color_); + } + case td_api::backgroundFillGradient::ID: { + auto gradient = static_cast(fill); + return GradientInfo(gradient->top_color_, gradient->bottom_color_); + } + default: + UNREACHABLE(); + return {}; + } +} + bool operator==(const GradientInfo &lhs, const GradientInfo &rhs) { return lhs.top_color == rhs.top_color && lhs.bottom_color == rhs.bottom_color; } @@ -110,17 +127,12 @@ Result get_background_type(const td_api::BackgroundType *type) { result = BackgroundType(pattern->is_moving_, pattern->color_, pattern->intensity_); break; } - case td_api::backgroundTypeSolid::ID: { - auto solid = static_cast(type); - result = BackgroundType(solid->color_); - break; - } - case td_api::backgroundTypeGradient::ID: { - auto gradient = static_cast(type); - if (gradient->gradient_ == nullptr) { - return Status::Error(400, "Gradient info must not be empty"); + case td_api::backgroundTypeFill::ID: { + auto fill = static_cast(type); + if (fill->fill_ == nullptr) { + return Status::Error(400, "Fill info must not be empty"); } - result = BackgroundType(GradientInfo(gradient->gradient_->top_color_, gradient->gradient_->bottom_color_)); + result = BackgroundType(get_gradient_info(fill->fill_.get())); break; } default: @@ -173,8 +185,15 @@ BackgroundType get_background_type(bool is_pattern, } } -static td_api::object_ptr get_gradient_info_object(const GradientInfo &gradient) { - return td_api::make_object(gradient.top_color, gradient.bottom_color); +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); + } + return td_api::make_object(gradient.top_color, gradient.bottom_color); } td_api::object_ptr get_background_type_object(const BackgroundType &type) { @@ -184,9 +203,9 @@ td_api::object_ptr get_background_type_object(const Back 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(type.color); + return td_api::make_object(get_background_fill_object(type.color)); case BackgroundType::Type::Gradient: - return td_api::make_object(get_gradient_info_object(type.gradient)); + return td_api::make_object(get_background_fill_object(type.gradient)); default: UNREACHABLE(); return nullptr; diff --git a/td/telegram/BackgroundType.h b/td/telegram/BackgroundType.h index a0586f0f8..6ed6968db 100644 --- a/td/telegram/BackgroundType.h +++ b/td/telegram/BackgroundType.h @@ -22,6 +22,10 @@ struct GradientInfo { GradientInfo() = default; GradientInfo(int32 top_color, int32 bottom_color) : top_color(top_color), bottom_color(bottom_color) { } + + bool is_solid() const { + return top_color == bottom_color; + } }; bool operator==(const GradientInfo &lhs, const GradientInfo &rhs); @@ -44,7 +48,8 @@ struct BackgroundType { } explicit BackgroundType(int32 color) : type(Type::Solid), color(color) { } - BackgroundType(GradientInfo gradient) : type(Type::Gradient), gradient(gradient) { + BackgroundType(GradientInfo gradient) + : type(gradient.is_solid() ? Type::Solid : Type::Gradient), color(gradient.top_color), gradient(gradient) { } bool is_server() const { diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 3cc0dc45f..87b55fc59 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -1271,9 +1271,14 @@ class CliClient final : public Actor { return td_api::make_object(send_date); } - td_api::object_ptr get_gradient_background(int32 top_color, int32 bottom_color) { - auto gradient_info = td_api::make_object(top_color, bottom_color); - return td_api::make_object(std::move(gradient_info)); + td_api::object_ptr get_solid_background(int32 color) { + auto solid = td_api::make_object(color); + return td_api::make_object(std::move(solid)); + } + + td_api::object_ptr get_gradient_background(int32 top_color, int32 bottom_color) { + auto gradient = td_api::make_object(top_color, bottom_color); + return td_api::make_object(std::move(gradient)); } static td_api::object_ptr execute(td_api::object_ptr f) { @@ -2104,9 +2109,9 @@ class CliClient final : public Actor { send_get_background_url(td_api::make_object(false, 0, 0)); send_get_background_url(td_api::make_object(true, 0xFFFFFF, 100)); send_get_background_url(td_api::make_object(true, 0xABCDEF, 49)); - send_get_background_url(td_api::make_object(-1)); - send_get_background_url(td_api::make_object(0xABCDEF)); - send_get_background_url(td_api::make_object(0x1000000)); + send_get_background_url(get_solid_background(-1)); + send_get_background_url(get_solid_background(0xABCDEF)); + send_get_background_url(get_solid_background(0x1000000)); send_get_background_url(get_gradient_background(0xABCDEF, 0xFEDCBA)); send_get_background_url(get_gradient_background(0, 0)); send_get_background_url(get_gradient_background(-1, -1)); @@ -2123,8 +2128,8 @@ class CliClient final : public Actor { td_api::make_object(as_input_file(args)), td_api::make_object(true, 0xabcdef, 49), op == "sbgpd")); } else if (op == "sbgs" || op == "sbgsd") { - send_request(td_api::make_object( - nullptr, td_api::make_object(to_integer(args)), op == "sbgsd")); + send_request(td_api::make_object(nullptr, get_solid_background(to_integer(args)), + op == "sbgsd")); } else if (op == "sbgg" || op == "sbggd") { string top_color; string bottom_color;