IRCAppWindow.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "IRCAppWindow.h"
  2. #include "IRCClientWindow.h"
  3. #include "IRCClientWindowListModel.h"
  4. #include <LibGUI/GStackWidget.h>
  5. #include <LibGUI/GTableView.h>
  6. #include <LibGUI/GBoxLayout.h>
  7. IRCAppWindow::IRCAppWindow()
  8. : GWindow()
  9. , m_client("127.0.0.1", 6667)
  10. {
  11. set_title(String::format("IRC Client: %s:%d", m_client.hostname().characters(), m_client.port()));
  12. set_rect(200, 200, 600, 400);
  13. setup_client();
  14. setup_widgets();
  15. }
  16. IRCAppWindow::~IRCAppWindow()
  17. {
  18. }
  19. void IRCAppWindow::setup_client()
  20. {
  21. m_client.on_connect = [this] {
  22. m_client.join_channel("#test");
  23. };
  24. m_client.on_join = [this] (const String& channel_name) {
  25. ensure_window(IRCClientWindow::Channel, channel_name);
  26. };
  27. m_client.connect();
  28. }
  29. void IRCAppWindow::setup_widgets()
  30. {
  31. auto* widget = new GWidget(nullptr);
  32. widget->set_fill_with_background_color(true);
  33. set_main_widget(widget);
  34. widget->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  35. auto* window_list = new GTableView(widget);
  36. window_list->set_headers_visible(false);
  37. window_list->set_model(OwnPtr<IRCClientWindowListModel>(m_client.client_window_list_model()));
  38. window_list->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  39. window_list->set_preferred_size({ 120, 0 });
  40. m_client.client_window_list_model()->on_activation = [this] (IRCClientWindow& window) {
  41. m_container->set_active_widget(&window);
  42. };
  43. m_container = new GStackWidget(widget);
  44. create_subwindow(IRCClientWindow::Server, "Server");
  45. }
  46. IRCClientWindow& IRCAppWindow::create_subwindow(IRCClientWindow::Type type, const String& name)
  47. {
  48. return *new IRCClientWindow(m_client, type, name, m_container);
  49. }
  50. IRCClientWindow& IRCAppWindow::ensure_window(IRCClientWindow::Type type, const String& name)
  51. {
  52. for (int i = 0; i < m_client.window_count(); ++i) {
  53. auto& window = m_client.window_at(i);
  54. if (window.name() == name)
  55. return window;
  56. }
  57. return create_subwindow(type, name);
  58. }