topmostfriend/TopMostFriend/SettingsWindow.cs

430 lines
17 KiB
C#
Raw Normal View History

2022-08-25 23:54:01 +00:00
using System;
2022-08-26 00:01:04 +00:00
using System.Diagnostics;
2022-08-25 23:54:01 +00:00
using System.Drawing;
2022-08-26 00:01:04 +00:00
using System.IO;
2022-08-25 23:54:01 +00:00
using System.Windows.Forms;
2022-08-26 00:01:04 +00:00
using TopMostFriend.Languages;
2022-08-25 23:54:01 +00:00
namespace TopMostFriend {
public class SettingsWindow : Form {
public static SettingsWindow Instance;
public static void Display() {
if(Instance != null) {
Instance.Show();
return;
}
Instance = new SettingsWindow();
Instance.Show();
}
public int KeyCode { get; set; }
public readonly TextBox FgKey;
public readonly CheckBox FgModCtrl;
public readonly CheckBox FgModAlt;
public readonly CheckBox FgModShift;
2022-08-25 23:57:51 +00:00
public readonly CheckBox FgModWindows;
2022-08-25 23:54:01 +00:00
2022-08-25 23:54:53 +00:00
public readonly CheckBox FlAlwaysAdmin;
2022-08-25 23:55:31 +00:00
public readonly CheckBox FlToggleNotification;
2022-08-25 23:56:27 +00:00
public readonly CheckBox FlShiftClickBlacklist;
public readonly CheckBox FlShowHotkeyIcon;
2022-08-25 23:59:00 +00:00
public readonly CheckBox FlShowWindowList;
2022-08-26 00:01:04 +00:00
public readonly CheckBox FlAlwaysRetryAsAdmin;
public readonly CheckBox FlRevertOnExit;
public readonly ComboBox LangSelect;
2022-08-25 23:54:53 +00:00
2022-08-25 23:54:01 +00:00
public SettingsWindow() {
2022-08-26 00:01:04 +00:00
Text = Locale.String(@"SettingsTitle");
2022-08-25 23:54:01 +00:00
Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
StartPosition = FormStartPosition.CenterScreen;
FormBorderStyle = FormBorderStyle.FixedSingle;
AutoScaleMode = AutoScaleMode.Dpi;
2022-08-26 00:01:04 +00:00
ClientSize = new Size(430, 407);
2022-08-25 23:54:01 +00:00
MinimizeBox = MaximizeBox = false;
2022-08-26 00:01:04 +00:00
TopMost = true;
2022-08-25 23:54:01 +00:00
KeyCode = Settings.Get(Program.FOREGROUND_HOTKEY_SETTING, 0);
Button applyButton = new Button {
2022-08-26 00:01:04 +00:00
Text = Locale.String(@"SettingsApply"),
2022-08-25 23:54:01 +00:00
Size = new Size(75, 23),
Location = new Point(ClientSize.Width - 81, ClientSize.Height - 30),
TabIndex = 10003,
2022-08-26 00:01:04 +00:00
Anchor = AnchorStyles.Bottom | AnchorStyles.Right,
2022-08-25 23:54:01 +00:00
};
applyButton.Click += ApplyButton_Click;
Button cancelButton = new Button {
2022-08-26 00:01:04 +00:00
Text = Locale.String(@"SettingsCancel"),
2022-08-25 23:54:01 +00:00
Size = applyButton.Size,
Location = new Point(ClientSize.Width - 162, applyButton.Location.Y),
TabIndex = 10002,
2022-08-26 00:01:04 +00:00
Anchor = applyButton.Anchor,
2022-08-25 23:54:01 +00:00
};
cancelButton.Click += CancelButton_Click;
Button okButton = new Button {
2022-08-26 00:01:04 +00:00
Text = Locale.String(@"SettingsOk"),
2022-08-25 23:54:01 +00:00
Size = applyButton.Size,
Location = new Point(ClientSize.Width - 243, applyButton.Location.Y),
TabIndex = 10001,
2022-08-26 00:01:04 +00:00
Anchor = applyButton.Anchor,
2022-08-25 23:54:01 +00:00
};
okButton.Click += OkButton_Click;
GroupBox hotKeyGroup = new GroupBox {
2022-08-26 00:01:04 +00:00
Text = Locale.String(@"SettingsHotKeysTitle"),
2022-08-25 23:54:01 +00:00
Location = new Point(6, 6),
Size = new Size(Width - 18, 70),
2022-08-26 00:01:04 +00:00
Anchor = AnchorStyles.Left | AnchorStyles.Right,
2022-08-25 23:54:01 +00:00
};
2022-08-25 23:54:53 +00:00
GroupBox flagsGroup = new GroupBox {
2022-08-26 00:01:04 +00:00
Text = Locale.String(@"SettingsOptionsTitle"),
Location = new Point(6, 80),
Size = new Size(Width - 18, 170),
Anchor = hotKeyGroup.Anchor,
2022-08-25 23:56:27 +00:00
};
2022-08-26 00:01:04 +00:00
GroupBox langGroup = new GroupBox {
Text = Locale.String(@"SettingsLanguageTitle"),
Location = new Point(6, 254),
Size = new Size(Width - 18, 55),
Anchor = hotKeyGroup.Anchor,
};
GroupBox otherGroup = new GroupBox {
Text = Locale.String(@"SettingsOtherTitle"),
Location = new Point(6, 313),
2022-08-25 23:56:27 +00:00
Size = new Size(Width - 18, 55),
2022-08-26 00:01:04 +00:00
Anchor = hotKeyGroup.Anchor,
2022-08-25 23:54:53 +00:00
};
2022-08-25 23:54:01 +00:00
Controls.AddRange(new Control[] {
2022-08-25 23:59:00 +00:00
applyButton, cancelButton, okButton,
2022-08-26 00:01:04 +00:00
hotKeyGroup, flagsGroup, langGroup, otherGroup,
2022-08-25 23:54:01 +00:00
});
2022-08-26 00:01:04 +00:00
hotKeyGroup.Controls.Add(new Label {
2022-08-25 23:54:01 +00:00
AutoSize = true,
2022-08-26 00:01:04 +00:00
Text = Locale.String(@"SettingsHotKeysToggle"),
2022-08-25 23:54:01 +00:00
Location = new Point(8, 17),
});
2022-08-25 23:54:53 +00:00
2022-08-26 00:01:04 +00:00
CreateHotKeyInput(
hotKeyGroup,
() => KeyCode,
keyCode => KeyCode = keyCode
);
FlToggleNotification = new CheckBox {
Text = Locale.String(@"SettingsOptionsToggleNotify"),
2022-08-25 23:54:53 +00:00
Location = new Point(10, 20),
2022-08-26 00:01:04 +00:00
Checked = Settings.Get(Program.TOGGLE_BALLOON_SETTING, Program.ToggleBalloonDefault),
2022-08-25 23:54:53 +00:00
AutoSize = true,
2022-08-25 23:56:27 +00:00
TabIndex = 201,
2022-08-25 23:54:53 +00:00
};
2022-08-26 00:01:04 +00:00
FlShowHotkeyIcon = new CheckBox {
Text = Locale.String(@"SettingsOptionsToggleNotifyIcon"),
2022-08-25 23:55:31 +00:00
Location = new Point(10, 40),
2022-08-26 00:01:04 +00:00
Checked = Settings.Get(Program.SHOW_HOTKEY_ICON, true),
2022-08-25 23:55:31 +00:00
AutoSize = true,
2022-08-25 23:56:27 +00:00
TabIndex = 202,
};
2022-08-26 00:01:04 +00:00
FlAlwaysRetryAsAdmin = new CheckBox {
Text = Locale.String(@"SettingsOptionsElevatedRetry"),
2022-08-25 23:56:27 +00:00
Location = new Point(10, 60),
2022-08-26 00:01:04 +00:00
Checked = Settings.Get(Program.ALWAYS_RETRY_ELEVATED, false),
2022-08-25 23:56:27 +00:00
AutoSize = true,
TabIndex = 203,
};
2022-08-26 00:01:04 +00:00
FlShiftClickBlacklist = new CheckBox {
Text = Locale.String(@"SettingsOptionsShiftBlacklist"),
2022-08-25 23:56:27 +00:00
Location = new Point(10, 80),
2022-08-26 00:01:04 +00:00
Checked = Settings.Get(Program.SHIFT_CLICK_BLACKLIST, true),
2022-08-25 23:56:27 +00:00
AutoSize = true,
TabIndex = 204,
};
2022-08-26 00:01:04 +00:00
FlRevertOnExit = new CheckBox {
Text = Locale.String(@"SettingsOptionsRevertOnExit"),
2022-08-25 23:59:00 +00:00
Location = new Point(10, 100),
2022-08-26 00:01:04 +00:00
Checked = Settings.Get(Program.REVERT_ON_EXIT, false),
2022-08-25 23:59:00 +00:00
AutoSize = true,
TabIndex = 205,
};
2022-08-26 00:01:04 +00:00
FlShowWindowList = new CheckBox {
Text = Locale.String(@"SettingsOptionsShowTrayList"),
Location = new Point(10, 120),
Checked = Settings.Get(Program.SHOW_WINDOW_LIST, true),
AutoSize = true,
TabIndex = 206,
};
FlAlwaysAdmin = new CheckBox {
Text = Locale.String(@"SettingsOptionsAlwaysAdmin"),
Location = new Point(10, 140),
Checked = Settings.Get(Program.ALWAYS_ADMIN_SETTING, false),
AutoSize = true,
TabIndex = 207,
};
2022-08-25 23:56:27 +00:00
2022-08-26 00:01:04 +00:00
CheckBox[] options = new[] {
2022-08-25 23:59:00 +00:00
FlAlwaysAdmin, FlToggleNotification, FlShiftClickBlacklist,
2022-08-26 00:01:04 +00:00
FlShowHotkeyIcon, FlShowWindowList, FlAlwaysRetryAsAdmin,
FlRevertOnExit,
};
flagsGroup.Controls.AddRange(options);
foreach(CheckBox option in options)
if((option.Width + (option.Left * 2)) > flagsGroup.ClientSize.Width)
ClientSize = new Size(option.Width + 30, ClientSize.Height);
LangSelect = new ComboBox {
Anchor = AnchorStyles.Left | AnchorStyles.Right,
DropDownStyle = ComboBoxStyle.DropDownList,
Size = new Size(langGroup.Width - 24, 21),
Location = new Point(12, 22),
};
LangSelect.Items.AddRange(Locale.GetAvailableLanguages());
LangSelect.SelectedItem = Locale.GetCurrentLanguage();
langGroup.Controls.Add(LangSelect);
2022-08-25 23:56:27 +00:00
Button titleBlacklist = new Button {
Size = new Size(120, 23),
Location = new Point(10, 20),
2022-08-26 00:01:04 +00:00
Text = Locale.String(@"SettingsOtherBlacklistButton"),
2022-08-25 23:56:27 +00:00
TabIndex = 301,
2022-08-26 00:01:04 +00:00
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowOnly,
2022-08-25 23:56:27 +00:00
};
titleBlacklist.Click += (s, e) => {
2022-08-26 00:01:04 +00:00
string[] newList = BlacklistWindow.Display(Locale.String(@"SettingsOtherBlacklistWindowTitle"), Program.GetBlacklistedTitles());
2022-08-25 23:56:27 +00:00
if(newList != null) {
Program.ApplyBlacklistedTitles(newList);
Program.SaveBlacklistedTitles();
}
2022-08-25 23:55:31 +00:00
};
2022-08-25 23:54:53 +00:00
2022-08-26 00:01:04 +00:00
Button startWithWindows = new Button {
Size = new Size(120, 23),
Location = new Point(134, 20),
Text = Locale.String(@"SettingsOtherStartupButton"),
TabIndex = 302,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowOnly,
};
startWithWindows.Click += (s, e) => {
DialogResult dr = MessageBox.Show(Locale.String(@"SettingsOtherStartupConfirm"), Program.TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), @"TopMostFriend.lnk");
if(File.Exists(path))
File.Delete(path);
if(dr == DialogResult.Yes) {
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(path);
shortcut.TargetPath = Application.ExecutablePath;
shortcut.Save();
}
};
Button resetSettings = new Button {
Size = new Size(120, 23),
Location = new Point(258, 20),
Text = Locale.String(@"SettingsOtherResetButton"),
TabIndex = 303,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowOnly,
};
resetSettings.Click += (s, e) => {
DialogResult dr = MessageBox.Show(Locale.String(@"SettingsOtherResetConfirm"), Program.TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
if(dr == DialogResult.Yes) {
Settings.Remove(Program.FOREGROUND_HOTKEY_SETTING);
Settings.Remove(Program.PROCESS_SEPARATOR_SETTING);
Settings.Remove(Program.LIST_SELF_SETTING);
Settings.Remove(Program.LIST_BACKGROUND_PATH_SETTING);
Settings.Remove(Program.LIST_BACKGROUND_LAYOUT_SETTING);
Settings.Remove(Program.ALWAYS_ADMIN_SETTING);
Settings.Remove(Program.TOGGLE_BALLOON_SETTING);
Settings.Remove(Program.SHIFT_CLICK_BLACKLIST);
Settings.Remove(Program.TITLE_BLACKLIST);
Settings.Remove(Program.SHOW_HOTKEY_ICON);
Settings.Remove(Program.SHOW_WINDOW_LIST);
Settings.Remove(Program.LAST_VERSION);
Settings.Remove(Program.ALWAYS_RETRY_ELEVATED);
Settings.Remove(Program.REVERT_ON_EXIT);
Program.Shutdown();
Process.Start(Application.ExecutablePath);
Application.Exit();
}
};
Button[] otherButtons = new[] { titleBlacklist, startWithWindows, resetSettings };
otherGroup.Controls.AddRange(otherButtons);
2022-08-25 23:54:01 +00:00
}
2022-08-26 00:01:04 +00:00
public static void CreateHotKeyInput(
Control target,
Func<int> getKeyCode,
Action<int> setKeyCode,
int offsetX = 0,
int offsetY = 0
) {
int modX = 120 + offsetX;
int modY = 34 + offsetY;
int keyCode = getKeyCode();
Button fgReset = new Button {
Text = Locale.String(@"SettingsHotKeysReset"),
Location = new Point(target.Width - 85 + offsetX, modY),
TabIndex = 105,
};
TextBox fgKey = new TextBox {
Text = ((Keys)(keyCode >> 16)).ToString(),
Location = new Point(12 + offsetX, modY + 2),
TabIndex = 101,
};
CheckBox fgModCtrl = new CheckBox {
Text = @"CTRL",
Location = new Point(modX, modY),
Checked = (keyCode & (int)Win32ModKeys.MOD_CONTROL) > 0,
Appearance = Appearance.Button,
Size = new Size(50, 23),
TextAlign = ContentAlignment.MiddleCenter,
TabIndex = 102,
};
CheckBox fgModWindows = new CheckBox {
Text = @"WIN",
Location = new Point(modX + 50, modY),
Checked = (keyCode & (int)Win32ModKeys.MOD_WIN) > 0,
Appearance = fgModCtrl.Appearance,
Size = fgModCtrl.Size,
TextAlign = fgModCtrl.TextAlign,
TabIndex = 103,
};
CheckBox fgModAlt = new CheckBox {
Text = @"ALT",
Location = new Point(modX + 100, modY),
Checked = (keyCode & (int)Win32ModKeys.MOD_ALT) > 0,
Appearance = fgModCtrl.Appearance,
Size = fgModCtrl.Size,
TextAlign = fgModCtrl.TextAlign,
TabIndex = 104,
};
CheckBox fgModShift = new CheckBox {
Text = @"SHIFT",
Location = new Point(modX + 150, modY),
Checked = (keyCode & (int)Win32ModKeys.MOD_SHIFT) > 0,
Appearance = fgModCtrl.Appearance,
Size = fgModCtrl.Size,
TextAlign = fgModCtrl.TextAlign,
TabIndex = 105,
};
fgReset.Click += (s, e) => {
fgModCtrl.Checked = fgModAlt.Checked = fgModShift.Checked = false;
fgKey.Text = ((Keys)0).ToString();
setKeyCode(0);
};
fgModCtrl.Click += (s, e) => {
if(s is CheckBox cb) {
if(cb.Checked)
keyCode |= (int)Win32ModKeys.MOD_CONTROL;
else
keyCode &= ~(int)Win32ModKeys.MOD_CONTROL;
setKeyCode(keyCode);
}
};
fgModAlt.Click += (s, e) => {
if(s is CheckBox cb) {
if(cb.Checked)
keyCode |= (int)Win32ModKeys.MOD_ALT;
else
keyCode &= ~(int)Win32ModKeys.MOD_ALT;
setKeyCode(keyCode);
}
};
fgModShift.Click += (s, e) => {
if(s is CheckBox cb) {
if(cb.Checked)
keyCode |= (int)Win32ModKeys.MOD_SHIFT;
else
keyCode &= ~(int)Win32ModKeys.MOD_SHIFT;
setKeyCode(keyCode);
}
};
fgModWindows.Click += (s, e) => {
if(s is CheckBox cb) {
if(cb.Checked)
keyCode |= (int)Win32ModKeys.MOD_WIN;
else
keyCode &= ~(int)Win32ModKeys.MOD_WIN;
setKeyCode(keyCode);
}
};
fgKey.KeyDown += (s, e) => {
if(!(s is TextBox textBox))
return;
e.Handled = e.SuppressKeyPress = true;
textBox.Text = e.KeyCode.ToString();
keyCode &= 0xFFFF;
keyCode |= (int)e.KeyCode << 16;
setKeyCode(keyCode);
};
target.Controls.AddRange(new Control[] {
fgModCtrl, fgModAlt, fgModShift, fgModWindows, fgReset, fgKey,
});
2022-08-25 23:54:01 +00:00
}
public void Apply() {
2022-08-26 00:01:04 +00:00
if(LangSelect.SelectedItem is LanguageInfo li) {
if(li != Locale.GetCurrentLanguage()) {
if(Locale.GetPreferredLanguage() == li.Id)
Settings.Remove(Program.LANGUAGE);
else
Settings.Set(Program.LANGUAGE, li.Id);
Locale.SetLanguage(li);
MessageBox.Show(Locale.String(@"SettingsLanguageChangedInfo"), Program.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
2022-08-25 23:54:01 +00:00
Settings.Set(Program.FOREGROUND_HOTKEY_SETTING, KeyCode);
2022-08-25 23:54:53 +00:00
Settings.Set(Program.ALWAYS_ADMIN_SETTING, FlAlwaysAdmin.Checked);
2022-08-25 23:56:27 +00:00
Settings.Set(Program.TOGGLE_BALLOON_SETTING, FlToggleNotification.Checked);
Settings.Set(Program.SHIFT_CLICK_BLACKLIST, FlShiftClickBlacklist.Checked);
Settings.Set(Program.SHOW_HOTKEY_ICON, FlShowHotkeyIcon.Checked);
2022-08-25 23:59:00 +00:00
Settings.Set(Program.SHOW_WINDOW_LIST, FlShowWindowList.Checked);
2022-08-26 00:01:04 +00:00
Settings.Set(Program.ALWAYS_RETRY_ELEVATED, FlAlwaysRetryAsAdmin.Checked);
Settings.Set(Program.REVERT_ON_EXIT, FlRevertOnExit.Checked);
2022-08-25 23:54:01 +00:00
Program.SetForegroundHotKey(KeyCode);
}
private void OkButton_Click(object sender, EventArgs e) {
Apply();
Close();
}
private void CancelButton_Click(object sender, EventArgs e) {
Close();
}
private void ApplyButton_Click(object sender, EventArgs e) {
Apply();
}
protected override void OnFormClosed(FormClosedEventArgs e) {
base.OnFormClosed(e);
Instance.Dispose();
Instance = null;
}
}
}