Skip to content

Commit

Permalink
Add v1.0 code files & setup tray icon and hidden window.
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementGre committed Nov 11, 2023
1 parent b231dd8 commit 8182676
Show file tree
Hide file tree
Showing 15 changed files with 692 additions and 288 deletions.
119 changes: 74 additions & 45 deletions App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,79 @@
using Microsoft.UI.Xaml;
using H.NotifyIcon;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Xaml.Shapes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace ThreeFingersDragOnWindows
using System.Diagnostics;
using ThreeFingersDragOnWindows.src.settings;
using ThreeFingersDragOnWindows.src.touchpad;
using ThreeFingersDragOnWindows.src.Utils;

namespace ThreeFingersDragOnWindows;

public partial class App : Application
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : Application

public static SettingsData SettingsData;
private SettingsWindow _settingsWindow;


public HandlerWindow HandlerWindow;


public App()
{
Debug.WriteLine("Starting ThreeFingersDragOnWindows...");
this.InitializeComponent();

SettingsData = SettingsData.load();


HandlerWindow = new HandlerWindow(this);
//if (SettingsData.IsFirstRun) ShowSettingsWindow();


//_notifyIcon.Icon = new Icon("Resources/icon.ico");
//_notifyIcon.Text = "ThreeFingersDragOnWindows";
//_notifyIcon.Click += (_, _) => ShowSettingsWindow();

//var button = new ToolStripButton("Quit");
//button.Click += (_, _) => Quit();
//_notifyIcon.ContextMenuStrip = new ContextMenuStrip();
//_notifyIcon.ContextMenuStrip.Items.Add(button);
//_notifyIcon.Visible = true;
}


public void OpenSettingsWindow()
{
_settingsWindow = new SettingsWindow(this);
_settingsWindow.Activate();
}

public void OnClosePrefsWindow()
{
SettingsData = null;
}

public void Quit()
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
}

/// <summary>
/// Invoked when the application is launched.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new SettingsWindow();
m_window.Activate();
}

private Window m_window;
HandlerWindow?.Close();
_settingsWindow?.Close();
}

public void OnTouchpadContact(TouchpadContact[] contacts)
{
_settingsWindow?.OnTouchpadContact(contacts);
}

public bool DoTouchpadExist()
{
return HandlerWindow.TouchpadExists;
}

public bool DoTouchpadRegistered()
{
return HandlerWindow.TouchpadRegistered;
}


}

Binary file modified Assets/icon.ico
Binary file not shown.
Binary file added Assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions SettingsData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
using Windows.Storage;

namespace ThreeFingersDragOnWindows.src.settings;

public class SettingsData
{
public bool AllowReleaseAndRestart { get; set; } = true;
public int ReleaseDelay { get; set; } = 500;

public bool ThreeFingersMove { get; set; } = true;
public float MouseSpeed { get; set; } = 30;
public float MouseAcceleration { get; set; } = 1;

public static bool IsFirstRun { get; set; } = true;

public static SettingsData load()
{
var mySerializer = new XmlSerializer(typeof(SettingsData));
var myFileStream = new FileStream(getPath(true), FileMode.Open);
var up = (SettingsData) mySerializer.Deserialize(myFileStream);
myFileStream.Close();
return up;
}

public void save()
{
var mySerializer = new XmlSerializer(typeof(SettingsData));
var myWriter = new StreamWriter(getPath(false));
mySerializer.Serialize(myWriter, this);
myWriter.Close();
}

private static string getPath(bool createIfEmpty)
{
var dirPath = ApplicationData.Current.LocalFolder.Path;
var filePath = Path.Combine(dirPath, "preferences.xml");

if (!Directory.Exists(dirPath) || !File.Exists(filePath))
{
Debug.WriteLine("First run: creating settings file");
Directory.CreateDirectory(dirPath);
IsFirstRun = true;
if (createIfEmpty) new SettingsData().save();
}

return filePath;
}
}
9 changes: 4 additions & 5 deletions SettingsWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>

<Window
x:Class="ThreeFingersDragOnWindows.SettingsWindow"
x:Class="ThreeFingersDragOnWindows.src.settings.SettingsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ThreeFingersDragOnWindows"
xmlns:local="using:ThreeFingersDragOnWindows.src.settings"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
mc:Ignorable="d"
Closed="Window_Closed">


<RelativePanel>
Expand Down
121 changes: 96 additions & 25 deletions SettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,107 @@
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ViewManagement;

namespace ThreeFingersDragOnWindows
using System.Diagnostics;
using ThreeFingersDragOnWindows.src.Utils;

