diff --git a/FluentTerminal.App.ViewModels/FluentTerminal.App.ViewModels.csproj b/FluentTerminal.App.ViewModels/FluentTerminal.App.ViewModels.csproj index 43f9702..ade3830 100644 --- a/FluentTerminal.App.ViewModels/FluentTerminal.App.ViewModels.csproj +++ b/FluentTerminal.App.ViewModels/FluentTerminal.App.ViewModels.csproj @@ -7,7 +7,6 @@ - diff --git a/FluentTerminal.App.ViewModels/Settings/TerminalPageViewModel.cs b/FluentTerminal.App.ViewModels/Settings/TerminalPageViewModel.cs index cd3bf00..c40b72a 100644 --- a/FluentTerminal.App.ViewModels/Settings/TerminalPageViewModel.cs +++ b/FluentTerminal.App.ViewModels/Settings/TerminalPageViewModel.cs @@ -1,17 +1,15 @@ using FluentTerminal.App.Services; using FluentTerminal.Models; using FluentTerminal.Models.Enums; +using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; -using ReactiveUI; -using System; using System.Collections.Generic; using System.Linq; -using System.Reactive.Linq; using System.Threading.Tasks; namespace FluentTerminal.App.ViewModels.Settings { - public class TerminalPageViewModel : ReactiveObject + public class TerminalPageViewModel : ViewModelBase { private readonly TerminalOptions _terminalOptions; private readonly ISettingsService _settingsService; @@ -65,7 +63,7 @@ namespace FluentTerminal.App.ViewModels.Settings { _terminalOptions.CursorBlink = value; _settingsService.SaveTerminalOptions(_terminalOptions); - this.RaisePropertyChanged(); + RaisePropertyChanged(); } } } @@ -79,7 +77,7 @@ namespace FluentTerminal.App.ViewModels.Settings { _terminalOptions.FontFamily = value; _settingsService.SaveTerminalOptions(_terminalOptions); - this.RaisePropertyChanged(); + RaisePropertyChanged(); } } } @@ -92,7 +90,8 @@ namespace FluentTerminal.App.ViewModels.Settings if (_terminalOptions.BackgroundOpacity != value) { _terminalOptions.BackgroundOpacity = value; - this.RaisePropertyChanged(); + _settingsService.SaveTerminalOptions(_terminalOptions); + RaisePropertyChanged(); } } } @@ -105,7 +104,8 @@ namespace FluentTerminal.App.ViewModels.Settings if (_terminalOptions.Padding != value) { _terminalOptions.Padding = value; - this.RaisePropertyChanged(); + _settingsService.SaveTerminalOptions(_terminalOptions); + RaisePropertyChanged(); } } } @@ -126,12 +126,12 @@ namespace FluentTerminal.App.ViewModels.Settings { _terminalOptions.ScrollBackLimit = intValue; _settingsService.SaveTerminalOptions(_terminalOptions); - this.RaisePropertyChanged(); + RaisePropertyChanged(); } } else { - this.RaisePropertyChanged(); + RaisePropertyChanged(); } } } @@ -145,7 +145,7 @@ namespace FluentTerminal.App.ViewModels.Settings { _terminalOptions.BoldText = value; _settingsService.SaveTerminalOptions(_terminalOptions); - this.RaisePropertyChanged(); + RaisePropertyChanged(); } } } @@ -161,7 +161,7 @@ namespace FluentTerminal.App.ViewModels.Settings { _terminalOptions.FontSize = value; _settingsService.SaveTerminalOptions(_terminalOptions); - this.RaisePropertyChanged(); + RaisePropertyChanged(); } } } @@ -180,10 +180,10 @@ namespace FluentTerminal.App.ViewModels.Settings _isEditingCursorStyle = true; _terminalOptions.CursorStyle = value; _settingsService.SaveTerminalOptions(_terminalOptions); - this.RaisePropertyChanged(); - this.RaisePropertyChanged(nameof(BlockIsSelected)); - this.RaisePropertyChanged(nameof(BarIsSelected)); - this.RaisePropertyChanged(nameof(UnderlineIsSelected)); + RaisePropertyChanged(); + RaisePropertyChanged(nameof(BlockIsSelected)); + RaisePropertyChanged(nameof(BarIsSelected)); + RaisePropertyChanged(nameof(UnderlineIsSelected)); _isEditingCursorStyle = false; } } @@ -199,10 +199,10 @@ namespace FluentTerminal.App.ViewModels.Settings _isEditingScrollBarStyle = true; _terminalOptions.ScrollBarStyle = value; _settingsService.SaveTerminalOptions(_terminalOptions); - this.RaisePropertyChanged(); - this.RaisePropertyChanged(nameof(HiddenIsSelected)); - this.RaisePropertyChanged(nameof(AutoHidingIsSelected)); - this.RaisePropertyChanged(nameof(VisibleIsSelected)); + RaisePropertyChanged(); + RaisePropertyChanged(nameof(HiddenIsSelected)); + RaisePropertyChanged(nameof(AutoHidingIsSelected)); + RaisePropertyChanged(nameof(VisibleIsSelected)); _isEditingScrollBarStyle = false; } } @@ -238,15 +238,6 @@ namespace FluentTerminal.App.ViewModels.Settings Sizes = Enumerable.Range(1, 72); _terminalOptions = _settingsService.GetTerminalOptions(); - - this.ObservableForProperty(x => x.BackgroundOpacity).Throttle(TimeSpan.FromMilliseconds(800)).ObserveOn(RxApp.MainThreadScheduler).Subscribe(x => - { - _settingsService.SaveTerminalOptions(_terminalOptions); - }); - this.ObservableForProperty(x => x.Padding).Throttle(TimeSpan.FromMilliseconds(800)).ObserveOn(RxApp.MainThreadScheduler).Subscribe(x => - { - _settingsService.SaveTerminalOptions(_terminalOptions); - }); } } } \ No newline at end of file diff --git a/FluentTerminal.App/FluentTerminal.App.csproj b/FluentTerminal.App/FluentTerminal.App.csproj index 0df9a24..8f3830f 100644 --- a/FluentTerminal.App/FluentTerminal.App.csproj +++ b/FluentTerminal.App/FluentTerminal.App.csproj @@ -138,6 +138,7 @@ + KeyBindingsView.xaml diff --git a/FluentTerminal.App/Utilities/DebouncedAction.cs b/FluentTerminal.App/Utilities/DebouncedAction.cs new file mode 100644 index 0000000..736c200 --- /dev/null +++ b/FluentTerminal.App/Utilities/DebouncedAction.cs @@ -0,0 +1,49 @@ +using System; +using Windows.UI.Core; +using Windows.UI.Xaml; + +namespace FluentTerminal.App.Utilities +{ + public class DebouncedAction + { + private DispatcherTimer timer; + private readonly CoreDispatcher _dispatcher; + private readonly TimeSpan _interval; + private readonly Action _action; + private T _parameter; + + public DebouncedAction(CoreDispatcher dispatcher, TimeSpan interval, Action action) + { + _dispatcher = dispatcher; + _interval = interval; + _action = action; + } + + public void Invoke(T parameter) + { + _parameter = parameter; + + timer?.Stop(); + timer = null; + + timer = new DispatcherTimer + { + Interval = _interval + }; + timer.Tick += (s, e) => + { + if (timer == null) + { + return; + } + + timer?.Stop(); + timer = null; + + _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => _action.Invoke(_parameter)); + }; + + timer.Start(); + } + } +} diff --git a/FluentTerminal.App/Views/XtermTerminalView.xaml.cs b/FluentTerminal.App/Views/XtermTerminalView.xaml.cs index 5c06a79..6822cc2 100644 --- a/FluentTerminal.App/Views/XtermTerminalView.xaml.cs +++ b/FluentTerminal.App/Views/XtermTerminalView.xaml.cs @@ -1,4 +1,5 @@ using Fleck; +using FluentTerminal.App.Utilities; using FluentTerminal.App.ViewModels; using FluentTerminal.App.ViewModels.Utilities; using FluentTerminal.Models; @@ -37,6 +38,7 @@ namespace FluentTerminal.App.Views private MenuFlyoutItem _pasteMenuItem; private IWebSocketConnection _webSocket; private WebView _webView; + private DebouncedAction _optionsChanged; public XtermTerminalView() { @@ -54,6 +56,12 @@ namespace FluentTerminal.App.Views return settings; }; + _optionsChanged = new DebouncedAction(Dispatcher, TimeSpan.FromMilliseconds(800), async options => + { + var serialized = JsonConvert.SerializeObject(options); + await ExecuteScriptAsync($"changeOptions('{serialized}')"); + }); + InitializeComponent(); StartMediatorTask(); @@ -74,8 +82,8 @@ namespace FluentTerminal.App.Views public Task ChangeOptions(TerminalOptions options) { - var serialized = JsonConvert.SerializeObject(options); - return ExecuteScriptAsync($"changeOptions('{serialized}')"); + _optionsChanged.Invoke(options); + return Task.CompletedTask; } public Task ChangeTheme(TerminalTheme theme)