Skip to content

Commit 0be19cc

Browse files
committed
fix crash on exit in single file mode, use smooth scrolling for file browser
1 parent 803b715 commit 0be19cc

File tree

3 files changed

+59
-36
lines changed

3 files changed

+59
-36
lines changed

edit/main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
#include "../libs/json/single_include/nlohmann/json.hpp"
77
#include <algorithm>
88

9+
10+
#if defined(__WIIU__)
11+
#include <sysapp/launch.h>
12+
#endif
13+
914
#if defined(PC)
1015
#define START_PATH "."
1116
#elif defined(SWITCH)
@@ -57,6 +62,19 @@ int main(int argc, char* argv[])
5762
}
5863
}
5964

65+
// setup the quit callback (used by FileBrowser or EditorView in single file mode)
66+
auto quitaction = [display]() {
67+
#ifdef __WIIU__
68+
// will exit via procui loop in RootDisplay
69+
SYSLaunchMenu();
70+
#else
71+
display->exitRequested = true;
72+
display->isRunning = false;
73+
#endif
74+
};
75+
76+
events->quitaction = quitaction;
77+
6078
// if the start path isn't a directory,use single file mode
6179
if (startPath != START_PATH && !std::filesystem::is_directory(startPath))
6280
{

gui/FileBrowser.cpp

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
#include <sys/stat.h>
1414
#include <algorithm>
1515

16-
#if defined(__WIIU__)
17-
#include <sysapp/launch.h>
18-
#endif
19-
2016
#if defined(WIN32)
2117
#include <sys/types.h>
2218
#endif
@@ -79,23 +75,40 @@ bool FileBrowser::process(InputEvents* events)
7975

8076
int cardY = currentCard->yAbs;
8177

82-
if (!touchMode) {
83-
// keep the cursor on screen if using buttons
84-
if (highlighted <= 3) {
85-
this->y = 0;
86-
updateUI |= true;
87-
}
88-
else
89-
{
90-
if (cardY < 10) {
91-
this->y += currentCard->height;
92-
updateUI |= true;
93-
}
94-
if (cardY > SCREEN_HEIGHT - (currentCard->height + 10)) {
95-
this->y -= currentCard->height;
96-
updateUI |= true;
97-
}
98-
}
78+
// Below code is lifted from HBAS's AppList.cpp
79+
// TODO: consolidate into Chesto's ListElement
80+
if (!touchMode && this->elements.size() > this->highlighted && this->highlighted >= 0 && this->elements[this->highlighted])
81+
{
82+
// if our highlighted position is large enough, force scroll the screen so that our cursor stays on screen
83+
Element* curTile = this->elements[this->highlighted];
84+
85+
// the y-position of the currently highlighted tile, precisely on them screen (accounting for scroll)
86+
// this means that if it's < 0 or > SCREEN_HEIGHT then it's not visible
87+
int normalizedY = curTile->y + this->y;
88+
89+
// if we're FAR out of range upwards, speed up the scroll wheel (additive) to get back in range quicker
90+
if (normalizedY < -200)
91+
events->wheelScroll += 0.15;
92+
93+
// far out of range, for bottom of screen
94+
else if (normalizedY > SCREEN_HEIGHT - curTile->height + 200)
95+
events->wheelScroll -= 0.15;
96+
97+
// if we're slightly out of range above, recenter at the top row slowly
98+
else if (normalizedY < -100)
99+
events->wheelScroll = 1;
100+
101+
// special case, scroll when we're on the bottom row of the top of the not-yet-scrolled screen
102+
else if (this->y == 0 && normalizedY > SCREEN_HEIGHT/2)
103+
events->wheelScroll -= 0.5;
104+
105+
// if we're out of range below, recenter at bottom row
106+
else if (normalizedY > SCREEN_HEIGHT - curTile->height + 100)
107+
events->wheelScroll = -1;
108+
109+
// if the card is this close to the top, just set it the list offset to 0 to scroll up to the top
110+
else if (this->y != 0 && this->highlighted < cardsPerRow)
111+
events->wheelScroll = 1;
99112

100113
if (this->elements[this->highlighted] && this->elements[this->highlighted]->elasticCounter == NO_HIGHLIGHT)
101114
{
@@ -255,18 +268,7 @@ void FileBrowser::listfiles()
255268
// new folder, file, and exit buttons
256269
Container* con = new Container(ROW_LAYOUT, 10);
257270

258-
auto quitaction = [mainDisplay](){
259-
#ifdef __WIIU__
260-
// will exit via procui loop in RootDisplay
261-
SYSLaunchMenu();
262-
#else
263-
mainDisplay->exitRequested = true;
264-
mainDisplay->isRunning = false;
265-
#endif
266-
};
267-
268-
con->add((new Button("Exit", SELECT_BUTTON, true))->setAction(quitaction));
269-
mainDisplay->events->quitaction = quitaction;
271+
con->add((new Button("Exit", SELECT_BUTTON, true))->setAction(mainDisplay->events->quitaction));
270272

271273
con->add((new Button("New Folder", X_BUTTON, true))->setAction([this, mainDisplay](){
272274
std::function<void(const char*)> createFunc = [this](const char* name){

gui/MainDisplay.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,15 @@ void MainDisplay::closeEditor()
7474

7575
if (!success) {
7676
std::cout << "Failed to launch " << callbackPath << std::endl;
77-
exit(1);
7877
}
7978
}
8079

81-
exit(0);
82-
return;
80+
if (events->quitaction != NULL) {
81+
events->quitaction();
82+
return;
83+
}
84+
85+
// if the quitaction didn't work, just let the editor close
8386
}
8487

8588
// otherwise, we'll go back to the file browser (this is _probably_ safe)

0 commit comments

Comments
 (0)