Skip to content

Commit 5b90d08

Browse files
authored
Merge pull request #623 from Flow-Launcher/GlyphIcon
Add Glyph Icon Support
2 parents 661b8cf + d24379f commit 5b90d08

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
lines changed

Flow.Launcher.Plugin/GlyphInfo.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows.Media;
7+
8+
namespace Flow.Launcher.Plugin
9+
{
10+
public record GlyphInfo(string FontFamily, string Glyph);
11+
}

Flow.Launcher.Plugin/Result.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ public string IcoPath
4747

4848
public delegate ImageSource IconDelegate();
4949

50-
public IconDelegate Icon;
50+
/// <summary>
51+
/// Delegate to Get Image Source
52+
/// </summary>
53+
public IconDelegate Icon { get; set; }
54+
55+
/// <summary>
56+
/// Information for Glyph Icon
57+
/// </summary>
58+
public GlyphInfo Glyph { get; init; }
5159

5260

5361
/// <summary>

Flow.Launcher/ResultListBox.xaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
<ColumnDefinition Width="0" />
4343
</Grid.ColumnDefinitions>
4444
<Image x:Name="ImageIcon" Width="32" Height="32" HorizontalAlignment="Left"
45-
Source="{Binding Image}" />
45+
Source="{Binding Image}" Visibility="{Binding ShowIcon}" />
46+
<TextBlock Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center"
47+
Text="{Binding Glyph.Glyph}" FontFamily="{Binding Glyph.FontFamily}" FontSize="24"
48+
Visibility="{Binding ShowGlyph}"/>
4649
<Grid Margin="5 0 5 0" Grid.Column="1" HorizontalAlignment="Stretch">
4750
<Grid.RowDefinitions>
4851
<RowDefinition />

Flow.Launcher/ViewModel/ResultViewModel.cs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Flow.Launcher.Infrastructure.Logger;
77
using Flow.Launcher.Infrastructure.UserSettings;
88
using Flow.Launcher.Plugin;
9+
using System.IO;
910

1011
namespace Flow.Launcher.ViewModel
1112
{
@@ -16,24 +17,49 @@ public ResultViewModel(Result result, Settings settings)
1617
if (result != null)
1718
{
1819
Result = result;
20+
21+
if (Result.Glyph is { FontFamily: not null } glyph)
22+
{
23+
// Checks if it's a system installed font, which does not require path to be provided.
24+
if (glyph.FontFamily.EndsWith(".ttf") || glyph.FontFamily.EndsWith(".otf"))
25+
{
26+
var fontPath = Result.Glyph.FontFamily;
27+
Glyph = Path.IsPathRooted(fontPath)
28+
? Result.Glyph
29+
: Result.Glyph with
30+
{
31+
FontFamily = Path.Combine(Result.PluginDirectory, fontPath)
32+
};
33+
}
34+
else
35+
{
36+
Glyph = glyph;
37+
}
38+
}
1939
}
2040

2141
Settings = settings;
2242
}
2343

24-
public Settings Settings { get; private set; }
44+
private Settings Settings { get; }
45+
46+
public Visibility ShowOpenResultHotkey =>
47+
Settings.ShowOpenResultHotkey ? Visibility.Visible : Visibility.Hidden;
2548

26-
public Visibility ShowOpenResultHotkey => Settings.ShowOpenResultHotkey ? Visibility.Visible : Visibility.Hidden;
49+
public Visibility ShowIcon => Result.IcoPath != null || Result.Icon is not null || Glyph == null
50+
? Visibility.Visible
51+
: Visibility.Hidden;
2752

53+
public Visibility ShowGlyph => Glyph is not null ? Visibility.Visible : Visibility.Hidden;
2854
public string OpenResultModifiers => Settings.OpenResultModifiers;
2955

3056
public string ShowTitleToolTip => string.IsNullOrEmpty(Result.TitleToolTip)
31-
? Result.Title
32-
: Result.TitleToolTip;
57+
? Result.Title
58+
: Result.TitleToolTip;
3359

3460
public string ShowSubTitleToolTip => string.IsNullOrEmpty(Result.SubTitleToolTip)
35-
? Result.SubTitle
36-
: Result.SubTitleToolTip;
61+
? Result.SubTitle
62+
: Result.SubTitleToolTip;
3763

3864
private volatile bool ImageLoaded;
3965

@@ -48,10 +74,14 @@ public ImageSource Image
4874
ImageLoaded = true;
4975
_ = LoadImageAsync();
5076
}
77+
5178
return image;
5279
}
5380
private set => image = value;
5481
}
82+
83+
public GlyphInfo Glyph { get; set; }
84+
5585
private async ValueTask LoadImageAsync()
5686
{
5787
var imagePath = Result.IcoPath;
@@ -64,7 +94,9 @@ private async ValueTask LoadImageAsync()
6494
}
6595
catch (Exception e)
6696
{
67-
Log.Exception($"|ResultViewModel.Image|IcoPath is empty and exception when calling Icon() for result <{Result.Title}> of plugin <{Result.PluginDirectory}>", e);
97+
Log.Exception(
98+
$"|ResultViewModel.Image|IcoPath is empty and exception when calling Icon() for result <{Result.Title}> of plugin <{Result.PluginDirectory}>",
99+
e);
68100
}
69101
}
70102

@@ -74,7 +106,7 @@ private async ValueTask LoadImageAsync()
74106
image = ImageLoader.Load(imagePath);
75107
return;
76108
}
77-
109+
78110
// We need to modify the property not field here to trigger the OnPropertyChanged event
79111
Image = await Task.Run(() => ImageLoader.Load(imagePath)).ConfigureAwait(false);
80112
}

0 commit comments

Comments
 (0)