forked from openkylin/ukui-panel
feat(panel):增加panel初始化功能
This commit is contained in:
parent
f8f66c71f1
commit
202d00348b
|
@ -10,10 +10,10 @@
|
|||
|
||||
namespace UkuiPanel {
|
||||
|
||||
class WidgetConfigLoaderPrivate
|
||||
class ConfigLoaderPrivate
|
||||
{
|
||||
public:
|
||||
WidgetConfigLoaderPrivate() = default;
|
||||
ConfigLoaderPrivate() = default;
|
||||
|
||||
QString appid;
|
||||
QString containerId;
|
||||
|
@ -31,52 +31,52 @@ public:
|
|||
static QString configPath();
|
||||
};
|
||||
|
||||
QMap<QString, QMap<QString, WidgetConfig*>* > WidgetConfigLoaderPrivate::globalCache = QMap<QString, QMap<QString, WidgetConfig*>* >();
|
||||
QMap<QString, QMap<QString, WidgetConfig*>* > ConfigLoaderPrivate::globalCache = QMap<QString, QMap<QString, WidgetConfig*>* >();
|
||||
|
||||
bool WidgetConfigLoaderPrivate::hasContainer(const QString &container)
|
||||
bool ConfigLoaderPrivate::hasContainer(const QString &container)
|
||||
{
|
||||
return globalCache.contains(container);
|
||||
}
|
||||
|
||||
QString WidgetConfigLoaderPrivate::constructFileName(const QString &widgetId)
|
||||
QString ConfigLoaderPrivate::constructFileName(const QString &widgetId)
|
||||
{
|
||||
return widgetId + QStringLiteral(".conf");
|
||||
}
|
||||
|
||||
QString WidgetConfigLoaderPrivate::rootConfigPath()
|
||||
QString ConfigLoaderPrivate::rootConfigPath()
|
||||
{
|
||||
return constructFilePath(globalAppid(), globalContainerId());
|
||||
}
|
||||
|
||||
QString WidgetConfigLoaderPrivate::globalAppid()
|
||||
QString ConfigLoaderPrivate::globalAppid()
|
||||
{
|
||||
return QStringLiteral("org.ukui");
|
||||
}
|
||||
|
||||
QString WidgetConfigLoaderPrivate::globalContainerId()
|
||||
QString ConfigLoaderPrivate::globalContainerId()
|
||||
{
|
||||
return QStringLiteral("widget-config-global");
|
||||
}
|
||||
|
||||
QString WidgetConfigLoaderPrivate::constructFilePath(const QString &appid, const QString &containerId)
|
||||
QString ConfigLoaderPrivate::constructFilePath(const QString &appid, const QString &containerId)
|
||||
{
|
||||
return QString(QDir::homePath() + QStringLiteral("/.config/%1/%2/")).arg(appid, containerId);
|
||||
}
|
||||
|
||||
// ====== WidgetConfigLoader ====== //
|
||||
WidgetConfigLoader::WidgetConfigLoader(const QString &appid, const QString &containerId) : d(new WidgetConfigLoaderPrivate)
|
||||
// ====== ConfigLoader ====== //
|
||||
ConfigLoader::ConfigLoader(const QString &appid, const QString &containerId) : d(new ConfigLoaderPrivate)
|
||||
{
|
||||
d->appid = appid;
|
||||
d->containerId = containerId;
|
||||
}
|
||||
|
||||
WidgetConfig *WidgetConfigLoader::getConfig(const QString &widgetId)
|
||||
WidgetConfig *ConfigLoader::getConfig(const QString &widgetId)
|
||||
{
|
||||
return WidgetConfigLoader::loadConfig(widgetId, d->containerId, d->appid);
|
||||
return ConfigLoader::loadWidgetConfig(widgetId, d->containerId, d->appid);
|
||||
}
|
||||
|
||||
WidgetConfig *
|
||||
WidgetConfigLoader::loadConfig(const QString &widgetId, const QString &containerId, const QString &appid)
|
||||
ConfigLoader::loadWidgetConfig(const QString &widgetId, const QString &containerId, const QString &appid)
|
||||
{
|
||||
if (widgetId.isEmpty()) {
|
||||
return nullptr;
|
||||
|
@ -85,25 +85,25 @@ WidgetConfigLoader::loadConfig(const QString &widgetId, const QString &container
|
|||
QString app = appid;
|
||||
QString container = containerId;
|
||||
if (appid.isEmpty() || containerId.isEmpty()) {
|
||||
app = WidgetConfigLoaderPrivate::globalAppid();
|
||||
container = WidgetConfigLoaderPrivate::globalContainerId();
|
||||
app = ConfigLoaderPrivate::globalAppid();
|
||||
container = ConfigLoaderPrivate::globalContainerId();
|
||||
}
|
||||
|
||||
QString cacheKey = app + "-" + container;
|
||||
if (WidgetConfigLoaderPrivate::hasContainer(cacheKey)) {
|
||||
const QMap<QString, WidgetConfig*> *map = WidgetConfigLoaderPrivate::globalCache.value(cacheKey);
|
||||
if (ConfigLoaderPrivate::hasContainer(cacheKey)) {
|
||||
const QMap<QString, WidgetConfig*> *map = ConfigLoaderPrivate::globalCache.value(cacheKey);
|
||||
if (map->contains(widgetId)) {
|
||||
return map->value(widgetId);
|
||||
}
|
||||
}
|
||||
|
||||
QString configPath = WidgetConfigLoaderPrivate::constructFilePath(app, container);
|
||||
QString configPath = ConfigLoaderPrivate::constructFilePath(app, container);
|
||||
if (!QFile::exists(configPath)) {
|
||||
QDir().mkpath(configPath);
|
||||
}
|
||||
|
||||
// not create.
|
||||
QString filename = configPath + WidgetConfigLoaderPrivate::constructFileName(widgetId);
|
||||
QString filename = configPath + ConfigLoaderPrivate::constructFileName(widgetId);
|
||||
// QFile f(filename);
|
||||
// if (!f.exists()) {
|
||||
// f.open(QFile::WriteOnly);
|
||||
|
@ -111,25 +111,30 @@ WidgetConfigLoader::loadConfig(const QString &widgetId, const QString &container
|
|||
// }
|
||||
|
||||
auto widgetConfig = new WidgetConfig(filename);
|
||||
if (WidgetConfigLoaderPrivate::hasContainer(cacheKey)) {
|
||||
WidgetConfigLoaderPrivate::globalCache.value(cacheKey)->insert(widgetId, widgetConfig);
|
||||
if (ConfigLoaderPrivate::hasContainer(cacheKey)) {
|
||||
ConfigLoaderPrivate::globalCache.value(cacheKey)->insert(widgetId, widgetConfig);
|
||||
} else {
|
||||
auto map = new QMap<QString, WidgetConfig*>;
|
||||
map->insert(widgetId, widgetConfig);
|
||||
WidgetConfigLoaderPrivate::globalCache.insert(cacheKey, map);
|
||||
ConfigLoaderPrivate::globalCache.insert(cacheKey, map);
|
||||
}
|
||||
|
||||
return widgetConfig;
|
||||
}
|
||||
|
||||
QString WidgetConfigLoader::configFileName(const QString &widgetId)
|
||||
QString ConfigLoader::configFileName(const QString &widgetId)
|
||||
{
|
||||
return WidgetConfigLoaderPrivate::constructFileName(widgetId);
|
||||
return ConfigLoaderPrivate::constructFileName(widgetId);
|
||||
}
|
||||
|
||||
QString WidgetConfigLoader::getConfigFileName(const QString &widgetId)
|
||||
QString ConfigLoader::getConfigFileName(const QString &widgetId)
|
||||
{
|
||||
return WidgetConfigLoaderPrivate::constructFilePath(d->appid, d->containerId) + WidgetConfigLoader::configFileName(widgetId);
|
||||
return ConfigLoaderPrivate::constructFilePath(d->appid, d->containerId) + ConfigLoader::configFileName(widgetId);
|
||||
}
|
||||
|
||||
IniConfig *ConfigLoader::loadConfig(const QString &containerId, const QString &appid)
|
||||
{
|
||||
return loadWidgetConfig(containerId, containerId, appid);
|
||||
}
|
||||
|
||||
} // UkuiPanel
|
||||
|
|
|
@ -8,20 +8,22 @@
|
|||
|
||||
namespace UkuiPanel {
|
||||
|
||||
class WidgetConfigLoaderPrivate;
|
||||
class ConfigLoaderPrivate;
|
||||
|
||||
class WidgetConfigLoader
|
||||
class ConfigLoader
|
||||
{
|
||||
public:
|
||||
static QString configFileName(const QString &widgetId);
|
||||
static WidgetConfig *loadConfig(const QString &widgetId, const QString &containerId = QString(), const QString &appid = QString());
|
||||
static WidgetConfig *loadWidgetConfig(const QString &widgetId, const QString &containerId = QString(), const QString &appid = QString());
|
||||
static IniConfig *loadConfig(const QString &containerId = QString(), const QString &appid = QString());
|
||||
|
||||
explicit WidgetConfigLoader(const QString &appid, const QString &containerId);
|
||||
|
||||
explicit ConfigLoader(const QString &appid, const QString &containerId);
|
||||
WidgetConfig *getConfig(const QString &widgetId);
|
||||
QString getConfigFileName(const QString &widgetId);
|
||||
|
||||
private:
|
||||
WidgetConfigLoaderPrivate *d {nullptr};
|
||||
ConfigLoaderPrivate *d {nullptr};
|
||||
};
|
||||
|
||||
} // UkuiPanel
|
||||
|
|
|
@ -6,12 +6,12 @@
|
|||
|
||||
namespace UkuiPanel {
|
||||
|
||||
class WidgetConfigLoader;
|
||||
class ConfigLoader;
|
||||
|
||||
class WidgetConfig : public IniConfig
|
||||
{
|
||||
Q_OBJECT
|
||||
friend class WidgetConfigLoader;
|
||||
friend class ConfigLoader;
|
||||
public:
|
||||
WidgetConfig() = delete;
|
||||
|
||||
|
|
|
@ -6,4 +6,4 @@ if(TARGET Qt6::Core)
|
|||
find_dependency(Qt6Core5Compat @REQUIRED_QT_VERSION@)
|
||||
endif()
|
||||
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/ukui-notification-targets.cmake")
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/ukui-panel-framework-targets.cmake")
|
|
@ -1,11 +1,11 @@
|
|||
prefix=/usr
|
||||
exec_prefix=${prefix}
|
||||
libdir=${prefix}/lib/@CMAKE_LIBRARY_ARCHITECTURE@
|
||||
includedir=${prefix}/include/ukui-notification
|
||||
includedir=${prefix}/include/ukui-panel-framework
|
||||
|
||||
Name: ukui-notification
|
||||
Description: ukui-notification header files
|
||||
Description: ukui-panel-framework header files
|
||||
URL: https://www.ukui.org/
|
||||
Version: @VERSION@
|
||||
Cflags: -I${includedir}
|
||||
Libs: -L${libdir} -lukui-notification
|
||||
Libs: -L${libdir} -lukui-panel-framework
|
|
@ -236,4 +236,9 @@ void SharedEngineView::resizeEvent(QResizeEvent *event)
|
|||
QQuickWindow::resizeEvent(event);
|
||||
}
|
||||
|
||||
QQuickItem *SharedEngineView::rootObject() const
|
||||
{
|
||||
return d->rootItem;
|
||||
}
|
||||
|
||||
} // UkuiPanel
|
||||
|
|
|
@ -22,6 +22,7 @@ public:
|
|||
explicit SharedEngineView(QWindow *parent = nullptr);
|
||||
QQmlEngine *engine() const;
|
||||
QQmlContext *rootContext() const;
|
||||
QQuickItem *rootObject() const;
|
||||
void setResizeMode(SharedEngineView::ResizeMode resizeMode);
|
||||
|
||||
public Q_SLOTS:
|
||||
|
|
|
@ -171,10 +171,10 @@ WidgetConfig *Widget::config() const
|
|||
}
|
||||
|
||||
if (m.value("IsGlobal").toBool()) {
|
||||
return WidgetConfigLoader::loadConfig(d->metaData.id());
|
||||
return ConfigLoader::loadWidgetConfig(d->metaData.id());
|
||||
}
|
||||
|
||||
return WidgetConfigLoader::loadConfig(d->metaData.id(), d->container->containerId(), d->container->appid());
|
||||
return ConfigLoader::loadWidgetConfig(d->metaData.id(), d->container->containerId(), d->container->appid());
|
||||
}
|
||||
|
||||
void Widget::setContainer(WidgetContainer *container)
|
||||
|
|
|
@ -13,6 +13,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|||
|
||||
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Quick Widgets LinguistTools REQUIRED)
|
||||
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick Widgets LinguistTools REQUIRED)
|
||||
find_package(PkgConfig REQUIRED)
|
||||
find_package(KF5WindowSystem)
|
||||
|
||||
find_package(KF5WindowSystem REQUIRED)
|
||||
|
||||
|
@ -22,10 +24,25 @@ include_directories(3rd-parties/qtsingleapplication/src)
|
|||
include_directories(../framework/view)
|
||||
include_directories(src)
|
||||
include_directories(src/view)
|
||||
include_directories(../framework/widget)
|
||||
include_directories(../framework/widget-ui)
|
||||
include_directories(../framework/view)
|
||||
|
||||
set(TS_FILES translations/panel_zh_CN.ts)
|
||||
set(QRC_FILES qml/qml.qrc)
|
||||
|
||||
set(UKUI_PANEL_EXTERNAL_LIBS "")
|
||||
set(UKUI_PANEL_PC_PKGS kysdk-waylandhelper)
|
||||
|
||||
foreach(PC_LIB IN ITEMS ${UKUI_PANEL_PC_PKGS})
|
||||
pkg_check_modules(${PC_LIB} REQUIRED IMPORTED_TARGET ${PC_LIB})
|
||||
if(${${PC_LIB}_FOUND})
|
||||
include_directories(${${PC_LIB}_INCLUDE_DIRS})
|
||||
link_directories(${${PC_LIB}_LIBRARY_DIRS})
|
||||
list(APPEND UKUI_PANEL_EXTERNAL_LIBS PkgConfig::${PC_LIB})
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
set(PROJECT_SOURCES
|
||||
src/main.cpp
|
||||
src/shell.cpp
|
||||
|
@ -62,6 +79,7 @@ target_link_libraries(${PROJECT_NAME}
|
|||
Qt${QT_VERSION_MAJOR}::Widgets
|
||||
KF5::WindowSystem
|
||||
qtsingleapplication
|
||||
${UKUI_PANEL_EXTERNAL_LIBS}
|
||||
ukui-panel-framework
|
||||
)
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import QtQuick 2.15
|
||||
import QtQuick.Window 2.15
|
||||
import QtQuick.Layouts 1.15
|
||||
|
||||
Rectangle {
|
||||
color: "pink"
|
||||
RowLayout {
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ Shell::Shell(QObject *parent) : QObject(parent)
|
|||
void Shell::start()
|
||||
{
|
||||
QScreen *primary = QGuiApplication::primaryScreen();
|
||||
m_panels.insert(primary, new Panel);
|
||||
m_panels.insert(primary, new Panel(primary, "panel0"));
|
||||
|
||||
auto view = m_panels.value(primary);
|
||||
view->show();
|
||||
|
|
|
@ -1,18 +1,131 @@
|
|||
|
||||
|
||||
#include "panel.h"
|
||||
|
||||
#include <QUrl>
|
||||
#include <windowmanager/windowmanager.h>
|
||||
#include <KWindowSystem>
|
||||
|
||||
#include "widget.h"
|
||||
#include "widget-loader.h"
|
||||
#include "widget-item.h"
|
||||
#include "widget-config-loader.h"
|
||||
|
||||
#define DEFAULT_PANEL_SIZE 48
|
||||
static const QString PANEL_SIZE_KEY = QStringLiteral("panelSize");
|
||||
static const QString WIDGETS_KEY = QStringLiteral("widgets");
|
||||
static const QStringList DEFAULT_LOADING_WIDGETS = {"org.ukui.menu.starter", "org.ukui.panel.taskManager", "org.ukui.systemTray"};
|
||||
|
||||
namespace UkuiPanel {
|
||||
|
||||
Panel::Panel(QWindow *parent) : QQuickView(parent)
|
||||
Panel::Panel(QScreen *screen, const QString &id, QWindow *parent) : SharedEngineView(parent), m_screen(screen)
|
||||
{
|
||||
setResizeMode(QQuickView::SizeRootObjectToView);
|
||||
resize(600, 400);
|
||||
m_id = id;
|
||||
m_config = ConfigLoader::loadConfig(m_id, "ukui-panel");
|
||||
connect(m_screen, &QScreen::geometryChanged, this, &Panel::onScreenGeometryChanged);
|
||||
setResizeMode(SharedEngineView::SizeRootObjectToView);
|
||||
setFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
|
||||
KWindowSystem::setType(this->winId(), NET::Dock);
|
||||
|
||||
kdk::WindowManager::setOnAllDesktops(this->winId());
|
||||
initGeomtry();
|
||||
|
||||
const QUrl url(QStringLiteral("qrc:/main.qml"));
|
||||
setSource(url);
|
||||
initWidgets();
|
||||
}
|
||||
|
||||
QRect Panel::refreshRect()
|
||||
{
|
||||
QRect screenGeometry = m_screen->geometry();
|
||||
switch (m_position) {
|
||||
case Bottom:
|
||||
KWindowSystem::setExtendedStrut(winId(),
|
||||
/* Left */ 0, 0, 0,
|
||||
/* Right */ 0, 0, 0,
|
||||
/* Top */ 0, 0, 0,
|
||||
/* Bottom */ this->height(), geometry().left(), geometry().right()
|
||||
);
|
||||
return screenGeometry.adjusted(0, screenGeometry.height() - this->height(), 0, 0);
|
||||
case Left:
|
||||
KWindowSystem::setExtendedStrut(winId(),
|
||||
/* Left */ this->width(), geometry().top(), geometry().bottom(),
|
||||
/* Right */ 0, 0, 0,
|
||||
/* Top */ 0, 0, 0,
|
||||
/* Bottom */ 0, 0, 0
|
||||
);
|
||||
return screenGeometry.adjusted(0, 0, this->width() - screenGeometry.width(), 0);
|
||||
case Top:
|
||||
KWindowSystem::setExtendedStrut(winId(),
|
||||
/* Left */ 0, 0, 0,
|
||||
/* Right */ 0, 0, 0,
|
||||
/* Top */ this->height(), geometry().left(), geometry().right(),
|
||||
/* Bottom */ 0, 0, 0
|
||||
);
|
||||
return screenGeometry.adjusted(0, 0, 0, this->height() - screenGeometry.height());
|
||||
case Right:
|
||||
KWindowSystem::setExtendedStrut(winId(),
|
||||
/* Left */ 0, 0, 0,
|
||||
/* Right */ this->width(), geometry().top(), geometry().bottom(),
|
||||
/* Top */ 0, 0, 0,
|
||||
/* Bottom */ 0, 0, 0
|
||||
);
|
||||
return screenGeometry.adjusted(screenGeometry.width() - this->width(), 0, 0, 0);
|
||||
};
|
||||
}
|
||||
|
||||
void Panel::onScreenGeometryChanged(const QRect &geometry)
|
||||
{
|
||||
Q_UNUSED(geometry)
|
||||
kdk::WindowManager::setGeometry(this, refreshRect());
|
||||
}
|
||||
|
||||
void Panel::initWidgets()
|
||||
{
|
||||
QStringList loadingWidgets = DEFAULT_LOADING_WIDGETS;
|
||||
QVariant widgets = m_config->getValue(WIDGETS_KEY);
|
||||
if(widgets.isNull()) {
|
||||
m_config->setValue(WIDGETS_KEY, DEFAULT_LOADING_WIDGETS);
|
||||
} else {
|
||||
loadingWidgets = widgets.toStringList();
|
||||
}
|
||||
if(!m_container) {
|
||||
m_container = new WidgetContainer("ukui-panel", m_id, this);
|
||||
}
|
||||
|
||||
UkuiPanel::WidgetLoader loader;
|
||||
for (const auto &id : loadingWidgets) {
|
||||
UkuiPanel::Widget *widget = loader.loadWidget(id);
|
||||
if (!widget) {
|
||||
continue;
|
||||
}
|
||||
m_container->addWidget(widget);
|
||||
UkuiPanel::WidgetItem *widgetItem = UkuiPanel::WidgetItem::itemForWidget(widget);
|
||||
if (!widgetItem) {
|
||||
continue;
|
||||
}
|
||||
widgetItem->setParentItem(rootObject());
|
||||
}
|
||||
}
|
||||
|
||||
void Panel::changeScreen(QScreen *screen)
|
||||
{
|
||||
disconnect(m_screen, &QScreen::geometryChanged, this, &Panel::onScreenGeometryChanged);
|
||||
m_screen = screen;
|
||||
connect(m_screen, &QScreen::geometryChanged, this, &Panel::onScreenGeometryChanged);
|
||||
initGeomtry();
|
||||
}
|
||||
|
||||
void Panel::initGeomtry()
|
||||
{
|
||||
QVariant size = m_config->getValue(PANEL_SIZE_KEY);
|
||||
int panelSize = DEFAULT_PANEL_SIZE;
|
||||
if(size.isNull()) {
|
||||
m_config->setValue(PANEL_SIZE_KEY, DEFAULT_PANEL_SIZE);
|
||||
} else {
|
||||
panelSize = size.toInt();
|
||||
}
|
||||
|
||||
resize(m_screen->geometry().width(), panelSize);
|
||||
kdk::WindowManager::setGeometry(this, refreshRect());
|
||||
}
|
||||
} // UkuiPanel
|
||||
|
|
|
@ -4,14 +4,39 @@
|
|||
#define UKUI_PANEL_PANEL_H
|
||||
|
||||
#include <QQuickView>
|
||||
#include <QScreen>
|
||||
#include <QRect>
|
||||
#include "shared-engine-view.h"
|
||||
#include "ini-config.h"
|
||||
#include "widget-container.h"
|
||||
|
||||
namespace UkuiPanel {
|
||||
|
||||
class Panel : public QQuickView
|
||||
class Panel : public SharedEngineView
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit Panel(QWindow *parent = nullptr);
|
||||
enum Position {
|
||||
Bottom,
|
||||
Left,
|
||||
Top,
|
||||
Right
|
||||
};
|
||||
Panel(QScreen *screen, const QString &id, QWindow *parent = nullptr);
|
||||
void changeScreen(QScreen *screen);
|
||||
|
||||
private Q_SLOTS:
|
||||
void onScreenGeometryChanged(const QRect &geometry);
|
||||
private:
|
||||
QRect refreshRect();
|
||||
void initWidgets();
|
||||
void initGeomtry();
|
||||
|
||||
QScreen *m_screen = nullptr;
|
||||
Position m_position = Bottom;
|
||||
QString m_id;
|
||||
IniConfig *m_config = nullptr;
|
||||
WidgetContainer* m_container = nullptr;
|
||||
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue