Fix occasional crashes on long-tapping poll items

The crash was happening when Repeater was adding context menu items
instantiated by PollPreview to context menu owned by MessageListViewItem.

It's fixed by instantiating those extra menu items inside context menu
itself. Generic ListElement couldn't be used because it doesn't like
functions as property values, hence this NamedAction thing.
This commit is contained in:
Slava Monich 2020-12-05 19:31:34 +02:00
parent 5d1a545317
commit bd695d1a91
6 changed files with 145 additions and 35 deletions

View file

@ -31,6 +31,7 @@ SOURCES += src/harbour-fernschreiber.cpp \
src/fernschreiberutils.cpp \ src/fernschreiberutils.cpp \
src/knownusersmodel.cpp \ src/knownusersmodel.cpp \
src/mceinterface.cpp \ src/mceinterface.cpp \
src/namedaction.cpp \
src/notificationmanager.cpp \ src/notificationmanager.cpp \
src/processlauncher.cpp \ src/processlauncher.cpp \
src/stickermanager.cpp \ src/stickermanager.cpp \
@ -159,6 +160,7 @@ HEADERS += \
src/fernschreiberutils.h \ src/fernschreiberutils.h \
src/knownusersmodel.h \ src/knownusersmodel.h \
src/mceinterface.h \ src/mceinterface.h \
src/namedaction.h \
src/notificationmanager.h \ src/notificationmanager.h \
src/processlauncher.h \ src/processlauncher.h \
src/stickermanager.h \ src/stickermanager.h \

View file

