~kiwec/ke

81897912e9e202ab39071d34016ddd92743c7de7 — Wolf Clément 3 years ago cabcee5
Fix scrolling on file opening
4 files changed, 29 insertions(+), 13 deletions(-)

M src/Buffer.cpp
M src/Buffer.hpp
M src/Document.hpp
M src/File.cpp
M src/Buffer.cpp => src/Buffer.cpp +10 -10
@@ 57,19 57,25 @@ void Buffer::draw(Canvas canvas) {
}

bool Buffer::move_down() {
	auto old_x = current_line_nb;
	if(!Document::move_down()) return false;
	scroll(old_x);
	Editable::replace(&current_line->text);
	return true;
}

bool Buffer::move_to(uint line_number) {
	auto old_x = current_line_nb;
	if(!Document::move_to(line_number)) return false;
	scroll(old_x);
	Editable::replace(&current_line->text);
	return true;
}

bool Buffer::move_up() {
	auto old_x = current_line_nb;
	if(!Document::move_up()) return false;
	scroll(old_x);
	Editable::replace(&current_line->text);
	return true;
}


@@ 230,8 236,6 @@ bool Buffer::handle_key(tb_event *ev) {
}

void Buffer::insert(wchar_t c, bool log_events) {
	force = L"";

	if(c == L'\n') {
		bool at_end = is_at_EOL();
		auto x = get_x();


@@ 269,10 273,6 @@ void Buffer::insert(wchar_t c, bool log_events) {
			auto normal_indent = current_line->prev()->get_next_indentation();
			auto current_indent = current_line->get_current_indentation();

			std::wostringstream tmp;
			tmp << "current " << current_indent << " - normal " << normal_indent << " - x " << x << " - c " << c;
			force = tmp.str();

			// Deindent
			if(c == L'}' && x > 0 && current_indent == normal_indent) {
				if(current_line->text[x - 1] == L'\t') {


@@ 338,16 338,16 @@ void Buffer::remove_line(Line* l) {
	}
}

void Buffer::scroll(uint old_x) {
	if(old_x == current_line_nb) return;
void Buffer::scroll(uint old_current_line_nb) {
	if(old_current_line_nb == current_line_nb) return;

	const uint padding_min = 0.3 * m_canvas.height();
	const uint padding_max = 0.7 * m_canvas.height();

	if(old_x > current_line_nb) {
	if(old_current_line_nb > current_line_nb) {
		// Scroll up
		if(current_line_nb < padding_min)
			m_first_visible_line_nb = 0;
			m_first_visible_line_nb = 1;
		else if(m_first_visible_line_nb + padding_min > current_line_nb)
			m_first_visible_line_nb = current_line_nb - padding_min;
	} else {

M src/Buffer.hpp => src/Buffer.hpp +16 -2
@@ 13,20 13,34 @@ class Action;

class Buffer : public File, public Editable {
private:
	/// First visible line number, starts at 1 (just like the gutter)
	/// If the user scrolled down, this number will be higher
	uint m_first_visible_line_nb;

	/// Dimensions of the buffer on the screen
	Canvas m_canvas;

	/// Event history - contains undo/redo events
	History m_history;

public:
	Buffer(std::wstring filename) : File(filename), Editable(&current_line->text)
	{
		// Assume initial buffer height for scrolling
		m_canvas = Canvas{ 0, 0, tb_width(), tb_height() };

		// Scroll to the end of the file
		m_first_visible_line_nb = 1;
		Syntax::update(this);
		scroll(1);

		// Go to end of the line
		goto_end();
	}

	// String used for debugging - prints in status bar
	std::wstring force = L"";

	/// Draw the buffer on the screen
	void draw(Canvas canvas);

	/// Handle keypresses


@@ 68,7 82,7 @@ public:
	void screen_to(uint x, uint y);
	
	/// Update m_first_visible_line_nb
	void scroll(uint old_x);
	void scroll(uint old_current_line_nb);

	/// Set [modified] and update syntax highlighting
	void update();

M src/Document.hpp => src/Document.hpp +1 -1
@@ 9,7 9,7 @@ protected:
	Line* current_line;
	Line* last_line;

	/// Currently selected line number
	/// Currently selected line number - starts at 1
	uint current_line_nb;

	/// Number of lines in the document

M src/File.cpp => src/File.cpp +2 -0
@@ 4,6 4,7 @@
#include "Document.hpp"
#include "File.hpp"
#include "Line.hpp"
#include "Syntax.hpp"

void File::open() {
	// Safety checks


@@ 20,6 21,7 @@ void File::open() {
	for(current_line = first_line; std::getline(file, str); nb_lines++) {
		std::wstring wstr = converter.from_bytes(str);
		current_line->insert(wstr, 0);
		Syntax::update(current_line);
		current_line = current_line->make_next();
	}