Skip to content

Commit

Permalink
Add feature to close all Tiefsee
Browse files Browse the repository at this point in the history
  • Loading branch information
hbl917070 committed Mar 18, 2024
1 parent fffd45f commit 598b397
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@

TiefseeLauncher/x64
TiefseeLauncher/Resource.aps
TiefseeLauncher/Release
9 changes: 2 additions & 7 deletions BuildAll/BuildAll.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

<!-- 編譯這些專案 -->
<Exec Command="dotnet publish ../TiefseeLauncherDll/TiefseeLauncherDll.csproj -r win-x64 -c Release -o ../Output" />
<MSBuild Projects="../TiefseeLauncher/TiefseeLauncher.vcxproj" />
<MSBuild Projects="../Tiefsee/Tiefsee.csproj" />
<MSBuild Projects="../TiefseeLauncher/TiefseeLauncher.vcxproj" Properties="Configuration=Release" />
<MSBuild Projects="../Tiefsee/Tiefsee.csproj" Properties="Configuration=Release"/>

<!-- 刪除無用檔案 -->
<Delete Files="$(MSBuildProjectDirectory)/../Output/Tiefsee.pdb" />
Expand All @@ -40,11 +40,6 @@
<Delete Files="$(MSBuildProjectDirectory)/../Output/BuildAll.deps.json" />
</Target>


<Target Name="PreBuild" BeforeTargets="PreBuildEvent">

</Target>

<!-- 打包成 zip -->
<Target Name="ZipOutputPath" AfterTargets="Build">
<ZipDirectory Overwrite="true" SourceDirectory="$(MSBuildProjectDirectory)/../Output" DestinationFile="$(MSBuildProjectDirectory)/../Tiefsee.zip" />
Expand Down
8 changes: 7 additions & 1 deletion Tiefsee/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,19 @@ static void Main(string[] args) {
var args2 = AppInstance.GetActivatedEventArgs();
if (args2 != null) {
if (args2.Kind == ActivationKind.StartupTask) {
args = new string[] { "none" };
args = ["none"];
}
}
}
catch { }
}

// 啟動參數是 closeAll
if (args.Length == 1 && args[0] == "closeAll") {
QuickRun.CloseAllWindow();
return;
}

bool argsIsNone = (args.Length == 1 && args[0] == "none"); // 啟動參數是 none

