103 lines
3.5 KiB
C++
103 lines
3.5 KiB
C++
#include "BoostLog.h"
|
|
#include "Client.h"
|
|
#include <ftxui/component/component.hpp>
|
|
#include <ftxui/component/screen_interactive.hpp>
|
|
|
|
ftxui::ButtonOption Style() {
|
|
using namespace ftxui;
|
|
auto option = ButtonOption::Animated();
|
|
option.transform = [](const EntryState &s) {
|
|
auto element = text(s.label);
|
|
if (s.focused) {
|
|
element |= bold;
|
|
}
|
|
return element | center | borderEmpty | flex;
|
|
};
|
|
return option;
|
|
}
|
|
|
|
int main(int argc, char const *argv[]) try {
|
|
Client client;
|
|
std::string remoteId;
|
|
std::string message;
|
|
auto screen = ftxui::ScreenInteractive::Fullscreen();
|
|
int depth = 0;
|
|
std::string dialogMessga = "请不要不要";
|
|
ftxui::InputOption option;
|
|
option.multiline = false;
|
|
auto offerInput = ftxui::Input(&remoteId, "remote id", option);
|
|
auto offerButton = ftxui::Button(
|
|
"Offer",
|
|
[&]() {
|
|
if (!client.setRemoteId(remoteId)) {
|
|
depth = 1;
|
|
dialogMessga = "请先连接信令服务器!";
|
|
}
|
|
},
|
|
ftxui::ButtonOption::Border());
|
|
auto offerLayout = ftxui::Container::Horizontal({
|
|
offerInput,
|
|
offerButton,
|
|
});
|
|
|
|
auto messageInput = ftxui::Input(&message, "请输入要发送的消息...");
|
|
auto messageButton = ftxui::Button(
|
|
"发送",
|
|
[&client, &message]() {
|
|
if (!message.empty()) {
|
|
client.sendMessage(message);
|
|
message.clear();
|
|
}
|
|
},
|
|
ftxui::ButtonOption());
|
|
auto messageLayout = ftxui::Container::Horizontal({
|
|
messageInput,
|
|
messageButton,
|
|
});
|
|
|
|
auto layout = ftxui::Container::Vertical({
|
|
offerLayout,
|
|
messageLayout,
|
|
});
|
|
|
|
auto okButton = ftxui::Button("好的", [&depth]() { depth = 0; });
|
|
auto dialog = ftxui::Renderer(okButton, [&]() {
|
|
return ftxui::vbox({
|
|
ftxui::paragraph(dialogMessga),
|
|
ftxui::separator(),
|
|
ftxui::hbox({ftxui::filler(), okButton->Render()}),
|
|
}) |
|
|
ftxui::border;
|
|
});
|
|
|
|
auto mainPage = ftxui::Renderer(layout, [&] {
|
|
return ftxui::window(ftxui::text("WebRTC 测试"),
|
|
ftxui::vbox({
|
|
ftxui::hbox({ftxui::text("ID: "), ftxui::text(client.id())}),
|
|
ftxui::separator(),
|
|
ftxui::paragraph(client.log()) | ftxui::flex, // logger
|
|
ftxui::separator(),
|
|
ftxui::text("通过信令服务器发送 offer") | ftxui::align_right,
|
|
ftxui::hbox({offerInput->Render(), offerButton->Render()}),
|
|
ftxui::separator(),
|
|
ftxui::text("通过 DataChannel 发送消息") | ftxui::align_right,
|
|
ftxui::hbox({messageInput->Render() | ftxui::yframe, messageButton->Render()}),
|
|
}));
|
|
});
|
|
|
|
auto component = ftxui::Container::Tab({mainPage, dialog}, &depth);
|
|
component = ftxui::Renderer(component, [&]() {
|
|
ftxui::Element document = mainPage->Render();
|
|
|
|
if (depth == 1) {
|
|
document = ftxui::dbox({document, dialog->Render() | ftxui::clear_under | ftxui::center});
|
|
}
|
|
return document;
|
|
});
|
|
|
|
screen.Loop(component);
|
|
return 0;
|
|
} catch (const std::exception &e) {
|
|
std::cout << "Error: " << e.what() << std::endl;
|
|
}
|