feat: 窗口预览标题增加可配置项

根据gsetting配置窗口标题的显示模式

Log: 窗口预览图的标题在特效模式下可配置显示模式
Task: https://pms.uniontech.com/zentao/task-view-85600.html
Change-Id: I3fa043b6485c6d92d2f77bcb0b9fe43a00bfb70b
This commit is contained in:
shaojun 2021-09-14 17:08:36 +08:00
parent fcdb466208
commit c33693ad6e
8 changed files with 104 additions and 19 deletions

View File

@ -654,6 +654,11 @@ void AppItem::showPreview()
connect(m_appPreviewTips, &PreviewContainer::requestCancelPreviewWindow, this, &AppItem::onResetPreview);
connect(m_appPreviewTips, &PreviewContainer::requestHidePopup, this, &AppItem::onResetPreview);
// 预览标题显示方式的配置
if (m_activeAppSettings->keys().contains("previewTitle")) {
m_appPreviewTips->setTitleDisplayMode(m_activeAppSettings->get("previewTitle").toInt());
}
showPopupWindow(m_appPreviewTips, true);
}

View File

@ -60,6 +60,7 @@ AppSnapshot::AppSnapshot(const WId wid, QWidget *parent)
, m_closeAble(false)
, m_isWidowHidden(false)
, m_title(new TipsWidget(this))
, m_3DtitleBtn(nullptr)
, m_waitLeaveTimer(new QTimer(this))
, m_closeBtn2D(new DIconButton(this))
, m_wmHelper(DWindowManagerHelper::instance())
@ -95,6 +96,50 @@ void AppSnapshot::setWindowState()
}
}
// 每次更新窗口信息时更新标题
void AppSnapshot::updateTitle()
{
// 2D不显示
if (!m_wmHelper->hasComposite())
return;
if (!m_3DtitleBtn) {
m_3DtitleBtn = new DPushButton(this);
m_3DtitleBtn->setAccessibleName("AppPreviewTitle");
m_3DtitleBtn->setBackgroundRole(QPalette::Base);
m_3DtitleBtn->setForegroundRole(QPalette::Text);
m_3DtitleBtn->setFocusPolicy(Qt::NoFocus);
m_3DtitleBtn->setAttribute(Qt::WA_TransparentForMouseEvents);
m_3DtitleBtn->setFixedHeight(36);
m_3DtitleBtn->setVisible(false);
}
QFontMetrics fm(m_3DtitleBtn->font());
int textWidth = fm.width(title()) + 10 + BTN_TITLE_MARGIN;
int titleWidth = SNAP_WIDTH - (TITLE_MARGIN * 2 + BORDER_MARGIN);
if (textWidth < titleWidth) {
m_3DtitleBtn->setFixedWidth(textWidth);
m_3DtitleBtn->setText(title());
} else {
QString str = title();
/*某些特殊字符只显示一半 如"Q"," W",所以加一个空格保证字符显示完整,*/
str.insert(0, " ");
QString strTtile = m_3DtitleBtn->fontMetrics().elidedText(str, Qt::ElideRight, titleWidth - BTN_TITLE_MARGIN);
m_3DtitleBtn->setText(strTtile);
m_3DtitleBtn->setFixedWidth(titleWidth + BTN_TITLE_MARGIN);
}
// 移动到预览图中下
m_3DtitleBtn->move(QPoint(SNAP_WIDTH / 2, SNAP_HEIGHT - m_3DtitleBtn->height() / 2 - TITLE_MARGIN) - m_3DtitleBtn->rect().center());
}
void AppSnapshot::setTitleVisible(bool bVisible)
{
if (m_3DtitleBtn)
m_3DtitleBtn->setVisible(bVisible && m_wmHelper->hasComposite());
}
void AppSnapshot::closeWindow() const
{
const auto display = QX11Info::display();
@ -135,6 +180,7 @@ void AppSnapshot::setWindowInfo(const WindowInfo &info)
QString strTtile = m_title->fontMetrics().elidedText(m_windowInfo.title, Qt::ElideRight, width() - m_closeBtn2D->width());
m_title->setText(strTtile);
getWindowState();
updateTitle();
}
void AppSnapshot::dragEnterEvent(QDragEnterEvent *e)

View File

@ -28,6 +28,7 @@
#include <DIconButton>
#include <DWindowManagerHelper>
#include <DPushButton>
#include <com_deepin_dde_daemon_dock.h>
#include <com_deepin_dde_daemon_dock_entry.h>
@ -40,6 +41,14 @@ DGUI_USE_NAMESPACE
#define SNAP_CLOSE_BTN_WIDTH (24)
#define SNAP_CLOSE_BTN_MARGIN (5)
// 标题到左右两边的距离
#define TITLE_MARGIN (20)
// 标题的文本到标题背景两边的距离
#define BTN_TITLE_MARGIN (6)
// 高亮框边距
#define BORDER_MARGIN (8)
struct SHMInfo;
struct _XImage;
@ -66,6 +75,8 @@ public:
inline const QRectF snapshotGeometry() const { return m_snapshotSrcRect; }
inline const QString title() const { return m_windowInfo.title; }
void setWindowState();
void setTitleVisible(bool bVisible);
QString appTitle() { return m_3DtitleBtn ? m_3DtitleBtn->text() : QString(); }
signals:
void entered(const WId wid) const;
@ -91,6 +102,7 @@ private:
XImage *getImageXlib();
QRect rectRemovedShadow(const QImage &qimage, unsigned char *prop_to_return_gtk);
void getWindowState();
void updateTitle();
private:
const WId m_wid;
@ -102,6 +114,8 @@ private:
QRectF m_snapshotSrcRect;
Dock::TipsWidget *m_title;
DPushButton *m_3DtitleBtn;
QTimer *m_waitLeaveTimer;
DIconButton *m_closeBtn2D;
DWindowManagerHelper *m_wmHelper;

