add back/forward history to tabs

also lots of reformatting in .cmake because clion is weird and did that for some reason
This commit is contained in:
ouwou
2022-04-27 16:23:50 -04:00
parent db28abaa44
commit e8f16292d1
11 changed files with 191 additions and 33 deletions

View File

@@ -57,6 +57,7 @@ void ChannelTabSwitcherHandy::AddChannelTab(Snowflake id) {
CheckUnread(id);
CheckPageIcon(page, *channel);
AppendPageHistory(page, id);
}
void ChannelTabSwitcherHandy::ReplaceActiveTab(Snowflake id) {
@@ -78,6 +79,7 @@ void ChannelTabSwitcherHandy::ReplaceActiveTab(Snowflake id) {
CheckUnread(id);
CheckPageIcon(page, *channel);
AppendPageHistory(page, id);
}
}
@@ -103,6 +105,14 @@ void ChannelTabSwitcherHandy::UseTabsState(const TabsState &state) {
}
}
void ChannelTabSwitcherHandy::GoBackOnCurrent() {
AdvanceOnCurrent(-1);
}
void ChannelTabSwitcherHandy::GoForwardOnCurrent() {
AdvanceOnCurrent(1);
}
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);
@@ -143,6 +153,41 @@ void ChannelTabSwitcherHandy::CheckPageIcon(HdyTabPage *page, const ChannelData
hdy_tab_page_set_icon(page, nullptr);
}
void ChannelTabSwitcherHandy::AppendPageHistory(HdyTabPage *page, Snowflake channel) {
auto it = m_page_history.find(page);
if (it == m_page_history.end()) {
m_page_history[page] = PageHistory { { channel }, 0 };
return;
}
// drop everything beyond current position
it->second.Visited.resize(++it->second.CurrentVisitedIndex);
it->second.Visited.push_back(channel);
}
void ChannelTabSwitcherHandy::AdvanceOnCurrent(size_t by) {
auto *current = hdy_tab_view_get_selected_page(m_tab_view);
if (current == nullptr) return;
auto history = m_page_history.find(current);
if (history == m_page_history.end()) return;
if (by + history->second.CurrentVisitedIndex < 0 || by + history->second.CurrentVisitedIndex >= history->second.Visited.size()) return;
history->second.CurrentVisitedIndex += by;
const auto to_id = history->second.Visited.at(history->second.CurrentVisitedIndex);
// temporarily point current index to the end so that it doesnt fuck up the history
// remove it immediately after cuz the emit will call ReplaceActiveTab
const auto real = history->second.CurrentVisitedIndex;
history->second.CurrentVisitedIndex = history->second.Visited.size() - 1;
m_signal_channel_switched_to.emit(to_id);
// iterator might not be valid
history = m_page_history.find(current);
if (history != m_page_history.end()) {
history->second.Visited.pop_back();
}
history->second.CurrentVisitedIndex = real;
}
ChannelTabSwitcherHandy::type_signal_channel_switched_to ChannelTabSwitcherHandy::signal_channel_switched_to() {
return m_signal_channel_switched_to;
}

View File

@@ -22,11 +22,16 @@ public:
TabsState GetTabsState();
void UseTabsState(const TabsState &state);
void GoBackOnCurrent();
void GoForwardOnCurrent();
private:
void CheckUnread(Snowflake id);
void ClearPage(HdyTabPage *page);
void OnPageIconLoad(HdyTabPage *page, const Glib::RefPtr<Gdk::Pixbuf> &pb);
void CheckPageIcon(HdyTabPage *page, const ChannelData &data);
void AppendPageHistory(HdyTabPage *page, Snowflake channel);
void AdvanceOnCurrent(size_t by);
HdyTabBar *m_tab_bar;
Gtk::Widget *m_tab_bar_wrapped;
@@ -38,6 +43,13 @@ private:
// need to hold a reference to the pixbuf data
std::unordered_map<HdyTabPage *, Glib::RefPtr<Gdk::Pixbuf>> m_page_icons;
struct PageHistory {
std::vector<Snowflake> Visited;
size_t CurrentVisitedIndex;
};
std::unordered_map<HdyTabPage *, PageHistory> m_page_history;
friend void selected_page_notify_cb(HdyTabView *, GParamSpec *, ChannelTabSwitcherHandy *);
friend gboolean close_page_cb(HdyTabView *, HdyTabPage *, ChannelTabSwitcherHandy *);

View File

@@ -180,6 +180,14 @@ TabsState ChatWindow::GetTabsState() {
void ChatWindow::UseTabsState(const TabsState &state) {
m_tab_switcher->UseTabsState(state);
}
void ChatWindow::GoBack() {
m_tab_switcher->GoBackOnCurrent();
}
void ChatWindow::GoForward() {
m_tab_switcher->GoForwardOnCurrent();
}
#endif
Snowflake ChatWindow::GetActiveChannel() const {

View File

@@ -39,6 +39,8 @@ public:
void OpenNewTab(Snowflake id);
TabsState GetTabsState();
void UseTabsState(const TabsState &state);
void GoBack();
void GoForward();
#endif
protected: