Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
garyng committed Oct 17, 2020
2 parents accb848 + 3dfda72 commit c04487b
Show file tree
Hide file tree
Showing 24 changed files with 384 additions and 81 deletions.
30 changes: 19 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,25 @@ A `yaml` file for configuring the app's behavior.

```yaml
directory: shortcuts
activation: LWin + Escape
autoHide: true
topmost: true
```
- `directory` specifies the directory that contains the [shortcuts file](#shortcuts-file).
- `directory`: the directory that contains the [shortcuts file](#shortcuts-file).
- `activation`: the shortcut key to activate the main window (see [Keys enum](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.keys?view=netcore-3.1) for available keys)
- Default value: `LWin + Escape`
- `autoHide`: automatically hides the window when focus is lost
- Default value: `false`
- `topmost`: keep the window always on top
- Default value: `false`

Order of configuration precedence:

1. `wims.yml` at home directory
1. `wims.yml` at the same directory as the app's exe

The last loaded key wins.

The last loaded config key wins.

## Shortcuts file

Expand All @@ -72,11 +80,11 @@ contexts:
exe: devenv.exe
```

- `"vs"` is the name of the context, which can be used by a `shortcut`.
- `icon` contains the path to an image. `svg` is also supported.
- `match` contains the conditions for determining whether the context is active.
- `exe` will match the process name of the current active window. Normally using `exe` is sufficient enough.
- `class` will match the class name of the current active window.
- `"vs"`: the name of the context, which can be used by a `shortcut`.
- `icon`: the path to an image. `svg` is also supported.
- `match`: the conditions for determining whether the context is active.
- `exe`: match the process name of the current active window. Normally using `exe` is sufficient enough.
- `class`: match the class name of the current active window.
- Regex can be used in both `exe` and `class`, just wrap them inside `//`, eg: `/*.exe/`

Here is another `context` that matches a Visual Studio Code window:
Expand Down Expand Up @@ -107,11 +115,11 @@ shortcuts:
- LeftArrow
```

- `"Navigate backward"` is the name of the shortcut.
- `"Navigate backward"`: the name of the shortcut.
- This is used for matching the search query when [searching with text](#search-by-text).
- `context` is the name of the [`context`](#contexts).
- `context`: the name of the [`context`](#contexts).
- If this is left empty, the shortcut is considered as "global", and will be active only when there is no active context.
- `sequence` is a sequence of chords that are pressed subsequently, eg: `Ctrl + K, Ctrl + F`
- `sequence`: a sequence of chords that are pressed subsequently, eg: `Ctrl + K, Ctrl + F`
- A chord is a sequence of keys that are pressed at the same time, eg: `Ctrl + Shift + P`
- This is used for matching the search query when [searching with keys](#search-by-keys).
# Development
Expand Down
16 changes: 16 additions & 0 deletions src/Wims.Core/Models/WimsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,21 @@ public class WimsConfig
/// The home directory where all the <see cref="ShortcutsRaw"/> files are saved.
/// </summary>
public string Directory { get; set; }


/// <summary>
/// Shortcut for activating the app.
/// </summary>
public string Activation { get; set; }

/// <summary>
/// Auto hide app when lost focus.
/// </summary>
public bool AutoHide { get; set; }

/// <summary>
/// Keep window always on top
/// </summary>
public bool Topmost { get; set; }
}
}
16 changes: 16 additions & 0 deletions src/Wims.Tests/OrderedRangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,21 @@ static IEnumerable<object> FillGapsWithMin()
}
};
}

[TestCase(0, 0, 0)]
[TestCase(1, 0, 1)]
[TestCase(1, 10, 9)]
[TestCase(10, 1, 9)]
public void Should_CalculateLengthCorrectly(int start, int end, int expected)
{
// Arrange
var range = new OrderedRange(start, end);

// Act
var result = range.Length;

// Assert
result.Should().Be(expected);
}
}
}
1 change: 1 addition & 0 deletions src/Wims.Ui/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Style BasedOn="{StaticResource MaterialDesignScrollBarMinimal}" TargetType="{x:Type ScrollBar}">
<Setter Property="materialDesign:ScrollBarAssist.ThumbCornerRadius" Value="4" />
<Setter Property="materialDesign:ScrollBarAssist.ThumbWidth" Value="8" />
<Setter Property="materialDesign:ScrollBarAssist.ThumbHeight" Value="8" />
</Style>
<FontFamily x:Key="RobotoMono">pack://application;,,,/Fonts/#Roboto Mono</FontFamily>

Expand Down
8 changes: 8 additions & 0 deletions src/Wims.Ui/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,17 @@ public App()

private void PreprocessConfig(WimsConfig config)
{
// todo: move this and rename this as SetDefaultConfig?
// todo: consolidate all defaults value in one place

config.Directory = string.IsNullOrEmpty(config.Directory)
? Directory.GetCurrentDirectory()
: Path.GetFullPath(config.Directory);

config.Activation = string.IsNullOrEmpty(config.Activation)
? "LWin+Escape"
: config.Activation;

}

protected override async void OnStartup(StartupEventArgs e)
Expand Down
2 changes: 2 additions & 0 deletions src/Wims.Ui/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using ReactiveUI;
using Splat;
using Splat.Microsoft.Extensions.DependencyInjection;
using Wims.Ui.Converters;
using Wims.Ui.Profiles;
using Wims.Ui.Requests;
using Wims.Ui.Validators;
Expand All @@ -32,6 +33,7 @@ public void ConfigureServices(IHostEnvironment environment, IConfiguration confi
services.AddSingleton<IFileSystem>(new FileSystem());

services.AddSingleton<ContextService>();
services.AddSingleton<IErrorHandler, ErrorHandler>();


//services.Scan(scan => scan
Expand Down
1 change: 1 addition & 0 deletions src/Wims.Ui/Controls/Highlighter/OrderedRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class OrderedRange
{
public int Start { get; }
public int End { get; }
public int Length => End - Start;

/// <param name="start">Inclusive start index</param>
/// <param name="end">Exclusive end index</param>
Expand Down
29 changes: 15 additions & 14 deletions src/Wims.Ui/Controls/Highlighter/TextBlockHighlighter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace Wims.Ui.Controls.Highlighter
{
public class TextBlockHighlighter : DependencyObject
{
public static readonly DependencyProperty HighlightBrushProperty = DependencyProperty.RegisterAttached(
"HighlightBrush", typeof(Brush), typeof(TextBlockHighlighter),
new PropertyMetadata(default(Brush), UpdateHighlight));
public static readonly DependencyProperty EnableProperty = DependencyProperty.RegisterAttached(
"Enable", typeof(bool), typeof(TextBlockHighlighter), new PropertyMetadata(true, UpdateHighlight));

public static void SetHighlightBrush(DependencyObject element, Brush value)
public static void SetEnable(DependencyObject element, bool value)
{
element.SetValue(HighlightBrushProperty, value);
element.SetValue(EnableProperty, value);
}

public static Brush GetHighlightBrush(DependencyObject element)
public static bool GetEnable(DependencyObject element)
{
return (Brush) element.GetValue(HighlightBrushProperty);
return (bool) element.GetValue(EnableProperty);
}

public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
Expand Down Expand Up @@ -49,7 +47,7 @@ public static void SetRanges(DependencyObject element, List<Range> value)

public static List<OrderedRange> GetRanges(DependencyObject element)
{
return (List<OrderedRange>)element.GetValue(RangesProperty);
return (List<OrderedRange>) element.GetValue(RangesProperty);
}

private static void UpdateHighlight(DependencyObject d, DependencyPropertyChangedEventArgs e)
Expand All @@ -60,8 +58,12 @@ private static void UpdateHighlight(DependencyObject d, DependencyPropertyChange
var text = GetText(tb);
if (string.IsNullOrEmpty(text)) return;

var brush = GetHighlightBrush(tb);
if (brush == null) return;
var enabled = GetEnable(tb);
if (!enabled)
{
tb.Inlines.Add(new Run(text));
return;
}

var ranges = GetRanges(tb).ToList();
if (ranges?.Count == 0)
Expand All @@ -76,11 +78,10 @@ private static void UpdateHighlight(DependencyObject d, DependencyPropertyChange
// todo: will this be too slow?
.OrderBy(item => item.range.Start)
.ThenBy(item => item.range.End)
.Where(item => item.range.End <= text.Length)
.Select(item => new Run(text[item.range.Start..item.range.End])
{
// FontWeight = item.highlight ? FontWeights.Heavy : tb.FontWeight,
Foreground = item.highlight ? brush : tb.Foreground
// Background = r.Highlight ? Brushes.Yellow : tb.Background
TextDecorations = item.highlight ? TextDecorations.Underline : null
});

tb.Inlines.AddRange(runs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;

namespace Wims.Ui.Utils
namespace Wims.Ui.Converters
{
public class ChordRoConverter : IYamlTypeConverter
{
Expand Down
28 changes: 28 additions & 0 deletions src/Wims.Ui/Converters/ObjectIsEqualConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;

namespace Wims.Ui.Converters
{
public class ObjectIsEqualConverter : MarkupExtension, IValueConverter
{
private static Lazy<ObjectIsEqualConverter> _instance =
new Lazy<ObjectIsEqualConverter>(() => new ObjectIsEqualConverter());

public override object ProvideValue(IServiceProvider serviceProvider)
{
return _instance.Value;
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value?.Equals(parameter);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return new NotSupportedException();
}
}
}
44 changes: 44 additions & 0 deletions src/Wims.Ui/Converters/PackIconToImageConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
using MaterialDesignThemes.Wpf;

namespace Wims.Ui.Converters
{
/// <summary>
/// Converts a <see cref="PackIcon" /> to an DrawingImage.
/// Use the ConverterParameter to pass a Brush.
/// </summary>
public class PackIconToImageConverter : MarkupExtension, IValueConverter
{
private static Lazy<PackIconToImageConverter> _instance =
new Lazy<PackIconToImageConverter>(() => new PackIconToImageConverter());

public override object ProvideValue(IServiceProvider serviceProvider)
{
return _instance.Value;
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is PackIcon icon)) return value;

GeometryDrawing geoDrawing = new GeometryDrawing();

geoDrawing.Brush = parameter as Brush ?? Brushes.Black;
geoDrawing.Pen = new Pen(geoDrawing.Brush, 0.25);
geoDrawing.Geometry = Geometry.Parse(icon.Data);

var drawingGroup = new DrawingGroup {Children = {geoDrawing}, Transform = new ScaleTransform(1, -1)};

return new DrawingImage {Drawing = drawingGroup};
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using ReactiveUI;
using SharpVectors.Converters;

namespace Wims.Ui
namespace Wims.Ui.Converters
{
public class RxStringToImageSourceConverter : IBindingTypeConverter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Windows.Markup;
using Wims.Core.Dto;

namespace Wims.Ui.Utils
namespace Wims.Ui.Converters
{
public class SequenceDtoToStringConverter : MarkupExtension, IValueConverter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Windows.Media.Imaging;
using SharpVectors.Converters;

namespace Wims.Ui
namespace Wims.Ui.Converters
{
public class StringToImageSourceConverter : MarkupExtension, IValueConverter
{
Expand Down
23 changes: 23 additions & 0 deletions src/Wims.Ui/ErrorHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Windows;

namespace Wims.Ui
{
public interface IErrorHandler
{
void OnError(Exception e);
}

public class ErrorHandler : IErrorHandler
{
public void OnError(Exception e)
{
Application.Current.Dispatcher.Invoke(() =>
{
var window = new ErrorWindow(e);
window.ShowDialog();
});

}
}
}
Loading

0 comments on commit c04487b

Please sign in to comment.