Skip to content

Commit

Permalink
Add working config and autostart
Browse files Browse the repository at this point in the history
  • Loading branch information
Naamloos committed May 12, 2022
1 parent 55a0d39 commit 504920c
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 27 deletions.
55 changes: 55 additions & 0 deletions src/FlippedTaskbar11/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace TopCenterStart11
{
internal class Config
{
const string CONFIGFILE = "config.json";

private static Config instance;

public int PollingRate { get; set; } = 100;

/// <summary>
/// Do not call the constructor manually. Call Config.Load() instead.
/// </summary>
public Config() { }

public void Save()
{
using FileStream fs = getFile();
fs.Position = 0;
JsonSerializer.Serialize(fs, this);
}

public static Config Load()
{
if (instance == null)
{
using FileStream fs = getFile();
instance = JsonSerializer.Deserialize<Config>(fs, new JsonSerializerOptions());
}

return instance;
}

private static FileStream getFile()
{
if (!File.Exists("config.json"))
{
var fs = File.Create("config.json");
StreamWriter sw = new StreamWriter(fs);
sw.Write(JsonSerializer.Serialize(new Config()));
sw.Dispose();
}

return File.Open("config.json", FileMode.OpenOrCreate);
}
}
}
2 changes: 1 addition & 1 deletion src/FlippedTaskbar11/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FlippedTaskbar11
namespace TopCenterStart11
{
internal static class Program
{
Expand Down
57 changes: 50 additions & 7 deletions src/FlippedTaskbar11/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 53 additions & 4 deletions src/FlippedTaskbar11/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
using FlippedTaskbar11.TaskbarLogics;
using Microsoft.Win32;
using System.Reflection;
using TopCenterStart11.TaskbarLogics;

namespace FlippedTaskbar11
namespace TopCenterStart11
{
public partial class SettingsForm : Form
{
const string ENABLE_AUTOSTART = "Enable Autostart";
const string DISABLE_AUTOSTART = "Disable Autostart";
const string REGISTRY_ITEM = "TopCenterStart11";
const string REGISTRY_AUTOSTART_PATH = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";

private Config config;
private TaskbarManager taskbar;

public SettingsForm()
{
InitializeComponent();

taskbar = new TaskbarManager();
config = Config.Load();
setAppropriateAutoStartText();
taskbar = new TaskbarManager(config);

trayIcon.ContextMenuStrip = new ContextMenuStrip();
trayIcon.ContextMenuStrip.Items.AddRange(new ToolStripItem[]
{
new ToolStripLabel("Welcome to TopCenterStart11! <3"),
new ToolStripButton("Show Settings", null, onTrayShow, "Show Settings"),
new ToolStripButton("Exit", null, onTrayExit, "Exit")
});

pollingRate.Value = config.PollingRate;

taskbar.StartTaskbarLoop();
}

Expand All @@ -41,11 +54,47 @@ private void onTrayExit(object? sender, EventArgs eventargs)
Application.Exit();
}

private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
private void onFormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.Hide();
}

private void onAutoStartClick(object sender, EventArgs e)
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_AUTOSTART_PATH, true);

if (!getAutoStartEnabled())
{
key.SetValue(REGISTRY_ITEM, Assembly.GetEntryAssembly().Location);
}
else
{
key.DeleteValue(REGISTRY_ITEM, false);
}

setAppropriateAutoStartText();
}

private void onSaveClick(object sender, EventArgs e)
{
// Copy config values from the UI to the config and save
config.PollingRate = (int)pollingRate.Value;
config.Save();
}

private void setAppropriateAutoStartText()
{
autoStart.Text = getAutoStartEnabled() ? DISABLE_AUTOSTART : ENABLE_AUTOSTART;
}

private bool getAutoStartEnabled()
{
var path = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
RegistryKey key = Registry.CurrentUser.OpenSubKey(path, true);

return key.GetValueNames().Contains(REGISTRY_ITEM);
}
}
}
27 changes: 14 additions & 13 deletions src/FlippedTaskbar11/TaskbarLogic/TaskbarManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using FlippedTaskbar11.TaskbarLogic;
using TopCenterStart11.TaskbarLogic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace FlippedTaskbar11.TaskbarLogics
namespace TopCenterStart11.TaskbarLogics
{
internal class TaskbarManager
{
Expand All @@ -25,23 +25,24 @@ internal class TaskbarManager

public const int TASKBAR_HEIGHT = 48;

public const int POLLING_RATE_MS = 100;
private Config config;

private IntPtr startHwnd;
private IntPtr thumbnailHwnd;
private IntPtr taskbarHwnd;
private IntPtr switcherHwnd;

WIN32.DEVMODE devmode;
WIN32.RECT workingArea;
private WIN32.DEVMODE devmode;
private WIN32.RECT workingArea;

WIN32.RECT taskbarWindowRect;
WIN32.RECT taskbarClientRect;
private WIN32.RECT taskbarWindowRect;
private WIN32.RECT taskbarClientRect;

private CancellationTokenSource cancellationTokenSource;

public TaskbarManager()
public TaskbarManager(Config config)
{
this.config = config;
cancellationTokenSource = new CancellationTokenSource();

// pre-fetch windows on launch.
Expand Down Expand Up @@ -102,7 +103,7 @@ private async Task doTaskbarLoopAsync()
// update taskbar window
WIN32.UpdateWindow(taskbarHwnd);
// Set new position for taskbar
WIN32.SetWindowPos(taskbarHwnd, IntPtr.Zero, taskbarWindowRect.Left, 0,
WIN32.SetWindowPos(taskbarHwnd, (IntPtr)1, taskbarWindowRect.Left, 0,
taskbarWindowRect.Right, taskbarClientRect.Bottom, 0x0400);
// show window
//WIN32.ShowWindow(taskbarHwnd, 5);
Expand All @@ -119,8 +120,8 @@ private async Task doTaskbarLoopAsync()
workingArea = working;
}

if(POLLING_RATE_MS > 0)
await Task.Delay(POLLING_RATE_MS);
if(config.PollingRate > 0)
await Task.Delay(config.PollingRate);
}
while (!cancellationTokenSource.IsCancellationRequested);
}
Expand All @@ -137,8 +138,8 @@ private async Task doWindowPlacementLoopAsync()
placeUnderTaskbar(thumbnailHwnd);
placeStart(startHwnd);

if (POLLING_RATE_MS > 0)
await Task.Delay(POLLING_RATE_MS);
if (config.PollingRate > 0)
await Task.Delay(config.PollingRate);
}
while (!cancellationTokenSource.IsCancellationRequested);
}
Expand Down
2 changes: 1 addition & 1 deletion src/FlippedTaskbar11/TaskbarLogic/Win32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using System.Text;
using System.Threading.Tasks;

namespace FlippedTaskbar11.TaskbarLogic
namespace TopCenterStart11.TaskbarLogic
{
public static class WIN32
{
Expand Down
2 changes: 1 addition & 1 deletion src/FlippedTaskbar11.sln → src/TopCenterStart11.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32505.173
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlippedTaskbar11", "FlippedTaskbar11\FlippedTaskbar11.csproj", "{E5DD7A89-AFD9-40FB-B24C-7B276F80A80A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TopCenterStart11", "FlippedTaskbar11\TopCenterStart11.csproj", "{E5DD7A89-AFD9-40FB-B24C-7B276F80A80A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down

0 comments on commit 504920c

Please sign in to comment.