-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
110 lines (92 loc) · 3.55 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System.Windows.Data;
using System.Windows.Media;
using MahApps.Metro.Controls;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Commands;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using PerMonitorDpi;
namespace AdaptiveKeyboardConfig
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : MetroWindow
{
public MainWindow()
{
Apps = new ObservableCollection<AppEntry>();
InitializeComponent();
this.DataContext = this;
AddApp = new DelegateCommand<string>(showAvailableAppsContextMenu);
RemoveApp = new DelegateCommand<string>(removeApps, p => appList.SelectedIndex >= 0);
Loaded += (sender, args) =>
{
Apps.AddRange(AppEntry.LoadAppsFromRegistry(this, 32));
};
appList.SelectionChanged += (s, e) => RemoveApp.RaiseCanExecuteChanged();
this.Loaded += (s, e) =>
{
PerMonitorDpi = new PerMonitorDpiHelper(this);
PerMonitorDpi.BindLayoutTransformTo(this.Content as FrameworkElement);
};
}
async void removeApps(string p)
{
// FIXME: Dirty code to realize removal animation
var idx = appList.SelectedIndex;
var appsToRemove = appList.SelectedItems.Cast<AppEntry>().ToArray();
foreach (var item in appsToRemove)
item.MarkForDeletion();
await Task.Delay(200);
foreach (var item in appsToRemove)
{
item.RemoveRegistryEntry();
Apps.Remove(item);
}
if (idx >= appList.Items.Count)
idx = appList.Items.Count - 1;
if (idx >= 0)
{
appList.SelectedIndex = idx;
var item = (ListBoxItem)(appList.ItemContainerGenerator.ContainerFromItem(appList.SelectedItem));
if (item != null)
item.Focus();
}
}
void showAvailableAppsContextMenu(string p)
{
addAppContextMenu.Items.Clear();
foreach (var app in AppEntry.EnumAppsExcluding(Apps.Concat(new[] {AppEntry.FromVisual(this)})))
{
app.LoadIconFromModule(this, 32);
addAppContextMenu.Items.Add(app);
}
addAppContextMenu.IsOpen = true;
}
void addAppMenuItemClick(object sender, EventArgs e)
{
var app = ((MenuItem)sender).DataContext as AppEntry;
if (Apps.FirstOrDefault(a => a.Path == app.Path) != null)
return;
Apps.Insert(0, app);
appList.ScrollIntoView(app);
app.UpdateRegistryEntry();
}
private void modeSwitchButtonClick(object sender, RoutedEventArgs e)
{
var app = ((Button)sender).DataContext as AppEntry;
if (app == null)
return;
app.Mode = (Mode)(((int)app.Mode + 1) % Enum.GetNames(typeof(Mode)).Length);
}
public PerMonitorDpiHelper PerMonitorDpi;
public ObservableCollection<AppEntry> Apps { get; set; }
public DelegateCommand<string> AddApp { get; set; }
public DelegateCommand<string> RemoveApp { get; set; }
}
}