Skip to content

Commit

Permalink
OmnibarService for MenuBar control + switch theme fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbouez committed Oct 8, 2023
1 parent f476376 commit 879c6ef
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Coho.UI/Controls/Menus/MenuBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Windows.Controls;
using System.Windows.Forms;
using Coho.UI.CommandManaging;
using Coho.UI.Controls.Omnibar;
using Coho.UI.Controls.Ribbon;
using Coho.UI.Interfaces;
using Coho.UI.Tools;
Expand Down Expand Up @@ -178,5 +179,6 @@ private void OnLoaded(object sender, RoutedEventArgs e)
}

CommandManager.RebuildCommandsCache(this);
OmnibarSearchService.RegisterOmnibarSearchService(new MenuBarOmnibarSearchService());
}
}
51 changes: 51 additions & 0 deletions Coho.UI/Controls/Omnibar/MenuBarOmnibarSearchService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// *********************************************************
//
// Coho.UI
// RibbonOmnibarSearchService.cs
// Copyright (c) Sébastien Bouez. All rights reserved.
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// *********************************************************

using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Coho.UI.CommandManaging;

namespace Coho.UI.Controls.Omnibar;

public class MenuBarOmnibarSearchService : OmnibarSearchServiceBase
{
public override bool IsDefault
{
get
{
return true;
}
}

public override string DisplayName
{
get
{
return "Search command";
}
}

public override IEnumerable<OmnibarSearchResult> ExecuteSearch(string terms)
{
IEnumerable<OmnibarSearchResult> foundCommands = CommandManager.FindCommands(terms);

IEnumerable<OmnibarSearchResult> filteredCommands = from a in foundCommands
where (a.LinkedOriginalObject is FrameworkElement el) && el.IsEnabled
select a;

return filteredCommands;
}
}
14 changes: 13 additions & 1 deletion Coho.UI/Controls/Omnibar/OmnibarControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private void GotoOmniboxResultPage()
break;

case OmnibarSearchResult.EOmnibarCommandType.RibbonCommand:
selectedSearchResult.CommandRibbonButton?.RaiseClick();
HandleCommandClick(selectedSearchResult);
break;
}

Expand All @@ -119,6 +119,18 @@ private void GotoOmniboxResultPage()
PopupOmnibarResults.IsOpen = false;
}

private void HandleCommandClick(OmnibarSearchResult selectedSearchResult)
{
if (selectedSearchResult.CommandRibbonButton != null)
{
selectedSearchResult.CommandRibbonButton.RaiseClick();
}
else if (selectedSearchResult.LinkedOriginalObject is MenuItem menuItem)
{
menuItem.RaiseEvent(new RoutedEventArgs(MenuItem.ClickEvent));
}
}

private void ListOmnibarFilesResults_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (ListOmnibarResults.SelectedItem != null && e.Key == Key.Enter)
Expand Down
6 changes: 3 additions & 3 deletions Coho.UI/Dialogs/OpenFileDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal ThemedSpecialDialogOptions Options
{
get;
set;
} = new ThemedSpecialDialogOptions();
} = new();

private void OnLoaded(object sender, RoutedEventArgs e)
{
Expand All @@ -62,7 +62,7 @@ private void OnLoaded(object sender, RoutedEventArgs e)
private void BuildFileTypesCombo()
{
CbFileType.IsEnabled = false;
foreach (KeyValuePair<string, string> keyValuePair in Options.FileTypes!)
foreach (KeyValuePair<string, string> keyValuePair in Options.FileTypes)
{
CbFileType.Items.Add(new ComboBoxItem
{
Expand Down Expand Up @@ -144,7 +144,7 @@ private bool ExtensionMatchDesiredFileTypes(string filePath)
return false;
}

return Options.FileTypes!.Values.Any(x => x.Split(';', StringSplitOptions.RemoveEmptyEntries).Any(y => y.Trim('*').Equals(fi.Extension, StringComparison.InvariantCulture)));
return Options.FileTypes.Values.Any(x => x.Split(';', StringSplitOptions.RemoveEmptyEntries).Any(y => y.Trim('*').Equals(fi.Extension, StringComparison.InvariantCulture)));
}

private void TbFileName_OnKeyUp(object sender, KeyEventArgs e)
Expand Down
2 changes: 0 additions & 2 deletions Coho.UI/Dialogs/ThemedMultiTaskDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ namespace Coho.UI.Dialogs;

public static class ThemedMultiTaskDialog
{


public static void Show(string title, string message, List<TaskRunnerBase> tasksToRun)
{
Show(title, message, Application.Current.MainWindow!, tasksToRun);
Expand Down
14 changes: 6 additions & 8 deletions Coho.UI/UIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ private static void InstallTheme(string baseThemeName)
resourcesToRemove.Add(Application.Current.Resources.MergedDictionaries[i]);
}

_customThemedResourceNames.TryGetValue(Application.Current.Resources.MergedDictionaries[i].Source.OriginalString, out ThemeScheme? dt);

if (dt != null && dt != Theme)
if (_customThemedResourceNames.ContainsKey(Application.Current.Resources.MergedDictionaries[i].Source.OriginalString))
{
resourcesToRemove.Add(Application.Current.Resources.MergedDictionaries[i]);
}
Expand All @@ -112,6 +110,11 @@ private static void InstallTheme(string baseThemeName)
Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
}

Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri(string.Format(CultureInfo.InvariantCulture, "/Coho.UI;component/Themes/{0}.xaml", baseThemeName), UriKind.RelativeOrAbsolute)
});

foreach (KeyValuePair<string, ThemeScheme?> themedResource in _customThemedResourceNames.Where(x =>
x.Value == null || x.Value.Equals(Theme)))
{
Expand All @@ -120,10 +123,5 @@ private static void InstallTheme(string baseThemeName)
Source = new Uri(themedResource.Key, UriKind.RelativeOrAbsolute)
});
}

Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri(string.Format(CultureInfo.InvariantCulture, "/Coho.UI;component/Themes/{0}.xaml", baseThemeName), UriKind.RelativeOrAbsolute)
});
}
}
10 changes: 9 additions & 1 deletion Coho.UI/Windows/FluentWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,15 @@ public virtual bool IsSpecialState

protected void OnSourceInitializedBase(Window sender)
{
HwndSource? windowHandleSource = HwndSource.FromHwnd(new WindowInteropHelper(sender).Handle);
if (!sender.IsVisible)
{
return;
}

var interopHelper = new WindowInteropHelper(sender);
interopHelper.EnsureHandle();

HwndSource? windowHandleSource = HwndSource.FromHwnd(interopHelper.Handle);
if (windowHandleSource == null)
{
return;
Expand Down

0 comments on commit 879c6ef

Please sign in to comment.