Skip to content

Commit aa9765c

Browse files
committed
feat(SurfaceListing): improved cursor movement behavior
1 parent cc54d50 commit aa9765c

6 files changed

Lines changed: 55 additions & 28 deletions

File tree

‎src/support/utils.cpp‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,6 @@ bool handle_key_press(ISurface* surface, QKeyEvent* e) {
155155
else if(e->matches(QKeySequence::MoveToPreviousChar)) {
156156
if(col > 0) surface->set_position(row, col - 1);
157157
}
158-
else if(e->matches(QKeySequence::MoveToNextLine)) {
159-
surface->set_position(row + 1, col);
160-
}
161-
else if(e->matches(QKeySequence::MoveToPreviousLine)) {
162-
if(row > 0) surface->set_position(row - 1, col);
163-
}
164158
else if(e->matches(QKeySequence::MoveToStartOfLine)) {
165159
surface->set_position(row, 0);
166160
}

‎src/views/surface/graph/graph.cpp‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,22 @@ void SurfaceGraph::set_mode(RDRenderMode m) {
7272
this->invalidate();
7373
}
7474

75-
void SurfaceGraph::set_position(int row, int col) {
76-
if(rd_surfacegraph_set_pos(m_surface, row, col)) this->invalidate();
75+
bool SurfaceGraph::set_position(int row, int col) {
76+
if(rd_surfacegraph_set_pos(m_surface, row, col)) {
77+
this->invalidate();
78+
return true;
79+
}
80+
81+
return false;
7782
}
7883

79-
void SurfaceGraph::select(int row, int col) {
80-
if(rd_surfacegraph_select(m_surface, row, col)) this->invalidate();
84+
bool SurfaceGraph::select(int row, int col) {
85+
if(rd_surfacegraph_select(m_surface, row, col)) {
86+
this->invalidate();
87+
return true;
88+
}
89+
90+
return false;
8191
}
8292

8393
bool SurfaceGraph::go_back() {
@@ -172,8 +182,18 @@ GraphViewNode* SurfaceGraph::create_node(RDGraphNode n, const RDGraph*) {
172182

173183
void SurfaceGraph::keyPressEvent(QKeyEvent* e) {
174184
if(!utils::handle_key_press(this, e)) {
175-
if(e->key() == Qt::Key_Space)
185+
RDSurfacePos pos = this->get_position();
186+
auto [row, col] = pos;
187+
188+
if(e->matches(QKeySequence::MoveToNextLine)) {
189+
this->set_position(row + 1, col);
190+
}
191+
else if(e->matches(QKeySequence::MoveToPreviousLine)) {
192+
if(row > 0) this->set_position(row - 1, col);
193+
}
194+
else if(e->key() == Qt::Key_Space) {
176195
Q_EMIT switch_view();
196+
}
177197
else
178198
GraphView::keyPressEvent(e);
179199

‎src/views/surface/graph/graph.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class SurfaceGraph: public GraphView, public ISurface {
3030
void jump_to(RDAddress address) override;
3131
bool invalidate() override;
3232
void set_mode(RDRenderMode m) override;
33-
void set_position(int row, int col) override;
34-
void select(int row, int col) override;
33+
bool set_position(int row, int col) override;
34+
bool select(int row, int col) override;
3535
// clang-format on
3636

3737
public Q_SLOTS:

‎src/views/surface/isurface.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class ISurface {
1515
virtual bool invalidate() = 0;
1616
virtual bool go_back() = 0;
1717
virtual bool go_forward() = 0;
18-
virtual void set_position(int row, int col) = 0;
19-
virtual void select(int row, int col) = 0;
18+
virtual bool set_position(int row, int col) = 0;
19+
virtual bool select(int row, int col) = 0;
2020
[[nodiscard]] virtual RDRenderMode get_mode() const = 0;
2121
[[nodiscard]] virtual bool has_selection() const = 0;
2222
[[nodiscard]] virtual bool can_go_back() const = 0;

‎src/views/surface/listing.cpp‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ void SurfaceListing::set_mode(RDRenderMode m) {
107107
this->viewport()->update();
108108
}
109109

110-
void SurfaceListing::set_position(int row, int col) {
111-
rd_surface_set_pos(m_surface, row, col);
110+
bool SurfaceListing::set_position(int row, int col) {
111+
return rd_surface_set_pos(m_surface, row, col);
112112
}
113113

114-
void SurfaceListing::select(int row, int col) {
115-
rd_surface_select(m_surface, row, col);
114+
bool SurfaceListing::select(int row, int col) {
115+
return rd_surface_select(m_surface, row, col);
116116
}
117117

118118
bool SurfaceListing::go_back() {
@@ -239,10 +239,25 @@ void SurfaceListing::wheelEvent(QWheelEvent* e) {
239239

240240
void SurfaceListing::keyPressEvent(QKeyEvent* e) {
241241
if(!utils::handle_key_press(this, e)) {
242-
QScrollBar64* vscroll = this->verticalScrollBar();
243242
auto [row, col] = this->get_position();
244243

245-
if(e->matches(QKeySequence::MoveToEndOfLine)) {
244+
if(e->matches(QKeySequence::MoveToNextLine)) {
245+
if(row + 1 < this->visible_rows()) {
246+
this->set_position(row + 1, col);
247+
}
248+
else {
249+
if(!rd_surface_scroll(m_surface, 1)) return;
250+
}
251+
}
252+
else if(e->matches(QKeySequence::MoveToPreviousLine)) {
253+
if(row - 1 >= 0) {
254+
this->set_position(row - 1, col);
255+
}
256+
else {
257+
if(!rd_surface_scroll(m_surface, -1)) return;
258+
}
259+
}
260+
else if(e->matches(QKeySequence::MoveToEndOfLine)) {
246261
rd_surface_set_pos(m_surface, row, this->visible_columns());
247262
}
248263
else if(e->matches(QKeySequence::MoveToNextPage)) {
@@ -252,12 +267,10 @@ void SurfaceListing::keyPressEvent(QKeyEvent* e) {
252267
if(!rd_surface_scroll(m_surface, -this->visible_rows())) return;
253268
}
254269
else if(e->matches(QKeySequence::MoveToStartOfDocument)) {
255-
vscroll->setValue(0);
256-
return;
270+
rd_surface_set_pos(m_surface, 0, col);
257271
}
258272
else if(e->matches(QKeySequence::MoveToEndOfDocument)) {
259-
vscroll->setValue(vscroll->value() + this->visible_rows() - 1);
260-
return;
273+
rd_surface_set_pos(m_surface, this->visible_rows(), col);
261274
}
262275
else if(e->matches(QKeySequence::SelectEndOfLine)) {
263276
rd_surface_select(m_surface, row, this->visible_columns());
@@ -340,7 +353,7 @@ int SurfaceListing::visible_columns() const {
340353
}
341354

342355
int SurfaceListing::visible_rows() const {
343-
return qCeil(this->viewport()->height() / surface_renderer::cell_height());
356+
return qFloor(this->viewport()->height() / surface_renderer::cell_height());
344357
}
345358

346359
RDSurfacePos SurfaceListing::get_surface_coords(QPoint pt) const {

‎src/views/surface/listing.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class SurfaceListing: public QScrollArea64, public ISurface {
3030
[[nodiscard]] int visible_columns() const;
3131
[[nodiscard]] int visible_rows() const;
3232
void set_mode(RDRenderMode m) override;
33-
void set_position(int row, int col) override;
34-
void select(int row, int col) override;
33+
bool set_position(int row, int col) override;
34+
bool select(int row, int col) override;
3535
// clang-format on
3636

3737
public Q_SLOTS:

0 commit comments

Comments
 (0)