-
Notifications
You must be signed in to change notification settings - Fork 37
Fix: cursor jumping to end in rename dialog #337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
log: Validate input only on focus loss instead of on every keystroke. Bug:https://pms.uniontech.com/bug-view-345447.html
deepin pr auto review我来对这个diff进行代码审查:
改进后的代码建议: class RenameDialog {
private:
static const QString INVALID_CHARS_REGEX; // 定义为类静态常量
static const QRegularExpression INVALID_CHARS_PATTERN;
};
const QString RenameDialog::INVALID_CHARS_REGEX = "(^\\s+|[/\\\\:*\"'?<>|\r\n\t])";
const QRegularExpression RenameDialog::INVALID_CHARS_PATTERN(INVALID_CHARS_REGEX);
// 在showDialog中:
connect(m_lineEdit, &DLineEdit::textChanged, [=](){
QString text = m_lineEdit->text();
if(m_nOkBtnIndex >= 0) {
getButton(m_nOkBtnIndex)->setEnabled(!text.isEmpty());
}
});
connect(m_lineEdit->lineEdit(), &QLineEdit::editingFinished, [=](){
QLineEdit *lineEdit = m_lineEdit->lineEdit();
if (!lineEdit) return; // 安全性检查
QString text = lineEdit->text();
if(INVALID_CHARS_PATTERN.match(text).hasMatch() || text.startsWith(" ")) {
lineEdit->setText(text.remove(INVALID_CHARS_PATTERN));
}
});这样的改进:
|
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdjusts rename dialog validation so illegal characters are stripped only when editing finishes (focus loss), preventing the cursor from jumping to the end while still disabling the OK button on empty input. Sequence diagram for rename dialog text validation on focus losssequenceDiagram
actor User
participant RenameDialog
participant DLineEdit
participant QLineEdit
participant OkButton
User->>RenameDialog: Open rename dialog
RenameDialog->>DLineEdit: create m_lineEdit
DLineEdit->>QLineEdit: create internal lineEdit
RenameDialog->>DLineEdit: connect textChanged
RenameDialog->>QLineEdit: connect editingFinished
User->>QLineEdit: Type characters
QLineEdit-->>DLineEdit: textChanged(text)
DLineEdit-->>RenameDialog: textChanged(text)
RenameDialog->>RenameDialog: Check if text is empty
alt text is empty
RenameDialog->>OkButton: setEnabled(false)
else text is not empty
RenameDialog->>OkButton: setEnabled(true)
end
User->>QLineEdit: Change focus (finish editing)
QLineEdit-->>RenameDialog: editingFinished()
RenameDialog->>RenameDialog: Validate text with regex
alt text contains illegal characters or starts with space
RenameDialog->>QLineEdit: setText(cleanedText)
else text is valid
Note over RenameDialog,QLineEdit: Text remains unchanged
end
Class diagram for updated rename dialog validation logicclassDiagram
class RenameDialog {
- DLineEdit* m_lineEdit
- int m_nOkBtnIndex
+ int showDialog(QString strReName, QString strAlias, QWidget* parent)
}
class DLineEdit {
+ QLineEdit* lineEdit()
+ signal textChanged(QString text)
}
class QLineEdit {
+ QString text()
+ void setText(QString text)
+ signal textChanged(QString text)
+ signal editingFinished()
}
class DButton {
+ void setEnabled(bool enabled)
}
RenameDialog *-- DLineEdit : owns
DLineEdit *-- QLineEdit : wraps
RenameDialog --> QLineEdit : uses for validation
RenameDialog --> DButton : controls enabled state
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey - I've left some high level feedback:
- The regular expression pattern for invalid characters is now duplicated between the
textChangedandeditingFinishedhandlers; consider extracting it into a shared constant or helper to keep the logic in sync. - The explicit
text.startsWith(" ")check appears redundant given the(^\s+...)part of the regex already matches leading whitespace; you can likely simplify the condition to only rely on the regex match. - When updating the line edit text on
editingFinished, consider whether you need to preserve the cursor position/selection, as callingsetTextresets it and may affect UX if focus returns to the field.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The regular expression pattern for invalid characters is now duplicated between the `textChanged` and `editingFinished` handlers; consider extracting it into a shared constant or helper to keep the logic in sync.
- The explicit `text.startsWith(" ")` check appears redundant given the `(^\s+...)` part of the regex already matches leading whitespace; you can likely simplify the condition to only rely on the regex match.
- When updating the line edit text on `editingFinished`, consider whether you need to preserve the cursor position/selection, as calling `setText` resets it and may affect UX if focus returns to the field.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: LiHua000, max-lvs The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/merge |
log: Validate input only on focus loss instead of on every keystroke.
Bug:https://pms.uniontech.com/bug-view-345447.html
Summary by Sourcery
Adjust rename dialog input handling to validate and clean illegal characters only after editing is finished, preventing cursor position issues during typing while keeping confirmation button state tied to non-empty input.
Bug Fixes:
Enhancements: