Skip to content

Commit bd8e353

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

File tree

3 files changed

+54
-32
lines changed

3 files changed

+54
-32
lines changed

edit/main.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ int main(int argc, char* argv[])
5757
}
5858
}
5959

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

gui/FileBrowser.cpp

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,40 @@ bool FileBrowser::process(InputEvents* events)
7979

8080
int cardY = currentCard->yAbs;
8181

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

100117
if (this->elements[this->highlighted] && this->elements[this->highlighted]->elasticCounter == NO_HIGHLIGHT)
101118
{
@@ -255,18 +272,7 @@ void FileBrowser::listfiles()
255272
// new folder, file, and exit buttons
256273
Container* con = new Container(ROW_LAYOUT, 10);
257274

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;
275+
con->add((new Button("Exit", SELECT_BUTTON, true))->setAction(mainDisplay->events->quitaction));
270276

271277
con->add((new Button("New Folder", X_BUTTON, true))->setAction([this, mainDisplay](){
272278
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)