Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 89 additions & 54 deletions WinJump/Core/Config.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace WinJump.Core;

/// <summary>
/// Handles loading the configuration file
/// </summary>
internal sealed class Config {
public sealed class Config {
public static readonly int MAX_STICKY_DESKTOPS = 10;
public static readonly string LOCATION = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".winjump");

public static Config Current { get; private set; } = null!;

[JsonProperty("move-window-to")]
public required List<JumpWindowToDesktop> MoveWindowTo { get; set; }

Expand All @@ -30,83 +33,114 @@ internal sealed class Config {

[JsonProperty("change-desktops-with-scroll")]
public required bool ChangeDesktopsWithScroll { get; set; }


[JsonProperty("scroll-invert-direction")]
public required bool ScrollInvertDirection { get; set; }

[JsonProperty("sticky-desktops")]
public int StickyDesktops { get; set; }

public static Config Load() {
try {
public static Config Load()
{
try
{
EnsureCreated();

string content = File.ReadAllText(LOCATION);

var config = JsonConvert.DeserializeObject<Config>(content);
var config = JsonConvert.DeserializeObject<Config>(content) ?? throw new Exception("Failed to deserialize config file");

if(config == null) {
throw new Exception("Failed to deserialize config file");
}
Validate(config);

// Check for jump tos with duplicate shortcuts
for(int i = 0; i < config.JumpTo.Count; i++) {
var shortcut = config.JumpTo[i].Shortcut;
for(int j = i + 1; j < config.JumpTo.Count; j++) {
if(config.JumpTo[j].Shortcut.IsEqual(shortcut)) {
throw new Exception("Duplicate jump to shortcut");
}

if(config.JumpTo[i].Desktop <= 0) {
throw new Exception("Invalid desktop number");
}
}
Current = config;
return config;
}
catch
{
Current = Default();
return Current;
}
}
private static void Validate(Config config)
{
// JumpTo duplicates
for (int i = 0; i < config.JumpTo.Count; i++) {
var shortcut = config.JumpTo[i].Shortcut;

if (config.JumpTo[i].Desktop <= 0)
throw new Exception("Invalid desktop number");

for (int j = i + 1; j < config.JumpTo.Count; j++) {
if (config.JumpTo[j].Shortcut.IsEqual(shortcut))
throw new Exception("Duplicate jump to shortcut");
}
}

// Check for toggle groups with duplicate shortcuts
for(int i = 0; i < config.ToggleGroups.Count; i++) {
var shortcut = config.ToggleGroups[i].Shortcut;
for(int j = i + 1; j < config.ToggleGroups.Count; j++) {
if(config.ToggleGroups[j].Shortcut.IsEqual(shortcut)) {
throw new Exception("Duplicate toggle group shortcut");
}

if(config.ToggleGroups[i].Desktops.Any((d) => d <= 0)) {
throw new Exception("Invalid desktop number");
}
}
}
// ToggleGroups
for (int i = 0; i < config.ToggleGroups.Count; i++) {
var shortcut = config.ToggleGroups[i].Shortcut;

if (config.ToggleGroups[i].Desktops.Any(d => d <= 0))
throw new Exception("Invalid desktop number");

// Check for move windows with duplicate shortcuts
for(int i = 0; i < config.MoveWindowTo.Count; i++) {
var shortcut = config.MoveWindowTo[i].Shortcut;
for(int j = i + 1; j < config.MoveWindowTo.Count; j++) {
if(config.MoveWindowTo[j].Shortcut.IsEqual(shortcut)) {
throw new Exception("Duplicate move window shortcut");
}

if(config.MoveWindowTo[i].Desktop <= 0) {
throw new Exception("Invalid desktop number");
}
}
for (int j = i + 1; j < config.ToggleGroups.Count; j++) {
if (config.ToggleGroups[j].Shortcut.IsEqual(shortcut))
throw new Exception("Duplicate toggle group shortcut");
}
}

return config;
} catch(Exception) {
return Default();
// MoveWindowTo
for (int i = 0; i < config.MoveWindowTo.Count; i++) {
var shortcut = config.MoveWindowTo[i].Shortcut;

if (config.MoveWindowTo[i].Desktop <= 0)
throw new Exception("Invalid desktop number");

for (int j = i + 1; j < config.MoveWindowTo.Count; j++) {
if (config.MoveWindowTo[j].Shortcut.IsEqual(shortcut))
throw new Exception("Duplicate move window shortcut");
}
}
}

public static void EnsureCreated() {
if(File.Exists(LOCATION)) return;
var defaults = JObject.FromObject(Default());

// Create a file if it's not there
if (!File.Exists(LOCATION))
{
File.WriteAllText(LOCATION, defaults.ToString(Formatting.Indented));
return;
}

var config = Default();
string content = JsonConvert.SerializeObject(config, Formatting.Indented);
File.WriteAllText(LOCATION, content);
//Update a file with new options
var existing = JObject.Parse(File.ReadAllText(LOCATION));
bool changed = false;

foreach (var prop in defaults.Properties())
{
if (existing[prop.Name] == null)
{
existing[prop.Name] = prop.Value;
changed = true;
}
}

if (changed)
{
File.WriteAllText(
LOCATION,
existing.ToString(Formatting.Indented)
);
}
}

/// <summary>
/// Creates the default config file
/// </summary>
/// <returns>Default configuration</returns>
private static Config Default() {

var jumpTo = new List<JumpTo>();

uint desktop = 1;
Expand All @@ -133,6 +167,7 @@ private static Config Default() {
ToggleGroups = [],
JumpCurrentGoesToLast = true,
ChangeDesktopsWithScroll = false,
ScrollInvertDirection = false,
StickyDesktops = 0
};
}
Expand Down
Loading