Skip to content

Commit

Permalink
Merge pull request #3 from tsgsOFFICIAL/Feature/minimize-to-tray
Browse files Browse the repository at this point in the history
Feature - Minimize to tray V. 5.1.5.0🤓
  • Loading branch information
tsgsOFFICIAL authored Nov 3, 2024
2 parents 6efb3af + 81c141c commit c727486
Show file tree
Hide file tree
Showing 459 changed files with 399 additions and 372 deletions.
20 changes: 14 additions & 6 deletions CS2-AutoAccept/CS2-AutoAccept.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
<WindowsSdkPackageVersion>10.0.17763.46</WindowsSdkPackageVersion>
<RootNamespace>CS2_AutoAccept</RootNamespace>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
Expand All @@ -11,15 +12,15 @@
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<Description>A program to automatically find and press "ACCEPT" for you, when entering a competitive match in CS2.</Description>
<Copyright>©️ tsgsOFFICIAL - 2024</Copyright>
<FileVersion>5.1.4.5</FileVersion>
<FileVersion>5.1.5.0</FileVersion>
<AssemblyName>CS2-AutoAccept</AssemblyName>
</PropertyGroup>

<ItemGroup>
<Compile Remove="bin\Release\net6.0-windows10.0.17763.0\**" />
<EmbeddedResource Remove="bin\Release\net6.0-windows10.0.17763.0\**" />
<None Remove="bin\Release\net6.0-windows10.0.17763.0\**" />
<Page Remove="bin\Release\net6.0-windows10.0.17763.0\**" />
<Compile Remove="bin\Release\net6.0-windows10.0.17763.0\publish\**" />
<EmbeddedResource Remove="bin\Release\net6.0-windows10.0.17763.0\publish\**" />
<None Remove="bin\Release\net6.0-windows10.0.17763.0\publish\**" />
<Page Remove="bin\Release\net6.0-windows10.0.17763.0\publish\**" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -159,6 +160,7 @@
<None Include="bin\Release\net6.0-windows10.0.17763.0\publish\win-x86\fr\WindowsBase.resources.dll" />
<None Include="bin\Release\net6.0-windows10.0.17763.0\publish\win-x86\fr\WindowsFormsIntegration.resources.dll" />
<None Include="bin\Release\net6.0-windows10.0.17763.0\publish\win-x86\git2-a418d9d.dll" />
<None Include="bin\Release\net6.0-windows10.0.17763.0\publish\win-x86\Hardcodet.NotifyIcon.Wpf.dll" />
<None Include="bin\Release\net6.0-windows10.0.17763.0\publish\win-x86\hostfxr.dll" />
<None Include="bin\Release\net6.0-windows10.0.17763.0\publish\win-x86\hostpolicy.dll" />
<None Include="bin\Release\net6.0-windows10.0.17763.0\publish\win-x86\it\Microsoft.VisualBasic.Forms.resources.dll" />
Expand Down Expand Up @@ -225,7 +227,7 @@
<None Include="bin\Release\net6.0-windows10.0.17763.0\publish\win-x86\Microsoft.Win32.SystemEvents.dll" />
<None Include="bin\Release\net6.0-windows10.0.17763.0\publish\win-x86\Microsoft.Windows.SDK.NET.dll" />
<None Include="bin\Release\net6.0-windows10.0.17763.0\publish\win-x86\mscordaccore.dll" />
<None Include="bin\Release\net6.0-windows10.0.17763.0\publish\win-x86\mscordaccore_x86_x86_6.0.3324.36610.dll" />
<None Include="bin\Release\net6.0-windows10.0.17763.0\publish\win-x86\mscordaccore_x86_x86_6.0.3524.45918.dll" />
<None Include="bin\Release\net6.0-windows10.0.17763.0\publish\win-x86\mscordbi.dll" />
<None Include="bin\Release\net6.0-windows10.0.17763.0\publish\win-x86\mscorlib.dll" />
<None Include="bin\Release\net6.0-windows10.0.17763.0\publish\win-x86\mscorrc.dll" />
Expand Down Expand Up @@ -555,6 +557,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="2.0.1" />
<PackageReference Include="LibGit2Sharp" Version="0.30.0" />
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.3" />
<PackageReference Include="Tesseract" Version="5.2.0" />
Expand Down Expand Up @@ -582,4 +585,9 @@
</None>
</ItemGroup>

<ItemGroup>
<None Update="tessdata\**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
31 changes: 21 additions & 10 deletions CS2-AutoAccept/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Window x:Class="CS2_AutoAccept.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tb="http://www.hardcodet.net/taskbar"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="CS2_AutoAccept"
Expand Down Expand Up @@ -94,7 +95,7 @@
<WindowChrome.WindowChrome>
<WindowChrome GlassFrameThickness="1" ResizeBorderThickness="5" CaptionHeight="0"/>
</WindowChrome.WindowChrome>
<Grid>
<Grid>
<!-- Background -->
<Grid.Background>
<ImageBrush ImageSource="/background.png"></ImageBrush>
Expand Down Expand Up @@ -165,17 +166,27 @@
</Button.Resources>
</Button>

