From 55ba9f23514407c389962df8e765b2d0daafc2da Mon Sep 17 00:00:00 2001 From: YuSanka Date: Mon, 11 Dec 2023 11:20:29 +0100 Subject: [PATCH] Topbar: Implement Auth menu --- resources/icons/login.svg | 18 +++++++++++ resources/icons/logout.svg | 16 ++++++++++ resources/icons/user.svg | 18 +++++++++++ src/slic3r/GUI/GUI_App.cpp | 24 +------------- src/slic3r/GUI/GUI_App.hpp | 3 -- src/slic3r/GUI/MainFrame.cpp | 4 +-- src/slic3r/GUI/Plater.cpp | 2 -- src/slic3r/GUI/TopBar.cpp | 62 +++++++++++++++++++++++++++++++++++- src/slic3r/GUI/TopBar.hpp | 16 ++++++++-- 9 files changed, 130 insertions(+), 33 deletions(-) create mode 100644 resources/icons/login.svg create mode 100644 resources/icons/logout.svg create mode 100644 resources/icons/user.svg diff --git a/resources/icons/login.svg b/resources/icons/login.svg new file mode 100644 index 0000000000..0b01b66613 --- /dev/null +++ b/resources/icons/login.svg @@ -0,0 +1,18 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/resources/icons/logout.svg b/resources/icons/logout.svg new file mode 100644 index 0000000000..b2aa004057 --- /dev/null +++ b/resources/icons/logout.svg @@ -0,0 +1,16 @@ + + + + + + + + + + \ No newline at end of file diff --git a/resources/icons/user.svg b/resources/icons/user.svg new file mode 100644 index 0000000000..b750021fc4 --- /dev/null +++ b/resources/icons/user.svg @@ -0,0 +1,18 @@ + + + + + Abstract user icon + + + + + \ No newline at end of file diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index 52952840ef..c0b7600483 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -2498,11 +2498,6 @@ wxMenu* GUI_App::get_config_menu() local_menu->Append(config_id_base + ConfigMenuUpdateConf, _L("Check for Configuration Updates"), _L("Check for configuration updates")); local_menu->Append(config_id_base + ConfigMenuUpdateApp, _L("Check for Application Updates"), _L("Check for new version of application")); local_menu->AppendSeparator(); - wxMenuItem* updatable_item = local_menu->Append(config_id_base + ConfigMenuAuthLogin, _L("PrusaAuth Log in"), _L("")); - m_config_menu_updatable_items.emplace(ConfigMenuIDs::ConfigMenuAuthLogin, updatable_item); - updatable_item = local_menu->Append(config_id_base + ConfigMenuConnectDummy, _L("PrusaConnect Printers"), _L("")); - updatable_item->Enable(false); - m_config_menu_updatable_items.emplace(ConfigMenuIDs::ConfigMenuConnectDummy, updatable_item); local_menu->Append(config_id_base + ConfigMenuConnectDialog, _L("Connect Dialog"), _L("Connect Dialog")); local_menu->Append(config_id_base + ConfigMenuMediaDialog, _L("Media Dialog"), _L("Media Dialog")); #if defined(__linux__) && defined(SLIC3R_DESKTOP_INTEGRATION) @@ -2540,19 +2535,6 @@ wxMenu* GUI_App::get_config_menu() case ConfigMenuUpdateApp: app_version_check(true); break; - case ConfigMenuAuthLogin: - { - if (this->plater()->get_user_account()->is_logged()) - this->plater()->get_user_account()->do_logout(); - else - this->plater()->get_user_account()->do_login(); - } - break; - case ConfigMenuConnectDummy: - { - this->plater()->get_user_account()->enqueue_connect_printers_action(); - } - break; #ifdef __linux__ case ConfigMenuDesktopIntegration: show_desktop_integration_dialog(); @@ -2666,11 +2648,7 @@ wxMenu* GUI_App::get_config_menu() return local_menu; } -void GUI_App::update_config_menu() -{ - m_config_menu_updatable_items[ConfigMenuIDs::ConfigMenuAuthLogin]->SetItemLabel(this->plater()->get_user_account()->is_logged() ? _L("PrusaAuth Log out") : _L("PrusaAuth Log in")); - m_config_menu_updatable_items[ConfigMenuIDs::ConfigMenuConnectDummy]->Enable(this->plater()->get_user_account()->is_logged()); -} + void GUI_App::open_preferences(const std::string& highlight_option /*= std::string()*/, const std::string& tab_name/*= std::string()*/) { mainframe->preferences_dialog->show(highlight_option, tab_name); diff --git a/src/slic3r/GUI/GUI_App.hpp b/src/slic3r/GUI/GUI_App.hpp index d2f3b47a86..e062cb0373 100644 --- a/src/slic3r/GUI/GUI_App.hpp +++ b/src/slic3r/GUI/GUI_App.hpp @@ -99,8 +99,6 @@ enum ConfigMenuIDs { ConfigMenuUpdateConf, ConfigMenuUpdateApp, ConfigMenuMediaDialog, - ConfigMenuAuthLogin, - ConfigMenuConnectDummy, ConfigMenuConnectDialog, ConfigMenuDesktopIntegration, ConfigMenuPreferences, @@ -299,7 +297,6 @@ public: void update_mode(); wxMenu* get_config_menu(); - void update_config_menu(); bool has_unsaved_preset_changes() const; bool has_current_preset_changes() const; void update_saved_preset_from_current_preset(); diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index ece8cfc1e4..caff932c98 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -836,10 +836,10 @@ void MainFrame::create_preset_tabs() add_created_tab(new TabPrinter(m_tabpanel), wxGetApp().preset_bundle->printers.get_edited_preset().printer_technology() == ptFFF ? "printer" : "sla_printer"); m_webview = new WebViewPanel(m_tabpanel); - m_tabpanel->AddPage(m_webview, "PrusaConnect"); + dynamic_cast(m_tabpanel)->AddPage(m_webview, "PrusaConnect", ""); /* m_media = new MediaMainPanel(this); - m_tabpanel->AddPage(m_media, "Media"); + dynamic_cast(m_tabpanel)->AddPage(m_media, "Media"); */ } diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index f8a8160c91..dca2019296 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -887,7 +887,6 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame) std::string text = _u8L("Logged out."); this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID); this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, text); - wxGetApp().update_config_menu(); }); this->q->Bind(EVT_PA_ID_USER_SUCCESS, [this](PrusaAuthSuccessEvent& evt) { @@ -897,7 +896,6 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame) std::string text = format(_u8L("Logged as %1%."), username); this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID); this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, text); - wxGetApp().update_config_menu(); } else { // TODO } diff --git a/src/slic3r/GUI/TopBar.cpp b/src/slic3r/GUI/TopBar.cpp index 6c2c2e1ea9..291c70824e 100644 --- a/src/slic3r/GUI/TopBar.cpp +++ b/src/slic3r/GUI/TopBar.cpp @@ -2,6 +2,7 @@ #include "GUI_App.hpp" #include "Plater.hpp" +#include "UserAccount.hpp" //#include "wxExtensions.hpp" #include "format.hpp" #include "I18N.hpp" @@ -27,6 +28,10 @@ TopBarItemsCtrl::Button::Button(wxWindow* parent, const wxString& label, const s wxSize size = GetTextExtent(label) + wxSize(6 * btn_margin, int(1.5 * btn_margin)); if (icon_name.empty()) this->SetMinSize(size); + if (label.IsEmpty()) { + const int btn_side = px_cnt + btn_margin; + this->SetMinSize(wxSize(btn_side, btn_side)); + } else this->SetMinSize(wxSize(-1, size.y)); } @@ -62,6 +67,11 @@ TopBarItemsCtrl::ButtonWithPopup::ButtonWithPopup(wxWindow* parent, const wxStri this->SetLabel(label); } +TopBarItemsCtrl::ButtonWithPopup::ButtonWithPopup(wxWindow* parent, const std::string& icon_name, int icon_width/* = 20*/, int icon_height/* = 20*/) + :TopBarItemsCtrl::Button(parent, "", icon_name, icon_width) +{ +} + void TopBarItemsCtrl::ButtonWithPopup::SetLabel(const wxString& label) { wxString full_label = " " + label + " " + down_arrow; @@ -97,6 +107,42 @@ void TopBarItemsCtrl::ApplyWorkspacesMenu() } } +void TopBarItemsCtrl::CreateAuthMenu() +{ + m_user_menu_item = append_menu_item(&m_auth_menu, wxID_ANY, "", "", + [this](wxCommandEvent& e) { + m_auth_btn->set_selected(true); + wxGetApp().plater()->PopupMenu(&m_auth_menu, m_auth_btn->GetPosition()); + }, get_bmp_bundle("user", 16), nullptr, []() { return true; }, this); + + m_auth_menu.AppendSeparator(); + + m_connect_dummy_menu_item = append_menu_item(&m_auth_menu, wxID_ANY, _L("PrusaConnect Printers"), "", + [this](wxCommandEvent&) { wxGetApp().plater()->get_user_account()->enqueue_connect_printers_action(); }, + "", nullptr, []() { return wxGetApp().plater()->get_user_account()->is_logged(); }, this->GetParent()); + + m_login_menu_item = append_menu_item(&m_auth_menu, wxID_ANY, "", "", + [this](wxCommandEvent&) { + auto user_account = wxGetApp().plater()->get_user_account(); + if (user_account->is_logged()) + user_account->do_logout(); + else + user_account->do_login(); + }, get_bmp_bundle("login", 16), nullptr, []() { return true; }, this); +} + +void TopBarItemsCtrl::UpdateAuthMenu() +{ + auto user_account = wxGetApp().plater()->get_user_account(); + if (m_login_menu_item) { + m_login_menu_item->SetItemLabel(user_account->is_logged() ? _L("PrusaAuth Log out") : _L("PrusaAuth Log in")); + m_login_menu_item->SetBitmap(user_account->is_logged() ? *get_bmp_bundle("logout", 16) : *get_bmp_bundle("login", 16)); + } + + if (m_user_menu_item) + m_user_menu_item->SetItemLabel(user_account->is_logged() ? from_u8(user_account->get_username()) : _L("Anonymus")); +} + TopBarItemsCtrl::TopBarItemsCtrl(wxWindow *parent) : wxControl(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE | wxTAB_TRAVERSAL) { @@ -134,7 +180,7 @@ TopBarItemsCtrl::TopBarItemsCtrl(wxWindow *parent) : m_workspace_btn = new ButtonWithPopup(this, _L("Workspace"), "mode_simple"); m_sizer->AddStretchSpacer(20); - m_sizer->Add(m_workspace_btn, 1, wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT | wxRIGHT, 2 * m_btn_margin); + m_sizer->Add(m_workspace_btn, 1, wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT); m_workspace_btn->Bind(wxEVT_BUTTON, [this](wxCommandEvent& event) { m_workspace_btn->set_selected(true); @@ -143,6 +189,20 @@ TopBarItemsCtrl::TopBarItemsCtrl(wxWindow *parent) : }); m_workspaces_menu.Bind(wxEVT_MENU_CLOSE, [this](wxMenuEvent&) { m_workspace_btn->set_selected(false); }); + // create Auth menu + CreateAuthMenu(); + + m_auth_btn = new ButtonWithPopup(this, "user", 35); + m_sizer->Add(m_auth_btn, 1, wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT | wxRIGHT | wxLEFT, m_btn_margin); + + m_auth_btn->Bind(wxEVT_BUTTON, [this](wxCommandEvent& event) { + UpdateAuthMenu(); + m_auth_btn->set_selected(true); + wxPoint pos = m_auth_btn->GetPosition(); + wxGetApp().plater()->PopupMenu(&m_auth_menu, pos); + }); + m_auth_menu.Bind(wxEVT_MENU_CLOSE, [this](wxMenuEvent&) { m_auth_btn->set_selected(false); }); + this->Bind(wxEVT_PAINT, &TopBarItemsCtrl::OnPaint, this); } diff --git a/src/slic3r/GUI/TopBar.hpp b/src/slic3r/GUI/TopBar.hpp index 9ec522c2d0..e330a6d35a 100644 --- a/src/slic3r/GUI/TopBar.hpp +++ b/src/slic3r/GUI/TopBar.hpp @@ -36,14 +36,23 @@ class TopBarItemsCtrl : public wxControl ButtonWithPopup(wxWindow* parent, const wxString& label, const std::string& icon_name = ""); + ButtonWithPopup(wxWindow* parent, + const std::string& icon_name, + int icon_width = 20, + int icon_height = 20); ~ButtonWithPopup() {} void SetLabel(const wxString& label) override; }; - wxMenu m_main_menu; - wxMenu m_workspaces_menu; + wxMenu m_main_menu; + wxMenu m_workspaces_menu; + wxMenu m_auth_menu; + + wxMenuItem* m_user_menu_item{ nullptr }; + wxMenuItem* m_login_menu_item{ nullptr }; + wxMenuItem* m_connect_dummy_menu_item{ nullptr }; public: TopBarItemsCtrl(wxWindow* parent); @@ -64,6 +73,8 @@ public: void AppendMenuItem(wxMenu* menu, const wxString& title); void AppendMenuSeparaorItem(); void ApplyWorkspacesMenu(); + void CreateAuthMenu(); + void UpdateAuthMenu(); private: wxWindow* m_parent; @@ -71,6 +82,7 @@ private: wxBoxSizer* m_sizer; ButtonWithPopup* m_menu_btn {nullptr}; ButtonWithPopup* m_workspace_btn {nullptr}; + ButtonWithPopup* m_auth_btn {nullptr}; std::vector m_pageButtons; int m_selection {-1}; int m_btn_margin;