[Improvement] Hiding and showing pinned messages

This commit is contained in:
Eric Kotato 2022-09-10 20:30:57 +03:00 committed by Eric Kotato
parent aed2c8ae39
commit dfc98448ba
8 changed files with 171 additions and 24 deletions

View file

@ -912,6 +912,8 @@ PRIVATE
kotato/boxes/kotato_fonts_box.h
kotato/boxes/kotato_radio_box.cpp
kotato/boxes/kotato_radio_box.h
kotato/boxes/kotato_unpin_box.cpp
kotato/boxes/kotato_unpin_box.h
kotato/kotato_lang.cpp
kotato/kotato_lang.h
kotato/kotato_settings.cpp

View file

@ -31,6 +31,8 @@
"ktg_settings_show_json_settings": "Show settings file",
"ktg_settings_restart": "Restart Kotatogram",
"ktg_copy_btn_callback": "Copy callback data",
"ktg_pinned_message_show": "Show pinned message",
"ktg_pinned_message_hide": "Hide pinned message",
"ktg_settings_chats": "Chats",
"ktg_settings_sticker_height": "Sticker height: {pixels}px",
"ktg_settings_sticker_scale_both": "Apply to sticker width",
@ -91,6 +93,7 @@
"ktg_settings_top_bar_mute": "Mute in profile top bar",
"ktg_settings_messages": "Messages",
"ktg_settings_filters_hide_all": "Hide \"All chats\" folder",
"ktg_hide_pinned_message": "Hide",
"ktg_settings_tray_icon": "Tray icon",
"ktg_settings_tray_icon_default": "Default",
"ktg_settings_tray_icon_blue": "Blue",

View file

@ -44,6 +44,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "mainwidget.h"
#include "mainwindow.h"
#include "main/main_session.h"
#include "main/main_session_settings.h"
#include "window/notifications_manager.h"
#include "calls/calls_instance.h"
#include "spellcheck/spellcheck_types.h"

View file

