Skip to content

Commit a9cc4db

Browse files
Kakueeendeepin-bot[bot]
authored andcommitted
fix(filepreviewdialog): add save and restore center position functionality
- Implemented methods to save and restore the window's center position when switching views, ensuring a consistent user experience. - Updated the UI initialization and page switching logic to incorporate these new methods, enhancing usability when navigating through file previews. Log: These changes improve the user interface by maintaining the window's position, making it more user-friendly during file preview transitions. Bug: https://pms.uniontech.com/bug-view-334307.html
1 parent 6d46092 commit a9cc4db

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed

src/apps/dde-file-manager-preview/libdfm-preview/views/filepreviewdialog.cpp

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <QAction>
2222
#include <QHBoxLayout>
2323
#include <QGuiApplication>
24+
#include <QScreen>
2425
#include <QTimer>
2526
#include <QWindow>
2627
#include <QMimeType>
@@ -66,7 +67,7 @@ FilePreviewDialog::~FilePreviewDialog()
6667
void FilePreviewDialog::updatePreviewList(const QList<QUrl> &previewUrllist)
6768
{
6869
qCInfo(logLibFilePreview) << "FilePreviewDialog: updating preview list with" << previewUrllist.size() << "files";
69-
70+
7071
// 视频预览的前一秒禁止再次播放
7172
if (playingVideo) {
7273
qCDebug(logLibFilePreview) << "FilePreviewDialog: video is playing, ignoring preview list update";
@@ -95,7 +96,7 @@ void FilePreviewDialog::setEntryUrlList(const QList<QUrl> &urlList)
9596
qCDebug(logLibFilePreview) << "FilePreviewDialog: empty URL list provided for entry list";
9697
return;
9798
}
98-
99+
99100
QUrl currentUrl = fileList.at(currentPageIndex);
100101
if (urlList.contains(currentUrl)) {
101102
qCInfo(logLibFilePreview) << "FilePreviewDialog: setting directory preview mode with" << urlList.size() << "files";
@@ -158,7 +159,7 @@ void FilePreviewDialog::handleFileInfoRefreshFinished(const QUrl url, const QStr
158159
{
159160
Q_UNUSED(infoPtr)
160161
Q_UNUSED(isLinkOrg)
161-
162+
162163
if (UniversalUtils::urlEquals(url, fileList.at(currentPageIndex))) {
163164
qCDebug(logLibFilePreview) << "FilePreviewDialog: file info refreshed for current file, updating preview:" << url.toString();
164165
switchToPage(currentPageIndex);
@@ -247,7 +248,7 @@ bool FilePreviewDialog::eventFilter(QObject *obj, QEvent *event)
247248
void FilePreviewDialog::initUI()
248249
{
249250
qCDebug(logLibFilePreview) << "FilePreviewDialog: initializing UI components";
250-
251+
251252
closeBtn = new DFloatingButton(DStyle::SP_CloseButton, this);
252253
closeBtn->setStyleSheet("background-color: transparent;");
253254
closeBtn->setFixedSize(46, 46);
@@ -286,19 +287,22 @@ void FilePreviewDialog::initUI()
286287
connect(statusBar->preButton(), &QPushButton::clicked, this, &FilePreviewDialog::previousPage);
287288
connect(statusBar->nextButton(), &QPushButton::clicked, this, &FilePreviewDialog::nextPage);
288289
connect(statusBar->openButton(), &QPushButton::clicked, this, &FilePreviewDialog::openFile);
289-
290+
290291
qCDebug(logLibFilePreview) << "FilePreviewDialog: UI initialization completed";
291292
}
292293

293294
void FilePreviewDialog::switchToPage(int index)
294295
{
295296
qCInfo(logLibFilePreview) << "FilePreviewDialog: switching to page" << index << "of" << fileList.count() << "files";
296-
297+
297298
if (index < 0 || index >= fileList.count()) {
298299
qCWarning(logLibFilePreview) << "FilePreviewDialog: invalid page index:" << index << "valid range: 0 -" << (fileList.count() - 1);
299300
return;
300301
}
301-
302+
303+
// 在切换视图前记录当前窗口中心位置(如果窗口可见)
304+
saveCenterPos();
305+
302306
currentPageIndex = index;
303307
statusBar->preButton()->setEnabled(index > 0);
304308
statusBar->nextButton()->setEnabled(index < fileList.count() - 1);
@@ -356,6 +360,7 @@ void FilePreviewDialog::switchToPage(int index)
356360
int newPerviewHeight = preview->contentWidget()->size().height();
357361
setFixedSize(newPerviewWidth, newPerviewHeight + statusBar->height());
358362
playCurrentPreviewFile();
363+
restoreCenterPos();
359364
qCInfo(logLibFilePreview) << "FilePreviewDialog: successfully reused preview for file:" << fileList.at(index).toString();
360365
return;
361366
}
@@ -427,7 +432,10 @@ void FilePreviewDialog::switchToPage(int index)
427432
int newPerviewHeight = preview->contentWidget()->size().height();
428433
setFixedSize(newPerviewWidth, newPerviewHeight + statusBar->height());
429434
updateTitle();
430-
435+
436+
// 切换并调整大小后,尝试将窗口移动回之前的中心位置
437+
restoreCenterPos();
438+
431439
qCInfo(logLibFilePreview) << "FilePreviewDialog: successfully switched to page" << index << "for file:" << fileList.at(index).toString();
432440
}
433441

@@ -485,7 +493,7 @@ void FilePreviewDialog::updateTitle()
485493
}
486494
statusBar->title()->setText(elidedText);
487495
statusBar->title()->setHidden(statusBar->title()->text().isEmpty());
488-
496+
489497
qCDebug(logLibFilePreview) << "FilePreviewDialog: updated title to:" << elidedText;
490498
}
491499

@@ -498,3 +506,56 @@ QString FilePreviewDialog::generalKey(const QString &key)
498506

499507
return key;
500508
}
509+
510+
void FilePreviewDialog::saveCenterPos()
511+
{
512+
if (!isVisible()) {
513+
// 窗口不可见时不记录
514+
return;
515+
}
516+
517+
// 使用 frameGeometry 来考虑窗口装饰
518+
QRect fg = frameGeometry();
519+
previousCenter = fg.center();
520+
qCDebug(logLibFilePreview) << "FilePreviewDialog: saved center position:" << previousCenter;
521+
}
522+
523+
void FilePreviewDialog::restoreCenterPos()
524+
{
525+
if (previousCenter.isNull()) {
526+
// 没有记录过中心点
527+
return;
528+
}
529+
530+
// 计算新的左上角位置以保持中心点不变
531+
QSize s = size();
532+
QPoint newTopLeft(previousCenter.x() - s.width() / 2, previousCenter.y() - s.height() / 2);
533+
534+
// 限制窗口位置到记录中心所在屏幕的可用区域,避免跨屏显示
535+
QScreen *scr = QGuiApplication::screenAt(previousCenter);
536+
QRect avail;
537+
if (scr) {
538+
avail = scr->availableGeometry();
539+
} else if (QGuiApplication::primaryScreen()) {
540+
avail = QGuiApplication::primaryScreen()->availableGeometry();
541+
}
542+
543+
if (!avail.isNull()) {
544+
// 如果窗口大小大于可用区域,则将窗口居中显示在该屏幕内
545+
if (s.width() > avail.width() || s.height() > avail.height()) {
546+
QPoint centered(avail.center().x() - s.width() / 2, avail.center().y() - s.height() / 2);
547+
newTopLeft = centered;
548+
} else {
549+
// 将 newTopLeft 限定在 avail 内
550+
int x = qBound(avail.left(), newTopLeft.x(), avail.right() - s.width());
551+
int y = qBound(avail.top(), newTopLeft.y(), avail.bottom() - s.height());
552+
newTopLeft = QPoint(x, y);
553+
}
554+
}
555+
556+
// 将窗口移动到新位置,但确保在主线程和可见时操作
557+
if (isVisible()) {
558+
move(newTopLeft);
559+
qCDebug(logLibFilePreview) << "FilePreviewDialog: restored window to keep center at" << previousCenter << "moved to" << newTopLeft;
560+
}
561+
}

src/apps/dde-file-manager-preview/libdfm-preview/views/filepreviewdialog.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public slots:
5555
void updateTitle();
5656
QString generalKey(const QString &key);
5757

58+
// 保存/恢复中心位置,保证切换视图后窗口仍居中到之前的位置
59+
void saveCenterPos();
60+
void restoreCenterPos();
61+
5862
QList<QUrl> fileList;
5963

6064
DTK_WIDGET_NAMESPACE::DFloatingButton *closeBtn { Q_NULLPTR };
@@ -68,6 +72,8 @@ public slots:
6872
quint64 currentWinID { 0 };
6973
DFMBASE_NAMESPACE::AbstractBasePreview *preview { nullptr };
7074
DFMBASE_NAMESPACE::DialogManager *dialogManager { nullptr };
75+
// 在切换视图前记录中心点, 切换后恢复
76+
QPoint previousCenter { 0, 0 };
7177
};
7278
}
7379
#endif

src/plugins/filemanager/dfmplugin-titlebar/dialogs/connecttoserverdialog.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ void ConnectToServerDialog::onCurrentInputChanged(const QString &server)
139139
// After clearing history, show a disabled "No history" placeholder
140140
serverComboBox->addItem(tr("No history"));
141141
serverComboBox->model()->setData(serverComboBox->model()->index(serverComboBox->count() - 1, 0), 0, Qt::UserRole - 1);
142+
serverComboBox->setCurrentIndex(-1);
142143
serverComboBox->clearEditText();
143144
serverComboBox->completer()->setModel(new QStringListModel());
144145
SearchHistroyManager::instance()->clearHistory(supportedSchemes);
@@ -287,6 +288,7 @@ void ConnectToServerDialog::initServerDatas()
287288
// No history: show a disabled placeholder so dropdown shows a hint
288289
serverComboBox->addItem(tr("No history"));
289290
serverComboBox->model()->setData(serverComboBox->model()->index(serverComboBox->count() - 1, 0), 0, Qt::UserRole - 1);
291+
serverComboBox->setCurrentIndex(-1);
290292
serverComboBox->clearEditText();
291293
}
292294
}

0 commit comments

Comments
 (0)