Skip to content

Commit

Permalink
ui fix
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Aug 5, 2022
1 parent 17ca8ab commit bf9db38
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 85 deletions.
134 changes: 68 additions & 66 deletions ui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ MainWindow::MainWindow(QWidget *parent)
}
});

setupGRPC();
setup_grpc();

// Start last
if (NekoRay::dataStore->remember_enable) {
Expand Down Expand Up @@ -447,7 +447,6 @@ inline int groupId2TabIndex(int gid) {
return 0;
}

// changed
void MainWindow::on_tabWidget_currentChanged(int index) {
if (NekoRay::dataStore->refreshing_group_list) return;
if (tabIndex2GroupId(index) == NekoRay::dataStore->current_group) return;
Expand Down Expand Up @@ -503,7 +502,7 @@ void MainWindow::dialog_message_impl(const QString &sender, const QString &info)
}
}

// menu
// top bar & tray menu

inline bool dialog_is_using = false;

Expand Down Expand Up @@ -552,7 +551,7 @@ void MainWindow::on_menu_exit_triggered() {

core_process_killed = true;
hide();
exit_nekoray_core();
ExitNekorayCore();

if (exit_update) {
QDir::setCurrent(QApplication::applicationDirPath());
Expand All @@ -561,6 +560,57 @@ void MainWindow::on_menu_exit_triggered() {
qApp->quit();
}

void MainWindow::neko_set_spmode(int mode, bool save) {
if (mode != title_spmode) {
// DISABLE

if (title_spmode == NekoRay::SystemProxyMode::SYSTEM_PROXY) {
#ifndef NKR_NO_EXTERNAL
ClearSystemProxy();
#endif
} else if (title_spmode == NekoRay::SystemProxyMode::VPN) {
if (!StopVPNProcess()) {
refresh_status();
return;
}
}

// ENABLE

if (mode == NekoRay::SystemProxyMode::SYSTEM_PROXY) {
#ifndef NKR_NO_EXTERNAL
#ifdef Q_OS_WIN
if (mode == NekoRay::SystemProxyMode::SYSTEM_PROXY &&
!InRange(NekoRay::dataStore->inbound_http_port, 0, 65535)) {
auto btn = QMessageBox::warning(this, "NekoRay", tr("Http inbound is not enabled, can't set system proxy."),
"OK", tr("Settings"), "", 0, 0);
if (btn == 1) {
on_menu_basic_settings_triggered();
}
return;
}
#endif
SetSystemProxy("127.0.0.1",
NekoRay::dataStore->inbound_http_port,
NekoRay::dataStore->inbound_socks_port);
#endif
} else if (mode == NekoRay::SystemProxyMode::VPN) {
if (!StartVPNProcess()) {
refresh_status();
return;
}
}
}

if (save) {
NekoRay::dataStore->system_proxy_mode = mode;
NekoRay::dataStore->Save();
}

title_spmode = mode;
refresh_status();
}

void MainWindow::refresh_status(const QString &traffic_update) {
// From TrafficLooper
if (!traffic_update.isEmpty()) {
Expand All @@ -572,14 +622,15 @@ void MainWindow::refresh_status(const QString &traffic_update) {
return;
}
}
ui->label_speed->setText(traffic_update_cache);

// From UI
ui->label_speed->setText(traffic_update_cache);
if (last_test_time.addSecs(1) < QTime::currentTime()) {
if (last_test_time.addSecs(2) < QTime::currentTime()) {
ui->label_running->setText(tr("Running: %1").arg(running.isNull()
? tr("None")
: running->bean->DisplayName().left(50)));
}
//
auto display_http = tr("None");
if (InRange(NekoRay::dataStore->inbound_http_port, 0, 65535)) {
display_http = DisplayAddress(NekoRay::dataStore->inbound_address, NekoRay::dataStore->inbound_http_port);
Expand All @@ -589,10 +640,9 @@ void MainWindow::refresh_status(const QString &traffic_update) {
display_http
);
ui->label_inbound->setText(inbound_txt);
if (select_mode) {
ui->label_running->setText("[" + tr("Select") + "]");
}
//
ui->checkBox_VPN->setChecked(title_spmode == NekoRay::SystemProxyMode::VPN);
if (select_mode) ui->label_running->setText("[" + tr("Select") + "]");

auto make_title = [=](bool isTray) {
QStringList tt;
Expand All @@ -618,8 +668,7 @@ void MainWindow::refresh_status(const QString &traffic_update) {

// table显示

// update tab_index_GroupId
// refresh proxy list
// refresh_groups -> show_group -> refresh_proxy_list
void MainWindow::refresh_groups() {
NekoRay::dataStore->refreshing_group_list = true;

Expand Down Expand Up @@ -818,7 +867,7 @@ void MainWindow::on_menu_add_from_clipboard_triggered() {
}

void MainWindow::on_menu_move_triggered() {
auto ents = GetNowSelected();
auto ents = get_now_selected();
if (ents.isEmpty()) return;

auto items = QStringList{};
Expand All @@ -840,7 +889,7 @@ void MainWindow::on_menu_move_triggered() {
}

void MainWindow::on_menu_delete_triggered() {
auto ents = GetNowSelected();
auto ents = get_now_selected();
if (ents.count() == 0) return;
if (QMessageBox::question(this, tr("Confirmation"), QString(tr("Remove %1 item(s) ?")).arg(ents.count())) ==
QMessageBox::StandardButton::Yes) {
Expand All @@ -852,7 +901,7 @@ void MainWindow::on_menu_delete_triggered() {
}

void MainWindow::on_menu_reset_traffic_triggered() {
auto ents = GetNowSelected();
auto ents = get_now_selected();
if (ents.count() == 0) return;
if (QMessageBox::question(this,
tr("Confirmation"),
Expand All @@ -867,7 +916,7 @@ void MainWindow::on_menu_reset_traffic_triggered() {
}

void MainWindow::on_menu_profile_debug_info_triggered() {
auto ents = GetNowSelected();
auto ents = get_now_selected();
if (ents.count() != 1) return;
auto btn = QMessageBox::information(nullptr, "NekoRay", ents.first()->ToJsonBytes(), "OK", "Edit", "Reload", 0, 0);
if (btn == 1) {
Expand All @@ -881,7 +930,7 @@ void MainWindow::on_menu_profile_debug_info_triggered() {
}

void MainWindow::on_menu_copy_links_triggered() {
auto ents = GetNowSelected();
auto ents = get_now_selected();
QStringList links;
for (const auto &ent: ents) {
links += ent->bean->ToShareLink();
Expand All @@ -891,7 +940,7 @@ void MainWindow::on_menu_copy_links_triggered() {
}

void MainWindow::on_menu_export_config_triggered() {
auto ents = GetNowSelected();
auto ents = get_now_selected();
if (ents.count() != 1) return;
auto ent = ents.first();
auto result = NekoRay::BuildConfig(ent, false);
Expand All @@ -901,7 +950,7 @@ void MainWindow::on_menu_export_config_triggered() {
}

void MainWindow::display_qr_link(bool nkrFormat) {
auto ents = GetNowSelected();
auto ents = get_now_selected();
if (ents.count() != 1) return;

auto link = ents.first()->bean->ToShareLink();
Expand Down Expand Up @@ -1054,13 +1103,11 @@ void MainWindow::on_menu_remove_unavailable_triggered() {
}
}

// table 菜单弹出

void MainWindow::on_proxyListTable_customContextMenuRequested(const QPoint &pos) {
ui->menu_server->popup(ui->proxyListTable->viewport()->mapToGlobal(pos)); //弹出菜单
}

QMap<int, QSharedPointer<NekoRay::ProxyEntity>> MainWindow::GetNowSelected() {
QMap<int, QSharedPointer<NekoRay::ProxyEntity>> MainWindow::get_now_selected() {
auto items = ui->proxyListTable->selectedItems();
QMap<int, QSharedPointer<NekoRay::ProxyEntity>> map;
for (auto item: items) {
Expand All @@ -1071,50 +1118,6 @@ QMap<int, QSharedPointer<NekoRay::ProxyEntity>> MainWindow::GetNowSelected() {
return map;
}

void MainWindow::neko_set_spmode(int mode, bool save) {
#ifdef Q_OS_WIN
if (mode == NekoRay::SystemProxyMode::SYSTEM_PROXY && !InRange(NekoRay::dataStore->inbound_http_port, 0, 65535)) {
auto btn = QMessageBox::warning(this, "NekoRay", tr("Http inbound is not enabled, can't set system proxy."),
"OK", tr("Settings"), "", 0, 0);
if (btn == 1) {
on_menu_basic_settings_triggered();
}
return;
}
#endif

if (mode == NekoRay::SystemProxyMode::SYSTEM_PROXY) {
#ifndef NKR_NO_EXTERNAL
SetSystemProxy("127.0.0.1",
NekoRay::dataStore->inbound_http_port,
NekoRay::dataStore->inbound_socks_port);
#endif
} else if (mode == NekoRay::SystemProxyMode::VPN) {
if (!StartVPNProcess()) {
refresh_status();
return;
}
} else {
if (title_spmode == NekoRay::SystemProxyMode::SYSTEM_PROXY) {
#ifndef NKR_NO_EXTERNAL
ClearSystemProxy();
#endif
} else if (title_spmode == NekoRay::SystemProxyMode::VPN) {
if (!StopVPNProcess()) {
refresh_status();
return;
}
}
}

if (save) {
NekoRay::dataStore->system_proxy_mode = mode;
NekoRay::dataStore->Save();
}
title_spmode = mode;
refresh_status();
}

void MainWindow::keyPressEvent(QKeyEvent *event) {
switch (event->key()) {
case Qt::Key_Escape:
Expand Down Expand Up @@ -1162,7 +1165,6 @@ void MainWindow::show_log_impl(const QString &log) {
cursor.removeSelectedText();
continue;
}

break;
}
}
Expand Down
34 changes: 18 additions & 16 deletions ui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ Q_OBJECT

~MainWindow();

void dialog_message_impl(const QString &sender, const QString &info);

void refresh_proxy_list_impl(const int &id = -1, NekoRay::GroupSortAction groupSortAction = {});

void refresh_proxy_list(const int &id = -1) { refresh_proxy_list_impl(id, {}); };

void show_group(int gid);

void refresh_groups();

void refresh_status(const QString &traffic_update = "");
Expand All @@ -54,10 +52,6 @@ Q_OBJECT

void RegisterHotkey(bool unregister);

void HotkeyEvent(const QString &key);

bool exit_update = false;

signals:

void profile_selected(int id);
Expand Down Expand Up @@ -137,31 +131,39 @@ private slots:
//
int proxy_last_order = -1;
bool select_mode = false;
bool exit_update = false;

QMap<int, QSharedPointer<NekoRay::ProxyEntity>> get_now_selected();

void dialog_message_impl(const QString &sender, const QString &info);

QMap<int, QSharedPointer<NekoRay::ProxyEntity>> GetNowSelected();
void refresh_proxy_list_impl(const int &id = -1, NekoRay::GroupSortAction groupSortAction = {});

void keyPressEvent(QKeyEvent *event) override;

void closeEvent(QCloseEvent *event) override;

void show_group(int gid);
//

void HotkeyEvent(const QString &key);

bool StartVPNProcess();

bool StopVPNProcess();


// grpc and ...

static void exit_nekoray_core();
static void ExitNekorayCore();

void speedtest_current_group(int mode);

void test_current();

void setupGRPC();
void setup_grpc();

void CheckUpdate();

bool StartVPNProcess();

bool StopVPNProcess();

bool Tun2rayStartStop(bool start);

protected:
Expand Down
6 changes: 3 additions & 3 deletions ui/mainwindow_grpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using namespace NekoRay::rpc;
#endif

void MainWindow::setupGRPC() {
void MainWindow::setup_grpc() {
#ifndef NKR_NO_GRPC
// Setup Connection
defaultClient = new Client([=](const QString &errStr) {
Expand Down Expand Up @@ -198,14 +198,14 @@ void MainWindow::test_current() {
#endif
}

void MainWindow::exit_nekoray_core() {
void MainWindow::ExitNekorayCore() {
#ifndef NKR_NO_GRPC
NekoRay::rpc::defaultClient->Exit();
#endif
}

void MainWindow::neko_start(int _id) {
auto ents = GetNowSelected();
auto ents = get_now_selected();
auto ent = (_id < 0 && !ents.isEmpty()) ? ents.first() : NekoRay::profileManager->GetProfile(_id);
if (ent == nullptr) return;

Expand Down

0 comments on commit bf9db38

Please sign in to comment.