add tray
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
bin/
|
||||
obj/
|
||||
.idea
|
||||
.vs
|
||||
*.user
|
||||
+40
-3
@@ -1,6 +1,5 @@
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Windows;
|
||||
using System.Windows;
|
||||
using Application = System.Windows.Application;
|
||||
|
||||
namespace HttpKeys;
|
||||
|
||||
@@ -9,5 +8,43 @@ namespace HttpKeys;
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
private NotifyIcon _trayIcon;
|
||||
|
||||
protected override void OnStartup(StartupEventArgs e)
|
||||
{
|
||||
base.OnStartup(e);
|
||||
|
||||
_trayIcon = new NotifyIcon
|
||||
{
|
||||
Icon = new System.Drawing.Icon("app.ico"),
|
||||
Visible = true,
|
||||
Text = "My WPF Tray App"
|
||||
};
|
||||
|
||||
var menu = new ContextMenuStrip();
|
||||
menu.Items.Add("Открыть", null, (s, ev) => ShowMainWindow());
|
||||
menu.Items.Add("Выход", null, (s, ev) => ExitApplication());
|
||||
|
||||
_trayIcon.ContextMenuStrip = menu;
|
||||
_trayIcon.DoubleClick += (s, ev) => ShowMainWindow();
|
||||
}
|
||||
|
||||
private void ShowMainWindow()
|
||||
{
|
||||
if (MainWindow == null)
|
||||
MainWindow = new MainWindow();
|
||||
|
||||
MainWindow.ShowInTaskbar = true; // вернуть в панель задач
|
||||
MainWindow.Show();
|
||||
MainWindow.WindowState = WindowState.Normal;
|
||||
MainWindow.Activate();
|
||||
}
|
||||
|
||||
private void ExitApplication()
|
||||
{
|
||||
_trayIcon.Visible = false;
|
||||
_trayIcon.Dispose();
|
||||
Shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,13 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UseWPF>true</UseWPF>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="app.ico">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace HttpKeys;
|
||||
|
||||
|
||||
class Params
|
||||
{
|
||||
public string ActionId { get; set; } = null!;
|
||||
public string State { get; set; } = null!;
|
||||
public bool ToggleChanged { get; set; }
|
||||
}
|
||||
|
||||
|
||||
public class Listener
|
||||
{
|
||||
public event Action<string> Log;
|
||||
|
||||
public async Task Run()
|
||||
{
|
||||
var listener = new HttpListener();
|
||||
listener.Prefixes.Add("http://127.0.0.1:16888/");
|
||||
|
||||
listener.Start();
|
||||
Log("Listening on http://127.0.0.1:16888/");
|
||||
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true // чтобы Email/email/EMAIL не ломалось
|
||||
};
|
||||
|
||||
while (true)
|
||||
{
|
||||
var ctx = await listener.GetContextAsync();
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
if (ctx.Request.RawUrl != "/press")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
using var reader = new StreamReader(
|
||||
ctx.Request.InputStream,
|
||||
ctx.Request.ContentEncoding
|
||||
);
|
||||
|
||||
var body = await reader.ReadToEndAsync();
|
||||
var p = JsonSerializer.Deserialize<Params>(body, options);
|
||||
if (p == null)
|
||||
continue;
|
||||
|
||||
Log("---- REQUEST ----");
|
||||
Log("URL: " + p.ActionId);
|
||||
Log("URL: " + p.State);
|
||||
Log("URL: " + p.ToggleChanged);
|
||||
|
||||
var responseBytes = "OK"u8.ToArray();
|
||||
ctx.Response.StatusCode = 200;
|
||||
ctx.Response.ContentType = "text/plain; charset=utf-8";
|
||||
await ctx.Response.OutputStream.WriteAsync(responseBytes);
|
||||
ctx.Response.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log("Error: " + ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,12 @@
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:HttpKeys"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="450" Width="800">
|
||||
Title="MainWindow" Height="450" Width="800"
|
||||
ShowInTaskbar="True">
|
||||
<Grid>
|
||||
|
||||
<ListBox x:Name="LogList"
|
||||
FontFamily="Consolas"
|
||||
FontSize="13"
|
||||
ItemsSource="{Binding Logs}" />
|
||||
</Grid>
|
||||
</Window>
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System.Text;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
@@ -16,8 +18,34 @@ namespace HttpKeys;
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public ObservableCollection<string> Logs { get; } = new();
|
||||
public Listener _listener;
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
_listener = new Listener();
|
||||
_listener.Log += Log;
|
||||
|
||||
InitializeComponent();
|
||||
DataContext = this;
|
||||
Closing += MainWindow_Closing;
|
||||
Hide();
|
||||
ShowInTaskbar = false; // убрать из панели задач
|
||||
|
||||
Task.Run(async () => await _listener.Run());
|
||||
}
|
||||
|
||||
private void MainWindow_Closing(object? sender, CancelEventArgs e)
|
||||
{
|
||||
e.Cancel = true;
|
||||
|
||||
Hide();
|
||||
ShowInTaskbar = false; // убрать из панели задач
|
||||
}
|
||||
|
||||
private void Log(string log)
|
||||
{
|
||||
Dispatcher.Invoke(() => Logs.Add($"{DateTime.Now}:: {log}"));
|
||||
Console.WriteLine(log);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 66 KiB |
Reference in New Issue
Block a user