Wt examples: PopupChatWidget Class Reference

Wt examples 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
PopupChatWidget Class Reference

A popup chat widget. More...

`#include <PopupChatWidget.h>`

Inheritance diagram for PopupChatWidget:
[legend]

## Public Member Functions
| PopupChatWidget (SimpleChatServer &server, const std::string &id)
void | setName (const Wt::WString &name)
Public Member Functions inherited from SimpleChatWidget
| SimpleChatWidget (SimpleChatServer &server)
| Create a chat widget that will connect to the given server.
| ~SimpleChatWidget ()
| Delete a chat widget.
void | connect ()
void | disconnect ()
void | letLogin ()
| Show a simple login screen.
bool | startChat (const Wt::WString &user)
| Start a chat for the given user.
void | logout ()
SimpleChatServer & | server ()
int | userCount ()
const Wt::WString & | userName () const

## Protected Member Functions
virtual void | createLayout (std::unique_ptr< WWidget > messages, std::unique_ptr< WWidget > userList, std::unique_ptr< WWidget > messageEdit, std::unique_ptr< WWidget > sendButton, std::unique_ptr< WWidget > logoutButton)
virtual void | updateUsers ()
virtual void | newMessage ()
Protected Member Functions inherited from SimpleChatWidget
virtual void | createLayout (std::unique_ptr< Wt::WWidget > messages, std::unique_ptr< Wt::WWidget > userList, std::unique_ptr< Wt::WWidget > messageEdit, std::unique_ptr< Wt::WWidget > sendButton, std::unique_ptr< Wt::WWidget > logoutButton)
virtual void | render (Wt::WFlags< Wt::RenderFlag > flags)
bool | loggedIn () const

## Private Member Functions
void | toggleSize ()
void | goOnline ()
bool | minimized () const
std::unique_ptr< Wt::WContainerWidget > | createBar ()

## Private Attributes
Wt::WString | name_
Wt::WText * | title_
Wt::WWidget * | bar_
bool | online_
bool | minimized_
int | missedMessages_

## Detailed Description

A popup chat widget.

Definition at line 20 of file PopupChatWidget.h.

## Constructor & Destructor Documentation

## ◆ PopupChatWidget()

PopupChatWidget::PopupChatWidget | ( | SimpleChatServer & | server,
|  | const std::string & | id )

Definition at line 19 of file PopupChatWidget.C.

