diff --git a/src/abaddon.cpp b/src/abaddon.cpp index 3909590..1433d32 100644 --- a/src/abaddon.cpp +++ b/src/abaddon.cpp @@ -424,6 +424,9 @@ void Abaddon::SaveState() { AbaddonApplicationState state; state.ActiveChannel = m_main_window->GetChatActiveChannel(); state.Expansion = m_main_window->GetChannelList()->GetExpansionState(); +#ifdef WITH_LIBHANDY + state.Tabs = m_main_window->GetChatWindow()->GetTabsState(); +#endif const auto path = GetStateCachePath(); if (!util::IsFolder(path)) { @@ -450,6 +453,9 @@ void Abaddon::LoadState() { try { AbaddonApplicationState state = nlohmann::json::parse(data.begin(), data.end()); m_main_window->GetChannelList()->UseExpansionState(state.Expansion); +#ifdef WITH_LIBHANDY + m_main_window->GetChatWindow()->UseTabsState(state.Tabs); +#endif ActionChannelOpened(state.ActiveChannel); } catch (const std::exception &e) { printf("failed to load application state: %s\n", e.what()); diff --git a/src/components/channeltabswitcherhandy.cpp b/src/components/channeltabswitcherhandy.cpp index a9d2aef..bb28272 100644 --- a/src/components/channeltabswitcherhandy.cpp +++ b/src/components/channeltabswitcherhandy.cpp @@ -81,6 +81,28 @@ void ChannelTabSwitcherHandy::ReplaceActiveTab(Snowflake id) { } } +TabsState ChannelTabSwitcherHandy::GetTabsState() { + TabsState state; + + const gint num_pages = hdy_tab_view_get_n_pages(m_tab_view); + for (gint i = 0; i < num_pages; i++) { + auto *page = hdy_tab_view_get_nth_page(m_tab_view, i); + if (page != nullptr) { + if (const auto it = m_pages_rev.find(page); it != m_pages_rev.end()) { + state.Channels.push_back(it->second); + } + } + } + + return state; +} + +void ChannelTabSwitcherHandy::UseTabsState(const TabsState &state) { + for (auto id : state.Channels) { + AddChannelTab(id); + } +} + void ChannelTabSwitcherHandy::CheckUnread(Snowflake id) { if (auto it = m_pages.find(id); it != m_pages.end()) { hdy_tab_page_set_needs_attention(it->second, Abaddon::Get().GetDiscordClient().GetUnreadStateForChannel(id) > -1); diff --git a/src/components/channeltabswitcherhandy.hpp b/src/components/channeltabswitcherhandy.hpp index 6a2dbff..41c9c92 100644 --- a/src/components/channeltabswitcherhandy.hpp +++ b/src/components/channeltabswitcherhandy.hpp @@ -5,6 +5,7 @@ #include #include #include "discord/snowflake.hpp" + #include "state.hpp" class ChannelData; @@ -18,6 +19,8 @@ public: void AddChannelTab(Snowflake id); // switches to existing tab if it exists void ReplaceActiveTab(Snowflake id); + TabsState GetTabsState(); + void UseTabsState(const TabsState &state); private: void CheckUnread(Snowflake id); diff --git a/src/components/chatwindow.cpp b/src/components/chatwindow.cpp index 99ec8a0..f51b58f 100644 --- a/src/components/chatwindow.cpp +++ b/src/components/chatwindow.cpp @@ -172,6 +172,14 @@ void ChatWindow::SetTopic(const std::string &text) { void ChatWindow::OpenNewTab(Snowflake id) { m_tab_switcher->AddChannelTab(id); } + +TabsState ChatWindow::GetTabsState() { + return m_tab_switcher->GetTabsState(); +} + +void ChatWindow::UseTabsState(const TabsState &state) { + m_tab_switcher->UseTabsState(state); +} #endif Snowflake ChatWindow::GetActiveChannel() const { diff --git a/src/components/chatwindow.hpp b/src/components/chatwindow.hpp index d77afec..d09a87b 100644 --- a/src/components/chatwindow.hpp +++ b/src/components/chatwindow.hpp @@ -4,6 +4,7 @@ #include #include "discord/discord.hpp" #include "completer.hpp" +#include "state.hpp" #ifdef WITH_LIBHANDY class ChannelTabSwitcherHandy; @@ -36,6 +37,8 @@ public: #ifdef WITH_LIBHANDY void OpenNewTab(Snowflake id); + TabsState GetTabsState(); + void UseTabsState(const TabsState &state); #endif protected: diff --git a/src/state.cpp b/src/state.cpp index 043d181..bf4ab0f 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -24,9 +24,18 @@ void from_json(const nlohmann::json &j, ExpansionState &m) { j.at("c").get_to(m.Children); } +void to_json(nlohmann::json &j, const TabsState &m) { + j = m.Channels; +} + +void from_json(const nlohmann::json &j, TabsState &m) { + j.get_to(m.Channels); +} + void to_json(nlohmann::json &j, const AbaddonApplicationState &m) { j["active_channel"] = m.ActiveChannel; j["expansion"] = m.Expansion; + j["tabs"] = m.Tabs; } void from_json(const nlohmann::json &j, AbaddonApplicationState &m) { @@ -34,4 +43,6 @@ void from_json(const nlohmann::json &j, AbaddonApplicationState &m) { j.at("active_channel").get_to(m.ActiveChannel); if (j.contains("expansion")) j.at("expansion").get_to(m.Expansion); + if (j.contains("tabs")) + j.at("tabs").get_to(m.Tabs); } diff --git a/src/state.hpp b/src/state.hpp index 230808f..81c36d2 100644 --- a/src/state.hpp +++ b/src/state.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include #include "discord/snowflake.hpp" @@ -18,9 +19,17 @@ struct ExpansionState { friend void from_json(const nlohmann::json &j, ExpansionState &m); }; +struct TabsState { + std::vector Channels; + + friend void to_json(nlohmann::json &j, const TabsState &m); + friend void from_json(const nlohmann::json &j, TabsState &m); +}; + struct AbaddonApplicationState { Snowflake ActiveChannel; ExpansionStateRoot Expansion; + TabsState Tabs; friend void to_json(nlohmann::json &j, const AbaddonApplicationState &m); friend void from_json(const nlohmann::json &j, AbaddonApplicationState &m);