@ -6674,9 +6674,7 @@ void HistoryWidget::checkPinnedBarState() {
Expects(_pinnedTracker != nullptr);
Expects(_list != nullptr);
const auto hiddenId = _peer->canPinMessages()
? MsgId(0)
: session().settings().hiddenPinnedMessageId(_peer->id);
const auto hiddenId = session().settings().hiddenPinnedMessageId(_peer->id);
const auto currentPinnedId = Data::ResolveTopPinnedId(
_peer,
MsgId(0), // topicRootId
@ -6912,7 +6910,11 @@ void HistoryWidget::refreshPinnedBarButton(bool many, HistoryItem *item) {
button->clicks(
) | rpl::start_with_next([=] {
if (close) {
hidePinnedMessage();
// if (button->clickModifiers() & Qt::ControlModifier) {
// hidePinnedMessage(true);
// } else {
hidePinnedMessage();
// }
} else {
openSection();
}
@ -7353,14 +7355,14 @@ void HistoryWidget::editMessage(not_null<HistoryItem*> item) {
setInnerFocus();
}
void HistoryWidget::hidePinnedMessage() {
void HistoryWidget::hidePinnedMessage(bool force) {
Expects(_pinnedBar != nullptr);
const auto id = _pinnedTracker->currentMessageId();
if (!id.message) {
return;
}
if (_peer->canPinMessages()) {
if (_peer->canPinMessages() && !force) {
Window::ToggleMessagePinned(controller(), id.message, false);
} else {
const auto callback = [=] {

View file

@ -401,7 +401,7 @@ private:
void showKeyboardHideButton();
void toggleKeyboard(bool manual = true);
void startBotCommand();
void hidePinnedMessage();
void hidePinnedMessage(bool force = false);
void cancelFieldAreaState();
void unblockUser();
void sendBotStartCommand();

View file

@ -0,0 +1,89 @@
/*
This file is part of Kotatogram Desktop,
the unofficial app based on Telegram Desktop.
For license and copyright information please follow this link:
https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL
*/
#include "kotato/boxes/kotato_unpin_box.h"
#include "kotato/kotato_lang.h"
#include "lang/lang_keys.h"
#include "apiwrap.h"
#include "history/history_widget.h"
#include "ui/widgets/labels.h"
#include "data/data_channel.h"
#include "data/data_chat.h"
#include "data/data_changes.h"
#include "data/data_user.h"
#include "data/data_session.h"
#include "main/main_session.h"
#include "styles/style_layers.h"
#include "styles/style_boxes.h"
UnpinMessageBox::UnpinMessageBox(
QWidget*,
not_null<PeerData*> peer,
MsgId topicRootId,
MsgId msgId)
: _peer(peer)
, _api(&peer->session().mtp())
, _topicRootId(topicRootId)
, _msgId(msgId)
, _text(this, tr::lng_pinned_unpin_sure(tr::now), st::boxLabel) {
}
void UnpinMessageBox::prepare() {
addLeftButton(rktr("ktg_hide_pinned_message"), [this] { hideMessage(); });
addButton(tr::lng_pinned_unpin(), [this] { unpinMessage(); });
addButton(tr::lng_cancel(), [this] { closeBox(); });
auto height = st::boxPadding.top() + _text->height() + st::boxPadding.bottom();
setDimensions(st::boxWidth, height);
}
void UnpinMessageBox::resizeEvent(QResizeEvent *e) {
BoxContent::resizeEvent(e);
_text->moveToLeft(st::boxPadding.left(), st::boxPadding.top());
}
void UnpinMessageBox::keyPressEvent(QKeyEvent *e) {
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
unpinMessage();
} else if (e->key() == Qt::Key_Backspace) {
hideMessage();
} else {
BoxContent::keyPressEvent(e);
}
}
void UnpinMessageBox::unpinMessage() {
if (_requestId) return;
//auto flags = MTPmessages_UpdatePinnedMessage::Flags(0);
_requestId = _api.request(MTPmessages_UpdatePinnedMessage(
MTP_flags(MTPmessages_UpdatePinnedMessage::Flag::f_unpin),
_peer->input,
MTP_int(_msgId)
)).done([=](const MTPUpdates &result) {
_peer->session().api().applyUpdates(result);
Ui::hideLayer();
}).fail([=](const MTP::Error &error) {
Ui::hideLayer();
}).send();
}
void UnpinMessageBox::hideMessage() {
if (_requestId) return;
const auto makeThread = [=] {
return _topicRootId
? reinterpret_cast<Data::Thread*>(_peer->forumTopicFor(_topicRootId))
: reinterpret_cast<Data::Thread*>(_peer->owner().history(_peer).get());
};
const auto thread = makeThread();
thread->setHasPinnedMessages(!thread->hasPinnedMessages());
Ui::hideLayer();
}

View file

@ -0,0 +1,44 @@
/*
This file is part of Kotatogram Desktop,
the unofficial app based on Telegram Desktop.
For license and copyright information please follow this link:
https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL
*/
#pragma once
#include "boxes/abstract_box.h"
#include "mtproto/sender.h"
namespace Ui {
class FlatLabel;
} // namespace Ui
class UnpinMessageBox final : public Ui::BoxContent {
public:
UnpinMessageBox(
QWidget*,
not_null<PeerData*> peer,
MsgId topicRootId,
MsgId msgId);
protected:
void prepare() override;
void resizeEvent(QResizeEvent *e) override;
void keyPressEvent(QKeyEvent *e) override;
private:
void unpinMessage();
void hideMessage();
const not_null<PeerData*> _peer;
MTP::Sender _api;
MsgId _topicRootId = 0;
MsgId _msgId = 0;
object_ptr<Ui::FlatLabel> _text;
mtpRequestId _requestId = 0;
};

View file

@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "window/window_peer_menu.h"
#include "kotato/kotato_lang.h"
#include "kotato/kotato_settings.h"
#include "menu/menu_check_item.h"
#include "boxes/share_box.h"
@ -34,6 +35,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "boxes/peers/edit_contact_box.h"
#include "boxes/share_box.h"
#include "calls/calls_instance.h"
#include "kotato/boxes/kotato_unpin_box.h"
#include "ui/boxes/confirm_box.h"
#include "inline_bots/bot_attach_web_view.h" // InlineBots::PeerType.
#include "ui/boxes/report_box.h"
#include "ui/toast/toast.h"
@ -289,6 +292,7 @@ private:
void addBotToGroup();
void addNewMembers();
void addDeleteContact();
void addHidePin();
void addTTLSubmenu(bool addSeparator);
void addGiftPremium();
void addCreateTopic();
@ -926,6 +930,23 @@ void Filler::addDeleteContact() {
});
}
void Filler::addHidePin() {
if (!_thread) {
return;
}
if (_thread->hasPinnedMessages()) {
_addAction(
ktr("ktg_pinned_message_show"),
[=] { _thread->setHasPinnedMessages(false); },
&st::menuIconPin);
} else {
_addAction(
ktr("ktg_pinned_message_hide"),
[=] { _thread->setHasPinnedMessages(true); },
&st::menuIconPin);
}
}
void Filler::addDeleteTopic() {
if (!_topic || !_topic->canDelete()) {
return;
@ -1216,6 +1237,7 @@ void Filler::fillHistoryActions() {
addToggleMuteSubmenu(true);
addInfo();
addSupportInfo();
addHidePin();
addManageChat();
addCreatePoll();
addThemeEdit();
@ -2400,24 +2422,8 @@ void ToggleMessagePinned(
Box(PinMessageBox, item),
Ui::LayerOption::CloseOther);
} else {
const auto peer = item->history()->peer;
const auto session = &peer->session();
const auto callback = crl::guard(session, [=](Fn<void()> &&close) {
close();
session->api().request(MTPmessages_UpdatePinnedMessage(
MTP_flags(MTPmessages_UpdatePinnedMessage::Flag::f_unpin),
peer->input,
MTP_int(itemId.msg)
)).done([=](const MTPUpdates &result) {
session->api().applyUpdates(result);
}).send();
});
navigation->parentController()->show(
Ui::MakeConfirmBox({
.text = tr::lng_pinned_unpin_sure(),
.confirmed = callback,
.confirmText = tr::lng_pinned_unpin(),
}),
Box<UnpinMessageBox>(item->history()->peer, item->topicRootId(), item->id),
Ui::LayerOption::CloseOther);
}
}