mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Refactor and enhance history management system
Refactored the `History` class to separate query and last opened history into distinct models, introducing `LastOpenedHistoryItem` and deprecating `HistoryItem`. Added a `HistoryStyle` enum and property to allow users to toggle between "Query History" and "Last Opened History." Simplified history display logic and updated the UI with a dropdown for history style selection. Improved history item display with dynamic action population and relative timestamps. Enhanced performance by optimizing history storage with a maximum limit and streamlined logic for adding/retrieving items. Ensured backward compatibility by migrating legacy history data to the new format. Updated localization strings, removed deprecated properties, and cleaned up redundant code. Fixed bugs related to inconsistent history actions and edge cases with legacy data.
This commit is contained in:
parent
b84ca9b283
commit
f6d5a27e0b
12 changed files with 247 additions and 240 deletions
|
|
@ -7,6 +7,7 @@ using CommunityToolkit.Mvvm.DependencyInjection;
|
|||
using Flow.Launcher.Infrastructure.Hotkey;
|
||||
using Flow.Launcher.Infrastructure.Logger;
|
||||
using Flow.Launcher.Infrastructure.Storage;
|
||||
using Flow.Launcher.Localization.Attributes;
|
||||
using Flow.Launcher.Plugin;
|
||||
using Flow.Launcher.Plugin.SharedModels;
|
||||
|
||||
|
|
@ -214,64 +215,17 @@ namespace Flow.Launcher.Infrastructure.UserSettings
|
|||
}
|
||||
}
|
||||
}
|
||||
private bool _showHistoryOnHomePage = true;
|
||||
public bool ShowHistoryOnHomePage
|
||||
|
||||
private bool _showHistoryResultsForHomePage = false;
|
||||
public bool ShowHistoryResultsForHomePage
|
||||
{
|
||||
get
|
||||
{
|
||||
if (ShowHistoryQueryResultsForHomePage || ShowHistoryLastOpenedResultsForHomePage) return true;
|
||||
return _showHistoryOnHomePage;
|
||||
}
|
||||
get => _showHistoryResultsForHomePage;
|
||||
set
|
||||
{
|
||||
if (_showHistoryOnHomePage != value)
|
||||
if (_showHistoryResultsForHomePage != value)
|
||||
{
|
||||
_showHistoryOnHomePage = value;
|
||||
_showHistoryResultsForHomePage = value;
|
||||
OnPropertyChanged();
|
||||
if (value == false)
|
||||
{
|
||||
ShowHistoryQueryResultsForHomePage = false;
|
||||
ShowHistoryLastOpenedResultsForHomePage = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool _showHistoryQueryResultsForHomePage = true;
|
||||
public bool ShowHistoryQueryResultsForHomePage
|
||||
{
|
||||
get => _showHistoryQueryResultsForHomePage;
|
||||
set
|
||||
{
|
||||
if (_showHistoryQueryResultsForHomePage != value)
|
||||
{
|
||||
_showHistoryQueryResultsForHomePage = value;
|
||||
OnPropertyChanged();
|
||||
if (value && _showHistoryLastOpenedResultsForHomePage)
|
||||
{
|
||||
_showHistoryLastOpenedResultsForHomePage = false;
|
||||
OnPropertyChanged(nameof(ShowHistoryLastOpenedResultsForHomePage));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private bool _showHistoryLastOpenedResultsForHomePage;
|
||||
public bool ShowHistoryLastOpenedResultsForHomePage
|
||||
{
|
||||
get => _showHistoryLastOpenedResultsForHomePage;
|
||||
set
|
||||
{
|
||||
if (_showHistoryLastOpenedResultsForHomePage != value)
|
||||
{
|
||||
_showHistoryLastOpenedResultsForHomePage = value;
|
||||
OnPropertyChanged();
|
||||
if (value && _showHistoryQueryResultsForHomePage)
|
||||
{
|
||||
_showHistoryQueryResultsForHomePage = false;
|
||||
OnPropertyChanged(nameof(ShowHistoryQueryResultsForHomePage));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -560,6 +514,21 @@ namespace Flow.Launcher.Infrastructure.UserSettings
|
|||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public LastQueryMode LastQueryMode { get; set; } = LastQueryMode.Selected;
|
||||
|
||||
private HistoryStyle _historyStyle = HistoryStyle.Query;
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public HistoryStyle HistoryStyle
|
||||
{
|
||||
get => _historyStyle;
|
||||
set
|
||||
{
|
||||
if (_historyStyle != value)
|
||||
{
|
||||
_historyStyle = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public AnimationSpeeds AnimationSpeed { get; set; } = AnimationSpeeds.Medium;
|
||||
public int CustomAnimationLength { get; set; } = 360;
|
||||
|
|
@ -742,4 +711,14 @@ namespace Flow.Launcher.Infrastructure.UserSettings
|
|||
FullPathOpen,
|
||||
Directory
|
||||
}
|
||||
|
||||
[EnumLocalize]
|
||||
public enum HistoryStyle
|
||||
{
|
||||
[EnumLocalizeKey(nameof(Localize.queryHistory))]
|
||||
Query,
|
||||
|
||||
[EnumLocalizeKey(nameof(Localize.executedHistory))]
|
||||
LastOpened
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -164,13 +164,12 @@
|
|||
<system:String x:Key="KoreanImeSettingChangeFailSubTitle">Please check your system registry access or contact support.</system:String>
|
||||
<system:String x:Key="homePage">Home Page</system:String>
|
||||
<system:String x:Key="homePageToolTip">Show home page results when query text is empty.</system:String>
|
||||
<system:String x:Key="homePageHistory">Home Page History</system:String>
|
||||
<system:String x:Key="homePageHistoryTooltip">Choose the type of history to show on the home page</system:String>
|
||||
<system:String x:Key="queryHistory">Query History</system:String>
|
||||
<system:String x:Key="executedHistory">Last Opened History</system:String>
|
||||
<system:String x:Key="historyQueryResultsForHomePage">Show Query History</system:String>
|
||||
<system:String x:Key="historyLastOpenedResultsForHomePage">Show Last Opened History</system:String>
|
||||
<system:String x:Key="historyResultsCountForHomePage">Maximum History Results Shown</system:String>
|
||||
<system:String x:Key="historyResultsForHomePage">Show History Results in Home Page</system:String>
|
||||
<system:String x:Key="historyResultsCountForHomePage">Maximum History Results Shown in Home Page</system:String>
|
||||
<system:String x:Key="historyStyle">History Style</system:String>
|
||||
<system:String x:Key="historyStyleTooltip">Choose the type of history to show in the history and home page</system:String>
|
||||
<system:String x:Key="queryHistory">Query history</system:String>
|
||||
<system:String x:Key="executedHistory">Last opened history</system:String>
|
||||
<system:String x:Key="homeToggleBoxToolTip">This can only be edited if plugin supports Home feature and Home Page is enabled.</system:String>
|
||||
<system:String x:Key="showAtTopmost">Show Search Window at Foremost</system:String>
|
||||
<system:String x:Key="showAtTopmostToolTip">Overrides other programs' 'Always on Top' setting and displays Flow in the foremost position.</system:String>
|
||||
|
|
|
|||
|
|
@ -321,8 +321,8 @@ namespace Flow.Launcher
|
|||
InitializeContextMenu();
|
||||
break;
|
||||
case nameof(Settings.ShowHomePage):
|
||||
case nameof(Settings.ShowHistoryQueryResultsForHomePage):
|
||||
case nameof(Settings.ShowHistoryLastOpenedResultsForHomePage):
|
||||
case nameof(Settings.ShowHistoryResultsForHomePage):
|
||||
case nameof(Settings.HistoryStyle):
|
||||
if (_viewModel.QueryResultsSelected() && string.IsNullOrEmpty(_viewModel.QueryText))
|
||||
{
|
||||
_viewModel.QueryResults();
|
||||
|
|
|
|||
|
|
@ -147,6 +147,8 @@ public partial class SettingsPaneGeneralViewModel : BaseModel
|
|||
public List<LastQueryModeData> LastQueryModes { get; } =
|
||||
DropdownDataGeneric<LastQueryMode>.GetValues<LastQueryModeData>("LastQuery");
|
||||
|
||||
public List<HistoryStyleLocalized> HistoryStyles { get; } = HistoryStyleLocalized.GetValues();
|
||||
|
||||
public bool EnableDialogJump
|
||||
{
|
||||
get => Settings.EnableDialogJump;
|
||||
|
|
@ -213,6 +215,7 @@ public partial class SettingsPaneGeneralViewModel : BaseModel
|
|||
DropdownDataGeneric<SearchWindowAligns>.UpdateLabels(SearchWindowAligns);
|
||||
DropdownDataGeneric<SearchPrecisionScore>.UpdateLabels(SearchPrecisionScores);
|
||||
DropdownDataGeneric<LastQueryMode>.UpdateLabels(LastQueryModes);
|
||||
HistoryStyleLocalized.UpdateLabels(HistoryStyles);
|
||||
DropdownDataGeneric<DoublePinyinSchemas>.UpdateLabels(DoublePinyinSchemas);
|
||||
DropdownDataGeneric<DialogJumpWindowPositions>.UpdateLabels(DialogJumpWindowPositions);
|
||||
DropdownDataGeneric<DialogJumpResultBehaviours>.UpdateLabels(DialogJumpResultBehaviours);
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@
|
|||
xmlns:userSettings="clr-namespace:Flow.Launcher.Infrastructure.UserSettings;assembly=Flow.Launcher.Infrastructure"
|
||||
Title="General"
|
||||
d:DataContext="{d:DesignInstance settingsViewModels:SettingsPaneGeneralViewModel}"
|
||||
d:DesignHeight="450"
|
||||
d:DesignHeight="10000"
|
||||
d:DesignWidth="800"
|
||||
mc:Ignorable="d">
|
||||
<ui:Page.Resources>
|
||||
|
|
@ -245,6 +245,22 @@
|
|||
SelectedValuePath="Value" />
|
||||
</ui:SettingsCard>
|
||||
|
||||
<ui:SettingsCard
|
||||
Margin="0 14 0 0"
|
||||
Description="{DynamicResource historyStyleTooltip}"
|
||||
Header="{DynamicResource historyStyle}">
|
||||
<ui:SettingsCard.HeaderIcon>
|
||||
<ui:FontIcon Glyph="" />
|
||||
</ui:SettingsCard.HeaderIcon>
|
||||
|
||||
<ComboBox
|
||||
MaxWidth="200"
|
||||
DisplayMemberPath="Display"
|
||||
ItemsSource="{Binding HistoryStyles}"
|
||||
SelectedValue="{Binding Settings.HistoryStyle}"
|
||||
SelectedValuePath="Value" />
|
||||
</ui:SettingsCard>
|
||||
|
||||
<ui:SettingsCard
|
||||
Margin="0 14 0 0"
|
||||
Description="{DynamicResource autoRestartAfterChangingToolTip}"
|
||||
|
|
@ -392,24 +408,18 @@
|
|||
OnContent="{DynamicResource enable}" />
|
||||
</ui:SettingsCard>
|
||||
|
||||
<ui:SettingsExpander Margin="0 4 0 0" Header="{DynamicResource homePageHistory}" Description="{DynamicResource homePageHistoryTooltip}" IsEnabled="{Binding Settings.ShowHomePage}">
|
||||
<ui:SettingsExpander Margin="0 4 0 0" Header="{DynamicResource historyResultsForHomePage}">
|
||||
<ui:SettingsExpander.HeaderIcon>
|
||||
<ui:FontIcon Glyph="" />
|
||||
</ui:SettingsExpander.HeaderIcon>
|
||||
|
||||
<ui:ToggleSwitch
|
||||
IsOn="{Binding Settings.ShowHistoryOnHomePage}"
|
||||
IsOn="{Binding Settings.ShowHistoryResultsForHomePage}"
|
||||
OffContent="{DynamicResource disable}"
|
||||
OnContent="{DynamicResource enable}" />
|
||||
|
||||
<ui:SettingsExpander.Items>
|
||||
<ui:SettingsCard Header="{DynamicResource historyQueryResultsForHomePage}" IsEnabled="{Binding Settings.ShowHistoryOnHomePage}">
|
||||
<RadioButton GroupName="HistoryType" IsChecked="{Binding Settings.ShowHistoryQueryResultsForHomePage}" />
|
||||
</ui:SettingsCard>
|
||||
<ui:SettingsCard Header="{DynamicResource historyLastOpenedResultsForHomePage}" IsEnabled="{Binding Settings.ShowHistoryOnHomePage}">
|
||||
<RadioButton GroupName="HistoryType" IsChecked="{Binding Settings.ShowHistoryLastOpenedResultsForHomePage}" />
|
||||
</ui:SettingsCard>
|
||||
<ui:SettingsCard Header="{DynamicResource historyResultsCountForHomePage}" IsEnabled="{Binding Settings.ShowHistoryOnHomePage}">
|
||||
<ui:SettingsCard Header="{DynamicResource historyResultsCountForHomePage}">
|
||||
<ui:NumberBox
|
||||
Width="120"
|
||||
MinWidth="120"
|
||||
|
|
|
|||
|
|
@ -1,111 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.Json.Serialization;
|
||||
using Flow.Launcher.Infrastructure.UserSettings;
|
||||
using Flow.Launcher.Plugin;
|
||||
|
||||
namespace Flow.Launcher.Storage
|
||||
{
|
||||
public class History
|
||||
{
|
||||
//Legacy
|
||||
[JsonInclude]
|
||||
public List<HistoryItemLegacy> Items { get; private set; } = [];
|
||||
|
||||
[JsonInclude]
|
||||
public List<HistoryItem> LastOpenedHistoryItems { get; private set; } = [];
|
||||
|
||||
[JsonInclude]
|
||||
public List<HistoryItem> QueryHistoryItems { get; private set; } = [];
|
||||
|
||||
private int _maxHistory = 300;
|
||||
|
||||
public void AddToHistory(Result result, Settings settings)
|
||||
{
|
||||
if (!settings.ShowHistoryOnHomePage) return;
|
||||
if (settings.ShowHistoryQueryResultsForHomePage)
|
||||
{
|
||||
AddLastQuery(result);
|
||||
return;
|
||||
}
|
||||
AddLastOpened(result);
|
||||
}
|
||||
|
||||
|
||||
public List<HistoryItem> GetHistoryItems(Settings settings)
|
||||
{
|
||||
if (settings.ShowHistoryQueryResultsForHomePage) return QueryHistoryItems.PopulateActions(true);
|
||||
return LastOpenedHistoryItems.PopulateActions(false);
|
||||
}
|
||||
|
||||
public void PopulateHistoryFromLegacyHistory()
|
||||
{
|
||||
foreach (var item in Items)
|
||||
{
|
||||
QueryHistoryItems.Add(new HistoryItem
|
||||
{
|
||||
RawQuery = item.Query,
|
||||
ExecutedDateTime = item.ExecutedDateTime ?? DateTime.Now,
|
||||
QueryAction = HistoryHelper.GetQueryAction(item.Query)
|
||||
});
|
||||
}
|
||||
if (Items.Count > 0) Items.Clear();
|
||||
}
|
||||
|
||||
private void AddLastQuery(Result result)
|
||||
{
|
||||
if (string.IsNullOrEmpty(result.OriginQuery.RawQuery)) return;
|
||||
if (QueryHistoryItems.Count > _maxHistory)
|
||||
{
|
||||
QueryHistoryItems.RemoveAt(0);
|
||||
}
|
||||
|
||||
if (QueryHistoryItems.Count > 0 && QueryHistoryItems.Last().RawQuery == result.OriginQuery.RawQuery)
|
||||
{
|
||||
QueryHistoryItems.Last().ExecutedDateTime = DateTime.Now;
|
||||
}
|
||||
else
|
||||
{
|
||||
QueryHistoryItems.Add(new HistoryItem
|
||||
{
|
||||
RawQuery = result.OriginQuery.RawQuery,
|
||||
ExecutedDateTime = DateTime.Now,
|
||||
QueryAction = HistoryHelper.GetQueryAction(result.OriginQuery.RawQuery)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void AddLastOpened(Result result)
|
||||
{
|
||||
var item = new HistoryItem
|
||||
{
|
||||
Title = result.Title,
|
||||
SubTitle = result.SubTitle,
|
||||
PluginID = result.PluginID,
|
||||
RawQuery = result.OriginQuery.RawQuery,
|
||||
RecordKey = result.RecordKey,
|
||||
ExecutedDateTime = DateTime.Now,
|
||||
ExecuteAction = result.Action
|
||||
};
|
||||
|
||||
var existing = LastOpenedHistoryItems.
|
||||
FirstOrDefault(x => x.Title == item.Title && x.PluginID == item.PluginID);
|
||||
|
||||
|
||||
if (existing != null)
|
||||
{
|
||||
existing.ExecutedDateTime = DateTime.Now;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (LastOpenedHistoryItems.Count > _maxHistory)
|
||||
{
|
||||
LastOpenedHistoryItems.RemoveAt(0);
|
||||
}
|
||||
|
||||
LastOpenedHistoryItems.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -11,13 +11,13 @@ namespace Flow.Launcher.Storage;
|
|||
|
||||
public static class HistoryHelper
|
||||
{
|
||||
internal static List<HistoryItem> PopulateActions(this List<HistoryItem> items, bool isQuery)
|
||||
internal static List<LastOpenedHistoryItem> PopulateActions(this List<LastOpenedHistoryItem> items, bool isQuery)
|
||||
{
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (item.QueryAction != null && item.ExecuteAction != null) continue;
|
||||
if (isQuery && item.QueryAction == null) item.QueryAction = GetQueryAction(item.RawQuery);
|
||||
if (!isQuery && item.ExecuteAction == null) item.ExecuteAction = GetExecuteAction(item.PluginID, item.RawQuery, item.Title, item.SubTitle, item.RecordKey) ?? GetQueryAction(item.RawQuery);
|
||||
if (isQuery && item.QueryAction == null) item.QueryAction = GetQueryAction(item.Query);
|
||||
if (!isQuery && item.ExecuteAction == null) item.ExecuteAction = GetExecuteAction(item.PluginID, item.Query, item.Title, item.SubTitle, item.RecordKey) ?? GetQueryAction(item.Query);
|
||||
}
|
||||
|
||||
return items;
|
||||
|
|
|
|||
|
|
@ -1,21 +1,46 @@
|
|||
using System;
|
||||
using System.Text.Json.Serialization;
|
||||
using Flow.Launcher.Plugin;
|
||||
|
||||
namespace Flow.Launcher.Storage;
|
||||
|
||||
public class HistoryItem
|
||||
namespace Flow.Launcher.Storage
|
||||
{
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string SubTitle { get; set; } = string.Empty;
|
||||
public string PluginID { get; set; } = string.Empty;
|
||||
public string RawQuery { get; set; }
|
||||
public string RecordKey { get; set; } = string.Empty;
|
||||
public DateTime ExecutedDateTime { get; set; }
|
||||
[Obsolete("Use LastOpenedHistoryItem instead. This class will be removed in future versions.")]
|
||||
public class HistoryItem
|
||||
{
|
||||
public string Query { get; set; }
|
||||
public DateTime ExecutedDateTime { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public Func<ActionContext, bool> ExecuteAction { get; set; }
|
||||
public string GetTimeAgo()
|
||||
{
|
||||
return DateTimeAgo(ExecutedDateTime);
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public Func<ActionContext, bool> QueryAction { get; set; }
|
||||
private string DateTimeAgo(DateTime dt)
|
||||
{
|
||||
var span = DateTime.Now - dt;
|
||||
if (span.Days > 365)
|
||||
{
|
||||
int years = (span.Days / 365);
|
||||
if (span.Days % 365 != 0)
|
||||
years += 1;
|
||||
return $"about {years} {(years == 1 ? "year" : "years")} ago";
|
||||
}
|
||||
if (span.Days > 30)
|
||||
{
|
||||
int months = (span.Days / 30);
|
||||
if (span.Days % 31 != 0)
|
||||
months += 1;
|
||||
return $"about {months} {(months == 1 ? "month" : "months")} ago";
|
||||
}
|
||||
if (span.Days > 0)
|
||||
return $"about {span.Days} {(span.Days == 1 ? "day" : "days")} ago";
|
||||
if (span.Hours > 0)
|
||||
return $"about {span.Hours} {(span.Hours == 1 ? "hour" : "hours")} ago";
|
||||
if (span.Minutes > 0)
|
||||
return $"about {span.Minutes} {(span.Minutes == 1 ? "minute" : "minutes")} ago";
|
||||
if (span.Seconds > 5)
|
||||
return $"about {span.Seconds} seconds ago";
|
||||
if (span.Seconds <= 5)
|
||||
return "just now";
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace Flow.Launcher.Storage;
|
||||
|
||||
[Obsolete]
|
||||
public class HistoryItemLegacy
|
||||
{
|
||||
public string Query { get; set; }
|
||||
public DateTime? ExecutedDateTime { get; set; }
|
||||
}
|
||||
38
Flow.Launcher/Storage/LastOpenedHistoryItem.cs
Normal file
38
Flow.Launcher/Storage/LastOpenedHistoryItem.cs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
using System;
|
||||
using System.Text.Json.Serialization;
|
||||
using Flow.Launcher.Plugin;
|
||||
|
||||
namespace Flow.Launcher.Storage;
|
||||
|
||||
public class LastOpenedHistoryItem
|
||||
{
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string SubTitle { get; set; } = string.Empty;
|
||||
public string PluginID { get; set; } = string.Empty;
|
||||
public string Query { get; set; } = string.Empty;
|
||||
public string RecordKey { get; set; } = string.Empty;
|
||||
public DateTime ExecutedDateTime { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public Func<ActionContext, bool> ExecuteAction { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public Func<ActionContext, bool> QueryAction { get; set; }
|
||||
|
||||
public bool Equals(Result r)
|
||||
{
|
||||
if (string.IsNullOrEmpty(RecordKey) || string.IsNullOrEmpty(r.RecordKey))
|
||||
{
|
||||
return Title == r.Title
|
||||
&& SubTitle == r.SubTitle
|
||||
&& PluginID == r.PluginID
|
||||
&& Query == r.OriginQuery.RawQuery;
|
||||
}
|
||||
else
|
||||
{
|
||||
return RecordKey == r.RecordKey
|
||||
&& PluginID == r.PluginID
|
||||
&& Query == r.OriginQuery.RawQuery;
|
||||
}
|
||||
}
|
||||
}
|
||||
76
Flow.Launcher/Storage/QueryHistory.cs
Normal file
76
Flow.Launcher/Storage/QueryHistory.cs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.Json.Serialization;
|
||||
using CommunityToolkit.Mvvm.DependencyInjection;
|
||||
using Flow.Launcher.Infrastructure.UserSettings;
|
||||
using Flow.Launcher.Plugin;
|
||||
|
||||
namespace Flow.Launcher.Storage
|
||||
{
|
||||
public class History
|
||||
{
|
||||
[JsonInclude]
|
||||
#pragma warning disable CS0618 // Type or member is obsolete
|
||||
public List<HistoryItem> Items { get; private set; } = [];
|
||||
#pragma warning restore CS0618 // Type or member is obsolete
|
||||
|
||||
[JsonInclude]
|
||||
public List<LastOpenedHistoryItem> LastOpenedHistoryItems { get; private set; } = [];
|
||||
|
||||
private readonly int _maxHistory = 300;
|
||||
private static readonly Settings _settings = Ioc.Default.GetRequiredService<Settings>();
|
||||
|
||||
public void PopulateHistoryFromLegacyHistory()
|
||||
{
|
||||
if (Items.Count == 0) return;
|
||||
// Migrate old history items to new LastOpenedHistoryItems
|
||||
foreach (var item in Items)
|
||||
{
|
||||
LastOpenedHistoryItems.Add(new LastOpenedHistoryItem
|
||||
{
|
||||
Query = item.Query,
|
||||
ExecutedDateTime = item.ExecutedDateTime,
|
||||
QueryAction = HistoryHelper.GetQueryAction(item.Query)
|
||||
});
|
||||
}
|
||||
Items.Clear();
|
||||
}
|
||||
|
||||
public void Add(Result result)
|
||||
{
|
||||
if (string.IsNullOrEmpty(result.OriginQuery.RawQuery)) return;
|
||||
|
||||
// Maintain the max history limit
|
||||
if (LastOpenedHistoryItems.Count > _maxHistory)
|
||||
{
|
||||
LastOpenedHistoryItems.RemoveAt(0);
|
||||
}
|
||||
|
||||
// If the last item is the same as the current result, just update the timestamp
|
||||
if (LastOpenedHistoryItems.Count > 0 &&
|
||||
LastOpenedHistoryItems.Last().Equals(result))
|
||||
{
|
||||
LastOpenedHistoryItems.Last().ExecutedDateTime = DateTime.Now;
|
||||
}
|
||||
else
|
||||
{
|
||||
LastOpenedHistoryItems.Add(new LastOpenedHistoryItem
|
||||
{
|
||||
Title = result.Title,
|
||||
SubTitle = result.SubTitle,
|
||||
PluginID = result.PluginID,
|
||||
Query = result.OriginQuery.RawQuery,
|
||||
RecordKey = result.RecordKey,
|
||||
ExecutedDateTime = DateTime.Now,
|
||||
ExecuteAction = result.Action
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public List<LastOpenedHistoryItem> GetHistoryItems()
|
||||
{
|
||||
return LastOpenedHistoryItems.PopulateActions(_settings.HistoryStyle == HistoryStyle.Query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -40,7 +40,7 @@ namespace Flow.Launcher.ViewModel
|
|||
private string _queryTextBeforeLeaveResults;
|
||||
private string _ignoredQueryText; // Used to ignore query text change when switching between context menu and query results
|
||||
|
||||
private readonly FlowLauncherJsonStorage<History> _historyStorage;
|
||||
private readonly FlowLauncherJsonStorage<History> _historyItemsStorage;
|
||||
private readonly FlowLauncherJsonStorage<UserSelectedRecord> _userSelectedRecordStorage;
|
||||
private readonly FlowLauncherJsonStorageTopMostRecord _topMostRecord;
|
||||
private readonly History _history;
|
||||
|
|
@ -147,12 +147,10 @@ namespace Flow.Launcher.ViewModel
|
|||
}
|
||||
};
|
||||
|
||||
_historyStorage = new FlowLauncherJsonStorage<History>();
|
||||
|
||||
_historyItemsStorage = new FlowLauncherJsonStorage<History>();
|
||||
_userSelectedRecordStorage = new FlowLauncherJsonStorage<UserSelectedRecord>();
|
||||
_topMostRecord = new FlowLauncherJsonStorageTopMostRecord();
|
||||
|
||||
_history = _historyStorage.Load();
|
||||
_history = _historyItemsStorage.Load();
|
||||
_history.PopulateHistoryFromLegacyHistory();
|
||||
_userSelectedRecord = _userSelectedRecordStorage.Load();
|
||||
|
||||
|
|
@ -355,7 +353,7 @@ namespace Flow.Launcher.ViewModel
|
|||
if (QueryResultsSelected())
|
||||
{
|
||||
SelectedResults = History;
|
||||
History.SelectedIndex = _history.GetHistoryItems(Settings).Count - 1;
|
||||
History.SelectedIndex = _history.GetHistoryItems().Count - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -383,10 +381,10 @@ namespace Flow.Launcher.ViewModel
|
|||
[RelayCommand]
|
||||
public void ReverseHistory()
|
||||
{
|
||||
var historyItems = _history.GetHistoryItems(Settings);
|
||||
var historyItems = _history.GetHistoryItems();
|
||||
if (historyItems.Count > 0)
|
||||
{
|
||||
ChangeQueryText(historyItems[^lastHistoryIndex].RawQuery);
|
||||
ChangeQueryText(historyItems[^lastHistoryIndex].Query);
|
||||
if (lastHistoryIndex < historyItems.Count)
|
||||
{
|
||||
lastHistoryIndex++;
|
||||
|
|
@ -397,11 +395,10 @@ namespace Flow.Launcher.ViewModel
|
|||
[RelayCommand]
|
||||
public void ForwardHistory()
|
||||
{
|
||||
var historyItems = _history.GetHistoryItems(Settings);
|
||||
|
||||
var historyItems = _history.GetHistoryItems();
|
||||
if (historyItems.Count > 0)
|
||||
{
|
||||
ChangeQueryText(historyItems[^lastHistoryIndex].RawQuery);
|
||||
ChangeQueryText(historyItems[^lastHistoryIndex].Query);
|
||||
if (lastHistoryIndex > 1)
|
||||
{
|
||||
lastHistoryIndex--;
|
||||
|
|
@ -533,11 +530,10 @@ namespace Flow.Launcher.ViewModel
|
|||
Hide();
|
||||
}
|
||||
}
|
||||
|
||||
if (QueryResultsSelected())
|
||||
{
|
||||
_history.AddToHistory(result, Settings);
|
||||
_userSelectedRecord.Add(result);
|
||||
_history.Add(result);
|
||||
lastHistoryIndex = 1;
|
||||
}
|
||||
}
|
||||
|
|
@ -566,6 +562,7 @@ namespace Flow.Launcher.ViewModel
|
|||
resultsCopy.Add(resultCopy);
|
||||
}
|
||||
}
|
||||
|
||||
return resultsCopy;
|
||||
}
|
||||
|
||||
|
|
@ -612,11 +609,11 @@ namespace Flow.Launcher.ViewModel
|
|||
[RelayCommand]
|
||||
private void SelectPrevItem()
|
||||
{
|
||||
var historyItems = _history.GetHistoryItems(Settings);
|
||||
var historyItems = _history.GetHistoryItems();
|
||||
if (QueryResultsSelected() // Results selected
|
||||
&& string.IsNullOrEmpty(QueryText) // No input
|
||||
&& Results.Visibility != Visibility.Visible // No items in result list, e.g. when home page is off and no query text is entered, therefore the view is collapsed.
|
||||
&& historyItems.Count > 0)
|
||||
&& historyItems.Count > 0) // Have history items
|
||||
{
|
||||
lastHistoryIndex = 1;
|
||||
ReverseHistory();
|
||||
|
|
@ -1298,9 +1295,7 @@ namespace Flow.Launcher.ViewModel
|
|||
var query = QueryText.ToLower().Trim();
|
||||
History.Clear();
|
||||
|
||||
var items = _history.GetHistoryItems(Settings);
|
||||
|
||||
var results = GetHistoryResults(items);
|
||||
var results = GetHistoryItems(_history.GetHistoryItems());
|
||||
|
||||
if (!string.IsNullOrEmpty(query))
|
||||
{
|
||||
|
|
@ -1317,20 +1312,22 @@ namespace Flow.Launcher.ViewModel
|
|||
}
|
||||
}
|
||||
|
||||
private List<Result> GetHistoryResults(IEnumerable<HistoryItem> historyItems)
|
||||
private List<Result> GetHistoryItems(IEnumerable<LastOpenedHistoryItem> historyItems)
|
||||
{
|
||||
var results = new List<Result>();
|
||||
foreach (var h in historyItems)
|
||||
{
|
||||
var result = new Result
|
||||
{
|
||||
Title =
|
||||
Settings.ShowHistoryQueryResultsForHomePage ? Localize.executeQuery(h.RawQuery) : h.Title,
|
||||
Title = Settings.HistoryStyle == HistoryStyle.Query ?
|
||||
Localize.executeQuery(h.Query) :
|
||||
string.IsNullOrEmpty(h.Title) ? // Old migrated history items have no title
|
||||
Localize.executeQuery(h.Query) :
|
||||
h.Title,
|
||||
SubTitle = Localize.lastExecuteTime(h.ExecutedDateTime),
|
||||
IcoPath = Constant.HistoryIcon,
|
||||
PluginID = h.PluginID,
|
||||
OriginQuery = QueryBuilder.Build(h.RawQuery, PluginManager.NonGlobalPlugins),
|
||||
Action = Settings.ShowHistoryLastOpenedResultsForHomePage ? h.ExecuteAction : h.QueryAction,
|
||||
OriginQuery = new Query { RawQuery = h.Query },
|
||||
Action = Settings.HistoryStyle == HistoryStyle.Query ? h.QueryAction : h.ExecuteAction,
|
||||
Glyph = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\uE81C")
|
||||
};
|
||||
results.Add(result);
|
||||
|
|
@ -1452,7 +1449,8 @@ namespace Flow.Launcher.ViewModel
|
|||
true => Task.CompletedTask
|
||||
}).ToArray();
|
||||
|
||||
if (Settings.ShowHistoryOnHomePage)
|
||||
// Query history results for home page firstly so it will be put on top of the results
|
||||
if (Settings.ShowHistoryResultsForHomePage)
|
||||
{
|
||||
QueryHistoryTask(currentCancellationToken);
|
||||
}
|
||||
|
|
@ -1564,9 +1562,9 @@ namespace Flow.Launcher.ViewModel
|
|||
void QueryHistoryTask(CancellationToken token)
|
||||
{
|
||||
// Select last history results and revert its order to make sure last history results are on top
|
||||
var historyItems = _history.GetHistoryItems(Settings).TakeLast(Settings.MaxHistoryResultsToShowForHomePage).Reverse();
|
||||
var historyItems = _history.GetHistoryItems().TakeLast(Settings.MaxHistoryResultsToShowForHomePage).Reverse();
|
||||
|
||||
var results = GetHistoryResults(historyItems);
|
||||
var results = GetHistoryItems(historyItems);
|
||||
|
||||
if (token.IsCancellationRequested) return;
|
||||
|
||||
|
|
@ -1702,7 +1700,7 @@ namespace Flow.Launcher.ViewModel
|
|||
/// <returns>True if existing results should be cleared, false otherwise.</returns>
|
||||
private bool ShouldClearExistingResultsForNonQuery(ICollection<PluginPair> plugins)
|
||||
{
|
||||
if (!Settings.ShowHistoryQueryResultsForHomePage && (plugins.Count == 0 || plugins.All(x => x.Metadata.HomeDisabled == true)))
|
||||
if (!Settings.ShowHistoryResultsForHomePage && (plugins.Count == 0 || plugins.All(x => x.Metadata.HomeDisabled == true)))
|
||||
{
|
||||
App.API.LogDebug(ClassName, $"Existing results should be cleared for non-query");
|
||||
return true;
|
||||
|
|
@ -2166,7 +2164,7 @@ namespace Flow.Launcher.ViewModel
|
|||
/// </summary>
|
||||
public void Save()
|
||||
{
|
||||
_historyStorage.Save();
|
||||
_historyItemsStorage.Save();
|
||||
_userSelectedRecordStorage.Save();
|
||||
_topMostRecord.Save();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue