Skip to content

Commit

Permalink
[PTRun]Add setting to disable input delay
Browse files Browse the repository at this point in the history
  • Loading branch information
jaimecbernardo committed Jun 9, 2022
1 parent a11f9ab commit b540f9d
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 8 deletions.
53 changes: 46 additions & 7 deletions src/modules/launcher/PowerLauncher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public partial class MainWindow : IDisposable
private Timer _firstDeleteTimer = new Timer();
private bool _coldStateHotkeyPressed;
private bool _disposedValue;
private IDisposable _reactiveSubscription;

public MainWindow(PowerToysRunSettings settings, MainViewModel mainVM)
: this()
Expand Down Expand Up @@ -161,13 +162,18 @@ private void OnLoaded(object sender, RoutedEventArgs e)
SearchBox.QueryTextBox.DataContext = _viewModel;
SearchBox.QueryTextBox.PreviewKeyDown += Launcher_KeyDown;

Observable.FromEventPattern<TextChangedEventHandler, TextChangedEventArgs>(
add => SearchBox.QueryTextBox.TextChanged += add,
remove => SearchBox.QueryTextBox.TextChanged -= remove)
.Do(@event => ClearAutoCompleteText((TextBox)@event.Sender))
.Throttle(TimeSpan.FromMilliseconds(150))
.Do(@event => Dispatcher.InvokeAsync(() => PerformSearchQuery((TextBox)@event.Sender)))
.Subscribe();
SetupSearchTextBoxReactiveness(_viewModel.GetSearchQueryResultsWithoutDelaySetting());
_viewModel.RegisterSettingsChangeListener(
(s, prop_e) =>
{
if (prop_e.PropertyName == nameof(PowerToysRunSettings.SearchQueryResultsWithoutDelay))
{
Application.Current.Dispatcher.Invoke(() =>
{
SetupSearchTextBoxReactiveness(_viewModel.GetSearchQueryResultsWithoutDelaySetting());
});
}
});

// Set initial language flow direction
SearchBox_UpdateFlowDirection();
Expand All @@ -188,6 +194,32 @@ private void OnLoaded(object sender, RoutedEventArgs e)
BringProcessToForeground();
}

private void SetupSearchTextBoxReactiveness(bool showResultsImmediately)
{
if (_reactiveSubscription != null)
{
_reactiveSubscription.Dispose();
_reactiveSubscription = null;
}

SearchBox.QueryTextBox.TextChanged -= QueryTextBox_TextChanged;

if (showResultsImmediately)
{
SearchBox.QueryTextBox.TextChanged += QueryTextBox_TextChanged;
}
else
{
_reactiveSubscription = Observable.FromEventPattern<TextChangedEventHandler, TextChangedEventArgs>(
add => SearchBox.QueryTextBox.TextChanged += add,
remove => SearchBox.QueryTextBox.TextChanged -= remove)
.Do(@event => ClearAutoCompleteText((TextBox)@event.Sender))
.Throttle(TimeSpan.FromMilliseconds(150))
.Do(@event => Dispatcher.InvokeAsync(() => PerformSearchQuery((TextBox)@event.Sender)))
.Subscribe();
}
}

private void SuggestionsList_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var result = ((FrameworkElement)e.OriginalSource).DataContext;
Expand Down Expand Up @@ -430,6 +462,13 @@ private void SuggestionsList_SelectionChanged(object sender, SelectionChangedEve
}
}

private void QueryTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
var textBox = (TextBox)sender;
ClearAutoCompleteText(textBox);
PerformSearchQuery(textBox);
}

private void ClearAutoCompleteText(TextBox textBox)
{
var text = textBox.Text;
Expand Down
5 changes: 5 additions & 0 deletions src/modules/launcher/PowerLauncher/SettingsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public void ReadSettings()
_settings.UseCentralizedKeyboardHook = overloadSettings.Properties.UseCentralizedKeyboardHook;
}

if (_settings.SearchQueryResultsWithoutDelay != overloadSettings.Properties.SearchQueryResultsWithoutDelay)
{
_settings.SearchQueryResultsWithoutDelay = overloadSettings.Properties.SearchQueryResultsWithoutDelay;
}

