From e33dd923751240544fd9184c866629ae70b68e0e Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Wed, 28 Jul 2021 10:13:31 -0500 Subject: [PATCH] Save account selection in account menus --- application/Makefile | 1 + application/views/AccountMenuItem.cpp | 24 ++++++++++++++++++++++++ application/views/AccountMenuItem.h | 18 ++++++++++++++++++ application/views/AccountsMenu.cpp | 21 +++++++++++++++++++-- application/views/AccountsMenu.h | 12 ++++++++---- 5 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 application/views/AccountMenuItem.cpp create mode 100644 application/views/AccountMenuItem.h diff --git a/application/Makefile b/application/Makefile index 2b0cba0..d41617e 100644 --- a/application/Makefile +++ b/application/Makefile @@ -56,6 +56,7 @@ SRCS = \ application/preferences/PreferencesBehavior.cpp \ application/preferences/PreferencesReplicant.cpp \ application/views/AccountsMenu.cpp \ + application/views/AccountMenuItem.cpp \ application/views/RenderView.cpp \ application/views/ConversationAccountItem.cpp \ application/views/ConversationItem.cpp \ diff --git a/application/views/AccountMenuItem.cpp b/application/views/AccountMenuItem.cpp new file mode 100644 index 0000000..fa927c3 --- /dev/null +++ b/application/views/AccountMenuItem.cpp @@ -0,0 +1,24 @@ +/* + * Copyright 2021, Jaidyn Levesque + * All rights reserved. Distributed under the terms of the MIT license. + */ + +#include "AccountMenuItem.h" + +#include "AccountsMenu.h" + + +AccountMenuItem::AccountMenuItem(const char* label, BMessage* msg) + : + BMenuItem(label, msg) +{ +} + + +void +AccountMenuItem::SetMarked(bool mark) +{ + BMenuItem::SetMarked(mark); + if (mark == true) + ((AccountsMenu*)Menu())->SetDefaultSelection(this); +} diff --git a/application/views/AccountMenuItem.h b/application/views/AccountMenuItem.h new file mode 100644 index 0000000..a3010eb --- /dev/null +++ b/application/views/AccountMenuItem.h @@ -0,0 +1,18 @@ +/* + * Copyright 2021, Jaidyn Levesque + * All rights reserved. Distributed under the terms of the MIT license. + */ +#ifndef _ACCOUNT_MENU_ITEM_H +#define _ACCOUNT_MENU_ITEM_H + +#include + + +class AccountMenuItem : public BMenuItem { +public: + AccountMenuItem(const char* label, BMessage* msg); + + virtual void SetMarked(bool mark); +}; + +#endif // _ACCOUNT_MENU_ITEM_H diff --git a/application/views/AccountsMenu.cpp b/application/views/AccountsMenu.cpp index cb05429..2a070b0 100644 --- a/application/views/AccountsMenu.cpp +++ b/application/views/AccountsMenu.cpp @@ -8,6 +8,7 @@ #include #include +#include "AccountMenuItem.h" #include "MainWindow.h" #include "Server.h" #include "TheApp.h" @@ -17,6 +18,9 @@ #define B_TRANSLATION_CONTEXT "AccountsMenu" +int32 AccountsMenu::fDefaultSelection = 0; + + AccountsMenu::AccountsMenu(const char* name, BMessage msg, BMessage* allMsg) : BMenu(name), @@ -48,6 +52,15 @@ AccountsMenu::ObserveInteger(int32 what, int32 value) } +void +AccountsMenu::SetDefaultSelection(BMenuItem* item) +{ + fDefaultSelection = IndexOf(item); + if (fAllMessage != NULL) + fDefaultSelection--; +} + + void AccountsMenu::_PopulateMenu() { @@ -66,10 +79,14 @@ AccountsMenu::_PopulateMenu() label.RemoveChars(16, label.CountChars() - 16); label << B_UTF8_ELLIPSIS; } - AddItem(new BMenuItem(label.String(), new BMessage(fAccountMessage))); + AddItem(new AccountMenuItem(label.String(), new BMessage(fAccountMessage))); } - if (CountItems() > 0) + int32 selection = fDefaultSelection; + + if (fAllMessage == NULL && selection < CountItems() && selection >= 0) + ItemAt(selection)->SetMarked(true); + else if (CountItems() > 0) ItemAt(0)->SetMarked(true); else SetEnabled(false); diff --git a/application/views/AccountsMenu.h b/application/views/AccountsMenu.h index 2c1bfac..4a6d2bc 100644 --- a/application/views/AccountsMenu.h +++ b/application/views/AccountsMenu.h @@ -12,16 +12,20 @@ class AccountsMenu : public BMenu, public Observer { public: - AccountsMenu(const char* name, BMessage msg, BMessage* allMsg = NULL); - ~AccountsMenu(); + AccountsMenu(const char* name, BMessage msg, + BMessage* allMsg = NULL); + ~AccountsMenu(); - virtual void ObserveInteger(int32 what, int32 value); + virtual void ObserveInteger(int32 what, int32 value); + + void SetDefaultSelection(BMenuItem* item); private: - void _PopulateMenu(); + void _PopulateMenu(); BMessage fAccountMessage; BMessage* fAllMessage; + static int32 fDefaultSelection; }; #endif // _ACCOUNTS_MENU_H