<!-- Socials -->
<TextBlock Text="Join the Discord!" FontFamily="Roboto" FontSize="24" Foreground="GhostWhite" TextAlignment="Center"/>
<Button x:Name="Button_Discord" Background="#FF7289DA" Cursor="Hand" Click="Button_Click_Discord" ToolTip="Join my Discord server, its a great place for asking questions and hanging out :)" BorderThickness="0">
<Image Source="/Discord white.png"/>
<!-- Socials -->
<TextBlock Text="Join the Discord!" FontFamily="Roboto" FontSize="24" Foreground="GhostWhite" TextAlignment="Center"/>
<Button x:Name="Button_Discord" Background="#FF7289DA" Cursor="Hand" Click="Button_Click_Discord" ToolTip="Join my Discord server, its a great place for asking questions and hanging out :)" BorderThickness="0">
<Image Source="/Discord white.png"/>

<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5"/>
</Style>
</Button.Resources>
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5"/>
</Style>
</Button.Resources>
</Button>
</StackPanel>

<!-- NotifyIcon, that minimizes to system tray -->
<tb:TaskbarIcon x:Name="MyNotifyIcon" IconSource="/logo.png" ToolTipText="CS2 AutoAccept by tsgsOFFICIAL" DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}" Visibility="Collapsed">
<tb:TaskbarIcon.ContextMenu>
<ContextMenu>
<MenuItem Header="Minimize" Command="{Binding ToggleWindowCommand }" />
<MenuItem Header="Exit" Command="{Binding CloseCommand}" />
</ContextMenu>
</tb:TaskbarIcon.ContextMenu>
</tb:TaskbarIcon>
</Grid>
</Window>
207 changes: 97 additions & 110 deletions CS2-AutoAccept/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using System.Windows.Media;
using CS2_AutoAccept.Models;
using System.Threading.Tasks;
using System.Windows.Interop;
using System.Net.Http.Headers;
using System.Collections.Generic;
using System.Runtime.InteropServices;
Expand All @@ -39,25 +38,13 @@ public partial class MainWindow : Window
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
#endregion
#region DLL Imports For Keybinds
private const int WM_HOTKEY = 0x0312;

[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

private HwndSource _source;
private int _hotkeyPlusId;
private int _hotkeyMinusId;
#endregion
#endregion

private Updater? updater;
private Screen? _activeScreen;
private Thread? _scannerThread;
private CancellationTokenSource? cts;
private bool _scannerIsActive = false;
private bool _run_Continuously = false;
private bool _updateAvailable = false;
private bool _updateFailed = false;
Expand All @@ -75,11 +62,30 @@ public partial class MainWindow : Window
private string _basePath;
private string _updatePath;
private readonly bool debugMode = false;
public ICommand ToggleWindowCommand { get; }
public ICommand CloseCommand { get; }
private bool _isTrayIconVisible;

public bool IsTrayIconVisible
{
get => _isTrayIconVisible;
set
{
_isTrayIconVisible = value;
UpdateTrayIconVisibility();
}
}
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
Closed += MainWindow_Closed!;
IsTrayIconVisible = true;
DataContext = this;

ToggleWindowCommand = new RelayCommand(o => ToggleWindowState());
CloseCommand = new RelayCommand(o => CloseApplication());

// Event handler for double-click on TaskbarIcon
MyNotifyIcon.TrayMouseDoubleClick += OnTrayIconDoubleClick;

// Access command line arguments
string[] args = Environment.GetCommandLineArgs();
Expand Down Expand Up @@ -214,7 +220,59 @@ public MainWindow()
#endregion

}
private void ToggleWindowState()
{
if (WindowState == WindowState.Minimized)
{
Show();
WindowState = WindowState.Normal;
}
else
{
WindowState = WindowState.Minimized;
Hide();
}
}

