refactor:improve Synchronizer:swithToLayout

--the new approach is much clearer and easy to
read that the deprecated one
pull/20/head
Michail Vourlakos 4 years ago
parent 95486a1296
commit 41c2e2b033

@ -47,6 +47,7 @@ namespace MemoryUsage {
enum LayoutsMemory
{
Current = -1, /*current layouts memory usage*/
SingleLayout = 0, /* a single Layout is loaded in each time */
MultipleLayouts /* multiple layouts are loaded on runtime,based on Activities and one central layout for the rest unassigned Activities */
};

@ -166,9 +166,9 @@ QList<CentralLayout *> Manager::currentLayouts() const
return m_synchronizer->currentLayouts();
}
bool Manager::switchToLayout(QString layoutName, int previousMemoryUsage)
bool Manager::switchToLayout(QString layoutName, MemoryUsage::LayoutsMemory newMemoryUsage)
{
return m_synchronizer->switchToLayout(layoutName, previousMemoryUsage);
return m_synchronizer->switchToLayout(layoutName, newMemoryUsage);
}
void Manager::loadLayoutOnStartup(QString layoutName)
@ -275,6 +275,8 @@ void Manager::clearUnloadedContainmentsFromLinkedFile(QStringList containmentsId
KConfigGroup containment = containments.group(conId);
containment.deleteGroup();
}
containments.sync();
}
void Manager::showLatteSettingsDialog(int firstPage, bool toggleCurrentPage)

@ -89,6 +89,9 @@ public:
MemoryUsage::LayoutsMemory memoryUsage() const;
void setMemoryUsage(MemoryUsage::LayoutsMemory memoryUsage);
//! switch to specified layout, default previousMemoryUsage means that it didn't change
bool switchToLayout(QString layoutName, MemoryUsage::LayoutsMemory newMemoryUsage = MemoryUsage::Current);
//! returns the current and central layout based on activities and user preferences
QList<CentralLayout *>currentLayouts() const;
LaunchersSignals *launchersSignals() const;
@ -99,10 +102,6 @@ public slots:
void hideLatteSettingsDialog();
Q_INVOKABLE void showLatteSettingsDialog(int firstPage = Settings::Dialog::LayoutPage, bool toggleCurrentPage = false);
//! switch to specified layout, default previousMemoryUsage means that it didn't change
Q_INVOKABLE bool switchToLayout(QString layoutName, int previousMemoryUsage = -1);
Q_INVOKABLE QStringList centralLayoutsNames();
signals:

