This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.cs
175 lines (159 loc) · 6.82 KB
/
App.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Revolt.Channels;
using Revolt.Cli.Views.Windows;
using Terminal.Gui;
namespace Revolt.Cli
{
public static class App
{
public static Toplevel Toplevel;
public static MenuBar MenuBar;
public static RevoltClient Client;
public static MenuBarItem GroupsMenuBarItem;
public static MenuBarItem ServersMenuBarItem;
public static MenuBarItem DirectMessagesMenuBarItem;
public static bool HasReadied;
public static void Run()
{
RevoltClient client = null;
var top = Application.Top;
Toplevel = top;
// Creates the top-level window to show
var win = new Window("Revolt.Cli")
{
X = 0,
Y = 1, // Leave one row for the toplevel menu
// By using Dim.Fill(), it will automatically resize without manual intervention
Width = Dim.Fill(),
Height = Dim.Fill(),
};
// win.ColorScheme.Normal = new(Color.White);
top.Add(win);
// Creates a menubar, the item "New" has a help menu.
GroupsMenuBarItem = new MenuBarItem("_Groups", new List<MenuItem[]>());
ServersMenuBarItem = new MenuBarItem("_Servers", new List<MenuItem[]>());
DirectMessagesMenuBarItem = new MenuBarItem("_Direct Messages", new List<MenuItem[]>());
var menu = new MenuBar(new[]
{
new("_App", new MenuItem[]
{
new("_Home", "", () => new HomeWindow().Show()),
new("API Info", "", () =>
{
var info = client.ApiInfo;
var vortex = client.GetVortexInfo();
var autumn = client.GetAutumnInfo();
// todo: ansi
string StrBool(bool b)
=> b ? "true" : "false";
MessageBox.Query("Revolt API Info", $@"Versions
Api: {info.Version}
Vortex: {vortex.Version}
Autumn: {autumn.Version}
Features
Registration: {StrBool(info.Features.Registration)}
Email: {StrBool(info.Features.Email)}
Captcha: {StrBool(info.Features.Captcha.Enabled)}
Max Attachment Size: {autumn.Tags.Attachments.MaxSize / 1000 / 1000}MB", "Ok");
}),
new("_Reconnect", "", () =>
{
client.DisconnectWebsocket();
client.ConnectWebSocketAsync();
}),
new("_About", "", () =>
{
MessageBox.Query("Revolt.Cli", $@"Developed by Jan0660
Version {GetVersion()}
Source: https://github.com/Jan0660/Revolt.Cli", "Ok");
}),
new("_Quit", "", () =>
{
if (Quit())
{
top.Running = false;
}
}),
}),
GroupsMenuBarItem, ServersMenuBarItem, DirectMessagesMenuBarItem,
});
MenuBar = menu;
top.Add(menu);
static bool Quit()
{
var n = MessageBox.Query(50, 7, "Quit Revolt.Cli", "Are you sure you want to quit Revolt.Cli?",
"NOO!!!", "No");
return n == 0;
}
void MainView()
{
client = new(JsonConvert.DeserializeObject<Session>(
File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
"/session.json"))!);
Client = client;
client.OnReady += async () =>
{
try
{
var groupsMenuItems = new List<MenuItem>();
var serversMenuItems = new List<MenuItem>();
var dmMenuItems = new List<MenuItem>();
foreach (var channel in App.Client.ChannelsCache)
{
if (channel is GroupChannel groupChannel)
groupsMenuItems.Add(new MenuItem(groupChannel.Name, "",
() => new ChannelWindow(channel).Show()));
if (channel is DirectMessageChannel dmChannel)
dmMenuItems.Add(new MenuItem(dmChannel.OtherUser.Username, "",
() => new ChannelWindow(channel).Show()));
}
foreach (var server in App.Client.ServersCache)
{
var children = new List<MenuItem>();
foreach (var channelId in server.ChannelIds)
{
try
{
var channel = (TextChannel)App.Client.ChannelsCache.First(c => c._id == channelId);
children.Add(new(channel.Name, "", () => new ChannelWindow(channel).Show()));
}
catch
{
// voice channels crash this :zany_face:
}
}
var menu = new MenuBarItem(server.Name, server.Description?.Shorten(30) ?? "", null)
{
Children = children.ToArray()
};
serversMenuItems.Add(menu);
}
App.GroupsMenuBarItem.Children = groupsMenuItems.ToArray();
App.ServersMenuBarItem.Children = serversMenuItems.ToArray();
App.DirectMessagesMenuBarItem.Children = dmMenuItems.ToArray();
if (!HasReadied)
new HomeWindow().Show();
}
catch (Exception exc)
{
}
HasReadied = true;
};
client.ConnectWebSocketAsync();
}
if (!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/session.json"))
new LogInWindow().Show();
else
MainView();
Task.Delay(100).ContinueWith(_ => { Application.Refresh(); });
Application.Run();
}
public static string GetVersion()
=> typeof(ExtensionMethods).Assembly.GetName().Version!.ToString();
}
}