Skip to content

Commit

Permalink
Merge pull request #14 from enginkirmaci/develop
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
enginkirmaci authored Dec 20, 2023
2 parents 42fe73c + 0acb894 commit deacf36
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SnapIt.Common.Applications;

public class ApplicationInstance
public class AppInstance
{
private static readonly Mutex mutex = new Mutex(true, "{FF1FFB1E-5D42-4B8F-B42A-52DA1A1964B7}");

Expand Down
41 changes: 41 additions & 0 deletions SnapIt.Common/Applications/AppLauncher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace SnapIt.Common.Applications;

public class AppLauncher
{
public static void RunAsAdmin()
{
Run("runas", true);
}

public static void RunBypassSingleInstance()
{
Run("nosingle");
}

public static bool IsAdmin(string[] startupArgs)
{
return startupArgs.Any(arg => arg.Contains("runas"));
}

public static bool BypassSingleInstance(string[] startupArgs)
{
return startupArgs.Any(arg => arg.Contains("nosingle"));
}

private static void Run(string argument = null, bool useShellExecute = false)

Check warning on line 25 in SnapIt.Common/Applications/AppLauncher.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 25 in SnapIt.Common/Applications/AppLauncher.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 25 in SnapIt.Common/Applications/AppLauncher.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
var info = new ProcessStartInfo
{
UseShellExecute = useShellExecute,
FileName = Process.GetCurrentProcess().ProcessName // Application.ExecutablePath; // localAppDataPath + @"\microsoft\windowsapps\SnapIt.exe" // path to the appExecutionAlias
};

if (!string.IsNullOrEmpty(argument))
{
info.Verb = argument;
info.Arguments = $"-{argument}";
}

Process.Start(info);
}
}
21 changes: 0 additions & 21 deletions SnapIt.Common/Applications/RunAsAdministrator.cs

This file was deleted.

2 changes: 1 addition & 1 deletion SnapIt.Common/Dev.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class Dev
public const bool SkipLicense = false;
public const bool TestTrialEnded = false;
public const bool TestInTrial = false;
public const bool SkipRunAsAdmin = true;
public const bool SkipRunAsAdmin = false;
#else
public const bool IsActive = false;
public const bool ShowSnapWindowOnStartup = false;
Expand Down
11 changes: 7 additions & 4 deletions SnapIt.Services/KeyboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,15 @@ public async Task InitializeAsync()

public void SetSnappingStopped()
{
var map = new Dictionary<Combination, Action>
if (settingService.Settings != null)
{
{ Combination.FromString(settingService.Settings.StartStopShortcut.Replace(" ", string.Empty).Replace("Win", "LWin")), StartStopSnapping }
};
var map = new Dictionary<Combination, Action>
{
{ Combination.FromString(settingService.Settings.StartStopShortcut.Replace(" ", string.Empty).Replace("Win", "LWin")), StartStopSnapping }
};

globalHook?.OnCombination(map);
globalHook?.OnCombination(map);
}
}

public void Dispose()
Expand Down
4 changes: 2 additions & 2 deletions SnapIt.Test/App.xaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Application
x:Class="SnapIt.Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converter="clr-namespace:SnapIt.Common.Converters;assembly=SnapIt.Common"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
StartupUri="Window2.xaml">
StartupUri="Window3.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down
3 changes: 2 additions & 1 deletion SnapIt.Test/SnapIt.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@

<ItemGroup>
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.77" />
<PackageReference Include="Prism.DryIoc" Version="8.1.97" />
<PackageReference Include="Prism.DryIoc" Version="8.1.97" />
<PackageReference Include="WPF-UI" Version="3.0.0-preview.13" />
<PackageReference Include="WPF-UI.Tray" Version="3.0.0-preview.11" />
<PackageReference Include="MouseKeyHook" Version="5.7.1" />
</ItemGroup>

<ItemGroup>
Expand Down
16 changes: 16 additions & 0 deletions SnapIt.Test/Window3.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Window
x:Class="SnapIt.Test.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
Title="Window3"
Width="400"
Height="400"
Background="{ui:ThemeResource ApplicationBackgroundBrush}"
mc:Ignorable="d">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Click="Button_Click" Content="Select Browser" />
</Grid>
</Window>
200 changes: 200 additions & 0 deletions SnapIt.Test/Window3.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
//using System.Windows;
//using System.Windows.Automation;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation;
using Gma.System.MouseKeyHook;
using SnapIt.Common;
using SnapIt.Common.Entities;
using SnapIt.Services;

namespace SnapIt.Test;

public enum DragElement
{
None,
Window,
Tab
}