@ -525,98 +525,133 @@ void Synchronizer::unloadLayouts()
m_multipleModeInitialized = false;
}
bool Synchronizer::switchToLayout(QString layoutName, int previousMemoryUsage)
bool Synchronizer::memoryInitialized() const
{
qDebug() << " >>>>> SWITCHING >> " << layoutName << " __ " << previousMemoryUsage;
return ((m_manager->memoryUsage() == MemoryUsage::SingleLayout && m_centralLayouts.size()>0)
|| (m_manager->memoryUsage() == MemoryUsage::MultipleLayouts && m_multipleModeInitialized));
}
//! First Check If that Layout is already present and in that case
//! we can just switch to the proper Activity
bool Synchronizer::initSingleMode(QString layoutName)
{
QString layoutpath = layoutName.isEmpty() ? layoutPath(m_manager->corona()->universalSettings()->singleModeLayoutName()) : layoutPath(layoutName);
if (previousMemoryUsage == -1) {
if (m_manager->memoryUsage() == MemoryUsage::MultipleLayouts) {
CentralLayout *layout = centralLayout(layoutName);
if (layoutpath.isEmpty()) {
qDebug() << "Layout : " << layoutName << " was not found...";
return false;
}
if (layout) {
QStringList appliedActivities = layout->appliedActivities();
QString nextActivity = !layout->lastUsedActivity().isEmpty() ? layout->lastUsedActivity() : appliedActivities[0];
if (m_centralLayouts.size() > 0) {
emit currentLayoutIsSwitching(m_centralLayouts[0]->name());
}
if (!appliedActivities.contains(m_manager->corona()->activitiesConsumer()->currentActivity())) {
//! it means we are at a foreign activity and we can switch to correct one
m_activitiesController->setCurrentActivity(nextActivity);
return true;
}
}
} else if (m_manager->memoryUsage() == MemoryUsage::SingleLayout && m_centralLayouts.size()>0 && m_centralLayouts[0]->name() == layoutName) {
//! already loaded
return false;
}
//! this code must be called asynchronously because it can create crashes otherwise.
//! Tasks plasmoid case that triggers layouts switching through its context menu
QTimer::singleShot(350, [this, layoutName, layoutpath]() {
qDebug() << " ... initializing layout in single mode : " << layoutName << " - " << layoutpath;
unloadLayouts();
//! load the main layout/corona file
CentralLayout *newLayout = new CentralLayout(this, layoutpath, layoutName);
addLayout(newLayout);
m_manager->loadLatteLayout(layoutpath);
emit centralLayoutsChanged();
m_manager->corona()->universalSettings()->setSingleModeLayoutName(layoutName);
});
return true;
}
bool Synchronizer::initMultipleMode(QString layoutName)
{
if (m_multipleModeInitialized) {
return false;
}
//! When going from memory usage to different memory usage we first
//! send the layouts that will be changed. This signal creates the
//! nice animation that hides these docks/panels
if (previousMemoryUsage != -1) {
for (const auto layout : m_centralLayouts) {
emit currentLayoutIsSwitching(layout->name());
}
for (const auto layout : m_centralLayouts) {
emit currentLayoutIsSwitching(layout->name());
}
QString lPath = layoutPath(layoutName);
//! this code must be called asynchronously because it can create crashes otherwise.
//! Tasks plasmoid case that triggers layouts switching through its context menu
QTimer::singleShot(350, [this, layoutName]() {
qDebug() << " ... initializing layout in multiple mode : " << layoutName ;
unloadLayouts();
if ((m_manager->memoryUsage() == MemoryUsage::SingleLayout && !lPath.isEmpty()) || m_manager->memoryUsage() == MemoryUsage::MultipleLayouts) {
if (m_manager->memoryUsage() == MemoryUsage::SingleLayout && m_centralLayouts.size()>0) {
emit currentLayoutIsSwitching(m_centralLayouts[0]->name());
} else if (m_manager->memoryUsage() == MemoryUsage::MultipleLayouts && layoutName != Layout::MULTIPLELAYOUTSHIDDENNAME) {
//! do nothing
}
m_manager->loadLatteLayout(layoutPath(QString(Layout::MULTIPLELAYOUTSHIDDENNAME)));
//! this code must be called asynchronously because it is called
//! also from qml (Tasks plasmoid). This change fixes a very important
//! crash when switching sessions through the Tasks plasmoid Context menu
//! Latte was unstable and was crashing very often during changing
//! sessions.
QTimer::singleShot(350, [this, layoutName, lPath, previousMemoryUsage]() {
qDebug() << layoutName << " - " << lPath;
QString fixedLPath = lPath;
QString fixedLayoutName = layoutName;
bool initializingMultipleLayouts{(m_manager->memoryUsage() == MemoryUsage::MultipleLayouts) && !m_multipleModeInitialized};
if (m_manager->memoryUsage() == MemoryUsage::SingleLayout || initializingMultipleLayouts || previousMemoryUsage == MemoryUsage::MultipleLayouts) {
unloadLayouts();
//! load the main layout/corona file
if (initializingMultipleLayouts) {
fixedLayoutName = QString(Layout::MULTIPLELAYOUTSHIDDENNAME);
fixedLPath = layoutPath(fixedLayoutName);
} else if (fixedLayoutName != Layout::MULTIPLELAYOUTSHIDDENNAME) {
CentralLayout *newLayout = new CentralLayout(this, fixedLPath, fixedLayoutName);
addLayout(newLayout);
}
m_multipleModeInitialized = true;
m_manager->loadLatteLayout(fixedLPath);
emit centralLayoutsChanged();
if (initializingMultipleLayouts) {
m_multipleModeInitialized = true;
}
syncMultipleLayoutsToActivities();
});
emit centralLayoutsChanged();
}
return true;
}
if (m_manager->memoryUsage() == MemoryUsage::MultipleLayouts) {
syncMultipleLayoutsToActivities();
} else {
//! single layout
m_manager->corona()->universalSettings()->setSingleModeLayoutName(layoutName);
}
});
bool Synchronizer::switchToLayoutInSingleMode(QString layoutName)
{
if (!memoryInitialized() || m_manager->memoryUsage() != MemoryUsage::SingleLayout) {
return false;
}
if (m_centralLayouts.size()>0 && m_centralLayouts[0]->name() == layoutName) {
return true;
}
return initSingleMode(layoutName);
}
bool Synchronizer::switchToLayoutInMultipleMode(QString layoutName)
{
if (!memoryInitialized() || m_manager->memoryUsage() != MemoryUsage::MultipleLayouts) {
return false;
}
CentralLayout *layout = centralLayout(layoutName);
if (layout) {
QStringList appliedActivities = layout->appliedActivities();
QString nextActivity = !layout->lastUsedActivity().isEmpty() ? layout->lastUsedActivity() : appliedActivities[0];
if (!appliedActivities.contains(m_manager->corona()->activitiesConsumer()->currentActivity())) {
//! it means we are at a foreign activity and we can switch to correct one
m_activitiesController->setCurrentActivity(nextActivity);
return true;
}
} else {
qDebug() << "Layout : " << layoutName << " was not found...";
syncMultipleLayoutsToActivities();
}
return true;
}
bool Synchronizer::switchToLayout(QString layoutName, MemoryUsage::LayoutsMemory newMemoryUsage)
{
qDebug() << " >>>>> SWITCHING >> " << layoutName << " __ from memory: " << m_manager->memoryUsage() << " to memory: " << newMemoryUsage;
if (!memoryInitialized() || newMemoryUsage != m_manager->memoryUsage()) {
//! Initiate Layouts memory properly
if (newMemoryUsage != MemoryUsage::Current) {
m_manager->setMemoryUsage(newMemoryUsage);
} else {
newMemoryUsage = m_manager->memoryUsage();
}
return (newMemoryUsage == MemoryUsage::SingleLayout ? initSingleMode(layoutName) : initMultipleMode(layoutName));
}
if (m_manager->memoryUsage() == MemoryUsage::SingleLayout) {
return switchToLayoutInSingleMode(layoutName);
} else {
return switchToLayoutInMultipleMode(layoutName);
}
}
void Synchronizer::syncMultipleLayoutsToActivities()
{
qDebug() << " ---- --------- ------ syncMultipleLayoutsToActivities ------- ";

@ -21,6 +21,7 @@
#define LAYOUTSSYNCHRONIZER_H
// local
#include "../apptypes.h"
#include "../data/layoutdata.h"
#include "../data/layoutstable.h"
@ -83,7 +84,7 @@ public:
bool latteViewExists(Latte::View *view) const;
bool layoutExists(QString layoutName) const;
//! switch to specified layout, default previousMemoryUsage means that it didn't change
bool switchToLayout(QString layoutName, int previousMemoryUsage = -1);
bool switchToLayout(QString layoutName, MemoryUsage::LayoutsMemory newMemoryUsage = MemoryUsage::Current);
int centralLayoutPos(QString id) const;
@ -141,7 +142,14 @@ private:
void addLayout(CentralLayout *layout);
void unloadCentralLayout(CentralLayout *layout);
bool initSingleMode(QString layoutName);
bool initMultipleMode(QString layoutName);
bool switchToLayoutInMultipleMode(QString layoutName);
bool switchToLayoutInSingleMode(QString layoutName);
bool isAssigned(QString layoutName) const;
bool memoryInitialized() const;
QString layoutPath(QString layoutName);

@ -714,26 +714,14 @@ void Layouts::save()
//! send new layouts data in layoutsmanager
m_handler->corona()->layoutsManager()->synchronizer()->setLayoutsTable(currentLayouts);
//! make sure that there is a layout for free activities
//! send to layout manager in which layout to switch
MemoryUsage::LayoutsMemory inMemoryOption = inMultipleMode() ? Latte::MemoryUsage::MultipleLayouts : Latte::MemoryUsage::SingleLayout;
if (m_handler->corona()->layoutsManager()->memoryUsage() != inMemoryOption) {
MemoryUsage::LayoutsMemory previousMemoryUsage = m_handler->corona()->layoutsManager()->memoryUsage();
m_handler->corona()->layoutsManager()->setMemoryUsage(inMemoryOption);
if (m_handler->corona()->layoutsManager()->memoryUsage() == MemoryUsage::SingleLayout) {
m_handler->corona()->layoutsManager()->switchToLayout(m_handler->corona()->universalSettings()->singleModeLayoutName(), previousMemoryUsage);
} else {
m_handler->corona()->layoutsManager()->switchToLayout("", previousMemoryUsage);
}
} else {
if (m_handler->corona()->layoutsManager()->memoryUsage() == MemoryUsage::MultipleLayouts) {
m_handler->corona()->layoutsManager()->synchronizer()->syncMultipleLayoutsToActivities();
} else {
m_handler->corona()->layoutsManager()->switchToLayout(m_handler->corona()->universalSettings()->singleModeLayoutName());
}
if (inMemoryOption == MemoryUsage::SingleLayout) {
m_handler->corona()->layoutsManager()->switchToLayout(m_handler->corona()->universalSettings()->singleModeLayoutName(), MemoryUsage::SingleLayout);
} else {
m_handler->corona()->layoutsManager()->switchToLayout("", MemoryUsage::MultipleLayouts);
}
m_model->applyData();

Loading…
Cancel
Save