Make BackgroundFill fields private.

This commit is contained in:
levlam 2021-06-10 02:16:46 +03:00
parent 636aa3f74e
commit ab4373833b
2 changed files with 34 additions and 25 deletions

View File

@ -77,7 +77,7 @@ BackgroundFill::BackgroundFill(const telegram_api::wallPaperSettings *settings)
} }
} }
static Result<BackgroundFill> get_background_fill(const td_api::BackgroundFill *fill) { Result<BackgroundFill> BackgroundFill::get_background_fill(const td_api::BackgroundFill *fill) {
if (fill == nullptr) { if (fill == nullptr) {
return Status::Error(400, "Background fill info must be non-empty"); return Status::Error(400, "Background fill info must be non-empty");
} }
@ -181,19 +181,19 @@ Result<BackgroundFill> BackgroundFill::get_background_fill(Slice name) {
return BackgroundFill(color); return BackgroundFill(color);
} }
static string get_background_fill_color_hex_string(const BackgroundFill &fill, bool is_first) { string BackgroundFill::get_link(bool is_first) const {
switch (fill.get_type()) { switch (get_type()) {
case BackgroundFill::Type::Solid: case BackgroundFill::Type::Solid:
return get_color_hex_string(fill.top_color_); return get_color_hex_string(top_color_);
case BackgroundFill::Type::Gradient: case BackgroundFill::Type::Gradient:
return PSTRING() << get_color_hex_string(fill.top_color_) << '-' << get_color_hex_string(fill.bottom_color_) return PSTRING() << get_color_hex_string(top_color_) << '-' << get_color_hex_string(bottom_color_)
<< (is_first ? '?' : '&') << "rotation=" << fill.rotation_angle_; << (is_first ? '?' : '&') << "rotation=" << rotation_angle_;
case BackgroundFill::Type::FreeformGradient: { case BackgroundFill::Type::FreeformGradient: {
SliceBuilder sb; SliceBuilder sb;
sb << get_color_hex_string(fill.top_color_) << '~' << get_color_hex_string(fill.bottom_color_) << '~' sb << get_color_hex_string(top_color_) << '~' << get_color_hex_string(bottom_color_) << '~'
<< get_color_hex_string(fill.third_color_); << get_color_hex_string(third_color_);
if (fill.fourth_color_ != -1) { if (fourth_color_ != -1) {
sb << '~' << get_color_hex_string(fill.fourth_color_); sb << '~' << get_color_hex_string(fourth_color_);
} }
return sb.as_cslice().str(); return sb.as_cslice().str();
} }
@ -319,8 +319,7 @@ string BackgroundType::get_link() const {
return string(); return string();
} }
case Type::Pattern: { case Type::Pattern: {
string link = PSTRING() << "intensity=" << intensity_ string link = PSTRING() << "intensity=" << intensity_ << "&bg_color=" << fill_.get_link(false);
<< "&bg_color=" << get_background_fill_color_hex_string(fill_, false);
if (!mode.empty()) { if (!mode.empty()) {
link += "&mode="; link += "&mode=";
link += mode; link += mode;
@ -328,7 +327,7 @@ string BackgroundType::get_link() const {
return link; return link;
} }
case Type::Fill: case Type::Fill:
return get_background_fill_color_hex_string(fill_, true); return fill_.get_link(true);
default: default:
UNREACHABLE(); UNREACHABLE();
return string(); return string();
@ -376,7 +375,7 @@ Result<BackgroundType> BackgroundType::get_background_type(const td_api::Backgro
} }
case td_api::backgroundTypePattern::ID: { case td_api::backgroundTypePattern::ID: {
auto pattern_type = static_cast<const td_api::backgroundTypePattern *>(background_type); auto pattern_type = static_cast<const td_api::backgroundTypePattern *>(background_type);
TRY_RESULT(background_fill, ::td::get_background_fill(pattern_type->fill_.get())); TRY_RESULT(background_fill, BackgroundFill::get_background_fill(pattern_type->fill_.get()));
if (!is_valid_intensity(pattern_type->intensity_)) { if (!is_valid_intensity(pattern_type->intensity_)) {
return Status::Error(400, "Wrong intensity value"); return Status::Error(400, "Wrong intensity value");
} }
@ -384,7 +383,7 @@ Result<BackgroundType> BackgroundType::get_background_type(const td_api::Backgro
} }
case td_api::backgroundTypeFill::ID: { case td_api::backgroundTypeFill::ID: {
auto fill_type = static_cast<const td_api::backgroundTypeFill *>(background_type); auto fill_type = static_cast<const td_api::backgroundTypeFill *>(background_type);
TRY_RESULT(background_fill, ::td::get_background_fill(fill_type->fill_.get())); TRY_RESULT(background_fill, BackgroundFill::get_background_fill(fill_type->fill_.get()));
return BackgroundType(std::move(background_fill)); return BackgroundType(std::move(background_fill));
} }
default: default:
@ -421,15 +420,14 @@ BackgroundType::BackgroundType(bool is_fill, bool is_pattern,
} }
} }
static td_api::object_ptr<td_api::BackgroundFill> get_background_fill_object(const BackgroundFill &fill) { td_api::object_ptr<td_api::BackgroundFill> BackgroundFill::get_background_fill_object() const {
switch (fill.get_type()) { switch (get_type()) {
case BackgroundFill::Type::Solid: case BackgroundFill::Type::Solid:
return td_api::make_object<td_api::backgroundFillSolid>(fill.top_color_); return td_api::make_object<td_api::backgroundFillSolid>(top_color_);
case BackgroundFill::Type::Gradient: case BackgroundFill::Type::Gradient:
return td_api::make_object<td_api::backgroundFillGradient>(fill.top_color_, fill.bottom_color_, return td_api::make_object<td_api::backgroundFillGradient>(top_color_, bottom_color_, rotation_angle_);
fill.rotation_angle_);
case BackgroundFill::Type::FreeformGradient: { case BackgroundFill::Type::FreeformGradient: {
vector<int32> colors{fill.top_color_, fill.bottom_color_, fill.third_color_, fill.fourth_color_}; vector<int32> colors{top_color_, bottom_color_, third_color_, fourth_color_};
if (colors.back() == -1) { if (colors.back() == -1) {
colors.pop_back(); colors.pop_back();
} }
@ -446,10 +444,10 @@ td_api::object_ptr<td_api::BackgroundType> BackgroundType::get_background_type_o
case Type::Wallpaper: case Type::Wallpaper:
return td_api::make_object<td_api::backgroundTypeWallpaper>(is_blurred_, is_moving_); return td_api::make_object<td_api::backgroundTypeWallpaper>(is_blurred_, is_moving_);
case Type::Pattern: case Type::Pattern:
return td_api::make_object<td_api::backgroundTypePattern>(get_background_fill_object(fill_), intensity_, return td_api::make_object<td_api::backgroundTypePattern>(fill_.get_background_fill_object(), intensity_,
is_moving_); is_moving_);
case Type::Fill: case Type::Fill:
return td_api::make_object<td_api::backgroundTypeFill>(get_background_fill_object(fill_)); return td_api::make_object<td_api::backgroundTypeFill>(fill_.get_background_fill_object());
default: default:
UNREACHABLE(); UNREACHABLE();
return nullptr; return nullptr;

View File

@ -16,7 +16,7 @@
namespace td { namespace td {
struct BackgroundFill { class BackgroundFill {
int32 top_color_ = 0; int32 top_color_ = 0;
int32 bottom_color_ = 0; int32 bottom_color_ = 0;
int32 rotation_angle_ = 0; int32 rotation_angle_ = 0;
@ -35,7 +35,11 @@ struct BackgroundFill {
explicit BackgroundFill(const telegram_api::wallPaperSettings *settings); explicit BackgroundFill(const telegram_api::wallPaperSettings *settings);
static Result<BackgroundFill> get_background_fill(Slice name); static Result<BackgroundFill> get_background_fill(const td_api::BackgroundFill *fill);
string get_link(bool is_first) const;
td_api::object_ptr<td_api::BackgroundFill> get_background_fill_object() const;
enum class Type : int32 { Solid, Gradient, FreeformGradient }; enum class Type : int32 { Solid, Gradient, FreeformGradient };
Type get_type() const { Type get_type() const {
@ -48,6 +52,13 @@ struct BackgroundFill {
return Type::Gradient; return Type::Gradient;
} }
friend bool operator==(const BackgroundFill &lhs, const BackgroundFill &rhs);
friend class BackgroundType;
public:
static Result<BackgroundFill> get_background_fill(Slice name);
int64 get_id() const; int64 get_id() const;
bool is_dark() const; bool is_dark() const;