Skip to content

Commit

Permalink
title now shows session name + generalize session switching
Browse files Browse the repository at this point in the history
  • Loading branch information
williamhCode committed Dec 19, 2024
1 parent 3670b05 commit ef02344
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 47 deletions.
1 change: 1 addition & 0 deletions CmakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ set(APP_SRC
app/window_funcs.mm
app/options.cpp
app/path.cpp
app/task_helper.cpp

editor/state.cpp
editor/cursor.cpp
Expand Down
4 changes: 2 additions & 2 deletions res/lua/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ end, { nargs = "*" })

--- Sends a session command to neogurt
--- @param cmd string: command to send.
--- @param opts table: command options
--- @param opts table|nil: command options
--- @return any: result of the command
vim.g.neogurt_cmd = function(cmd, opts)
local chan_id = utils.get_neogurt_channel()
Expand Down Expand Up @@ -109,7 +109,7 @@ vim.g.neogurt_cmd = function(cmd, opts)
end

elseif cmd == "session_select" then
local curr_id = vim.g.neogurt_cmd("session_list", { sort = "time" })[1].id
local curr_id = vim.g.neogurt_cmd("session_info").id
local list = vim.g.neogurt_cmd("session_list", opts)
vim.ui.select(list, {
prompt = "Select a session",
Expand Down
23 changes: 23 additions & 0 deletions src/app/task_helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "./task_helper.hpp"
#include "utils/logger.hpp"
#include "utils/tsqueue.hpp"
#include <cassert>

static TSQueue<std::function<void()>> taskQueue;

void DeferToMainThread(std::function<void()>&& task) {
taskQueue.Push(std::move(task));

SDL_Event event;
SDL_zero(event);
event.type = EVENT_DEFERRED_TASK;
SDL_PushEvent(&event);
}

void ProcessNextMainThreadTask() {
assert(!taskQueue.Empty());

auto& task = taskQueue.Front();
task();
taskQueue.Pop();
}
9 changes: 9 additions & 0 deletions src/app/task_helper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "SDL3/SDL_events.h"
#include <functional>

inline const Uint32 EVENT_DEFERRED_TASK = SDL_RegisterEvents(1);

void DeferToMainThread(std::function<void()>&& task);
void ProcessNextMainThreadTask();
5 changes: 5 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "app/sdl_window.hpp"
#include "app/sdl_event.hpp"
#include "app/options.hpp"
#include "app/task_helper.hpp"
#include "editor/grid.hpp"
#include "editor/highlight.hpp"
#include "editor/state.hpp"
Expand Down Expand Up @@ -397,6 +398,10 @@ int main(int argc, char** argv) {
sdlEvents.Push(event);
break;
}

if (event.type == EVENT_DEFERRED_TASK) {
ProcessNextMainThreadTask();
}
}

} catch (const std::exception& e) {
Expand Down
75 changes: 36 additions & 39 deletions src/session/manager.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "./manager.hpp"
#include "app/task_helper.hpp"
#include "app/window_funcs.h"
#include "utils/color.hpp"
#include "utils/logger.hpp"
#include <algorithm>
#include <format>
#include <stdexcept>
#include <ranges>
#include <utility>
Expand All @@ -25,7 +27,7 @@ int SessionManager::SessionNew(const SessionNewOpts& opts) {
bool first = sessions.empty();

int id = currId++;
auto name = opts.name.empty() ? std::to_string(id) : opts.name;
auto name = opts.name.empty() ? std::format("session {}", id) : opts.name;

auto [it, success] = sessions.try_emplace(id, id, name, opts.dir);
if (!success) {
Expand Down Expand Up @@ -100,35 +102,26 @@ int SessionManager::SessionNew(const SessionNewOpts& opts) {
})
.value();

sizes.UpdateSizes(
window.size, window.dpiScale, editorState.fontFamily.DefaultFont().charSize,
options
);

if (first) {
renderer = Renderer(sizes);
} else {
renderer.Resize(sizes);
}

editorState.winManager.sizes = sizes;
editorState.winManager.gridManager = &editorState.gridManager;
editorState.cursor.Resize(sizes.charSize, sizes.dpiScale);

if (options.opacity < 1) {
auto& hl = editorState.hlTable[0];
hl.background = IntToColor(options.bgColor);
hl.background->a = options.opacity;
}

nvim.UiTryResize(sizes.uiWidth, sizes.uiHeight);

inputHandler = InputHandler(&nvim, &editorState.winManager, options);
if (first) {
sizes.UpdateSizes(
window.size, window.dpiScale, session.editorState.fontFamily.DefaultFont().charSize,
session.options
);
renderer = Renderer(sizes);
}

sessionsOrder.push_front(&session);
sessionsOrder.push_back(&session);

if (!opts.switchTo) {
SessionPrev();
if (opts.switchTo) {
SessionSwitch(session.id);
}

return id;
Expand Down Expand Up @@ -171,17 +164,13 @@ bool SessionManager::SessionSwitch(int id) {
}
auto& session = it->second;

UpdateSessionSizes(session);
inputHandler =
InputHandler(&session.nvim, &session.editorState.winManager, session.options);
session.reattached = true;

// move order to front
auto currIt = std::ranges::find(sessionsOrder, &session);
// move to front
if (currIt != sessionsOrder.end()) {
sessionsOrder.erase(currIt);
sessionsOrder.push_front(&session);
}
assert(currIt != sessionsOrder.end());
sessionsOrder.erase(currIt);
sessionsOrder.push_front(&session);

SessionSwitchInternal(session);

return true;
}
Expand Down Expand Up @@ -246,19 +235,14 @@ bool SessionManager::ShouldQuit() {
});

// check if no sessions left
auto* curr = CurrSession();
if (curr == nullptr) {
auto* currSession = CurrSession();
if (currSession == nullptr) {
return true;
}

// switch to most recent session if current session was removed
if (prevId != curr->id) {
auto& session = *curr;

UpdateSessionSizes(session);
inputHandler =
InputHandler(&session.nvim, &session.editorState.winManager, session.options);
session.reattached = true;
if (prevId != currSession->id) {
SessionSwitchInternal(*currSession);
}

return false;
Expand Down Expand Up @@ -300,6 +284,19 @@ void SessionManager::FontSizeReset(bool all) {
}
}

void SessionManager::SessionSwitchInternal(SessionState& session) {
UpdateSessionSizes(session);
inputHandler =
InputHandler(&session.nvim, &session.editorState.winManager, session.options);

DeferToMainThread([winPtr = window.Get(),
title = std::format("Neogurt - {}", session.name)] {
SDL_SetWindowTitle(winPtr, title.c_str());
});

session.reattached = true;
}

void SessionManager::UpdateSessionSizes(SessionState& session) {
session.editorState.fontFamily.TryChangeDpiScale(window.dpiScale);
sizes.UpdateSizes(
Expand Down
2 changes: 2 additions & 0 deletions src/session/manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ struct SessionManager {
void FontSizeReset(bool all = false);

private:
// all session switching leads to this function
void SessionSwitchInternal(SessionState& session);
void UpdateSessionSizes(SessionState& session);

// void LoadSessions(std::string_view filename);
Expand Down
12 changes: 6 additions & 6 deletions src/utils/tsqueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ struct TSQueue {
return queue.size();
}

bool Wait() {
std::unique_lock lock(mutex);
cv.wait(lock, [this]() { return !queue.empty() || exit; });
return !exit;
}

void Exit() {
{
std::scoped_lock lock(mutex);
exit = true;
}
cv.notify_all();
}

bool Wait() {
std::unique_lock lock(mutex);
cv.wait(lock, [this]() { return !queue.empty() || exit; });
return !exit;
}
};

0 comments on commit ef02344

Please sign in to comment.