View File

@ -29,10 +29,6 @@
#include <QPainter>
#include <QVBoxLayout>
#define BORDER_MARGIN 8
#define TITLE_MARGIN 20
#define BTN_TITLE_MARGIN 6
FloatingPreview::FloatingPreview(QWidget *parent)
: QWidget(parent)
, m_closeBtn3D(new DIconButton(this))
@ -77,6 +73,11 @@ AppSnapshot *FloatingPreview::trackedWindow()
return m_tracked;
}
void FloatingPreview::setFloatingTitleVisible(bool bVisible)
{
m_titleBtn->setVisible(bVisible);
}
void FloatingPreview::trackWindow(AppSnapshot *const snap)
{
if (!snap)
@ -90,21 +91,9 @@ void FloatingPreview::trackWindow(AppSnapshot *const snap)
m_closeBtn3D->setVisible(m_tracked->closeAble());
QFontMetrics fm(m_titleBtn->font());
int textWidth = fm.width(m_tracked->title()) + 10 + BTN_TITLE_MARGIN;
int titleWidth = width() - (TITLE_MARGIN * 2 + BORDER_MARGIN);
if (textWidth < titleWidth) {
m_titleBtn->setFixedWidth(textWidth);
m_titleBtn->setText(m_tracked->title());
} else {
QString str = m_tracked->title();
/*某些特殊字符只显示一半 如"Q"," W",所以加一个空格保证字符显示完整,*/
str.insert(0, " ");
QString strTtile = m_titleBtn->fontMetrics().elidedText(str, Qt::ElideRight, titleWidth - BTN_TITLE_MARGIN);
m_titleBtn->setText(strTtile);
m_titleBtn->setFixedWidth(titleWidth + BTN_TITLE_MARGIN);
}
// 显示此标题的前提条件:配置了标题跟随鼠标显示
// 此对象是共用的鼠标移动到哪个预览图title就移动到哪里显示所以他的text统一snap获取不再重复计算显示长度
m_titleBtn->setText(snap->appTitle());
QTimer::singleShot(0, this, [ = ] {
// 此处获取的snap->geometry()有可能是错误的所以做个判断并且在resizeEvent中也做处理

View File

@ -40,6 +40,7 @@ public:
WId trackedWid() const;
AppSnapshot *trackedWindow();
void setFloatingTitleVisible(bool bVisible);
public slots:
void trackWindow(AppSnapshot *const snap);

View File

@ -37,6 +37,7 @@ PreviewContainer::PreviewContainer(QWidget *parent)
, m_floatingPreview(new FloatingPreview(this))
, m_mouseLeaveTimer(new QTimer(this))
, m_wmHelper(DWindowManagerHelper::instance())
, m_titleMode(HoverShow)
{
m_windowListLayout = new QBoxLayout(QBoxLayout::LeftToRight, this);
m_windowListLayout->setSpacing(SPACING);
@ -96,6 +97,20 @@ void PreviewContainer::updateSnapshots()
snap->fetchSnapshot();
}
void PreviewContainer::setTitleDisplayMode(int mode)
{
m_titleMode = static_cast<TitleDisplayMode>(mode);
if (!m_wmHelper->hasComposite())
return;
m_floatingPreview->setFloatingTitleVisible(m_titleMode == HoverShow);
for (AppSnapshot *snap : m_snapshots) {
snap->setTitleVisible(m_titleMode == AlwaysShow);
}
}
void PreviewContainer::updateLayoutDirection(const Dock::Position dockPos)
{
if (m_wmHelper->hasComposite() && (dockPos == Dock::Top || dockPos == Dock::Bottom))

View File

@ -43,6 +43,12 @@ class PreviewContainer : public QWidget
public:
explicit PreviewContainer(QWidget *parent = 0);
enum TitleDisplayMode {
HoverShow = 0,
AlwaysShow = 1,
AlwaysHide = 2,
};
signals:
void requestActivateWindow(const WId wid) const;
void requestPreviewWindow(const WId wid) const;
@ -53,6 +59,7 @@ signals:
public:
void setWindowInfos(const WindowInfoMap &infos, const WindowList &allowClose);
void updateSnapshots();
void setTitleDisplayMode(int mode);
public slots:
void updateLayoutDirection(const Dock::Position dockPos);
@ -85,6 +92,7 @@ private:
DWindowManagerHelper *m_wmHelper;
QTimer *m_waitForShowPreviewTimer;
WId m_currentWId;
TitleDisplayMode m_titleMode;
};
#endif // PREVIEWCONTAINER_H

View File

@ -62,6 +62,13 @@
Control Module Enable
</description>
</key>
<key type="u" name="preview-title">
<default>0</default>
<summary>0 Mouse over show;1 Always show; 2 Always hide </summary>
<description>
The display mode of the application preview
</description>
</key>
</schema>
<schema path="/com/deepin/dde/dock/module/dockapp/" id="com.deepin.dde.dock.module.dockapp" gettext-domain="DDE">
<key type="b" name="control">