PurpleDialog.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
  3. * All rights reserved. Distributed under the terms of the MIT license.
  4. */
  5. #include "PurpleDialog.h"
  6. #include <libpurple/purple.h>
  7. #include <Button.h>
  8. #include <LayoutBuilder.h>
  9. #include <StringView.h>
  10. #include <TextView.h>
  11. #include "PurpleApp.h"
  12. PurpleDialog::PurpleDialog(const char* title, const char* primary,
  13. const char* secondary, PurpleAccount* account, va_list actions,
  14. size_t action_count, void* user_data)
  15. :
  16. BWindow(BRect(BPoint(-1000, -1000), BSize(300, 250)), _tr(title),
  17. B_FLOATING_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
  18. B_AUTO_UPDATE_SIZE_LIMITS),
  19. fUserData(user_data)
  20. {
  21. CenterOnScreen();
  22. _ParseActions(actions, action_count, PURPLE_REQUEST_ACTION);
  23. _InitActionInterface(primary, secondary);
  24. }
  25. void
  26. PurpleDialog::MessageReceived(BMessage* msg)
  27. {
  28. switch (msg->what)
  29. {
  30. case ACTION_BUTTON:
  31. {
  32. int32 id;
  33. if (msg->FindInt32("index", &id) != B_OK) break;
  34. PurpleRequestActionCb cb = fActions.ItemAt(id)->callback.action;
  35. cb(fUserData, fActions.ItemAt(id)->index);
  36. Quit();
  37. break;
  38. }
  39. default:
  40. BWindow::MessageReceived(msg);
  41. }
  42. }
  43. void
  44. PurpleDialog::_InitActionInterface(const char* label, const char* desc)
  45. {
  46. BStringView* primaryLabel = new BStringView("primaryText", _tr(label));
  47. primaryLabel->SetExplicitAlignment(
  48. BAlignment(B_ALIGN_CENTER, B_ALIGN_TOP));
  49. BTextView* secondaryLabel = new BTextView("secondaryText");
  50. secondaryLabel->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
  51. secondaryLabel->MakeEditable(false);
  52. secondaryLabel->SetWordWrap(true);
  53. secondaryLabel->SetText(_tr(desc));
  54. // Init buttons view
  55. BView* buttonsView = new BView("actionButtons", 0);
  56. BLayoutBuilder::Group<>(buttonsView, B_HORIZONTAL);
  57. for (int i = 0; i < fActions.CountItems(); i++) {
  58. RequestAction* action = fActions.ItemAt(i);
  59. BMessage* msg = new BMessage(ACTION_BUTTON);
  60. msg->AddInt32("index", action->index);
  61. BButton* button = new BButton(action->name.String(), msg);
  62. buttonsView->AddChild(button);
  63. }
  64. // Main layout
  65. BLayoutBuilder::Group<>(this, B_VERTICAL)
  66. .SetInsets(B_USE_DEFAULT_SPACING)
  67. .Add(primaryLabel)
  68. .Add(secondaryLabel)
  69. .Add(buttonsView)
  70. .AddGlue()
  71. .End();
  72. }
  73. void
  74. PurpleDialog::_ParseActions(va_list actions, int32 count,
  75. PurpleRequestType type)
  76. {
  77. for (int i = 0; i < count; i++) {
  78. RequestAction* action = new RequestAction;
  79. action->name = va_arg(actions, const char*);
  80. action->index = i;
  81. action->type = type;
  82. switch (type) {
  83. case PURPLE_REQUEST_ACTION:
  84. action->callback.action = va_arg(actions, PurpleRequestActionCb);
  85. break;
  86. }
  87. fActions.AddItem(action);
  88. }
  89. }