1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-11-22 05:03:56 +03:00

Basic support for terminal ui scroll bar

This commit is contained in:
Maxime Coste 2022-05-25 21:03:31 +10:00
parent bd83aa9c14
commit b644c32ed3
8 changed files with 63 additions and 35 deletions

View File

@ -222,8 +222,13 @@ void Client::redraw_ifn()
const auto& faces = context().faces();
if (m_ui_pending & Draw)
m_ui->draw(window.update_display_buffer(context()),
{
auto& db = window.update_display_buffer(context());
m_ui->draw(db.lines(),
{db.range().begin.line, db.range().end.line},
context().buffer().line_count(),
faces["Default"], faces["BufferPadding"]);
}
const bool update_menu_anchor = (m_ui_pending & Draw) and not (m_ui_pending & MenuHide) and
not m_menu.items.empty() and m_menu.style == MenuStyle::Inline;

View File

@ -142,10 +142,11 @@ JsonUI::JsonUI()
set_signal_handler(SIGINT, SIG_DFL);
}
void JsonUI::draw(const DisplayBuffer& display_buffer,
void JsonUI::draw(ConstArrayView<DisplayLine> lines,
Range<LineCount> range, LineCount buffer_line_count,
const Face& default_face, const Face& padding_face)
{
rpc_call("draw", display_buffer.lines(), default_face, padding_face);
rpc_call("draw", lines, range.begin, range.end, buffer_line_count, default_face, padding_face);
}
void JsonUI::draw_status(const DisplayLine& status_line,

View File

@ -21,7 +21,9 @@ public:
bool is_ok() const override { return m_stdin_watcher.fd() != -1; }
void draw(const DisplayBuffer& display_buffer,
void draw(ConstArrayView<DisplayLine> lines,
Range<LineCount> range,
LineCount buffer_line_count,
const Face& default_face,
const Face& buffer_padding) override;

View File

@ -614,7 +614,7 @@ std::unique_ptr<UserInterface> make_ui(UIType ui_type)
void info_show(const DisplayLine&, const DisplayLineList&, DisplayCoord, Face, InfoStyle) override {}
void info_hide() override {}
void draw(const DisplayBuffer&, const Face&, const Face&) override {}
void draw(ConstArrayView<DisplayLine>, Range<LineCount>, LineCount, const Face&, const Face&) override {}
void draw_status(const DisplayLine&, const DisplayLine&, const Face&) override {}
DisplayCoord dimensions() override { return {24,80}; }
void set_cursor(CursorMode, DisplayCoord) override {}

View File

@ -146,11 +146,6 @@ private:
write_field(line.atoms());
}
void write_field(const DisplayBuffer& display_buffer)
{
write_field(display_buffer.lines());
}
private:
RemoteBuffer& m_buffer;
uint32_t m_start;
@ -364,16 +359,6 @@ struct MsgReader::Reader<DisplayLine> {
}
};
template<>
struct MsgReader::Reader<DisplayBuffer> {
static DisplayBuffer read(MsgReader& reader)
{
DisplayBuffer db;
db.lines() = Reader<Vector<DisplayLine>>::read(reader);
return db;
}
};
class RemoteUI : public UserInterface
{
@ -393,7 +378,9 @@ public:
InfoStyle style) override;
void info_hide() override;
void draw(const DisplayBuffer& display_buffer,
void draw(ConstArrayView<DisplayLine> lines,
Range<LineCount> range,
LineCount buffer_line_count,
const Face& default_face,
const Face& padding_face) override;
@ -545,11 +532,13 @@ void RemoteUI::info_hide()
send_message(MessageType::InfoHide);
}
void RemoteUI::draw(const DisplayBuffer& display_buffer,
void RemoteUI::draw(ConstArrayView<DisplayLine> lines,
Range<LineCount> range,
LineCount buffer_line_count,
const Face& default_face,
const Face& padding_face)
{
send_message(MessageType::Draw, display_buffer, default_face, padding_face);
send_message(MessageType::Draw, lines, range.begin, range.end, buffer_line_count, default_face, padding_face);
}
void RemoteUI::draw_status(const DisplayLine& status_line,
@ -706,10 +695,12 @@ RemoteClient::RemoteClient(StringView session, StringView name, std::unique_ptr<
break;
case MessageType::Draw:
{
auto display_buffer = reader.read<DisplayBuffer>();
auto lines = reader.read<Vector<DisplayLine>>();
Range<LineCount> range = {reader.read<LineCount>(), reader.read<LineCount>()};
LineCount buffer_line_count = reader.read<LineCount>();
auto default_face = reader.read<Face>();
auto padding_face = reader.read<Face>();
m_ui->draw(display_buffer, default_face, padding_face);
m_ui->draw(lines, range, buffer_line_count, default_face, padding_face);
break;
}
case MessageType::DrawStatus:

View File

@ -554,9 +554,17 @@ void TerminalUI::refresh(bool force)
m_dirty = false;
}
template<typename T>
T div_round_up(T a, T b)
{
return (a - T(1)) / b + T(1);
}
static const DisplayLine empty_line = { String(" "), {} };
void TerminalUI::draw(const DisplayBuffer& display_buffer,
void TerminalUI::draw(ConstArrayView<DisplayLine> lines,
Range<LineCount> range,
LineCount buffer_line_count,
const Face& default_face,
const Face& padding_face)
{
@ -565,7 +573,7 @@ void TerminalUI::draw(const DisplayBuffer& display_buffer,
const DisplayCoord dim = dimensions();
const LineCount line_offset = content_line_offset();
LineCount line_index = line_offset;
for (const DisplayLine& line : display_buffer.lines())
for (const DisplayLine& line : lines)
m_window.draw(line_index++, line.atoms(), default_face);
auto face = merge_faces(default_face, padding_face);
@ -575,6 +583,17 @@ void TerminalUI::draw(const DisplayBuffer& display_buffer,
while (line_index < dim.line + line_offset)
m_window.draw(line_index++, padding, face);
if (m_scroll_bar)
{
const auto mark_height = min(div_round_up(sq(m_dimensions.line), buffer_line_count), m_dimensions.line);
const auto mark_line = range.begin * (m_dimensions.line - mark_height) / max(1_line, buffer_line_count - (range.end - range.begin));
for (auto line = 0_line; line < m_dimensions.line; ++line) {
const bool is_mark = line >= mark_line and line < mark_line + mark_height;
m_window.draw({line + line_offset, m_window.size.column - 1}, DisplayAtom(is_mark ? "" : ""), default_face);
}
}
m_dirty = true;
}
@ -650,6 +669,8 @@ void TerminalUI::check_resize(bool force)
kak_assert(m_window);
m_dimensions = terminal_size - 1_line;
if (m_scroll_bar)
m_dimensions -= {0_line, 1_col};
// if (char* csr = tigetstr((char*)"csr"))
// putp(tparm(csr, 0, ws.ws_row));
@ -958,12 +979,6 @@ Optional<Key> TerminalUI::get_next_key()
return Key{Key::Escape};
}
template<typename T>
T div_round_up(T a, T b)
{
return (a - T(1)) / b + T(1);
}
void TerminalUI::draw_menu()
{
// menu show may have not created the window if it did not fit.
@ -1492,6 +1507,13 @@ void TerminalUI::set_ui_options(const Options& options)
m_padding_char = find("terminal_padding_char").map([](StringView s) { return s.column_length() < 1 ? ' ' : s[0_char]; }).value_or(Codepoint{'~'});
m_padding_fill = find("terminal_padding_fill").map(to_bool).value_or(false);
bool scroll_bar = find("terminal_scroll_bar").map(to_bool).value_or(false);
if (scroll_bar != m_scroll_bar)
{
m_scroll_bar = scroll_bar;
check_resize(true);
}
}
}

View File

@ -30,7 +30,9 @@ public:
bool is_ok() const override { return (bool)m_window; }
void draw(const DisplayBuffer& display_buffer,
void draw(ConstArrayView<DisplayLine> lines,
Range<LineCount> range,
LineCount buffer_line_count,
const Face& default_face,
const Face& padding_face) override;
@ -163,6 +165,7 @@ private:
Codepoint m_padding_char = '~';
bool m_padding_fill = false;
bool m_scroll_bar = false;
bool m_dirty = false;

View File

@ -16,6 +16,8 @@ using DisplayLineList = Vector<DisplayLine, MemoryDomain::Display>;
struct DisplayCoord;
struct Face;
struct Key;
struct LineCount;
template<typename T> struct Range;
enum class MenuStyle
{
@ -63,7 +65,9 @@ public:
InfoStyle style) = 0;
virtual void info_hide() = 0;
virtual void draw(const DisplayBuffer& display_buffer,
virtual void draw(ConstArrayView<DisplayLine> lines,
Range<LineCount> range,
LineCount buffer_line_count,
const Face& default_face,
const Face& padding_face) = 0;