Skip to content

Commit

Permalink
Add systray icon and disable setup assistant options (#433, #255)
Browse files Browse the repository at this point in the history
  • Loading branch information
srwi committed Sep 14, 2024
1 parent e32dd20 commit 6db022e
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 9 deletions.
57 changes: 50 additions & 7 deletions EverythingToolbar.Launcher/Launcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Shell;
using EverythingToolbar.Helpers;
using Microsoft.Xaml.Behaviors;
using NHotkey;
Expand All @@ -19,17 +20,22 @@ namespace EverythingToolbar.Launcher
{
internal static class Launcher
{
private const string EventName = "EverythingToolbarToggleEvent";
private const string ToggleEventName = "EverythingToolbarToggleEvent";
private const string StartSetupAssistantEventName = "StartSetupAssistantEvent";
private const string MutexName = "EverythingToolbar.Launcher";
private static bool _searchWindowRecentlyClosed;
private static Timer _searchWindowRecentlyClosedTimer;
private static NotifyIcon notifyIcon;

private class LauncherWindow : Window
{
public LauncherWindow(NotifyIcon icon)
{
ToolbarLogger.Initialize("Launcher");

notifyIcon = icon;
SetupJumpList();

_searchWindowRecentlyClosedTimer = new Timer(500);
_searchWindowRecentlyClosedTimer.AutoReset = false;
_searchWindowRecentlyClosedTimer.Elapsed += (s, e) => { _searchWindowRecentlyClosed = false; };
Expand All @@ -48,7 +54,7 @@ public LauncherWindow(NotifyIcon icon)

StartToggleListener();

if (!File.Exists(Utils.GetTaskbarShortcutPath()))
if (!ToolbarSettings.User.IsSetupAssistantDisabled && !File.Exists(Utils.GetTaskbarShortcutPath()))
new SetupAssistant(icon).Show();

if (!ShortcutManager.Instance.AddOrReplace("FocusSearchBox",
Expand All @@ -70,6 +76,19 @@ public LauncherWindow(NotifyIcon icon)
SearchWindow.Instance.Hiding += OnSearchWindowHiding;
}

private void SetupJumpList()
{
JumpList jumpList = new JumpList();
jumpList.JumpItems.Add(new JumpTask
{
Title = Properties.Resources.ContextMenuRunSetupAssistant,
Description = Properties.Resources.ContextMenuRunSetupAssistant,
ApplicationPath = System.Reflection.Assembly.GetEntryAssembly().Location,
Arguments = "--run-setup-assistant"
});
JumpList.SetJumpList(Application.Current, jumpList);
}

private static void OnSearchWindowHiding(object sender, EventArgs e)
{
_searchWindowRecentlyClosed = true;
Expand All @@ -85,13 +104,22 @@ private void StartToggleListener()
{
Task.Factory.StartNew(() =>
{
var wh = new EventWaitHandle(false, EventResetMode.AutoReset, EventName);
var wh = new EventWaitHandle(false, EventResetMode.AutoReset, ToggleEventName);
while (true)
{
wh.WaitOne();
ToggleWindow();
}
});
Task.Factory.StartNew(() =>
{
var wh = new EventWaitHandle(false, EventResetMode.AutoReset, StartSetupAssistantEventName);
while (true)
{
wh.WaitOne();
OpenSetupAssistant();
}
});
}

private void ToggleWindow()
Expand All @@ -105,6 +133,14 @@ private void ToggleWindow()
SearchWindow.Instance.Toggle();
});
}

private void OpenSetupAssistant()
{
Dispatcher?.Invoke(() =>
{
new SetupAssistant(notifyIcon).Show();
});
}
}

private static string GetIconPath()
Expand All @@ -118,7 +154,7 @@ private static string GetIconPath()
}

[STAThread]
private static void Main()
private static void Main(string[] args)
{
using (new Mutex(false, MutexName, out var createdNew))
{
Expand All @@ -128,19 +164,26 @@ private static void Main()
{
var app = new Application();
trayIcon.Icon = Icon.ExtractAssociatedIcon(GetIconPath());
trayIcon.ContextMenu = new ContextMenu(new [] {
trayIcon.ContextMenu = new ContextMenu(new[] {
new MenuItem(Resources.ContextMenuRunSetupAssistant, (s, e) => { new SetupAssistant(trayIcon).Show(); }),
new MenuItem(Resources.ContextMenuQuit, (s, e) => { app.Shutdown(); })
});
trayIcon.Visible = true;
trayIcon.Visible = ToolbarSettings.User.IsTrayIconEnabled;
app.Run(new LauncherWindow(trayIcon));
}
}
else
{
try
{
EventWaitHandle.OpenExisting(EventName).Set();
if (args.Length > 0 && args[0] == "--run-setup-assistant")
{
EventWaitHandle.OpenExisting(StartSetupAssistantEventName).Set();
}
else
{
EventWaitHandle.OpenExisting(ToggleEventName).Set();
}
}
catch (Exception ex)
{
Expand Down
36 changes: 36 additions & 0 deletions EverythingToolbar.Launcher/Properties/Resources.Designer.cs

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

12 changes: 12 additions & 0 deletions EverythingToolbar.Launcher/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,16 @@
<data name="SetupAssistantFirstStepTitle" xml:space="preserve">
<value>1. Taskbar icon</value>
</data>
<data name="SetupAssistantFifthStepText" xml:space="preserve">
<value>By enabling the system tray icon you can quit the EverythingToolbar background process at any time. Without it, EverythingToolbar can always be quit via the task manager.</value>
</data>
<data name="SetupAssistantFifthStepTitle" xml:space="preserve">
<value>5. Configure system tray icon</value>
</data>
<data name="SetupAssistantDisableWarningText" xml:space="preserve">
<value>Are you sure you want to exit the setup assistant? EverythingToolbar will only be accessible using the keyboard shortcut. You can access the setup assistant again via the system tray icon to pin the search icon to the taskbar.</value>
</data>
<data name="SetupAssistantDisableWarningTitle" xml:space="preserve">
<value>Exit Setup Assistant</value>
</data>
</root>
22 changes: 21 additions & 1 deletion EverythingToolbar.Launcher/SetupAssistant.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
xmlns:b="clr-namespace:EverythingToolbar.Behaviors;assembly=EverythingToolbar"
xmlns:properties="clr-namespace:EverythingToolbar.Launcher.Properties"
WindowStartupLocation="CenterScreen"
Height="410"
Height="450"
Background="Transparent"
WindowStyle="ToolWindow"
Title="{x:Static properties:Resources.SetupAssistantTitle}"
Closed="OnClosed"
Closing="OnClosing"
Foreground="White"
Width="575">

Expand Down Expand Up @@ -203,6 +204,25 @@
Unchecked="AutostartChanged" />
</StackPanel>
</Border>
<TextBlock FontSize="14"
Text="{x:Static properties:Resources.SetupAssistantFifthStepTitle}"
FontWeight="Medium"
Margin="0, 20, 0, 10" />
<Border BorderBrush="#771D1D1D"
BorderThickness="1"
Background="#0cffffff"
CornerRadius="4"
Padding="10">
<StackPanel>
<TextBlock TextWrapping="Wrap"
Text="{x:Static properties:Resources.SetupAssistantFifthStepText}" />
<CheckBox x:Name="TrayIconCheckBox"
Foreground="White"
Content="{x:Static properties:Resources.CheckboxEnable}"
Checked="TrayIconChanged"
Unchecked="TrayIconChanged" />
</StackPanel>
</Border>
</StackPanel>
</TabItem>

Expand Down
32 changes: 31 additions & 1 deletion EverythingToolbar.Launcher/SetupAssistant.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public SetupAssistant(NotifyIcon icon)

AutostartCheckBox.IsChecked = Utils.GetAutostartState();
HideWindowsSearchCheckBox.IsChecked = !Utils.GetWindowsSearchEnabledState();
TrayIconCheckBox.IsChecked = ToolbarSettings.User.IsTrayIconEnabled;

CreateFileWatcher(_taskbarShortcutPath);

Expand All @@ -51,6 +52,10 @@ private void OnLoaded(object sender, RoutedEventArgs e)
}

_iconHasChanged = false;

// Bring to front
Topmost = true;
Topmost = false;
}

private void CreateFileWatcher(string taskbarShortcutPath)
Expand Down Expand Up @@ -101,14 +106,39 @@ private void AutostartChanged(object sender, RoutedEventArgs e)
Utils.SetAutostartState(AutostartCheckBox.IsChecked != null && (bool)AutostartCheckBox.IsChecked);
}

private void TrayIconChanged(object sender, RoutedEventArgs e)
{
ToolbarSettings.User.IsTrayIconEnabled = TrayIconCheckBox.IsChecked != null && (bool)TrayIconCheckBox.IsChecked;
}

private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (_unlockedPages == TotalPages)
return;

var disableSetupAssistant = MessageBox.Show(
Properties.Resources.SetupAssistantDisableWarningText,
Properties.Resources.SetupAssistantDisableWarningTitle,
MessageBoxButton.YesNo,
MessageBoxImage.Exclamation
) == MessageBoxResult.Yes;
if (disableSetupAssistant)
{
ToolbarSettings.User.IsSetupAssistantDisabled = disableSetupAssistant;
// Ensuring the user can access the setup assistant
ToolbarSettings.User.IsTrayIconEnabled = disableSetupAssistant;
}
e.Cancel = !disableSetupAssistant;
}

private void OnCloseClicked(object sender, RoutedEventArgs e)
{
Close();
}

private void OnClosed(object sender, EventArgs e)
{
_icon.Visible = true;
_icon.Visible = ToolbarSettings.User.IsTrayIconEnabled;

if (_watcher != null)
{
Expand Down
32 changes: 32 additions & 0 deletions EverythingToolbar/ToolbarSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public interface IToolbarSettings
[Option(DefaultValue = true)]
bool IsUpdateNotificationsEnabled { get; set; }

[Option(DefaultValue = false)]
bool IsSetupAssistantDisabled { get; set; }

[Option(DefaultValue = false)]
bool IsTrayIconEnabled { get; set; }

[Option(DefaultValue = true)]
bool IsAutoSelectFirstResult { get; set; }

Expand Down Expand Up @@ -485,6 +491,32 @@ public bool IsUpdateNotificationsEnabled
}
}

public bool IsSetupAssistantDisabled
{
get => _settings.IsSetupAssistantDisabled;
set
{
if (_settings.IsSetupAssistantDisabled != value)
{
_settings.IsSetupAssistantDisabled = value;
OnPropertyChanged();
}
}
}

public bool IsTrayIconEnabled
{
get => _settings.IsTrayIconEnabled;
set
{
if (_settings.IsTrayIconEnabled != value)
{
_settings.IsTrayIconEnabled = value;
OnPropertyChanged();
}
}
}

public bool IsAutoSelectFirstResult
{
get => _settings.IsAutoSelectFirstResult;
Expand Down

0 comments on commit 6db022e

Please sign in to comment.