homescreens/folio: FavouritesBar: Icon position animation bugfix

This fixes in issue where the favorites bar icons would previously animate based off of position changes. This was causing unwanted animations in situations like screen rotation and login.
This commit is contained in:
Micah Stanley 2024-11-07 05:17:57 +00:00 committed by Devin Lin
parent f5728e1a21
commit 2a5dec9cf0
4 changed files with 18 additions and 74 deletions

View file

@ -24,24 +24,6 @@ FavouritesModel::FavouritesModel(HomeScreen *parent)
: QAbstractListModel{parent} : QAbstractListModel{parent}
, m_homeScreen{parent} , m_homeScreen{parent}
{ {
connect(m_homeScreen->homeScreenState(), &HomeScreenState::pageWidthChanged, this, [this]() {
evaluateDelegatePositions(true);
});
connect(m_homeScreen->homeScreenState(), &HomeScreenState::pageHeightChanged, this, [this]() {
evaluateDelegatePositions(true);
});
connect(m_homeScreen->homeScreenState(), &HomeScreenState::pageCellWidthChanged, this, [this]() {
evaluateDelegatePositions(true);
});
connect(m_homeScreen->homeScreenState(), &HomeScreenState::pageCellHeightChanged, this, [this]() {
evaluateDelegatePositions(true);
});
connect(m_homeScreen->homeScreenState(), &HomeScreenState::favouritesBarLocationChanged, this, [this]() {
evaluateDelegatePositions(true);
});
connect(m_homeScreen->homeScreenState(), &HomeScreenState::pageOrientationChanged, this, [this]() {
evaluateDelegatePositions(true);
});
} }
int FavouritesModel::rowCount(const QModelIndex &parent) const int FavouritesModel::rowCount(const QModelIndex &parent) const
@ -59,8 +41,6 @@ QVariant FavouritesModel::data(const QModelIndex &index, int role) const
switch (role) { switch (role) {
case DelegateRole: case DelegateRole:
return QVariant::fromValue(m_delegates.at(index.row()).delegate); return QVariant::fromValue(m_delegates.at(index.row()).delegate);
case XPositionRole:
return QVariant::fromValue(m_delegates.at(index.row()).xPosition);
} }
return QVariant(); return QVariant();
@ -68,7 +48,7 @@ QVariant FavouritesModel::data(const QModelIndex &index, int role) const
QHash<int, QByteArray> FavouritesModel::roleNames() const QHash<int, QByteArray> FavouritesModel::roleNames() const
{ {
return {{DelegateRole, "delegate"}, {XPositionRole, "xPosition"}}; return {{DelegateRole, "delegate"}};
} }
void FavouritesModel::removeEntry(int row) void FavouritesModel::removeEntry(int row)
@ -83,8 +63,6 @@ void FavouritesModel::removeEntry(int row)
m_delegates.removeAt(row); m_delegates.removeAt(row);
endRemoveRows(); endRemoveRows();
evaluateDelegatePositions();
save(); save();
} }
@ -109,8 +87,6 @@ void FavouritesModel::moveEntry(int fromRow, int toRow)
} }
endMoveRows(); endMoveRows();
evaluateDelegatePositions();
save(); save();
} }
@ -136,22 +112,18 @@ bool FavouritesModel::addEntry(int row, FolioDelegate *delegate)
if (row == m_delegates.size()) { if (row == m_delegates.size()) {
beginInsertRows(QModelIndex(), row, row); beginInsertRows(QModelIndex(), row, row);
m_delegates.append({delegate, 0}); m_delegates.append({delegate, 0});
evaluateDelegatePositions(false);
endInsertRows(); endInsertRows();
} else if (m_delegates[row].delegate->type() == FolioDelegate::None) { } else if (m_delegates[row].delegate->type() == FolioDelegate::None) {
replaceGhostEntry(delegate); replaceGhostEntry(delegate);
} else { } else {
beginInsertRows(QModelIndex(), row, row); beginInsertRows(QModelIndex(), row, row);
m_delegates.insert(row, {delegate, 0}); m_delegates.insert(row, {delegate, 0});
evaluateDelegatePositions(false);
endInsertRows(); endInsertRows();
} }
// ensure saves are connected when requested by the delegate // ensure saves are connected when requested by the delegate
connectSaveRequests(delegate); connectSaveRequests(delegate);
evaluateDelegatePositions();
save(); save();
return true; return true;
@ -291,7 +263,6 @@ void FavouritesModel::loadFromJson(QJsonArray arr)
} }
} }
evaluateDelegatePositions(false);
endResetModel(); endResetModel();
} }
@ -398,25 +369,6 @@ QPointF FavouritesModel::getDelegateScreenPosition(int position) const
return {0, 0}; return {0, 0};
} }
void FavouritesModel::evaluateDelegatePositions(bool emitSignal)
{
auto homeScreenState = m_homeScreen->homeScreenState();
bool isLocationBottom = homeScreenState->favouritesBarLocation() == HomeScreenState::Bottom;
qreal cellLength = isLocationBottom ? homeScreenState->pageCellWidth() : homeScreenState->pageCellHeight();
qreal startPosition = getDelegateRowStartPos();
qreal currentPos = startPosition;
for (int i = 0; i < m_delegates.size(); ++i) {
m_delegates[adjustIndex(i)].xPosition = qRound(currentPos);
currentPos += cellLength;
}
if (emitSignal) {
Q_EMIT dataChanged(createIndex(0, 0), createIndex(m_delegates.size() - 1, 0), {XPositionRole});
}
}
qreal FavouritesModel::getDelegateRowStartPos() const qreal FavouritesModel::getDelegateRowStartPos() const
{ {
auto homeScreenState = m_homeScreen->homeScreenState(); auto homeScreenState = m_homeScreen->homeScreenState();

View file

@ -31,7 +31,6 @@ class FavouritesModel : public QAbstractListModel
public: public:
enum Roles { enum Roles {
DelegateRole = Qt::UserRole + 1, DelegateRole = Qt::UserRole + 1,
XPositionRole,
}; };
FavouritesModel(HomeScreen *parent = nullptr); FavouritesModel(HomeScreen *parent = nullptr);
@ -73,7 +72,6 @@ public:
private: private:
void connectSaveRequests(FolioDelegate *delegate); void connectSaveRequests(FolioDelegate *delegate);
void evaluateDelegatePositions(bool emitSignal = true);
// get the x (or y) position where delegates start being placed // get the x (or y) position where delegates start being placed
qreal getDelegateRowStartPos() const; qreal getDelegateRowStartPos() const;

View file

@ -20,45 +20,41 @@ MouseArea {
property var homeScreen property var homeScreen
// use to account for x-y positioning, because delegate x and y will include the screen margins
property real leftMargin
property real topMargin
signal delegateDragRequested(var item) signal delegateDragRequested(var item)
onPressAndHold: folio.HomeScreenState.openSettingsView() onPressAndHold: folio.HomeScreenState.openSettingsView()
Repeater { Repeater {
id: repeater
model: folio.FavouritesModel model: folio.FavouritesModel
delegate: Item { delegate: Item {
id: delegate id: delegate
property var delegateModel: model.delegate readonly property var delegateModel: model.delegate
property int index: model.index readonly property int index: model.index
property var dragState: folio.HomeScreenState.dragState readonly property var dragState: folio.HomeScreenState.dragState
property bool isDropPositionThis: dragState.candidateDropPosition.location === Folio.DelegateDragPosition.Favourites && readonly property bool isDropPositionThis: dragState.candidateDropPosition.location === Folio.DelegateDragPosition.Favourites &&
dragState.candidateDropPosition.favouritesPosition === delegate.index dragState.candidateDropPosition.favouritesPosition === delegate.index
property bool isAppHoveredOver: folio.HomeScreenState.swipeState === Folio.HomeScreenState.DraggingDelegate && readonly property bool isAppHoveredOver: folio.HomeScreenState.swipeState === Folio.HomeScreenState.DraggingDelegate &&
dragState.dropDelegate && dragState.dropDelegate &&
dragState.dropDelegate.type === Folio.FolioDelegate.Application && dragState.dropDelegate.type === Folio.FolioDelegate.Application &&
isDropPositionThis isDropPositionThis
// only one of them will be used, because of the anchors below readonly property bool isLocationBottom: folio.HomeScreenState.favouritesBarLocation === Folio.HomeScreenState.Bottom
// this is used due to the ability for the favourites bar to be in multiple locations
x: model.xPosition - leftMargin
y: model.xPosition - topMargin
anchors.verticalCenter: folio.HomeScreenState.favouritesBarLocation === Folio.HomeScreenState.Bottom ? parent.verticalCenter : undefined // get the normalized index position value from the center so we can animate it
anchors.horizontalCenter: folio.HomeScreenState.favouritesBarLocation === Folio.HomeScreenState.Bottom ? undefined : parent.horizontalCenter property double fromCenterValue: model.index - (repeater.count / 2)
Behavior on fromCenterValue {
NumberAnimation { duration: 250; easing.type: Easing.InOutQuad; }
}
Behavior on x { // multiply the 'fromCenterValue' by the cell size to get the actual position
NumberAnimation { duration: 250; easing.type: Easing.InOutQuad } readonly property int centerPosition: (isLocationBottom ? folio.HomeScreenState.pageCellWidth : folio.HomeScreenState.pageCellHeight) * fromCenterValue
}
Behavior on y { x: isLocationBottom ? centerPosition + parent.width / 2 : (parent.width - width) / 2
NumberAnimation { duration: 250; easing.type: Easing.InOutQuad } y: isLocationBottom ? (parent.height - height) / 2 : parent.height / 2 - centerPosition - folio.HomeScreenState.pageCellHeight
}
implicitWidth: folio.HomeScreenState.pageCellWidth implicitWidth: folio.HomeScreenState.pageCellWidth
implicitHeight: folio.HomeScreenState.pageCellHeight implicitHeight: folio.HomeScreenState.pageCellHeight

View file

@ -258,8 +258,6 @@ Item {
id: favouritesBar id: favouritesBar
folio: root.folio folio: root.folio
homeScreen: root homeScreen: root
leftMargin: root.leftMargin
topMargin: root.topMargin
// don't show in settings mode // don't show in settings mode
opacity: 1 - folio.HomeScreenState.settingsOpenProgress opacity: 1 - folio.HomeScreenState.settingsOpenProgress