2017-09-13 17:01:23 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
|
|
|
*/
|
|
|
|
#include "info/info_top_bar.h"
|
|
|
|
|
2017-11-03 18:26:14 +00:00
|
|
|
#include <rpl/never.h>
|
2017-09-15 17:34:41 +00:00
|
|
|
#include "styles/style_info.h"
|
2017-09-30 18:26:45 +00:00
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "info/info_wrap_widget.h"
|
2017-10-31 18:25:22 +00:00
|
|
|
#include "info/info_controller.h"
|
2017-09-30 18:26:45 +00:00
|
|
|
#include "storage/storage_shared_media.h"
|
2017-09-13 17:01:23 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
|
|
|
#include "ui/widgets/labels.h"
|
2017-11-03 12:03:00 +00:00
|
|
|
#include "ui/widgets/input_fields.h"
|
2017-09-30 18:26:45 +00:00
|
|
|
#include "ui/widgets/shadow.h"
|
2017-11-03 12:03:00 +00:00
|
|
|
#include "ui/wrap/fade_wrap.h"
|
|
|
|
#include "ui/wrap/padding_wrap.h"
|
|
|
|
#include "ui/search_field_controller.h"
|
2017-09-13 17:01:23 +00:00
|
|
|
|
|
|
|
namespace Info {
|
|
|
|
|
|
|
|
TopBar::TopBar(QWidget *parent, const style::InfoTopBar &st)
|
|
|
|
: RpWidget(parent)
|
|
|
|
, _st(st) {
|
|
|
|
setAttribute(Qt::WA_OpaquePaintEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TopBar::setTitle(rpl::producer<QString> &&title) {
|
|
|
|
_title.create(this, std::move(title), _st.title);
|
2017-09-15 17:34:41 +00:00
|
|
|
if (_back) {
|
|
|
|
_title->setAttribute(Qt::WA_TransparentForMouseEvents);
|
|
|
|
}
|
|
|
|
updateControlsGeometry(width());
|
2017-09-13 17:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TopBar::enableBackButton(bool enable) {
|
|
|
|
if (enable) {
|
|
|
|
_back.create(this, _st.back);
|
|
|
|
_back->clicks()
|
2017-10-03 14:57:11 +00:00
|
|
|
| rpl::start_to_stream(_backClicks, _back->lifetime());
|
2017-09-13 17:01:23 +00:00
|
|
|
} else {
|
|
|
|
_back.destroy();
|
|
|
|
}
|
|
|
|
if (_title) {
|
|
|
|
_title->setAttribute(Qt::WA_TransparentForMouseEvents, enable);
|
|
|
|
}
|
|
|
|
updateControlsGeometry(width());
|
|
|
|
}
|
|
|
|
|
2017-11-03 12:03:00 +00:00
|
|
|
void TopBar::createSearchView(
|
2017-11-03 18:26:14 +00:00
|
|
|
not_null<Ui::SearchFieldController*> controller,
|
|
|
|
rpl::producer<bool> &&shown) {
|
|
|
|
setSearchField(
|
|
|
|
controller->createField(this, _st.searchRow.field),
|
|
|
|
std::move(shown));
|
2017-11-03 12:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TopBar::pushButton(base::unique_qptr<Ui::RpWidget> button) {
|
|
|
|
auto weak = button.get();
|
2017-09-13 17:01:23 +00:00
|
|
|
_buttons.push_back(std::move(button));
|
2017-11-03 12:03:00 +00:00
|
|
|
weak->setParent(this);
|
2017-09-13 17:01:23 +00:00
|
|
|
weak->widthValue()
|
2017-09-27 18:29:13 +00:00
|
|
|
| rpl::start_with_next([this] {
|
2017-11-03 12:03:00 +00:00
|
|
|
updateControlsGeometry(width());
|
2017-09-30 18:26:45 +00:00
|
|
|
}, lifetime());
|
2017-09-13 17:01:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 12:03:00 +00:00
|
|
|
void TopBar::setSearchField(
|
2017-11-03 18:26:14 +00:00
|
|
|
base::unique_qptr<Ui::InputField> field,
|
|
|
|
rpl::producer<bool> &&shown) {
|
2017-11-03 12:03:00 +00:00
|
|
|
if (auto value = field.release()) {
|
2017-11-03 18:26:14 +00:00
|
|
|
createSearchView(value, std::move(shown));
|
2017-11-03 12:03:00 +00:00
|
|
|
} else {
|
|
|
|
_searchView = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 18:26:14 +00:00
|
|
|
void TopBar::createSearchView(
|
|
|
|
not_null<Ui::InputField*> field,
|
|
|
|
rpl::producer<bool> &&shown) {
|
2017-11-03 12:03:00 +00:00
|
|
|
_searchView = base::make_unique_q<Ui::FixedHeightWidget>(
|
|
|
|
this,
|
|
|
|
_st.searchRow.height);
|
|
|
|
auto wrap = _searchView.get();
|
|
|
|
|
|
|
|
field->setParent(wrap);
|
|
|
|
|
|
|
|
auto search = addButton(
|
|
|
|
base::make_unique_q<Ui::FadeWrapScaled<Ui::IconButton>>(
|
|
|
|
this,
|
|
|
|
object_ptr<Ui::IconButton>(this, _st.search)));
|
|
|
|
auto cancel = Ui::CreateChild<Ui::CrossButton>(
|
|
|
|
wrap,
|
|
|
|
_st.searchRow.fieldCancel);
|
|
|
|
|
|
|
|
auto toggleSearchMode = [=](bool enabled, anim::type animated) {
|
|
|
|
if (_title) {
|
|
|
|
_title->setVisible(!enabled);
|
|
|
|
}
|
|
|
|
field->setVisible(enabled);
|
|
|
|
cancel->toggleAnimated(enabled);
|
|
|
|
if (animated == anim::type::instant) {
|
|
|
|
cancel->finishAnimations();
|
|
|
|
}
|
|
|
|
search->toggle(!enabled, animated);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto cancelSearch = [=] {
|
|
|
|
if (!field->getLastText().isEmpty()) {
|
|
|
|
field->setText(QString());
|
|
|
|
} else {
|
|
|
|
toggleSearchMode(false, anim::type::normal);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
cancel->addClickHandler(cancelSearch);
|
|
|
|
field->connect(field, &Ui::InputField::cancelled, cancelSearch);
|
|
|
|
|
|
|
|
wrap->widthValue()
|
|
|
|
| rpl::start_with_next([=](int newWidth) {
|
|
|
|
auto availableWidth = newWidth
|
|
|
|
- _st.searchRow.fieldCancelSkip;
|
|
|
|
field->setGeometryToLeft(
|
|
|
|
_st.searchRow.padding.left(),
|
|
|
|
_st.searchRow.padding.top(),
|
|
|
|
availableWidth,
|
|
|
|
field->height());
|
|
|
|
cancel->moveToRight(0, 0);
|
|
|
|
}, wrap->lifetime());
|
|
|
|
|
|
|
|
widthValue()
|
|
|
|
| rpl::start_with_next([=](int newWidth) {
|
|
|
|
auto left = _back
|
|
|
|
? _st.back.width
|
|
|
|
: _st.titlePosition.x();
|
|
|
|
wrap->setGeometryToLeft(
|
|
|
|
left,
|
|
|
|
0,
|
|
|
|
newWidth - left,
|
|
|
|
wrap->height(),
|
|
|
|
newWidth);
|
|
|
|
}, wrap->lifetime());
|
|
|
|
|
|
|
|
search->entity()->addClickHandler([=] {
|
|
|
|
toggleSearchMode(true, anim::type::normal);
|
|
|
|
field->setFocus();
|
|
|
|
});
|
|
|
|
|
|
|
|
field->alive()
|
|
|
|
| rpl::start_with_done([=] {
|
|
|
|
field->setParent(nullptr);
|
|
|
|
removeButton(search);
|
2017-11-03 18:26:14 +00:00
|
|
|
setSearchField(nullptr, rpl::never<bool>());
|
2017-11-03 12:03:00 +00:00
|
|
|
}, _searchView->lifetime());
|
|
|
|
|
|
|
|
toggleSearchMode(
|
|
|
|
!field->getLastText().isEmpty(),
|
|
|
|
anim::type::instant);
|
2017-11-03 18:26:14 +00:00
|
|
|
|
|
|
|
std::move(shown)
|
|
|
|
| rpl::start_with_next([=](bool visible) {
|
|
|
|
if (!field->getLastText().isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
toggleSearchMode(false, anim::type::instant);
|
|
|
|
wrap->setVisible(visible);
|
|
|
|
search->toggle(visible, anim::type::instant);
|
|
|
|
}, wrap->lifetime());
|
2017-11-03 12:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TopBar::removeButton(not_null<Ui::RpWidget*> button) {
|
|
|
|
_buttons.erase(
|
|
|
|
std::remove(_buttons.begin(), _buttons.end(), button),
|
|
|
|
_buttons.end());
|
|
|
|
}
|
|
|
|
|
2017-09-13 17:01:23 +00:00
|
|
|
int TopBar::resizeGetHeight(int newWidth) {
|
|
|
|
updateControlsGeometry(newWidth);
|
|
|
|
return _st.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TopBar::updateControlsGeometry(int newWidth) {
|
|
|
|
auto right = 0;
|
|
|
|
for (auto &button : _buttons) {
|
2017-11-03 12:03:00 +00:00
|
|
|
if (!button) continue;
|
2017-09-13 17:01:23 +00:00
|
|
|
button->moveToRight(right, 0, newWidth);
|
|
|
|
right += button->width();
|
|
|
|
}
|
|
|
|
if (_back) {
|
2017-09-15 17:34:41 +00:00
|
|
|
_back->setGeometryToLeft(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
newWidth - right,
|
|
|
|
_back->height(),
|
|
|
|
newWidth);
|
|
|
|
}
|
|
|
|
if (_title) {
|
|
|
|
_title->moveToLeft(
|
|
|
|
_back ? _st.back.width : _st.titlePosition.x(),
|
|
|
|
_st.titlePosition.y(),
|
|
|
|
newWidth);
|
2017-09-13 17:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TopBar::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
p.fillRect(e->rect(), _st.bg);
|
|
|
|
}
|
|
|
|
|
2017-09-30 18:26:45 +00:00
|
|
|
rpl::producer<QString> TitleValue(
|
|
|
|
const Section §ion,
|
2017-10-31 18:25:22 +00:00
|
|
|
not_null<PeerData*> peer) {
|
2017-09-30 18:26:45 +00:00
|
|
|
return Lang::Viewer([&] {
|
|
|
|
switch (section.type()) {
|
|
|
|
case Section::Type::Profile:
|
2017-10-31 18:25:22 +00:00
|
|
|
if (auto user = peer->asUser()) {
|
|
|
|
return user->botInfo
|
2017-09-30 18:26:45 +00:00
|
|
|
? lng_info_bot_title
|
|
|
|
: lng_info_user_title;
|
2017-10-31 18:25:22 +00:00
|
|
|
} else if (auto channel = peer->asChannel()) {
|
|
|
|
return channel->isMegagroup()
|
2017-09-30 18:26:45 +00:00
|
|
|
? lng_info_group_title
|
|
|
|
: lng_info_channel_title;
|
2017-10-31 18:25:22 +00:00
|
|
|
} else if (peer->isChat()) {
|
2017-09-30 18:26:45 +00:00
|
|
|
return lng_info_group_title;
|
|
|
|
}
|
|
|
|
Unexpected("Bad peer type in Info::TitleValue()");
|
|
|
|
|
|
|
|
case Section::Type::Media:
|
|
|
|
switch (section.mediaType()) {
|
|
|
|
case Section::MediaType::Photo:
|
|
|
|
return lng_media_type_photos;
|
|
|
|
case Section::MediaType::Video:
|
|
|
|
return lng_media_type_videos;
|
|
|
|
case Section::MediaType::MusicFile:
|
|
|
|
return lng_media_type_songs;
|
|
|
|
case Section::MediaType::File:
|
|
|
|
return lng_media_type_files;
|
|
|
|
case Section::MediaType::VoiceFile:
|
|
|
|
return lng_media_type_audios;
|
|
|
|
case Section::MediaType::Link:
|
|
|
|
return lng_media_type_links;
|
|
|
|
case Section::MediaType::RoundFile:
|
|
|
|
return lng_media_type_rounds;
|
|
|
|
}
|
|
|
|
Unexpected("Bad media type in Info::TitleValue()");
|
|
|
|
|
|
|
|
case Section::Type::CommonGroups:
|
|
|
|
return lng_profile_common_groups_section;
|
|
|
|
|
|
|
|
}
|
|
|
|
Unexpected("Bad section type in Info::TitleValue()");
|
|
|
|
}());
|
|
|
|
}
|
|
|
|
|
2017-09-13 17:01:23 +00:00
|
|
|
} // namespace Info
|