if (_settings.MaxResultsToShow != overloadSettings.Properties.MaximumNumberOfResults)
{
_settings.MaxResultsToShow = overloadSettings.Properties.MaximumNumberOfResults;
Expand Down
10 changes: 10 additions & 0 deletions src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public void RegisterHotkey(IntPtr hwnd)
// SetCustomPluginHotkey();
}

public void RegisterSettingsChangeListener(System.ComponentModel.PropertyChangedEventHandler handler)
{
_settings.PropertyChanged += handler;
}

private void RegisterResultsUpdatedEvent()
{
foreach (var pair in PluginManager.GetPluginsForInterface<IResultUpdated>())
Expand Down Expand Up @@ -1104,5 +1109,10 @@ public long GetHotkeyEventTimeMs()
_hotkeyTimer.Reset();
return recordedTime;
}

public bool GetSearchQueryResultsWithoutDelaySetting()
{
return _settings.SearchQueryResultsWithoutDelay;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ public bool UseCentralizedKeyboardHook
}
}

private bool _searchQueryResultsWithoutDelay;

public bool SearchQueryResultsWithoutDelay
{
get
{
return _searchQueryResultsWithoutDelay;
}

set
{
if (_searchQueryResultsWithoutDelay != value)
{
_searchQueryResultsWithoutDelay = value;
OnPropertyChanged(nameof(SearchQueryResultsWithoutDelay));
}
}
}

public string Language { get; set; } = "en";

public Theme Theme { get; set; } = Theme.System;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public class PowerLauncherProperties
[JsonPropertyName("use_centralized_keyboard_hook")]
public bool UseCentralizedKeyboardHook { get; set; }

[JsonPropertyName("search_query_results_without_delay")]
public bool SearchQueryResultsWithoutDelay { get; set; }

public PowerLauncherProperties()
{
OpenPowerLauncher = new HotkeySettings(false, false, true, false, 32);
Expand All @@ -65,6 +68,7 @@ public PowerLauncherProperties()
Theme = Theme.System;
Position = StartupPosition.Cursor;
UseCentralizedKeyboardHook = false;
SearchQueryResultsWithoutDelay = false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,23 @@ public bool UseCentralizedKeyboardHook
}
}

public bool SearchQueryResultsWithoutDelay
{
get
{
return settings.Properties.SearchQueryResultsWithoutDelay;
}

set
{
if (settings.Properties.SearchQueryResultsWithoutDelay != value)
{
settings.Properties.SearchQueryResultsWithoutDelay = value;
UpdateSettings();
}
}
}

public HotkeySettings OpenFileLocation
{
get
Expand Down
3 changes: 3 additions & 0 deletions src/settings-ui/Settings.UI/Strings/en-us/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,9 @@
<data name="PowerLauncher_ClearInputOnLaunch.Content" xml:space="preserve">
<value>Clear the previous query on launch</value>
</data>
<data name="PowerLauncher_SearchQueryResultsWithoutDelay.Content" xml:space="preserve">
<value>Search query results without introducing a delay to wait for more input</value>
</data>
<data name="KeyboardManager_KeysMappingLayoutRightHeader.Text" xml:space="preserve">
<value>To:</value>
<comment>Keyboard Manager mapping keys view right header</comment>
Expand Down
5 changes: 4 additions & 1 deletion src/settings-ui/Settings.UI/Views/PowerLauncherPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@
</controls:Setting>
</controls:SettingExpander.Header>
<controls:SettingExpander.Content>
<CheckBox x:Uid="PowerLauncher_ClearInputOnLaunch" IsChecked="{x:Bind Mode=TwoWay, Path=ViewModel.ClearInputOnLaunch}" Margin="{StaticResource ExpanderSettingMargin}" />
<StackPanel>
<CheckBox x:Uid="PowerLauncher_ClearInputOnLaunch" IsChecked="{x:Bind Mode=TwoWay, Path=ViewModel.ClearInputOnLaunch}" Margin="{StaticResource ExpanderSettingMargin}" />
<CheckBox x:Uid="PowerLauncher_SearchQueryResultsWithoutDelay" IsChecked="{x:Bind Mode=TwoWay, Path=ViewModel.SearchQueryResultsWithoutDelay}" Margin="{StaticResource ExpanderSettingMargin}" />
</StackPanel>
</controls:SettingExpander.Content>
</controls:SettingExpander>
</controls:SettingsGroup>
Expand Down

0 comments on commit b540f9d

Please sign in to comment.