if (args.Length >= 1 && args[0] == "restart") { // 啟動參數是 restart
Expand Down
131 changes: 74 additions & 57 deletions Tiefsee/QuickRun.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.IO;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Text;

namespace Tiefsee;

Expand Down Expand Up @@ -33,14 +33,73 @@ public static void WindowFreed() {
/// </summary>
public static bool Check(string[] args) {

if (Program.startType == 1) { // 直接啟動
// 直接啟動
if (Program.startType == 1) {
return false;
}

// 找不到記錄 port 的資料夾
if (Directory.Exists(AppPath.appDataPort) == false) {
return false;
}

int port = GetPort();

// 沒有可以正常請求的 port
if (port == -1) {
return false;
}

NewWindow(args, port);
return true;
}

/// <summary>
/// 結束程式
/// </summary>
public static void Exit() {
runNumber = 0;
WindowFreed();
}

/// <summary>
/// 關閉全部的視窗
/// </summary>
public static void CloseAllWindow() {

// 如果是 直接啟動,直接強制結束所有 process
if (Program.startType == 1) {
Process[] proc = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
for (int i = proc.Length - 1; i >= 0; i--) {
try {
if (proc[i].Id == Process.GetCurrentProcess().Id)
continue;
proc[i].Kill(); // 關閉執行中的程式
}
catch { }
}

// 刪除所有的 port 檔案
foreach (string filePort in Directory.GetFiles(AppPath.appDataPort, "*")) {
try {
File.Delete(filePort);
continue;
}
catch { }
}
return;
}

int port = GetPort();
string uri = $"http://127.0.0.1:{port}/api/closeAllWindow";
SendRequest(uri);
}

/// <summary>
///
/// </summary>
private static int GetPort() {

foreach (string filePort in Directory.GetFiles(AppPath.appDataPort, "*")) { // 判斷目前已經開啟的視窗

try {
Expand All @@ -56,77 +115,35 @@ public static bool Check(string[] args) {
string port = Path.GetFileName(filePort);
// 偵測是否可用
string uri = $"http://127.0.0.1:{port}/api/check";
using (HttpClient client = new HttpClient()) {
client.Timeout = TimeSpan.FromSeconds(5); // 逾時
client.DefaultRequestHeaders.Add("User-Agent", Program.webvviewUserAgent);

HttpResponseMessage response = client.GetAsync(uri).Result;
}

if (Program.startType == 2) { // 快速啟動
NewWindow(args, port);
return true;
}
SendRequest(uri);

if (Program.startType == 3) { // 快速啟動且常駐
NewWindow(args, port);
return true;
}

if (Program.startType == 4) { // 單一執行個體
NewWindow(args, port);
return true;
}

if (Program.startType == 5) { // 單一執行個體且常駐
NewWindow(args, port);
return true;
}
return Int32.Parse(port);
}
catch { }

File.Delete(filePort); // 如果這個 port 超過時間沒有回應,就當做無法使用,將檔案刪除

}

return false;
return -1;
}

/// <summary>
/// 關閉全部的視窗 (結束程式)
///
/// </summary>
public static void CloseAllWindow() {
runNumber = 0;
WindowFreed();
private static void NewWindow(string[] args, int port) {
string base64 = Uri.EscapeDataString(string.Join("\n", args));
string uri = $"http://127.0.0.1:{port}/api/newWindow?path=" + base64;
SendRequest(uri);
}

/// <summary>
///
/// 發送 http 請求
/// </summary>
/// <param name="args"></param>
/// <param name="port"></param>
private static void NewWindow(string[] args, string port) {

// 啟動參數
StringBuilder sb = new();
for (int i = 0; i < args.Length; i++) {
if (i != args.Length - 1) {
sb.Append(args[i] + "\n");
}
else {
sb.Append(args[i]);
}
}

// 開啟新視窗
string base64 = Uri.EscapeDataString(sb.ToString());
string uri = $"http://127.0.0.1:{port}/api/newWindow?path=" + base64;

using (HttpClient client = new HttpClient()) {
private static Task<string> SendRequest(string uri) {
using (HttpClient client = new()) {
client.Timeout = TimeSpan.FromSeconds(5); // 逾時
client.DefaultRequestHeaders.Add("User-Agent", Program.webvviewUserAgent);
HttpResponseMessage response = client.GetAsync(uri).Result;
// string responseContent = response.Content.ReadAsStringAsync().Result;
return response.Content.ReadAsStringAsync();
}
}

Expand Down
11 changes: 11 additions & 0 deletions Tiefsee/Server/WebServerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public WebServerController(WebServer ws) {

webServer.RouteAdd("/api/check", Ckeck);
webServer.RouteAdd("/api/newWindow", NewWindow);
webServer.RouteAdd("/api/closeAllWindow", CloseAllWindow);

webServer.RouteAdd("/api/getExif", GetExif);
webServer.RouteAdd("/api/getPdf", GetPdf);
Expand Down Expand Up @@ -293,6 +294,16 @@ private void NewWindow(RequestData d) {
WriteString(d, "ok");
}

/// <summary>
/// 關閉全部的視窗
/// </summary>
private void CloseAllWindow(RequestData d) {
Adapter.UIThread(() => {
WebWindow.CloseAllWindow();
});
WriteString(d, "ok");
}

/// <summary>
///
/// </summary>
Expand Down
28 changes: 26 additions & 2 deletions Tiefsee/StartWindow.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Web.WebView2.Core;
using System.IO;
using System.Windows.Input;
using Windows.UI.StartScreen;

namespace Tiefsee;

Expand All @@ -27,8 +28,9 @@ public StartWindow() {
Adapter.Initialize();
Plugin.Init();

PortLock(); // 寫入檔案,表示此port已經被佔用
CheckWebView2(); // 檢查是否有webview2執行環境
PortLock(); // 寫入檔案,表示此 port 已經被佔用
CheckWebView2(); // 檢查是否有 webview2 執行環境
InitJumpTask(); // 初始化 JumpTask

//--------------

Expand Down Expand Up @@ -209,6 +211,28 @@ public void RunNotifyIcon() {
nIcon.ContextMenuStrip = cm;
}

/// <summary>
///
/// </summary>
public async void InitJumpTask() {

// 獲取默認的 JumpList
var jumpList = await Windows.UI.StartScreen.JumpList.LoadCurrentAsync();

// 清除默認的 JumpList
jumpList.Items.Clear();

if (Program.startType != 4 && Program.startType != 5) {
var item = JumpListItem.CreateWithArguments("closeAll", "Close all Tiefsee");
// item.Description = "Close all Tiefsee";
// item.Logo = new Uri("ms-appx:///t1.ico");
jumpList.Items.Add(item);
}

// 保存 JumpList
await jumpList.SaveAsync();
}

/// <summary>
/// 初始化webview2
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions Tiefsee/VW/WV_Window.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -66,10 +66,10 @@ public WebWindow NewSubWindow(string url, object[] args) {
}

/// <summary>
/// 關閉全部的視窗 (結束程式)
/// 關閉全部的視窗
/// </summary>
public void CloseAllWindow() {
QuickRun.CloseAllWindow();
public void Exit() {
QuickRun.Exit();
}

/// <summary>
Expand Down
20 changes: 20 additions & 0 deletions Tiefsee/WebWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class WebWindow : FormNone {
private bool isShow = false; // 是否已經顯式過視窗(用於單一啟動
public bool isDelayInit = false; // 是否延遲初始化(暫存視窗必須設定成true

public static List<WebWindow> webWindowList = new(); // 用於記錄所有視窗

// 用於記錄全螢幕前的狀態
private FormWindowState tempFormWindowState = FormWindowState.Normal;
private bool tempFullScreen = false;
Expand All @@ -33,6 +35,11 @@ public class WebWindow : FormNone {
///
/// </summary>
public WebWindow() {
webWindowList.Add(this);

this.FormClosed += (sender, e) => {
webWindowList.Remove(this);
};
}

/// <summary>
Expand Down Expand Up @@ -193,6 +200,19 @@ private static string GetHtmlFilePath(string fileName) {
return p;
}

/// <summary>
/// 關閉所有視窗
/// </summary>
public static void CloseAllWindow() {
int count = webWindowList.Count;
// 從後往前關閉,避免關閉後,webWindowList 的數量減少
for (int i = count - 1; i >= 0; i--) {
var item = webWindowList[i];
if (item == null || item.IsDisposed || item == tempWindow) { continue; }
webWindowList[i].Close();
}
}

/// <summary>
/// 觸發 js 的 baseWindow.onCreate
/// </summary>
Expand Down
Loading

0 comments on commit 598b397

Please sign in to comment.