public partial class Window3 : Window
{
private IKeyboardMouseEvents globalHook;
private WinApiService winApiService;
private AutomationElementCollection firefoxes;
private AutomationElementCollection currentfirefoxes;
private ActiveWindow active;
private bool isListening = false;
private bool isDragging = false;
private DragElement element = DragElement.None;

public Window3()
{
InitializeComponent();

globalHook = Hook.GlobalEvents();

globalHook.MouseMove += GlobalHook_MouseMove;
globalHook.MouseDown += GlobalHook_MouseDown;
globalHook.MouseUp += GlobalHook_MouseUp;

globalHook.MouseDragStarted += GlobalHook_MouseDragStarted;
globalHook.MouseDragFinished += GlobalHook_MouseDragFinished;

winApiService = new WinApiService();
}

private void GlobalHook_MouseDragStarted(object? sender, System.Windows.Forms.MouseEventArgs e)
{
Dev.Log();
isDragging = true;
element = DragElement.None;
}

private void GlobalHook_MouseMove(object? sender, System.Windows.Forms.MouseEventArgs e)
{
if (isDragging)
{
if (element == DragElement.None)
{
var currentWindow = winApiService.GetActiveWindow();

if (currentWindow != null)
{
if (currentWindow.Boundry.Equals(active.Boundry))
{
element = DragElement.Tab;
}
else
{
element = DragElement.Window;
}
}
}

Dev.Log(element);
}
}

private void GlobalHook_MouseDragFinished(object? sender, System.Windows.Forms.MouseEventArgs e)
{
Dev.Log();
isDragging = false;
}

private void GlobalHook_MouseDown(object? sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
active = winApiService.GetActiveWindow();
if (active?.Title != null && active.Title.Contains("firefox", System.StringComparison.InvariantCultureIgnoreCase))
{
//isListening = true;
firefoxes = AutomationElement.RootElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaWindowClass"));

foreach (AutomationElement item in firefoxes)
{
Dev.Log($"{item.Current.NativeWindowHandle} - {item.Current.Name}");
}
}
}
}

private void GlobalHook_MouseUp(object? sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (firefoxes != null && firefoxes.Count > 0)
{
Application.Current.Dispatcher.Invoke(async () =>
{
await Task.Delay(1000);
currentfirefoxes = AutomationElement.RootElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaWindowClass"));

AutomationElement found = null;

if (firefoxes != null && currentfirefoxes != null && currentfirefoxes.Count > firefoxes.Count)
{
foreach (AutomationElement item in currentfirefoxes)
{
Dev.Log($"{item.Current.NativeWindowHandle} - {item.Current.Name}");
}

var dictionary = new Dictionary<int, AutomationElement>();
var currentDictionary = new Dictionary<int, AutomationElement>();

foreach (AutomationElement current in currentfirefoxes)
{
currentDictionary.Add(current.Current.NativeWindowHandle, current);
}

foreach (AutomationElement item in firefoxes)
{
dictionary.Add(item.Current.NativeWindowHandle, item);
}

var dict1 = currentDictionary.Except(dictionary);

found = dict1.FirstOrDefault().Value;
}

Dev.Log($"FOUND : {found?.Current.Name}, {found?.Current.NativeWindowHandle}"); //name can be null here

firefoxes = null;
currentfirefoxes = null;
});
}
}
}

private string GetBrowserURL()
{
//var list = AutomationElement.RootElement.FindAll(TreeScope.Children,
// new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaWindowClass"));

//AutomationElement root = AutomationElement.RootElement.FindFirst(TreeScope.Children,
// new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaWindowClass"));

//Condition toolBar = new AndCondition(
//new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar),
//new PropertyCondition(AutomationElement.NameProperty, "Browser tabs"));
//var tool = root.FindFirst(TreeScope.Children, toolBar);

//var tool2 = TreeWalker.ControlViewWalker.GetNextSibling(tool);

//var children = tool2.FindAll(TreeScope.Children, Condition.TrueCondition);

//foreach (AutomationElement item in children)
//{
// foreach (AutomationElement i in item.FindAll(TreeScope.Children, Condition.TrueCondition))
// {
// foreach (AutomationElement ii in i.FindAll(TreeScope.Element, Condition.TrueCondition))
// {
// //if (ii.Current.LocalizedControlType == "edit")
// {
// //if (!ii.Current.BoundingRectangle.X.ToString().Contains("empty"))
// {
// try
// {
// ValuePattern activeTab = ii.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
// var activeUrl = activeTab.Current.Value;
// }
// catch (Exception ex)
// {
// }
// }
// }
// }
// }
//}

return string.Empty;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
GetBrowserURL();
}
}
15 changes: 15 additions & 0 deletions SnapIt/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="SnapIt.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<SnapIt.Properties.Settings>
<setting name="RunAsAdmin" serializeAs="String">
<value>True</value>
</setting>
</SnapIt.Properties.Settings>
</userSettings>
</configuration>
Loading

0 comments on commit deacf36

Please sign in to comment.