Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#364) Show help to create macros #449

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Lanceur.Core/MacroAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@ public class MacroAttribute : Attribute

#region Constructors

public MacroAttribute(string name)
public MacroAttribute(string name, bool isVisible = true)
{
IsVisible = isVisible;
_name = name.Trim().Replace("@", "").ToUpper();
}

#endregion Constructors

#region Properties

/// <summary>
/// Indicates whether this macro should appear in the list of macros
/// This is meant to create some macro for "privileged" people. That's
/// some debugging tools for the developer.
/// </summary>
public bool IsVisible { get; }
public string Name => $"@{_name}@";

#endregion Properties
Expand Down
48 changes: 48 additions & 0 deletions src/Lanceur.Infra/Utils/MacroLister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Lanceur.Core;
using System.Reflection;

namespace Lanceur.Infra.Utils;

public class MacroLister
{
#region Fields

private readonly object _source;
private MacroAttribute[] _macros;

#endregion Fields

#region Constructors

public MacroLister(object source)
{
_source = source;
}

#endregion Constructors

#region Methods

private MacroAttribute[] GetMacroAttributes()
{
return Assembly.GetAssembly(_source.GetType())!.GetTypes()
.SelectMany(t => t.GetCustomAttributes<MacroAttribute>())
.ToArray();
}

public IEnumerable<MacroAttribute> GetAttributes()
{
_macros ??= GetMacroAttributes();
return _macros.ToArray();
}

public IEnumerable<MacroAttribute> GetVisibleAttributes()
{
_macros ??= GetMacroAttributes();

return _macros.Where(t => t.IsVisible)
.ToArray(); ;
}

#endregion Methods
}
2 changes: 1 addition & 1 deletion src/Lanceur/Macros/Development/DebugMacro.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Lanceur.Macros.Development
{
[Macro("debug"), Description("Provides some debugging tools. But it is more an easter egg than something else")]
[Macro("debug", isVisible: false), Description("Provides some debugging tools. But it is more an easter egg than something else")]
public class DebugMacro : MacroQueryResult
{
#region Fields
Expand Down
2 changes: 1 addition & 1 deletion src/Lanceur/Macros/Development/FixIconMacro.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Lanceur.Macros.Development;

[Macro("fixicon"), Description("Fix icons when alias is a directory or an hyperlink")]
[Macro("fixicon", isVisible: false), Description("Fix icons when alias is a directory or an hyperlink")]
public class FixIconMacro : MacroQueryResult
{
#region Properties
Expand Down
5 changes: 4 additions & 1 deletion src/Lanceur/Views/KeywordsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,12 @@
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel>
<TextBox
<ComboBox
x:Name="BoxFileName"
Margin="5"
DisplayMemberPath="."
HorizontalAlignment="Stretch"
IsEditable="True"
Text="{Binding ElementName=Aliases, Path=SelectedItem.FileName, UpdateSourceTrigger=PropertyChanged}"
Validation.ErrorTemplate="{StaticResource TextControlValidationErrorTemplate}" />
<TextBlock
Expand Down
2 changes: 2 additions & 0 deletions src/Lanceur/Views/KeywordsView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public KeywordsView()
interaction.SetOutput(result.ToBool());
});


this.OneWayBind(ViewModel, vm => vm.MacroCollection, v => v.BoxFileName.ItemsSource).DisposeWith(d);
this.OneWayBind(ViewModel, vm => vm.Aliases, v => v.Aliases.ItemsSource).DisposeWith(d);
this.OneWayBind(ViewModel, vm => vm.BusyMessage, v => v.BusyMessage.Text).DisposeWith(d);
this.OneWayBind(ViewModel, vm => vm.IsBusy, v => v.BusyControl.Visibility).DisposeWith(d);
Expand Down
27 changes: 23 additions & 4 deletions src/Lanceur/Views/KeywordsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@
using ReactiveUI.Validation.Helpers;
using Splat;
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Windows.Media.TextFormatting;
using DynamicData;
using Humanizer;
using Lanceur.Core.BusinessLogic;
using Lanceur.SharedKernel.Utils;
using Lanceur.Utils;
using System.Reflection;
using Lanceur.Core;
using Lanceur.Infra.Utils;

namespace Lanceur.Views
{
Expand All @@ -45,6 +48,7 @@ public class KeywordsViewModel : RoutableViewModel, IValidatableViewModel, IActi
private readonly IPackagedAppSearchService _packagedAppSearchService;
private readonly ISchedulerProvider _schedulers;
private readonly IThumbnailManager _thumbnailManager;
private readonly MacroLister _macroLister;

#endregion Fields

Expand Down Expand Up @@ -72,6 +76,7 @@ public KeywordsViewModel(

ConfirmRemove = Interactions.YesNoQuestion(_schedulers.MainThreadScheduler);
AskLuaEditor = new();
_macroLister = new MacroLister(this);

this.WhenActivated(Activate);
}
Expand All @@ -88,6 +93,8 @@ public KeywordsViewModel(

[Reactive] public AliasQueryResult AliasToCreate { get; set; }

[Reactive] public ObservableCollection<string> MacroCollection { get; set; }

public Interaction<Script, string> AskLuaEditor { get; }

[Reactive] public string BusyMessage { get; set; }
Expand Down Expand Up @@ -309,8 +316,14 @@ private void SetupValidations(CompositeDisposable d)
{
var validateFileNameExists = this.WhenAnyValue(
x => x.SelectedAlias.FileName,
x => !x.IsNullOrEmpty()
);
x =>
{
if (x.IsNullOrEmpty()) return false;
if (!x.StartsWith('@')) return true;

return x.StartsWith('@') && _macroLister.GetAttributes().Any(a => a.Name == x.ToUpper());
});

var validateNameExists = this.WhenAnyValue(
x => x.SelectedAlias.SynonymsToAdd,
x => _aliasService.SelectNames(x.SplitCsv())
Expand All @@ -319,7 +332,7 @@ private void SetupValidations(CompositeDisposable d)
ValidationFileName = this.ValidationRule(
vm => vm.SelectedAlias.FileName,
validateFileNameExists,
"The path to the file shouldn't be empty."
"Either the path to the file is empty or the macro does not exist."
);
ValidationFileName.DisposeWith(d);

Expand Down Expand Up @@ -357,6 +370,12 @@ internal void Activate(CompositeDisposable d)
SetupValidations(d);
SetupCommands(_schedulers.MainThreadScheduler, _notify, d);
SetupBindings(_schedulers.MainThreadScheduler, d);

//load macros
var macros = _macroLister.GetVisibleAttributes()
.Select(x => x.Name)
.ToArray();
MacroCollection = new(macros);
}

public void HydrateSelectedAlias() => _aliasService.HydrateAlias(SelectedAlias);
Expand Down
Loading