Skip to content

Commit

Permalink
Merge pull request #720 from jensenr30/hunk-header
Browse files Browse the repository at this point in the history
Hunk header does not interfere with line wrapping
  • Loading branch information
Murmele authored Mar 28, 2024
2 parents dac0647 + 5e124bd commit 063099a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/ui/DiffView/HunkWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ const QString noNewLineAtEndOfFile =
HunkWidget::tr("No newline at end of file");
} // namespace

_HunkWidget::HunkLabel::HunkLabel(const QString &name, bool submodule,
QWidget *parent)
: QWidget(parent), mName(name) {}
void _HunkWidget::HunkLabel::paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QFontMetrics fm = fontMetrics();
QRect rect = fm.boundingRect(0, 0, this->rect().width(), 300, 0, mName);
painter.drawText(rect, Qt::AlignLeft | Qt::ElideRight, mName);
}

_HunkWidget::Header::Header(const git::Diff &diff, const git::Patch &patch,
int index, bool lfs, bool submodule,
QWidget *parent)
Expand All @@ -45,9 +56,9 @@ _HunkWidget::Header::Header(const git::Diff &diff, const git::Patch &patch,
mCheck->setVisible(diff.isStatusDiff() && !submodule &&
!patch.isConflicted());

QString header = (index >= 0) ? patch.header(index) : QString();
QString escaped = header.trimmed().toHtmlEscaped();
QLabel *label = new QLabel(DiffViewStyle::kHunkFmt.arg(escaped), this);
QString label_string = (index >= 0) ? patch.header(index) : QString();
label_string = label_string.trimmed().toHtmlEscaped();
HunkLabel *label = new HunkLabel(label_string, submodule, this);

if (patch.isConflicted()) {
mSave = new QToolButton(this);
Expand Down Expand Up @@ -132,7 +143,7 @@ _HunkWidget::Header::Header(const git::Diff &diff, const git::Patch &patch,
QHBoxLayout *layout = new QHBoxLayout(this);
layout->setContentsMargins(4, 4, 4, 4);
layout->addWidget(mCheck);
layout->addWidget(label);
layout->addWidget(label, 1);
layout->addStretch();
layout->addLayout(buttons);

Expand Down
12 changes: 12 additions & 0 deletions src/ui/DiffView/HunkWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ class DisclosureButton;
class Line;

namespace _HunkWidget {
class HunkLabel : public QWidget {
public:
HunkLabel(const QString &name, bool submodule, QWidget *parent = nullptr);

protected:
void paintEvent(QPaintEvent *event) override;

private:
QString mName;
QString mOldName;
};

class Header : public QFrame {
Q_OBJECT
public:
Expand Down

0 comments on commit 063099a

Please sign in to comment.