Use peer colors uniformly with mobile apps.

This commit is contained in:
John Preston 2017-12-01 14:21:40 +04:00
parent 36fe4ff327
commit 3bdce06e19
6 changed files with 73 additions and 56 deletions

View file

@ -478,7 +478,7 @@ void Panel::createUserpicCache(ImagePtr image) {
filled.setDevicePixelRatio(cRetinaFactor()); filled.setDevicePixelRatio(cRetinaFactor());
{ {
Painter p(&filled); Painter p(&filled);
EmptyUserpic(_user->colorIndex(), _user->name).paintSquare(p, 0, 0, st::callWidth, st::callWidth); EmptyUserpic(_user->id, _user->name).paintSquare(p, 0, 0, st::callWidth, st::callWidth);
} }
Images::prepareRound(filled, ImageRoundRadius::Large, ImageRoundCorner::TopLeft | ImageRoundCorner::TopRight); Images::prepareRound(filled, ImageRoundRadius::Large, ImageRoundCorner::TopLeft | ImageRoundCorner::TopRight);
_userPhoto = App::pixmapFromImageInPlace(std::move(filled)); _userPhoto = App::pixmapFromImageInPlace(std::move(filled));

View file

@ -57,10 +57,8 @@ ImagePtr generateUserpicImage(const style::icon &icon) {
return ImagePtr(App::pixmapFromImageInPlace(std::move(data)), "PNG"); return ImagePtr(App::pixmapFromImageInPlace(std::move(data)), "PNG");
} }
} // namespace style::color PeerUserpicColor(PeerId peerId) {
const style::color colors[] = {
style::color peerUserpicColor(int index) {
static style::color peerColors[kUserColorsCount] = {
st::historyPeer1UserpicBg, st::historyPeer1UserpicBg,
st::historyPeer2UserpicBg, st::historyPeer2UserpicBg,
st::historyPeer3UserpicBg, st::historyPeer3UserpicBg,
@ -70,12 +68,25 @@ style::color peerUserpicColor(int index) {
st::historyPeer7UserpicBg, st::historyPeer7UserpicBg,
st::historyPeer8UserpicBg, st::historyPeer8UserpicBg,
}; };
return peerColors[index]; return colors[PeerColorIndex(peerId)];
}
} // namespace
int PeerColorIndex(int32 bareId) {
const auto index = std::abs(bareId) % 7;
const int map[] = { 0, 7, 4, 1, 6, 3, 5 };
return map[index];
}
int PeerColorIndex(PeerId peerId) {
return PeerColorIndex(peerToBareInt(peerId));
} }
class EmptyUserpic::Impl { class EmptyUserpic::Impl {
public: public:
Impl(int index, const QString &name) : _color(peerUserpicColor(index)) { Impl(PeerId peerId, const QString &name)
: _color(PeerUserpicColor(peerId)) {
fillString(name); fillString(name);
} }
@ -196,11 +207,16 @@ void EmptyUserpic::Impl::fillString(const QString &name) {
EmptyUserpic::EmptyUserpic() = default; EmptyUserpic::EmptyUserpic() = default;
EmptyUserpic::EmptyUserpic(int index, const QString &name) : _impl(std::make_unique<Impl>(index, name)) { EmptyUserpic::EmptyUserpic(PeerId peerId, const QString &name)
: _impl(std::make_unique<Impl>(peerId, name)) {
} }
void EmptyUserpic::set(int index, const QString &name) { EmptyUserpic::EmptyUserpic(const QString &nonce, const QString &name)
_impl = std::make_unique<Impl>(index, name); : EmptyUserpic(qHash(nonce), name) {
}
void EmptyUserpic::set(PeerId peerId, const QString &name) {
_impl = std::make_unique<Impl>(peerId, name);
} }
void EmptyUserpic::clear() { void EmptyUserpic::clear() {
@ -269,10 +285,9 @@ void PeerClickHandler::onClick(Qt::MouseButton button) const {
} }
PeerData::PeerData(const PeerId &id) PeerData::PeerData(const PeerId &id)
: id(id) : id(id) {
, _colorIndex(peerColorIndex(id)) {
nameText.setText(st::msgNameStyle, QString(), _textNameOptions); nameText.setText(st::msgNameStyle, QString(), _textNameOptions);
_userpicEmpty.set(_colorIndex, QString()); _userpicEmpty.set(id, QString());
} }
void PeerData::updateNameDelayed( void PeerData::updateNameDelayed(
@ -298,7 +313,7 @@ void PeerData::updateNameDelayed(
name = newName; name = newName;
nameText.setText(st::msgNameStyle, name, _textNameOptions); nameText.setText(st::msgNameStyle, name, _textNameOptions);
if (useEmptyUserpic()) { if (useEmptyUserpic()) {
_userpicEmpty.set(_colorIndex, name); _userpicEmpty.set(id, name);
} }
Notify::PeerUpdate update(this); Notify::PeerUpdate update(this);
@ -337,7 +352,7 @@ void PeerData::setUserpic(
_userpic = userpic; _userpic = userpic;
_userpicLocation = location; _userpicLocation = location;
if (useEmptyUserpic()) { if (useEmptyUserpic()) {
_userpicEmpty.set(_colorIndex, name); _userpicEmpty.set(id, name);
} else { } else {
_userpicEmpty.clear(); _userpicEmpty.clear();
} }

View file

@ -67,16 +67,16 @@ inline bool isNotifyMuted(
return false; return false;
} }
static constexpr int kUserColorsCount = 8; int PeerColorIndex(PeerId peerId);
static constexpr int kChatColorsCount = 4; int PeerColorIndex(int32 bareId);
static constexpr int kChannelColorsCount = 4;
class EmptyUserpic { class EmptyUserpic {
public: public:
EmptyUserpic(); EmptyUserpic();
EmptyUserpic(int index, const QString &name); EmptyUserpic(PeerId peerId, const QString &name);
EmptyUserpic(const QString &nonce, const QString &name);
void set(int index, const QString &name); void set(PeerId peerId, const QString &name);
void clear(); void clear();
explicit operator bool() const { explicit operator bool() const {
@ -222,9 +222,6 @@ public:
LoadedStatus loadedStatus = NotLoaded; LoadedStatus loadedStatus = NotLoaded;
MTPinputPeer input; MTPinputPeer input;
int colorIndex() const {
return _colorIndex;
}
void setUserpic(ImagePtr userpic, StorageImageLocation location); void setUserpic(ImagePtr userpic, StorageImageLocation location);
void paintUserpic( void paintUserpic(
Painter &p, Painter &p,
@ -308,7 +305,6 @@ private:
NameWords _nameWords; // for filtering NameWords _nameWords; // for filtering
NameFirstChars _nameFirstChars; NameFirstChars _nameFirstChars;
int _colorIndex = 0;
TimeMs _lastFullUpdate = 0; TimeMs _lastFullUpdate = 0;
}; };

View file

@ -2981,7 +2981,9 @@ void HistoryContact::initDimensions() {
if (_contact) { if (_contact) {
_contact->loadUserpic(); _contact->loadUserpic();
} else { } else {
_photoEmpty.set(qAbs(_userId ? _userId : _parent->id) % kUserColorsCount, _name.originalText()); _photoEmpty.set(
_userId ? _userId : _parent->id,
_name.originalText());
} }
if (_contact && _contact->contact > 0) { if (_contact && _contact->contact > 0) {
_linkl = sendMessageClickHandler(_contact); _linkl = sendMessageClickHandler(_contact);

View file

@ -50,37 +50,38 @@ inline void initTextOptions() {
_textDlgOptions.maxw = st::columnMaximalWidthLeft * 2; _textDlgOptions.maxw = st::columnMaximalWidthLeft * 2;
} }
style::color fromNameFg(int index) { style::color FromNameFg(not_null<PeerData*> peer, bool selected) {
Expects(index >= 0 && index < 8); if (selected) {
style::color colors[] = { const style::color colors[] = {
st::historyPeer1NameFg, st::historyPeer1NameFgSelected,
st::historyPeer2NameFg, st::historyPeer2NameFgSelected,
st::historyPeer3NameFg, st::historyPeer3NameFgSelected,
st::historyPeer4NameFg, st::historyPeer4NameFgSelected,
st::historyPeer5NameFg, st::historyPeer5NameFgSelected,
st::historyPeer6NameFg, st::historyPeer6NameFgSelected,
st::historyPeer7NameFg, st::historyPeer7NameFgSelected,
st::historyPeer8NameFg, st::historyPeer8NameFgSelected,
}; };
return colors[index]; return colors[PeerColorIndex(peer->id)];
} else {
const style::color colors[] = {
st::historyPeer1NameFg,
st::historyPeer2NameFg,
st::historyPeer3NameFg,
st::historyPeer4NameFg,
st::historyPeer5NameFg,
st::historyPeer6NameFg,
st::historyPeer7NameFg,
st::historyPeer8NameFg,
};
return colors[PeerColorIndex(peer->id)];
}
} }
style::color fromNameFgSelected(int index) { MTPDmessage::Flags NewForwardedFlags(
Expects(index >= 0 && index < 8); not_null<PeerData*> peer,
style::color colors[] = { UserId from,
st::historyPeer1NameFgSelected, not_null<HistoryMessage*> fwd) {
st::historyPeer2NameFgSelected,
st::historyPeer3NameFgSelected,
st::historyPeer4NameFgSelected,
st::historyPeer5NameFgSelected,
st::historyPeer6NameFgSelected,
st::historyPeer7NameFgSelected,
st::historyPeer8NameFgSelected,
};
return colors[index];
}
MTPDmessage::Flags NewForwardedFlags(not_null<PeerData*> peer, UserId from, not_null<HistoryMessage*> fwd) {
auto result = NewMessageFlags(peer) | MTPDmessage::Flag::f_fwd_from; auto result = NewMessageFlags(peer) | MTPDmessage::Flag::f_fwd_from;
if (from) { if (from) {
result |= MTPDmessage::Flag::f_from_id; result |= MTPDmessage::Flag::f_from_id;
@ -1777,7 +1778,7 @@ void HistoryMessage::paintFromName(Painter &p, QRect &trect, bool selected) cons
if (isPost()) { if (isPost()) {
p.setPen(selected ? st::msgInServiceFgSelected : st::msgInServiceFg); p.setPen(selected ? st::msgInServiceFgSelected : st::msgInServiceFg);
} else { } else {
p.setPen(selected ? fromNameFgSelected(author()->colorIndex()) : fromNameFg(author()->colorIndex())); p.setPen(FromNameFg(author(), selected));
} }
author()->nameText.drawElided(p, trect.left(), trect.top(), trect.width()); author()->nameText.drawElided(p, trect.left(), trect.top(), trect.width());

View file

@ -153,7 +153,10 @@ ImagePtr ItemBase::getResultThumb() const {
QPixmap ItemBase::getResultContactAvatar(int width, int height) const { QPixmap ItemBase::getResultContactAvatar(int width, int height) const {
if (_result->_type == Result::Type::Contact) { if (_result->_type == Result::Type::Contact) {
auto result = EmptyUserpic(qHash(_result->_id) % kUserColorsCount, _result->getLayoutTitle()).generate(width); auto result = EmptyUserpic(
_result->_id,
_result->getLayoutTitle()
).generate(width);
if (result.height() != height * cIntRetinaFactor()) { if (result.height() != height * cIntRetinaFactor()) {
result = result.scaled(QSize(width, height) * cIntRetinaFactor(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); result = result.scaled(QSize(width, height) * cIntRetinaFactor(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
} }