21 : SimpleChatWidget(server),
22 missedMessages_(0)
23{
24 setId(id);
25
26 if (Wt::WApplication::instance()->environment().agentIsIE()) {
27 if (Wt::WApplication::instance()->environment().agent()
28 == Wt::UserAgent::IE6)
29 setPositionScheme(Wt::PositionScheme::Absolute);
30 else
31 setPositionScheme(Wt::PositionScheme::Fixed);
32 }
33
34 implementJavaScript
35 (&PopupChatWidget::toggleSize,
36 "{"
37 """let s = " WT_CLASS ".$('" + id + "');"
38 """s.classList.toggle('chat-maximized');"
39 """s.classList.toggle('chat-minimized');"
40 "}");
41
42 online_ = false;
43 minimized_ = true;
44 setStyleClass("chat-widget chat-minimized");
45
46 clear();
47 addWidget(createBar());
48 updateUsers();
49
50 connect();
51}
PopupChatWidget::online_
bool online_
Definition PopupChatWidget.h:39
PopupChatWidget::minimized_
bool minimized_
Definition PopupChatWidget.h:39
PopupChatWidget::createBar
std::unique_ptr< Wt::WContainerWidget > createBar()
Definition PopupChatWidget.C:69
PopupChatWidget::toggleSize
void toggleSize()
Definition PopupChatWidget.C:89
PopupChatWidget::updateUsers
virtual void updateUsers()
Definition PopupChatWidget.C:136
PopupChatWidget::missedMessages_
int missedMessages_
Definition PopupChatWidget.h:40
SimpleChatWidget::server
SimpleChatServer & server()
Definition SimpleChatWidget.h:54
SimpleChatWidget::connect
void connect()
Definition SimpleChatWidget.C:43
SimpleChatWidget::SimpleChatWidget
SimpleChatWidget(SimpleChatServer &server)
Create a chat widget that will connect to the given server.
Definition SimpleChatWidget.C:26

## Member Function Documentation

## ◆ createBar()

std::unique_ptr< Wt::WContainerWidget > PopupChatWidget::createBar | ( |  | ) |
| private

Definition at line 69 of file PopupChatWidget.C.

70{
71 auto bar(std::make_unique<Wt::WContainerWidget>());
72 bar->setStyleClass("chat-bar");
73
74 auto toggleButton(std::make_unique<Wt::WText>());
75 toggleButton->setInline(false);
76 toggleButton->setStyleClass("chat-minmax");
77 bar->clicked().connect(this, &PopupChatWidget::toggleSize);
78 bar->clicked().connect(this, &PopupChatWidget::goOnline);
79
80 bar->addWidget(std::move(toggleButton));
81
82 title_ = bar->addWidget(std::make_unique<Wt::WText>());
83
84 bar_ = bar.get();
85
86 return bar;
87}
PopupChatWidget::bar_
Wt::WWidget * bar_
Definition PopupChatWidget.h:38
PopupChatWidget::goOnline
void goOnline()
Definition PopupChatWidget.C:94
PopupChatWidget::title_
Wt::WText * title_
Definition PopupChatWidget.h:37

## ◆ createLayout()

void PopupChatWidget::createLayout | ( | std::unique_ptr< WWidget > | messages,
|  | std::unique_ptr< WWidget > | userList,
|  | std::unique_ptr< WWidget > | messageEdit,
|  | std::unique_ptr< WWidget > | sendButton,
|  | std::unique_ptr< WWidget > | logoutButton )
| protectedvirtual

Definition at line 118 of file PopupChatWidget.C.

123{
124 auto layout(std::make_unique<Wt::WVBoxLayout>());
125 layout->setContentsMargins(0, 0, 0, 0);
126 layout->setSpacing(0);
127
128 auto bar = layout->addWidget(createBar());
129 bar->setMinimumSize(Wt::WLength::Auto, 20);
130 layout->addWidget(std::move(messages), 1);
131 layout->addWidget(std::move(messageEdit));
132
133 setLayout(std::move(layout));
134}

## ◆ goOnline()

void PopupChatWidget::goOnline | ( |  | ) |
| private

Definition at line 94 of file PopupChatWidget.C.

95{
96 if (!online_) {
97 online_ = true;
98
99 int tries = 1;
100 Wt::WString name = name_;
101 if (name.empty())
102 name = server().suggestGuest();
103
104 while (!startChat(name)) {
105 if (name_.empty())
106 name = server().suggestGuest();
107 else
108 name = name_ + std::to_string(++tries);
109 }
110
111 name_ = name;
112 }
113
114 missedMessages_ = 0;
115 bar_->removeStyleClass("alert");
116}
PopupChatWidget::name_
Wt::WString name_
Definition PopupChatWidget.h:36
SimpleChatServer::suggestGuest
Wt::WString suggestGuest()
Get a suggestion for a guest user name.
Definition SimpleChatServer.C:134
SimpleChatWidget::startChat
bool startChat(const Wt::WString &user)
Start a chat for the given user.
Definition SimpleChatWidget.C:191

## ◆ minimized()

bool PopupChatWidget::minimized | ( |  | ) | const
| private

Definition at line 166 of file PopupChatWidget.C.

167{
168 return minimized_;
169}

## ◆ newMessage()

void PopupChatWidget::newMessage | ( |  | ) |
| protectedvirtual

Reimplemented from SimpleChatWidget.

Definition at line 156 of file PopupChatWidget.C.

157{
158 if (loggedIn() && minimized()) {
159 ++missedMessages_;
160 if (missedMessages_ == 1) {
161 bar_->addStyleClass("alert");
162 }
163 }
164}
PopupChatWidget::minimized
bool minimized() const
Definition PopupChatWidget.C:166
SimpleChatWidget::loggedIn
bool loggedIn() const
Definition SimpleChatWidget.C:171

## ◆ setName()

void PopupChatWidget::setName | ( | const Wt::WString & | name | ) |

Definition at line 53 of file PopupChatWidget.C.

54{
55 if (name.empty())
56 return;
57
58 if (online_) {
59 int tries = 1;
60 Wt::WString n = name;
61 while (!server().changeName(name_, n))
62 n = name + std::to_string(++tries);
63
64 name_ = n;
65 } else
66 name_ = name;
67}
SimpleChatWidget::changeName
void changeName(const Wt::WString &name)
Definition SimpleChatWidget.C:306

## ◆ toggleSize()

void PopupChatWidget::toggleSize | ( |  | ) |
| private

Definition at line 89 of file PopupChatWidget.C.

90{
91 minimized_ = !minimized_;
92}

## ◆ updateUsers()

void PopupChatWidget::updateUsers | ( |  | ) |
| protectedvirtual

Reimplemented from SimpleChatWidget.

Definition at line 136 of file PopupChatWidget.C.

137{
138 SimpleChatWidget::updateUsers();
139
140 int count = server().users().size();
141
142 if (!loggedIn()) {
143 if (count == 0)
144 title_->setText("Thoughts? Ventilate.");
145 else if (count == 1)
146 title_->setText("Chat: 1 user online");
147 else
148 title_->setText(Wt::WString("Chat: {1} users online").arg(count));
149 } else {
150 title_->setText(Wt::WString("Chat: <span class=\"self\">{1}</span>"
151 " <span class=\"online\">({2} user{3})</span>")
152 .arg(userName()).arg(count).arg(count == 1 ? "" : "s"));
153 }
154}
SimpleChatServer::users
UserSet users()
Get the users currently logged in.
Definition SimpleChatServer.C:178
SimpleChatWidget::updateUsers
virtual void updateUsers()
Definition SimpleChatWidget.C:320
SimpleChatWidget::userName
const Wt::WString & userName() const
Definition SimpleChatWidget.h:58

## Member Data Documentation

## ◆ bar_

Wt::WWidget* PopupChatWidget::bar_
| private

Definition at line 38 of file PopupChatWidget.h.

## ◆ minimized_

bool PopupChatWidget::minimized_
| private

Definition at line 39 of file PopupChatWidget.h.

## ◆ missedMessages_

int PopupChatWidget::missedMessages_
| private

Definition at line 40 of file PopupChatWidget.h.

## ◆ name_

Wt::WString PopupChatWidget::name_
| private

Definition at line 36 of file PopupChatWidget.h.

## ◆ online_

bool PopupChatWidget::online_
| private

Definition at line 39 of file PopupChatWidget.h.

## ◆ title_

Wt::WText* PopupChatWidget::title_
| private

Definition at line 37 of file PopupChatWidget.h.

The documentation for this class was generated from the following files:

- simplechat/PopupChatWidget.h
- simplechat/PopupChatWidget.C