Base for custom menubar items

Fixes #8
This commit is contained in:
Jaidyn Ann 2021-06-16 05:03:10 -05:00
parent d5aebac5c5
commit 560be5a810
5 changed files with 64 additions and 1 deletions

View File

@ -396,7 +396,14 @@ enum im_what_code {
// Requires: String "_label", Message "_msg", String "class" = "BMenuItem" // Requires: String "_label", Message "_msg", String "class" = "BMenuItem"
// Bool "x_to_protocol", int32 "x_perms" // Bool "x_to_protocol", int32 "x_perms"
// Allowed: int64 "instance" // Allowed: int64 "instance"
IM_REGISTER_CHATLIST_ITEM = 1102 IM_REGISTER_CHATLIST_ITEM = 1102,
//! Register a "Protocol" menu item →Caya
// Just an archived BMenuItem with extra slots; it adds a menu item to
// the menubar's "Protocol" menu.
// Requires: String "_label", Message "_msg", String "class" = "BMenuItem"
// Bool "x_to_protocol", int64 "instance"
IM_REGISTER_MENUBAR_ITEM = 1103
}; };

View File

@ -192,6 +192,20 @@ ProtocolLooper::AddChatPopUpItem(BMessage* archived)
} }
BObjectList<BMessage>
ProtocolLooper::MenuBarItems() const
{
return fMenuItems;
}
void
ProtocolLooper::AddMenuBarItem(BMessage* archived)
{
fMenuItems.AddItem(archived);
}
BString BString
ProtocolLooper::GetOwnId() ProtocolLooper::GetOwnId()
{ {

View File

@ -61,6 +61,10 @@ public:
ChatPopUpItems() const; ChatPopUpItems() const;
void AddChatPopUpItem(BMessage* archived); void AddChatPopUpItem(BMessage* archived);
BObjectList<BMessage>
MenuBarItems() const;
void AddMenuBarItem(BMessage* archived);
BString GetOwnId(); BString GetOwnId();
void SetOwnId(BString user_id); void SetOwnId(BString user_id);
@ -82,6 +86,7 @@ private:
CommandMap fCommands; CommandMap fCommands;
BObjectList<BMessage> fUserItems; BObjectList<BMessage> fUserItems;
BObjectList<BMessage> fChatItems; BObjectList<BMessage> fChatItems;
BObjectList<BMessage> fMenuItems;
ConversationAccountItem* ConversationAccountItem*
fListItem; fListItem;

View File

@ -550,6 +550,13 @@ Server::ImMessage(BMessage* msg)
looper->AddChatPopUpItem(new BMessage(*msg)); looper->AddChatPopUpItem(new BMessage(*msg));
break; break;
} }
case IM_REGISTER_MENUBAR_ITEM:
{
ProtocolLooper* looper = _LooperFromMessage(msg);
if (looper != NULL)
looper->AddMenuBarItem(new BMessage(*msg));
break;
}
case IM_PROTOCOL_READY: case IM_PROTOCOL_READY:
{ {
// Ready notification // Ready notification

View File

@ -348,6 +348,36 @@ MainWindow::SetConversation(Conversation* chat)
fRightView->SetItemWeight(0, weightChat, true); fRightView->SetItemWeight(0, weightChat, true);
fRightView->SetItemWeight(1, weightSend, true); fRightView->SetItemWeight(1, weightSend, true);
} }
// Remove "Protocol" menu
BMenuItem* chatMenuItem = fMenuBar->FindItem("Protocol");
BMenu* chatMenu;
if (chatMenuItem != NULL && (chatMenu = chatMenuItem->Submenu()) != NULL)
fMenuBar->RemoveItem(chatMenu);
// Add and populate "Protocol" menu, if appropriate
if (fConversation != NULL) {
ProtocolLooper* looper = fConversation->GetProtocolLooper();
BObjectList<BMessage> menuItems = looper->MenuBarItems();
for (int i = 0; i < menuItems.CountItems(); i++) {
BMessage* itemMsg = menuItems.ItemAt(i);
BMessage* msg = new BMessage(*itemMsg);
BMessage toSend;
msg->FindMessage("_msg", &toSend);
toSend.AddString("chat_id", fConversation->GetId());
toSend.AddInt64("instance", looper->GetInstance());
msg->ReplaceMessage("_msg", &toSend);
BMenuItem* item = new BMenuItem(msg);
if (item == NULL)
continue;
if (msg->GetBool("x_to_protocol", true) == true)
item->SetTarget(looper);
else
item->SetTarget(this);
chatMenu->AddItem(item);
}
}
} }