Skip to content

Commit

Permalink
Terminal: Scrollback buffer update bounds on resize, TextEdit: Larger…
Browse files Browse the repository at this point in the history
… default window size
  • Loading branch information
fido2020 committed Aug 29, 2021
1 parent 0894691 commit 4a3012f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
48 changes: 47 additions & 1 deletion Applications/Terminal/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "colours.h"
#include "escape.h"

#define SCROLLBACK_BUFFER_MAX 400

using namespace Lemon;

Colour* colours = coloursProfile2;
Expand Down Expand Up @@ -82,10 +84,19 @@ void ClearScrollbackBuffer() {
scrollbackBuffer = std::vector<TerminalLine>(terminalSize.y, TerminalLine(terminalSize.x, TerminalChar(0)));
}

void AddLine(){
if(scrollbackBuffer.size() < SCROLLBACK_BUFFER_MAX){
scrollbackBuffer.push_back(TerminalLine(terminalSize.x, TerminalChar(0)));
} else {
scrollbackBuffer.erase(scrollbackBuffer.begin());
scrollbackBuffer.push_back(TerminalLine(terminalSize.x, TerminalChar(0)));
}
}

void AdvanceCursorY() {
cursorPosition.y++;
while (cursorPosition.y > terminalSize.y) {
scrollbackBuffer.push_back(TerminalLine(terminalSize.x, TerminalChar(0)));
AddLine();
cursorPosition.y--;
}
}
Expand Down Expand Up @@ -480,6 +491,10 @@ int main(int argc, char** argv) {
while (isOpen) {
Lemon::WindowServer::Instance()->Poll();

// Make sure we do not resize more than necessary
bool shouldResize = false;
Vector2i newSize;

Lemon::LemonEvent ev;
while (terminalWindow->PollEvent(ev)) {
if (ev.event == EventWindowClosed) {
Expand Down Expand Up @@ -512,7 +527,38 @@ int main(int argc, char** argv) {
const char key = (char)ev.key;
write(ptyMasterFd, &key, 1);
}
} else if(ev.event == EventWindowResize) {
shouldResize = true;
newSize = ev.resizeBounds;
}
}

if(shouldResize){
int linesToAdd = newSize.y / characterSize.y - terminalSize.y;
int colsToAdd = newSize.x / characterSize.x - terminalSize.x;

terminalSize =
Vector2i{newSize.x / characterSize.x, newSize.y / characterSize.y};

for(auto it = FirstLine(); it != scrollbackBuffer.end(); ++it){
if(colsToAdd > 0){
it->insert(it->end(), colsToAdd, TerminalChar(0));
}

it->resize(terminalSize.x);
}

while(linesToAdd > 0){
AddLine();
linesToAdd--;
}

// Round to nearest character
terminalWindow->Resize({terminalSize.x * characterSize.x, terminalSize.y * characterSize.y});

// Make sure we repaint the window
std::unique_lock lock(paintMutex);
shouldPaint = true;
}

Lemon::WindowServer::Instance()->Wait();
Expand Down
2 changes: 1 addition & 1 deletion Applications/TextEdit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void OnWindowCmd(unsigned short cmd, Lemon::GUI::Window* win){
}

int main(int argc, char** argv){
window = new Lemon::GUI::Window("Text Editor", {512, 256}, WINDOW_FLAGS_RESIZABLE, Lemon::GUI::WindowType::GUI);
window = new Lemon::GUI::Window("Text Editor", {640, 400}, WINDOW_FLAGS_RESIZABLE, Lemon::GUI::WindowType::GUI);
window->CreateMenuBar();

fileMenu.first = "File";
Expand Down

0 comments on commit 4a3012f

Please sign in to comment.