namespace ThreeFingersDragOnWindows.src.settings;


public sealed partial class SettingsWindow : Window
{

public sealed partial class SettingsWindow : Window
private readonly App _app;

public SettingsWindow(App app)
{
public SettingsWindow()
{
this.InitializeComponent();
ExtendsContentIntoTitleBar = true; // enable custom titlebar
SetTitleBar(TitleBar); // set user ui element as titlebar
}
_app = app;
Debug.WriteLine("Starting SettingssWindow...");


this.InitializeComponent();
//ExtendsContentIntoTitleBar = true; // enable custom titlebar
//SetTitleBar(TitleBar); // set TitleBar element as titlebar

/* TouchpadExists.Text = _app.DoTouchpadExist() ? "Yes" : "No";
TouchpadRegistered.Text = _app.DoTouchpadRegistered() ? "Yes" : "No";
AllowReleaseAndRestart.IsChecked = App.Prefs.AllowReleaseAndRestart;
AllowReleaseAndRestart.Checked += (_, _) => App.Prefs.AllowReleaseAndRestart = true;
AllowReleaseAndRestart.Unchecked += (_, _) => App.Prefs.AllowReleaseAndRestart = false;
ReleaseDelay.Text = App.Prefs.ReleaseDelay.ToString();
ReleaseDelay.TextChanged += (_, _) => {
if (!int.TryParse(ReleaseDelay.Text, out var delay))
{
ReleaseDelay.Text = App.Prefs.ReleaseDelay.ToString();
return;
}
App.Prefs.ReleaseDelay = delay;
};
ThreeFingersMove.IsChecked = App.Prefs.ThreeFingersMove;
ThreeFingersMove.Checked += (_, _) => App.Prefs.ThreeFingersMove = true;
ThreeFingersMove.Unchecked += (_, _) => App.Prefs.ThreeFingersMove = false;
MouseSpeed.Value = App.Prefs.MouseSpeed;
MouseSpeed.ValueChanged += (_, _) => App.Prefs.MouseSpeed = (float)MouseSpeed.Value;
MouseAcceleration.Value = App.Prefs.MouseAcceleration;
MouseAcceleration.ValueChanged += (_, _) => App.Prefs.MouseAcceleration = (float)MouseAcceleration.Value; */
}


private void NavigationView_SelectionChanged(object sender, NavigationViewSelectionChangedEventArgs e)
{
Debug.WriteLine("Selection Changed");
}


////////// Close & quit //////////

private void NavigationView_SelectionChanged(object sender, NavigationViewSelectionChangedEventArgs e)
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Close();
}

private void QuitButton_Click(object sender, RoutedEventArgs e)
{
_app.Quit();
}

private void Window_Closed(object sender, WindowEventArgs e)
{
Debug.WriteLine("Hiding PrefsWindow, saving data...");
//App.SettingsData.save();
//_app.OnClosePrefsWindow();
}


////////// UI Tools //////////

/*private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
var regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}*/

private long _lastContact = 0;
private int _inputCount = 0;
private long _lastEventSpeed = 0;
public void OnTouchpadContact(TouchpadContact[] contacts)
{
_inputCount++;

if (_inputCount >= 20)
{
Console.WriteLine("Selection Changed");
_inputCount = 0;
_lastEventSpeed = (Ctms() - _lastContact) / 20;
_lastContact = Ctms();
}
//TouchpadContacts = string.Join(" | ", contacts.Select(c => c.ToString())) + " | Event speed: " + _lastEventSpeed + "ms";
}

private long Ctms()
{
return new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
}

}
14 changes: 11 additions & 3 deletions ThreeFingersDragOnWindows.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
</PropertyGroup>

<ItemGroup>
<Content Include="Assets\icon.ico" />
<Content Include="Assets\icon.png" />
<Content Include="Assets\SplashScreen.scale-200.png" />
<Content Include="Assets\LockScreenLogo.scale-200.png" />
<Content Include="Assets\Square150x150Logo.scale-200.png" />
Expand All @@ -23,8 +25,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.3.230602002" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.755" />
<PackageReference Include="H.NotifyIcon.WinUI" Version="2.0.115" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.230822000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.756" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>

Expand All @@ -34,7 +37,12 @@
package has not yet been restored.
-->
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<ProjectCapability Include="Msix"/>
<ProjectCapability Include="Msix" />
</ItemGroup>
<ItemGroup>
<Page Update="src\touchpad\HandlerWindow.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>

<!--
Expand Down
Loading

0 comments on commit 8182676

Please sign in to comment.