ConversationInfoWindow.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
  3. * All rights reserved. Distributed under the terms of the MIT license.
  4. */
  5. #include "ConversationInfoWindow.h"
  6. #include "Conversation.h"
  7. #include <libinterface/BitmapView.h>
  8. #include <Catalog.h>
  9. #include <LayoutBuilder.h>
  10. #include <StringFormat.h>
  11. #include <StringView.h>
  12. #include <TextView.h>
  13. #include "NotifyMessage.h"
  14. #undef B_TRANSLATION_CONTEXT
  15. #define B_TRANSLATION_CONTEXT "ConversationInfoWindow"
  16. ConversationInfoWindow::ConversationInfoWindow(Conversation* chat)
  17. :
  18. BWindow(BRect(200, 200, 300, 400),
  19. B_TRANSLATE("Room information"), B_FLOATING_WINDOW,
  20. B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS),
  21. fChat(chat)
  22. {
  23. _InitInterface();
  24. CenterOnScreen();
  25. chat->RegisterObserver(this);
  26. }
  27. ConversationInfoWindow::~ConversationInfoWindow()
  28. {
  29. fChat->UnregisterObserver(this);
  30. }
  31. void
  32. ConversationInfoWindow::ObserveString(int32 what, BString string)
  33. {
  34. Lock();
  35. switch (what) {
  36. case STR_ROOM_NAME:
  37. fNameLabel->SetText(string);
  38. break;
  39. }
  40. Unlock();
  41. }
  42. void
  43. ConversationInfoWindow::ObserveInteger(int32 what, int32 num)
  44. {
  45. Lock();
  46. switch (what) {
  47. case INT_ROOM_MEMBERS:
  48. _SetUserCountLabel(num);
  49. break;
  50. }
  51. Unlock();
  52. }
  53. void
  54. ConversationInfoWindow::ObservePointer(int32 what, void* ptr)
  55. {
  56. Lock();
  57. switch (what) {
  58. case PTR_ROOM_BITMAP:
  59. fIcon->SetBitmap((BBitmap*)ptr);
  60. break;
  61. }
  62. Unlock();
  63. }
  64. void
  65. ConversationInfoWindow::_InitInterface()
  66. {
  67. fIcon = new BitmapView("roomIcon");
  68. fIcon->SetBitmap(fChat->IconBitmap());
  69. fNameLabel = new BStringView("nameLabel", fChat->GetName());
  70. fNameLabel->SetFont(be_bold_font);
  71. fIdLabel = new BTextView("idLabel", be_fixed_font, NULL, B_WILL_DRAW);
  72. fIdLabel->SetWordWrap(false);
  73. fIdLabel->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
  74. fIdLabel->MakeEditable(false);
  75. _SetIdLabel(fChat->GetId());
  76. fUserCountLabel = new BStringView("userCountLabel", "");
  77. _SetUserCountLabel(fChat->Users().CountItems());
  78. // Centering is still my lyfeee
  79. fNameLabel->SetExplicitAlignment(BAlignment(B_ALIGN_CENTER, B_ALIGN_TOP));
  80. fIdLabel->SetExplicitAlignment(BAlignment(B_ALIGN_CENTER, B_ALIGN_TOP));
  81. fUserCountLabel->SetExplicitAlignment(BAlignment(B_ALIGN_CENTER, B_ALIGN_TOP));
  82. BLayoutBuilder::Group<>(this, B_HORIZONTAL, 10)
  83. .SetInsets(B_USE_DEFAULT_SPACING)
  84. .AddGroup(B_VERTICAL)
  85. .Add(fNameLabel)
  86. .Add(fIdLabel)
  87. .AddGlue()
  88. .End()
  89. .AddGroup(B_VERTICAL)
  90. .AddGroup(B_VERTICAL)
  91. .Add(fIcon)
  92. .Add(fUserCountLabel)
  93. .End()
  94. .End();
  95. }
  96. void
  97. ConversationInfoWindow::_SetIdLabel(BString id)
  98. {
  99. fIdLabel->SetText(id);
  100. fIdLabel->SetExplicitMinSize(
  101. BSize(be_fixed_font->StringWidth(id) + 5, B_SIZE_UNSET));
  102. }
  103. void
  104. ConversationInfoWindow::_SetUserCountLabel(int32 userCount)
  105. {
  106. BStringFormat pmFormat(B_TRANSLATE("{0, plural,"
  107. "=1{One lonely user}"
  108. "=2{Two partners}"
  109. "other{# members}}"));
  110. BString label;
  111. pmFormat.Format(label, userCount);
  112. fUserCountLabel->SetText(label);
  113. }