@ -20,7 +20,6 @@ import QtQuick 2.6
import Sailfish.Silica 1.0 import Sailfish.Silica 1.0
import "../js/twemoji.js" as Emoji import "../js/twemoji.js" as Emoji
import "../js/functions.js" as Functions import "../js/functions.js" as Functions
import QtQml.Models 2.3
import "../js/debug.js" as Debug import "../js/debug.js" as Debug
ListItem { ListItem {
@ -40,7 +39,6 @@ ListItem {
&& typeof chatView.contentComponentNames[myMessage.content['@type']] !== "undefined" ? && typeof chatView.contentComponentNames[myMessage.content['@type']] !== "undefined" ?
chatView.contentComponentNames[myMessage.content['@type']] : "" chatView.contentComponentNames[myMessage.content['@type']] : ""
readonly property ObjectModel additionalContextItems: ObjectModel {}
highlighted: (down || isSelected) && !menuOpen highlighted: (down || isSelected) && !menuOpen
openMenuOnPressAndHold: !messageListItem.precalculatedValues.pageIsSelecting openMenuOnPressAndHold: !messageListItem.precalculatedValues.pageIsSelecting
@ -80,7 +78,13 @@ ListItem {
sourceComponent: Component { sourceComponent: Component {
ContextMenu { ContextMenu {
Repeater { Repeater {
model: messageListItem.additionalContextItems model: (extraContentLoader.item && ("extraContextMenuItems" in extraContentLoader.item)) ?
extraContentLoader.item.extraContextMenuItems : 0
delegate: MenuItem {
visible: modelData.visible
text: modelData.name
onClicked: modelData.action()
}
} }
MenuItem { MenuItem {

View file

@ -19,6 +19,7 @@
import QtQuick 2.6 import QtQuick 2.6
import Sailfish.Silica 1.0 import Sailfish.Silica 1.0
import WerkWolf.Fernschreiber 1.0
import "../js/functions.js" as Functions import "../js/functions.js" as Functions
import "../js/twemoji.js" as Emoji import "../js/twemoji.js" as Emoji
@ -46,8 +47,19 @@ Item {
property bool highlighted property bool highlighted
width: parent.width width: parent.width
height: pollColumn.height height: pollColumn.height
opacity: 0 property list<NamedAction> extraContextMenuItems: [
Behavior on opacity { FadeAnimation {} } NamedAction {
visible: !pollData.is_closed && pollMessageComponent.canEdit
name: qsTr("Close Poll")
action: function () { tdLibWrapper.stopPoll(pollMessageComponent.chatId, pollMessageComponent.messageId) }
},
NamedAction {
visible: !pollData.is_closed && !pollMessageComponent.isQuiz && pollMessageComponent.hasAnswered
name: qsTr("Reset Answer")
action: function () { pollMessageComponent.resetChosen() }
}
]
function handleChoose(index) { function handleChoose(index) {
if(!pollData.type.allow_multiple_answers) { if(!pollData.type.allow_multiple_answers) {
chosenIndexes = [index]; chosenIndexes = [index];
@ -282,34 +294,4 @@ Item {
} }
} }
} }
Component {
id: closePollMenuItemComponent
MenuItem {
visible: !pollData.is_closed && pollMessageComponent.canEdit
text: qsTr("Close Poll")
onClicked: {
tdLibWrapper.stopPoll(pollMessageComponent.chatId, pollMessageComponent.messageId);
}
}
}
Component {
id: resetAnswerMenuItemComponent
MenuItem {
visible: !pollData.is_closed && !pollMessageComponent.isQuiz && pollMessageComponent.hasAnswered
text: qsTr("Reset Answer")
onClicked: {
pollMessageComponent.resetChosen()
}
}
}
Component.onCompleted: {
opacity = 1;
if(messageListItem && messageListItem.additionalContextItems ) {
messageListItem.additionalContextItems.append(closePollMenuItemComponent.createObject());
messageListItem.additionalContextItems.append(resetAnswerMenuItemComponent.createObject());
}
}
} }

View file

@ -29,12 +29,14 @@
#include <QQmlEngine> #include <QQmlEngine>
#include <QGuiApplication> #include <QGuiApplication>
#include <QLoggingCategory> #include <QLoggingCategory>
#include "appsettings.h" #include "appsettings.h"
#include "debuglogjs.h" #include "debuglogjs.h"
#include "tdlibfile.h" #include "tdlibfile.h"
#include "tdlibwrapper.h" #include "tdlibwrapper.h"
#include "chatlistmodel.h" #include "chatlistmodel.h"
#include "chatmodel.h" #include "chatmodel.h"
#include "namedaction.h"
#include "notificationmanager.h" #include "notificationmanager.h"
#include "mceinterface.h" #include "mceinterface.h"
#include "dbusadaptor.h" #include "dbusadaptor.h"
@ -66,6 +68,7 @@ int main(int argc, char *argv[])
const char *uri = "WerkWolf.Fernschreiber"; const char *uri = "WerkWolf.Fernschreiber";
qmlRegisterType<TDLibFile>(uri, 1, 0, "TDLibFile"); qmlRegisterType<TDLibFile>(uri, 1, 0, "TDLibFile");
qmlRegisterType<NamedAction>(uri, 1, 0, "NamedAction");
qmlRegisterSingletonType<DebugLogJS>(uri, 1, 0, "DebugLog", DebugLogJS::createSingleton); qmlRegisterSingletonType<DebugLogJS>(uri, 1, 0, "DebugLog", DebugLogJS::createSingleton);
AppSettings *appSettings = new AppSettings(view.data()); AppSettings *appSettings = new AppSettings(view.data());

65
src/namedaction.cpp Normal file
View file

@ -0,0 +1,65 @@
/*
This file is part of Fernschreiber.
Fernschreiber 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.
Fernschreiber 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.
You should have received a copy of the GNU General Public License
along with Fernschreiber. If not, see <http://www.gnu.org/licenses/>.
*/
#include "namedaction.h"
// A workaroud for
// ListElement: cannot use script for property value
NamedAction::NamedAction(QObject *parent) :
QObject(parent),
visible(true),
action(QJSValue::UndefinedValue)
{
}
bool NamedAction::getVisible() const
{
return visible;
}
void NamedAction::setVisible(bool newVisible)
{
if (visible != newVisible) {
visible = newVisible;
emit visibleChanged();
}
}
QString NamedAction::getName() const
{
return name;
}
void NamedAction::setName(QString newName)
{
if (name != newName) {
name = newName;
emit nameChanged();
}
}
QJSValue NamedAction::getAction() const
{
return action;
}
void NamedAction::setAction(QJSValue newAction)
{
action = newAction;
emit actionChanged();
}

54
src/namedaction.h Normal file
View file

@ -0,0 +1,54 @@
/*
This file is part of Fernschreiber.
Fernschreiber 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.
Fernschreiber 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.
You should have received a copy of the GNU General Public License
along with Fernschreiber. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef NAMEDACTION_H
#define NAMEDACTION_H
#include <QObject>
#include <QString>
#include <QJSValue>
class NamedAction : public QObject {
Q_OBJECT
Q_PROPERTY(bool visible READ getVisible WRITE setVisible NOTIFY visibleChanged)
Q_PROPERTY(QString name READ getName WRITE setName NOTIFY nameChanged)
Q_PROPERTY(QJSValue action READ getAction WRITE setAction NOTIFY actionChanged)
public:
NamedAction(QObject *parent = Q_NULLPTR);
bool getVisible() const;
void setVisible(bool newVisible);
QString getName() const;
void setName(QString newName);
QJSValue getAction() const;
void setAction(QJSValue newAction);
signals:
void visibleChanged();
void nameChanged();
void actionChanged();
private:
bool visible;
QString name;
QJSValue action;
};
#endif // NAMEDACTION_H