Add open all menu action
This commit is contained in:
@@ -62,7 +62,7 @@ public sealed class LauncherController : IDisposable
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
popup = new LauncherPopup(Config, LaunchItem, ShowSettings);
|
popup = new LauncherPopup(Config, LaunchItem, OpenAllItems, ShowSettings);
|
||||||
popup.Closed += (_, _) => popup = null;
|
popup.Closed += (_, _) => popup = null;
|
||||||
popup.ShowNearTaskbar();
|
popup.ShowNearTaskbar();
|
||||||
}
|
}
|
||||||
@@ -126,22 +126,78 @@ public sealed class LauncherController : IDisposable
|
|||||||
|
|
||||||
private void LaunchItem(LauncherItem item)
|
private void LaunchItem(LauncherItem item)
|
||||||
{
|
{
|
||||||
|
if (TryLaunchItem(item, out string? error))
|
||||||
|
{
|
||||||
|
popup?.Close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(error))
|
||||||
|
{
|
||||||
|
System.Windows.MessageBox.Show(error, "Taskbar Launcher");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OpenAllItems(LauncherItem menuItem)
|
||||||
|
{
|
||||||
|
List<string> failures = [];
|
||||||
|
int launchedCount = 0;
|
||||||
|
|
||||||
|
foreach (LauncherItem child in menuItem.Children)
|
||||||
|
{
|
||||||
|
if (child.Type == LauncherItemType.Menu || string.IsNullOrWhiteSpace(child.Target))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (TryLaunchItem(child, out string? error))
|
||||||
|
{
|
||||||
|
launchedCount++;
|
||||||
|
}
|
||||||
|
else if (!string.IsNullOrWhiteSpace(error))
|
||||||
|
{
|
||||||
|
failures.Add(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
popup?.Close();
|
||||||
|
|
||||||
|
if (failures.Count > 0)
|
||||||
|
{
|
||||||
|
string message = $"Opened {launchedCount} item(s), but {failures.Count} item(s) failed:\n\n" +
|
||||||
|
string.Join("\n\n", failures.Take(5));
|
||||||
|
System.Windows.MessageBox.Show(message, "Taskbar Launcher");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool TryLaunchItem(LauncherItem item, out string? error)
|
||||||
|
{
|
||||||
|
error = null;
|
||||||
|
|
||||||
if (item.Type == LauncherItemType.Menu || string.IsNullOrWhiteSpace(item.Target))
|
if (item.Type == LauncherItemType.Menu || string.IsNullOrWhiteSpace(item.Target))
|
||||||
{
|
{
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
string target = Environment.ExpandEnvironmentVariables(item.Target);
|
string target = Environment.ExpandEnvironmentVariables(item.Target);
|
||||||
|
|
||||||
ProcessStartInfo startInfo = new()
|
try
|
||||||
{
|
{
|
||||||
FileName = target,
|
ProcessStartInfo startInfo = new()
|
||||||
Arguments = item.Arguments ?? "",
|
{
|
||||||
UseShellExecute = true,
|
FileName = target,
|
||||||
WorkingDirectory = Directory.Exists(target) ? target : ConfigService.AppFolder
|
Arguments = item.Arguments ?? "",
|
||||||
};
|
UseShellExecute = true,
|
||||||
|
WorkingDirectory = Directory.Exists(target) ? target : ConfigService.AppFolder
|
||||||
|
};
|
||||||
|
|
||||||
Process.Start(startInfo);
|
Process.Start(startInfo);
|
||||||
popup?.Close();
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
error = $"{item.Title}: {ex.Message}";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,13 +17,15 @@ public partial class LauncherPopup : Window
|
|||||||
|
|
||||||
private readonly LauncherConfig config;
|
private readonly LauncherConfig config;
|
||||||
private readonly Action<LauncherItem> launchItem;
|
private readonly Action<LauncherItem> launchItem;
|
||||||
|
private readonly Action<LauncherItem> openAllItems;
|
||||||
private readonly Action showSettings;
|
private readonly Action showSettings;
|
||||||
|
|
||||||
public LauncherPopup(LauncherConfig config, Action<LauncherItem> launchItem, Action showSettings)
|
public LauncherPopup(LauncherConfig config, Action<LauncherItem> launchItem, Action<LauncherItem> openAllItems, Action showSettings)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.launchItem = launchItem;
|
this.launchItem = launchItem;
|
||||||
|
this.openAllItems = openAllItems;
|
||||||
this.showSettings = showSettings;
|
this.showSettings = showSettings;
|
||||||
BuildItems();
|
BuildItems();
|
||||||
SizeChanged += (_, _) => QueueReposition();
|
SizeChanged += (_, _) => QueueReposition();
|
||||||
@@ -102,7 +104,7 @@ public partial class LauncherPopup : Window
|
|||||||
{
|
{
|
||||||
Expander expander = new()
|
Expander expander = new()
|
||||||
{
|
{
|
||||||
Header = item.Title,
|
Header = CreateMenuHeader(item),
|
||||||
IsExpanded = expandMenusByDefault,
|
IsExpanded = expandMenusByDefault,
|
||||||
Margin = new Thickness(depth * 12, 4, 0, 4)
|
Margin = new Thickness(depth * 12, 4, 0, 4)
|
||||||
};
|
};
|
||||||
@@ -143,6 +145,34 @@ public partial class LauncherPopup : Window
|
|||||||
return button;
|
return button;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private TextBlock CreateMenuHeader(LauncherItem item)
|
||||||
|
{
|
||||||
|
System.Windows.Controls.MenuItem openAllMenuItem = new()
|
||||||
|
{
|
||||||
|
Header = "Open All",
|
||||||
|
IsEnabled = item.Children.Any(IsLaunchable)
|
||||||
|
};
|
||||||
|
openAllMenuItem.Click += (_, e) =>
|
||||||
|
{
|
||||||
|
openAllItems(item);
|
||||||
|
e.Handled = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
System.Windows.Controls.ContextMenu contextMenu = new();
|
||||||
|
contextMenu.Items.Add(openAllMenuItem);
|
||||||
|
|
||||||
|
return new TextBlock
|
||||||
|
{
|
||||||
|
Text = item.Title,
|
||||||
|
ContextMenu = contextMenu
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool IsLaunchable(LauncherItem item)
|
||||||
|
{
|
||||||
|
return item.Type != LauncherItemType.Menu && !string.IsNullOrWhiteSpace(item.Target);
|
||||||
|
}
|
||||||
|
|
||||||
private void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
|
private void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
|
||||||
{
|
{
|
||||||
string query = SearchBox.Text.Trim();
|
string query = SearchBox.Text.Trim();
|
||||||
|
|||||||
Reference in New Issue
Block a user