mirror of
https://github.com/celisej567/abaddon.git
synced 2026-09-13 20:18:50 +03:00
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:
@@ -63,6 +63,39 @@ Abaddon &Abaddon::Get() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
#ifdef _WIN32
|
||||
constexpr static guint BUTTON_BACK = 4;
|
||||
constexpr static guint BUTTON_FORWARD = 5;
|
||||
#else
|
||||
constexpr static guint BUTTON_BACK = 8;
|
||||
constexpr static guint BUTTON_FORWARD = 9;
|
||||
#endif
|
||||
|
||||
static void HandleButtonEvents(GdkEvent *event, MainWindow *main_window) {
|
||||
if (event->type != GDK_BUTTON_PRESS) return;
|
||||
|
||||
auto *widget = gtk_get_event_widget(event);
|
||||
if (widget == nullptr) return;
|
||||
auto *window = gtk_widget_get_toplevel(widget);
|
||||
if (static_cast<void *>(window) != static_cast<void *>(main_window->gobj())) return; // is this the right way???
|
||||
|
||||
switch (event->button.button) {
|
||||
case BUTTON_BACK:
|
||||
main_window->GoBack();
|
||||
break;
|
||||
case BUTTON_FORWARD:
|
||||
main_window->GoForward();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void MainEventHandler(GdkEvent *event, void *main_window) {
|
||||
HandleButtonEvents(event, static_cast<MainWindow *>(main_window));
|
||||
gtk_main_do_event(event);
|
||||
}
|
||||
#endif
|
||||
|
||||
int Abaddon::StartGTK() {
|
||||
m_gtk_app = Gtk::Application::create("com.github.uowuo.abaddon");
|
||||
|
||||
@@ -113,6 +146,10 @@ int Abaddon::StartGTK() {
|
||||
m_main_window->set_title(APP_TITLE);
|
||||
m_main_window->set_position(Gtk::WIN_POS_CENTER);
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
gdk_event_handler_set(&MainEventHandler, m_main_window.get(), nullptr);
|
||||
#endif
|
||||
|
||||
if (!m_settings.IsValid()) {
|
||||
Gtk::MessageDialog dlg(*m_main_window, "The settings file could not be opened!", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
|
||||
dlg.set_position(Gtk::WIN_POS_CENTER);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 *);
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -39,6 +39,8 @@ public:
|
||||
void OpenNewTab(Snowflake id);
|
||||
TabsState GetTabsState();
|
||||
void UseTabsState(const TabsState &state);
|
||||
void GoBack();
|
||||
void GoForward();
|
||||
#endif
|
||||
|
||||
protected:
|
||||
|
||||
@@ -153,6 +153,16 @@ void MainWindow::UpdateMenus() {
|
||||
OnViewSubmenuPopup();
|
||||
}
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
void MainWindow::GoBack() {
|
||||
m_chat.GoBack();
|
||||
}
|
||||
|
||||
void MainWindow::GoForward() {
|
||||
m_chat.GoForward();
|
||||
}
|
||||
#endif
|
||||
|
||||
void MainWindow::OnDiscordSubmenuPopup() {
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
auto channel_id = GetChatActiveChannel();
|
||||
|
||||
@@ -25,6 +25,11 @@ public:
|
||||
void UpdateChatReactionRemove(Snowflake id, const Glib::ustring ¶m);
|
||||
void UpdateMenus();
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
void GoBack();
|
||||
void GoForward();
|
||||
#endif
|
||||
|
||||
ChannelList *GetChannelList();
|
||||
ChatWindow *GetChatWindow();
|
||||
MemberList *GetMemberList();
|
||||
|
||||
Reference in New Issue
Block a user