Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the ability to hide the program window in the tray. #99

Merged
merged 5 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/abaddon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ int Abaddon::StartGTK() {

// store must be checked before this can be called
m_main_window->UpdateComponents();
m_main_window->UpdateSettingsMenu(m_settings.GetSettings().HideToTray);

// crashes for some stupid reason if i put it somewhere else
SetupUserMenu();
Expand All @@ -232,6 +233,8 @@ int Abaddon::StartGTK() {
m_main_window->signal_action_add_recipient().connect(sigc::mem_fun(*this, &Abaddon::ActionAddRecipient));
m_main_window->signal_action_view_pins().connect(sigc::mem_fun(*this, &Abaddon::ActionViewPins));
m_main_window->signal_action_view_threads().connect(sigc::mem_fun(*this, &Abaddon::ActionViewThreads));
m_main_window->signal_action_hide_to_tray().connect(sigc::mem_fun(*this,&Abaddon::ActionHideToTray));


m_main_window->GetChannelList()->signal_action_channel_item_select().connect(sigc::bind(sigc::mem_fun(*this, &Abaddon::ActionChannelOpened), true));
m_main_window->GetChannelList()->signal_action_guild_leave().connect(sigc::mem_fun(*this, &Abaddon::ActionLeaveGuild));
Expand All @@ -246,11 +249,25 @@ int Abaddon::StartGTK() {
m_main_window->GetChatWindow()->signal_action_reaction_remove().connect(sigc::mem_fun(*this, &Abaddon::ActionReactionRemove));

ActionReloadCSS();
if(m_settings.GetSettings().HideToTray) {
m_tray = Gtk::StatusIcon::create("discord");
m_tray->signal_activate().connect(sigc::mem_fun(this, &Abaddon::on_tray_click));
m_tray->signal_popup_menu().connect(sigc::mem_fun(this, &Abaddon::on_tray_popup_menu));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
m_tray->signal_activate().connect(sigc::mem_fun(this, &Abaddon::on_tray_click));
m_tray->signal_popup_menu().connect(sigc::mem_fun(this, &Abaddon::on_tray_popup_menu));
m_tray->signal_activate().connect(sigc::mem_fun(*this, &Abaddon::on_tray_click));
m_tray->signal_popup_menu().connect(sigc::mem_fun(*this, &Abaddon::on_tray_popup_menu));

}
m_tray_menu = Gtk::make_managed<Gtk::Menu>();
m_tray_exit = Gtk::make_managed<Gtk::MenuItem>("exit",false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
m_tray_exit = Gtk::make_managed<Gtk::MenuItem>("exit",false);
m_tray_exit = Gtk::make_managed<Gtk::MenuItem>("Quit",false);


m_tray_exit->signal_activate().connect(sigc::mem_fun(this, &Abaddon::on_tray_menu_click));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
m_tray_exit->signal_activate().connect(sigc::mem_fun(this, &Abaddon::on_tray_menu_click));
m_tray_exit->signal_activate().connect(sigc::mem_fun(*this, &Abaddon::on_tray_menu_click));


m_tray_menu->append(*m_tray_exit);
m_tray_menu->show_all();

m_main_window->signal_hide().connect(sigc::mem_fun(this, &Abaddon::on_window_hide));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
m_main_window->signal_hide().connect(sigc::mem_fun(this, &Abaddon::on_window_hide));
m_main_window->signal_hide().connect(sigc::mem_fun(*this, &Abaddon::on_window_hide));

m_gtk_app->signal_shutdown().connect(sigc::mem_fun(*this, &Abaddon::OnShutdown), false);

m_main_window->UpdateMenus();

m_gtk_app->hold();
m_main_window->show();
return m_gtk_app->run(*m_main_window);
}
Expand Down Expand Up @@ -890,6 +907,32 @@ EmojiResource &Abaddon::GetEmojis() {
return m_emojis;
}

void Abaddon::on_tray_click() {
m_main_window->set_visible(!m_main_window->is_visible());
}
void Abaddon::on_tray_menu_click() {
m_gtk_app->quit();
}
void Abaddon::on_tray_popup_menu(int button, int activate_time) {
m_tray->popup_menu_at_position(*m_tray_menu,button, activate_time);
}
void Abaddon::ActionHideToTray(bool value) {
m_settings.GetSettings().HideToTray = value;

if(!value){
m_tray.reset();
}else{
m_tray = Gtk::StatusIcon::create("discord");
m_tray->signal_activate().connect(sigc::mem_fun(this,&Abaddon::on_tray_click));
m_tray->signal_popup_menu().connect(sigc::mem_fun(this,&Abaddon::on_tray_popup_menu));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
m_tray->signal_activate().connect(sigc::mem_fun(this,&Abaddon::on_tray_click));
m_tray->signal_popup_menu().connect(sigc::mem_fun(this,&Abaddon::on_tray_popup_menu));
m_tray->signal_activate().connect(sigc::mem_fun(*this,&Abaddon::on_tray_click));
m_tray->signal_popup_menu().connect(sigc::mem_fun(*this,&Abaddon::on_tray_popup_menu));

}
}
void Abaddon::on_window_hide() {
if(!m_settings.GetSettings().HideToTray){
m_gtk_app->quit();
}
}

int main(int argc, char **argv) {
if (std::getenv("ABADDON_NO_FC") == nullptr)
Platform::SetupFonts();
Expand Down
8 changes: 8 additions & 0 deletions src/abaddon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Abaddon {
void ActionAddRecipient(Snowflake channel_id);
void ActionViewPins(Snowflake channel_id);
void ActionViewThreads(Snowflake channel_id);
void ActionHideToTray(bool value);

bool ShowConfirm(const Glib::ustring &prompt, Gtk::Window *window = nullptr);

Expand Down Expand Up @@ -113,13 +114,19 @@ class Abaddon {
Gtk::MenuItem *m_user_menu_roles;
Gtk::MenuItem *m_user_menu_remove_recipient;
Gtk::Menu *m_user_menu_roles_submenu;
Gtk::Menu* m_tray_menu;
Gtk::MenuItem* m_tray_exit;

void on_user_menu_insert_mention();
void on_user_menu_ban();
void on_user_menu_kick();
void on_user_menu_copy_id();
void on_user_menu_open_dm();
void on_user_menu_remove_recipient();
void on_tray_click();
void on_tray_popup_menu(int button, int activate_time);
void on_tray_menu_click();
void on_window_hide();

private:
SettingsManager m_settings;
Expand All @@ -138,5 +145,6 @@ class Abaddon {
Glib::RefPtr<Gtk::Application> m_gtk_app;
Glib::RefPtr<Gtk::CssProvider> m_css_provider;
Glib::RefPtr<Gtk::CssProvider> m_css_low_provider; // registered with a lower priority to allow better customization
Glib::RefPtr<Gtk::StatusIcon> m_tray;
std::unique_ptr<MainWindow> m_main_window; // wah wah cant create a gtkstylecontext fuck you
};
2 changes: 2 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void SettingsManager::ReadSettings() {
SMBOOL("gui", "save_state", SaveState);
SMBOOL("gui", "stock_emojis", ShowStockEmojis);
SMBOOL("gui", "unreads", Unreads);
SMBOOL("gui", "HideToTray", HideToTray);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SMBOOL("gui", "HideToTray", HideToTray);
SMBOOL("gui", "hide_to_tray", HideToTray);

for consistency with the other settings names

SMINT("http", "concurrent", CacheHTTPConcurrency);
SMSTR("http", "user_agent", UserAgent);
SMSTR("style", "expandercolor", ChannelsExpanderColor);
Expand Down Expand Up @@ -101,6 +102,7 @@ void SettingsManager::Close() {
SMBOOL("gui", "save_state", SaveState);
SMBOOL("gui", "stock_emojis", ShowStockEmojis);
SMBOOL("gui", "unreads", Unreads);
SMBOOL("gui", "HideToTray", HideToTray);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SMBOOL("gui", "HideToTray", HideToTray);
SMBOOL("gui", "hide_to_tray", HideToTray);

SMINT("http", "concurrent", CacheHTTPConcurrency);
SMSTR("http", "user_agent", UserAgent);
SMSTR("style", "expandercolor", ChannelsExpanderColor);
Expand Down
2 changes: 2 additions & 0 deletions src/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class SettingsManager {
#endif
bool Unreads { true };

bool HideToTray { false };

// [http]
int CacheHTTPConcurrency { 20 };
std::string UserAgent { "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36" };
Expand Down
18 changes: 18 additions & 0 deletions src/windows/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,15 @@ void MainWindow::SetupMenu() {
m_menu_view_sub.append(m_menu_view_go_forward);
#endif

m_menu_settings.set_label("settings");
m_menu_settings.set_submenu(m_menu_settings_sub);
m_menu_settings_hide_to_tray.set_label("hide to tray");
m_menu_settings_sub.append(m_menu_settings_hide_to_tray);

m_menu_bar.append(m_menu_file);
m_menu_bar.append(m_menu_discord);
m_menu_bar.append(m_menu_view);
m_menu_bar.append(m_menu_settings);
m_menu_bar.show_all();

m_menu_discord_connect.signal_activate().connect([this] {
Expand Down Expand Up @@ -339,6 +345,10 @@ void MainWindow::SetupMenu() {
}
});

m_menu_settings_hide_to_tray.signal_activate().connect([this]{
m_signal_action_hide_to_tray.emit(m_menu_settings_hide_to_tray.get_active());
});

#ifdef WITH_LIBHANDY
m_menu_view_go_back.signal_activate().connect([this] {
GoBack();
Expand All @@ -348,6 +358,8 @@ void MainWindow::SetupMenu() {
GoForward();
});
#endif


}

MainWindow::type_signal_action_connect MainWindow::signal_action_connect() {
Expand Down Expand Up @@ -385,3 +397,9 @@ MainWindow::type_signal_action_view_pins MainWindow::signal_action_view_pins() {
MainWindow::type_signal_action_view_threads MainWindow::signal_action_view_threads() {
return m_signal_action_view_threads;
}
MainWindow::type_signal_action_hide_to_tray MainWindow::signal_action_hide_to_tray() {
return m_signal_action_hide_to_tray;
}
void MainWindow::UpdateSettingsMenu(bool hide_to_tray) {
m_menu_settings_hide_to_tray.set_active(hide_to_tray);
}
9 changes: 9 additions & 0 deletions src/windows/mainwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MainWindow : public Gtk::Window {
void UpdateChatReactionAdd(Snowflake id, const Glib::ustring &param);
void UpdateChatReactionRemove(Snowflake id, const Glib::ustring &param);
void UpdateMenus();
void UpdateSettingsMenu(bool hide_to_tray);

#ifdef WITH_LIBHANDY
void GoBack();
Expand Down Expand Up @@ -80,6 +81,11 @@ class MainWindow : public Gtk::Window {
Gtk::MenuItem m_menu_view_go_back;
Gtk::MenuItem m_menu_view_go_forward;
#endif
Gtk::MenuItem m_menu_settings;
Gtk::Menu m_menu_settings_sub;
Gtk::CheckMenuItem m_menu_settings_hide_to_tray;


void OnViewSubmenuPopup();

public:
Expand All @@ -89,6 +95,7 @@ class MainWindow : public Gtk::Window {
typedef sigc::signal<void> type_signal_action_reload_css;
typedef sigc::signal<void> type_signal_action_join_guild;
typedef sigc::signal<void> type_signal_action_set_status;
typedef sigc::signal<void, bool> type_signal_action_hide_to_tray;
// this should probably be removed
typedef sigc::signal<void, Snowflake> type_signal_action_add_recipient; // channel id
typedef sigc::signal<void, Snowflake> type_signal_action_view_pins; // channel id
Expand All @@ -103,6 +110,7 @@ class MainWindow : public Gtk::Window {
type_signal_action_add_recipient signal_action_add_recipient();
type_signal_action_view_pins signal_action_view_pins();
type_signal_action_view_threads signal_action_view_threads();
type_signal_action_hide_to_tray signal_action_hide_to_tray();

private:
type_signal_action_connect m_signal_action_connect;
Expand All @@ -114,4 +122,5 @@ class MainWindow : public Gtk::Window {
type_signal_action_add_recipient m_signal_action_add_recipient;
type_signal_action_view_pins m_signal_action_view_pins;
type_signal_action_view_threads m_signal_action_view_threads;
type_signal_action_hide_to_tray m_signal_action_hide_to_tray;
};