private void CloseApplication()
{
Close();
}
private void UpdateTrayIconVisibility()
{
// Show or hide the tray icon based on IsTrayIconVisible
MyNotifyIcon.Visibility = IsTrayIconVisible ? Visibility.Visible : Visibility.Collapsed;
}
#region EventHandlers
protected override void OnStateChanged(EventArgs e)
{
base.OnStateChanged(e);

// Find context menu items and update texts based on WindowState
var menu = (System.Windows.Controls.ContextMenu)MyNotifyIcon.ContextMenu;
var toggleMenuItem = (System.Windows.Controls.MenuItem)menu.Items[0];

if (WindowState == WindowState.Minimized)
{
string toolTipText = _scannerIsActive == true ? "AutoAccept is running in the background" : "CS2 AutoAccept is minimized";

Hide();
ShowNotification("CS2 AutoAccept", toolTipText);
toggleMenuItem.Header = "Restore";
MyNotifyIcon.ToolTipText = toolTipText;
}
else
{
toggleMenuItem.Header = "Minimize";
MyNotifyIcon.ToolTipText = "CS2 AutoAccept by tsgsOFFICIAL";
}
}
private void OnTrayIconDoubleClick(object sender, RoutedEventArgs e)
{
// Show the window and restore it to normal state
Show();
WindowState = WindowState.Normal;
}
/// <summary>
/// Event handler for download progress
/// </summary>
Expand Down Expand Up @@ -282,7 +340,7 @@ public void Button_Click_Maximize(object sender, RoutedEventArgs e)
/// <param Name="e"></param>
private void Button_Click_Close(object sender, RoutedEventArgs e)
{
this.Close();
Close();
}
/// <summary>
/// Open Github to download the newest version
Expand Down Expand Up @@ -352,6 +410,7 @@ private void Program_state_Checked(object sender, RoutedEventArgs e)
_scannerThread = new Thread(new ParameterizedThreadStart(Scanner)) { IsBackground = true };
cts = new CancellationTokenSource();
_scannerThread.Start(cts!.Token);
_scannerIsActive = true;
// Change to a brighter color
Program_state.Foreground = new SolidColorBrush(Colors.LawnGreen);
Program_state.Content = "AutoAccept (ON)";
Expand All @@ -366,6 +425,7 @@ private void Program_state_Unchecked(object sender, RoutedEventArgs e)
// PrintToLog("{Program_state_Unchecked}");
cts!.Cancel();
Program_state_continuously.IsChecked = false;
_scannerIsActive = false;

// Change to a darker color
Program_state.Foreground = new SolidColorBrush(Colors.Red);
Expand Down Expand Up @@ -478,98 +538,6 @@ private void WindowSizeChangedEventHandler(object sender, SizeChangedEventArgs e
}
#endregion
/// <summary>
/// Listens for keybinds
/// </summary>
/// <param name="hwnd"></param>
/// <param name="msg"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <param name="handled"></param>
/// <returns></returns>
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// Keybinds available are:
// NumPad + = Start AutoAccept
// Numpad ++ = Start AutoAccept 24/7
// NumPad - = Stop AutoAccept
if (msg == WM_HOTKEY)
{
int id = wParam.ToInt32();

if (id == _hotkeyPlusId)
{
Dispatcher.Invoke(() =>
{
if (Program_state.IsEnabled)
{
// If the program is already running
if (Program_state.IsChecked ?? false)
{
Program_state_continuously.IsChecked = true;
ShowNotification("AutoAccept 24/7 activated!", "AutoAccept 24/7 has been activated!");
}
else
{
Program_state.IsChecked = true;
ShowNotification("AutoAccept activated!", "AutoAccept has been activated!");
}
}
});
}
else if (id == _hotkeyMinusId)
{
Dispatcher.Invoke(() =>
{
if (Program_state.IsEnabled)
{
if (Program_state.IsChecked ?? false)
{
Program_state.IsChecked = false;
ShowNotification("AutoAccept canceled!", "AutoAccept has been canceled!");
}
}
});
}
}

return IntPtr.Zero;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
IntPtr hWnd = new WindowInteropHelper(this).Handle;
_source = HwndSource.FromHwnd(hWnd);
_source.AddHook(WndProc);

// Register '+' hotkey
const uint MOD_NONE = 0x0000;
const uint VK_PLUS = 0xBB; // OEM '+'
const uint VK_NUMPAD_PLUS = 0x6B; // Numpad '+'

_hotkeyPlusId = GetHashCode();
RegisterHotKey(hWnd, _hotkeyPlusId, MOD_NONE, VK_PLUS);
RegisterHotKey(hWnd, _hotkeyPlusId, MOD_NONE, VK_NUMPAD_PLUS);

// Register '-' hotkey
const uint VK_MINUS = 0xBD; // OEM '-'
const uint VK_NUMPAD_MINUS = 0x6D; // Numpad '-'

_hotkeyMinusId = GetHashCode() + 1; // Use a different id for '-' hotkey
RegisterHotKey(hWnd, _hotkeyMinusId, MOD_NONE, VK_MINUS);
RegisterHotKey(hWnd, _hotkeyMinusId, MOD_NONE, VK_NUMPAD_MINUS);
}

private void MainWindow_Closed(object sender, EventArgs e)
{
if (_source != null)
{
_source.RemoveHook(WndProc);
_source = null!;
}

UnregisterHotKey(new WindowInteropHelper(this).Handle, _hotkeyPlusId);
UnregisterHotKey(new WindowInteropHelper(this).Handle, _hotkeyMinusId);
}
/// <summary>
/// Restore the window size, if it was previously opened & changed
/// </summary>
private void RestoreSizeIfSaved()
Expand Down Expand Up @@ -1306,4 +1274,23 @@ private void ShowNotification(string title, string message)
});
}
}
}

public class RelayCommand : ICommand
{
private readonly Action<object> _execute;
private readonly Func<object, bool> _canExecute;

public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
_execute = execute;
_canExecute = canExecute;
}

public bool CanExecute(object parameter) => _canExecute == null || _canExecute(parameter);

public void Execute(object parameter) => _execute(parameter);

public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}
Binary file not shown.
Loading

0 comments on commit c727486

Please sign in to comment.