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());
{
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);
_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");
}
} // namespace
style::color peerUserpicColor(int index) {
static style::color peerColors[kUserColorsCount] = {
style::color PeerUserpicColor(PeerId peerId) {
const style::color colors[] = {
st::historyPeer1UserpicBg,
st::historyPeer2UserpicBg,
st::historyPeer3UserpicBg,
@ -70,12 +68,25 @@ style::color peerUserpicColor(int index) {
st::historyPeer7UserpicBg,
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 {
public:
Impl(int index, const QString &name) : _color(peerUserpicColor(index)) {
Impl(PeerId peerId, const QString &name)
: _color(PeerUserpicColor(peerId)) {
fillString(name);
}
@ -196,11 +207,16 @@ void EmptyUserpic::Impl::fillString(const QString &name) {
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) {
_impl = std::make_unique<Impl>(index, name);
EmptyUserpic::EmptyUserpic(const QString &nonce, const QString &name)
: EmptyUserpic(qHash(nonce), name) {
}
void EmptyUserpic::set(PeerId peerId, const QString &name) {
_impl = std::make_unique<Impl>(peerId, name);
}
void EmptyUserpic::clear() {
@ -269,10 +285,9 @@ void PeerClickHandler::onClick(Qt::MouseButton button) const {
}
PeerData::PeerData(const PeerId &id)
: id(id)
, _colorIndex(peerColorIndex(id)) {
: id(id) {
nameText.setText(st::msgNameStyle, QString(), _textNameOptions);
_userpicEmpty.set(_colorIndex, QString());
_userpicEmpty.set(id, QString());
}
void PeerData::updateNameDelayed(
@ -298,7 +313,7 @@ void PeerData::updateNameDelayed(
name = newName;
nameText.setText(st::msgNameStyle, name, _textNameOptions);
if (useEmptyUserpic()) {
_userpicEmpty.set(_colorIndex, name);
_userpicEmpty.set(id, name);
}
Notify::PeerUpdate update(this);
@ -337,7 +352,7 @@ void PeerData::setUserpic(
_userpic = userpic;
_userpicLocation = location;
if (useEmptyUserpic()) {
_userpicEmpty.set(_colorIndex, name);
_userpicEmpty.set(id, name);
} else {
_userpicEmpty.clear();
}

View File

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

View File

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

View File

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

View File

@ -153,7 +153,10 @@ ImagePtr ItemBase::getResultThumb() const {
QPixmap ItemBase::getResultContactAvatar(int width, int height) const {
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()) {
result = result.scaled(QSize(width, height) * cIntRetinaFactor(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}