Add open all menu action
This commit is contained in:
@@ -62,7 +62,7 @@ public sealed class LauncherController : IDisposable
|
||||
return;
|
||||
}
|
||||
|
||||
popup = new LauncherPopup(Config, LaunchItem, ShowSettings);
|
||||
popup = new LauncherPopup(Config, LaunchItem, OpenAllItems, ShowSettings);
|
||||
popup.Closed += (_, _) => popup = null;
|
||||
popup.ShowNearTaskbar();
|
||||
}
|
||||
@@ -126,13 +126,63 @@ public sealed class LauncherController : IDisposable
|
||||
|
||||
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))
|
||||
{
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
string target = Environment.ExpandEnvironmentVariables(item.Target);
|
||||
|
||||
try
|
||||
{
|
||||
ProcessStartInfo startInfo = new()
|
||||
{
|
||||
FileName = target,
|
||||
@@ -142,6 +192,12 @@ public sealed class LauncherController : IDisposable
|
||||
};
|
||||
|
||||
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 Action<LauncherItem> launchItem;
|
||||
private readonly Action<LauncherItem> openAllItems;
|
||||
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();
|
||||
this.config = config;
|
||||
this.launchItem = launchItem;
|
||||
this.openAllItems = openAllItems;
|
||||
this.showSettings = showSettings;
|
||||
BuildItems();
|
||||
SizeChanged += (_, _) => QueueReposition();
|
||||
@@ -102,7 +104,7 @@ public partial class LauncherPopup : Window
|
||||
{
|
||||
Expander expander = new()
|
||||
{
|
||||
Header = item.Title,
|
||||
Header = CreateMenuHeader(item),
|
||||
IsExpanded = expandMenusByDefault,
|
||||
Margin = new Thickness(depth * 12, 4, 0, 4)
|
||||
};
|
||||
@@ -143,6 +145,34 @@ public partial class LauncherPopup : Window
|
||||
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)
|
||||
{
|
||||
string query = SearchBox.Text.Trim();
|
||||
|
||||
Reference in New Issue
Block a user