Skip to content

Commit

Permalink
refactor window resizing logic + fix resize bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
williamhCode committed Dec 19, 2024
1 parent b170d67 commit 3670b05
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 109 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,38 +101,31 @@ local cmds_table = {
dir = "~/",
switch_to = true, -- switch to session after creating it
},

-- returns success (bool)
session_kill = {
id = 0
},

-- returns new session id
session_restart = {
id = 0,
curr_dir = false, -- use cwd instead of session dir
},

-- returns success (bool)
session_switch = {
id = "number",
},

-- switch to previous session
-- returns success (bool)
session_prev = {},

-- returns session info table (id, name, dir)
session_info = {
id = 0
},

-- returns list of session info tables
session_list = {
sort = "id", -- id, name, time (recency)
reverse = false,
},

-- spawns a session picker using vim.ui.select()
-- wrapper around session_list
session_select = {
Expand All @@ -144,7 +137,6 @@ local cmds_table = {
[1] = "number",
all = false, -- change in all sessions
},

font_size_reset = {
all = false, -- change in all sessions
},
Expand Down
26 changes: 18 additions & 8 deletions res/lua/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,50 @@ local utils = require("utils")

-- if value is a type name, the option is required
local cmds_table = {
-- returns session id
session_new = {
name = "",
dir = "~/",
switch_to = true,
switch_to = true, -- switch to session after creating it
},
-- returns success (bool)
session_kill = {
id = 0
},
-- returns new session id
session_restart = {
id = 0,
curr_dir = false,
curr_dir = false, -- use cwd instead of session dir
},
-- returns success (bool)
session_switch = {
id = "number",
},
-- switch to previous session
-- returns success (bool)
session_prev = {},
-- returns session info table (id, name, dir)
session_info = {
id = 0
},
-- returns list of session info tables
session_list = {
-- id, name, time (recency)
sort = "id",
sort = "id", -- id, name, time (recency)
reverse = false,
},
-- spawns a session picker using vim.ui.select()
-- wrapper around session_list
session_select = {
-- forwarded to list opts
sort = "id",
reverse = false,
},

font_size_change = {
[1] = "number",
all = false,
all = false, -- change in all sessions
},
font_size_reset = {
all = false,
all = false, -- change in all sessions
},
}

Expand All @@ -65,7 +73,9 @@ vim.api.nvim_create_user_command("Neogurt", function(cmd_opts)

-- print command output
local result = vim.g.neogurt_cmd(cmd, opts)
vim.print(result)
if result ~= vim.NIL then
vim.print(result)
end

end, { nargs = "*" })

Expand Down
5 changes: 1 addition & 4 deletions src/editor/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,7 @@ void ParseEditorState(UiEvents& uiEvents, EditorState& editorState) {

// apply margins and msg_set_pos after all events
for (auto* e : margins) {
if (!editorState.winManager.ViewportMargins(*e)) {
// defer event if failed
// uiEvents.queue[0].emplace_front(*e);
}
editorState.winManager.ViewportMargins(*e);
}
for (auto* e : msgSetPos) {
editorState.winManager.MsgSetPos(*e);
Expand Down
134 changes: 59 additions & 75 deletions src/editor/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,73 @@ int WinManager::CalcMaxTexPerPage(const Win& win) {
return 3;
}

void WinManager::InitRenderData(Win& win) {
void WinManager::UpdateWinAttributes(Win& win) {
auto pos = glm::vec2(win.startCol, win.startRow) * sizes.charSize;
auto size = glm::vec2(win.width, win.height) * sizes.charSize;

auto numQuads = win.height * std::min(win.width, 80);
win.rectData.CreateBuffers(numQuads);
win.textData.CreateBuffers(numQuads);
win.shapeData.CreateBuffers(numQuads);
bool posChanged = win.pos != pos;
bool sizeChanged =
(win.size != size) || (win.sRenderTexture.charSize != sizes.charSize);
bool dpiChanged = win.sRenderTexture.dpiScale != sizes.dpiScale;

win.sRenderTexture =
ScrollableRenderTexture(size, sizes.dpiScale, sizes.charSize, CalcMaxTexPerPage(win));
win.sRenderTexture.UpdatePos(pos);

win.grid.dirty = true;
win.posDirty |= posChanged;
win.sizeDirty |= sizeChanged || dpiChanged;

win.pos = pos;
win.size = size;
}

void WinManager::UpdateRenderData(Win& win) {
auto pos = glm::vec2(win.startCol, win.startRow) * sizes.charSize;
auto size = glm::vec2(win.width, win.height) * sizes.charSize;

bool posChanged = pos != win.pos;
// sometimes size can be equal, but charSize different
// for hidden window, win.sRenderTexture.charSize will always be vec2(0, 0)
bool sizeChanged = size != win.size || sizes.charSize != win.sRenderTexture.charSize;
if (!posChanged && !sizeChanged) {
return;
}

if (sizeChanged) {
auto numQuads = win.height * std::min(win.width, 80);
win.rectData.CreateBuffers(numQuads);
win.textData.CreateBuffers(numQuads);
win.shapeData.CreateBuffers(numQuads);

win.sRenderTexture =
ScrollableRenderTexture(size, sizes.dpiScale, sizes.charSize, CalcMaxTexPerPage(win));
win.sRenderTexture.UpdateMargins(win.margins);
void WinManager::UpdateSizes(const SizeHandler& _sizes) {
std::lock_guard lock(windowsMutex);
sizes = _sizes;
for (auto& [id, win] : windows) {
UpdateWinAttributes(win);
}
win.sRenderTexture.UpdatePos(pos);

win.grid.dirty = true;

win.pos = pos;
win.size = size;
}

void WinManager::TryChangeDpiScale(float dpiScale) {
void WinManager::UpdateRenderData() {
std::lock_guard lock(windowsMutex);
for (auto& [id, win] : windows) {
if (dpiScale != win.sRenderTexture.dpiScale) {
if (win.hidden) continue;

if (win.sizeDirty) {
auto numQuads = win.height * std::min(win.width, 80);
win.rectData.CreateBuffers(numQuads);
win.textData.CreateBuffers(numQuads);
win.shapeData.CreateBuffers(numQuads);

win.sRenderTexture = ScrollableRenderTexture(
win.size, sizes.dpiScale, sizes.charSize, CalcMaxTexPerPage(win)
);
win.grid.dirty = true;
}

if (win.sizeDirty || win.posDirty) {
win.sRenderTexture.UpdatePos(win.pos);
}

if (win.sizeDirty || win.updateMargins) {
win.sRenderTexture.UpdateMargins(win.margins);
win.grid.dirty = true;
}

if (win.scrollDist) {
win.sRenderTexture.UpdateViewport(*win.scrollDist);
}

// reset dirty flags
win.sizeDirty = false;
win.posDirty = false;
win.updateMargins = false;
win.scrollDist.reset();
}
}

void WinManager::UpdateScrolling(float dt) {
std::lock_guard lock(windowsMutex);
for (auto& [id, win] : windows) {
if (win.sRenderTexture.scrolling) {
win.sRenderTexture.UpdateScrolling(dt);
dirty = true;
}
}
}
Expand All @@ -99,11 +108,7 @@ void WinManager::Pos(const event::WinPos& e) {

win.hidden = false;

if (first) {
InitRenderData(win);
} else {
UpdateRenderData(win);
}
UpdateWinAttributes(win);
}

// TODO: find a more robust way to handle
Expand All @@ -129,7 +134,7 @@ void WinManager::FloatPos(int grid) {

win.hidden = false;

UpdateRenderData(win);
UpdateWinAttributes(win);
}

void WinManager::FloatPos(const event::WinFloatPos& e) {
Expand Down Expand Up @@ -186,11 +191,7 @@ void WinManager::FloatPos(const event::WinFloatPos& e) {
.zindex = e.zindex,
};

if (first) {
InitRenderData(win);
} else {
UpdateRenderData(win);
}
UpdateWinAttributes(win);
}

void WinManager::ExternalPos(const event::WinExternalPos& e) {
Expand Down Expand Up @@ -251,11 +252,7 @@ void WinManager::MsgSetPos(const event::MsgSetPos& e) {
}
msgWinId = e.grid;

if (first) {
InitRenderData(win);
} else {
UpdateRenderData(win);
}
UpdateWinAttributes(win);
}

void WinManager::Viewport(const event::WinViewport& e) {
Expand All @@ -267,33 +264,22 @@ void WinManager::Viewport(const event::WinViewport& e) {
}
auto& win = it->second;

bool shouldScroll = //
std::abs(e.scrollDelta) > 0 && //
bool shouldScroll =
std::abs(e.scrollDelta) > 0 &&
std::abs(e.scrollDelta) <= win.height - (win.margins.top + win.margins.bottom);

// LOG_INFO("WinManager::Viewport: grid {} scrollDelta {} shouldScroll {}", e.grid,

if (!shouldScroll) return;
float scrollDist = e.scrollDelta * sizes.charSize.y;
win.sRenderTexture.UpdateViewport(scrollDist);
win.scrollDist = e.scrollDelta * sizes.charSize.y;
}

void WinManager::UpdateScrolling(float dt) {
std::lock_guard lock(windowsMutex);
for (auto& [id, win] : windows) {
if (win.sRenderTexture.scrolling) {
win.sRenderTexture.UpdateScrolling(dt);
dirty = true;
}
}
}

bool WinManager::ViewportMargins(const event::WinViewportMargins& e) {
void WinManager::ViewportMargins(const event::WinViewportMargins& e) {
std::lock_guard lock(windowsMutex);
auto it = windows.find(e.grid);
if (it == windows.end()) {
// LOG_ERR("WinManager::ViewportMargins: window {} not found", e.grid);
return false;
return;
}
auto& win = it->second;

Expand All @@ -302,9 +288,7 @@ bool WinManager::ViewportMargins(const event::WinViewportMargins& e) {
win.margins.left = e.left;
win.margins.right = e.right;

win.sRenderTexture.UpdateMargins(win.margins);

return true;
win.updateMargins = true;
}

void WinManager::Extmark(const event::WinExtmark& e) {
Expand Down
23 changes: 15 additions & 8 deletions src/editor/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,23 @@ struct Win {
return floatData.has_value();
}

// rendering data
// win attributes
glm::vec2 pos;
glm::vec2 size;
glm::vec2 size{0, 0};

bool posDirty;
bool sizeDirty;

// rendering data
QuadRenderData<RectQuadVertex, true> rectData;
QuadRenderData<TextQuadVertex, true> textData;
QuadRenderData<ShapeQuadVertex, true> shapeData;

ScrollableRenderTexture sRenderTexture;

// other updates
std::optional<float> scrollDist; // update viewport
bool updateMargins;
};

// for input handler
Expand All @@ -74,12 +82,12 @@ struct WinManager {

private:
int CalcMaxTexPerPage(const Win& win);
void InitRenderData(Win& win);
void UpdateRenderData(Win& win);
void UpdateWinAttributes(Win& win);

public:
// updates dpi scale for all windows if changed
void TryChangeDpiScale(float dpiScale);
void UpdateSizes(const SizeHandler& sizes);
void UpdateRenderData(); // updates all windows rendering data
void UpdateScrolling(float dt);

void Pos(const event::WinPos& e);
void FloatPos(int grid);
Expand All @@ -89,8 +97,7 @@ struct WinManager {
void Close(const event::WinClose& e);
void MsgSetPos(const event::MsgSetPos& e);
void Viewport(const event::WinViewport& e);
void UpdateScrolling(float dt);
bool ViewportMargins(const event::WinViewportMargins& e);
void ViewportMargins(const event::WinViewportMargins& e);
void Extmark(const event::WinExtmark& e);

MouseInfo GetMouseInfo(glm::vec2 mousePos) const;
Expand Down
Loading

0 comments on commit 3670b05

Please sign in to comment.