Files
FluentTerminal/FluentTerminal.App/Views/TerminalView.xaml.cs
T
Leonardo Di Stasio 37b4fd283a Add theme background image feature (#545)
* Add a background image property in theme

* Add the browse command to theme settings page

The goal was to create a command that permit to browse user pictures.
The user can now select is picture by browsing file or to simply copy the
link of the image in the textbox.

* Try to show the image that we download

* Change the save image method in file service

We call the method `ImportImageFile` so that we can show a picker dialog
and directly import the image in the roaming folder.
We make some check to avoid the application crash, still need to test

* Reorganize the UI for image import

* Add background brush converter

Now that we have a background image we need to set the background according
to the theme color or the selected image.
To do this we need to use the `ThemeViewModel` and the new converter to
select the right brush to use (`ImageBrush` or ` AcrylicBrush`)

* Use the File object insted of a image path

* Add background style to content and toolbar

* Create the ImageFile object

* Use the ImageFile object so that we can serialize the theme

* Remove commented code

* Remove the base64 string of the image file

* Disable if it's a preinstalled theme

* Add new event when background image changed

* Make the RemoveImportedImage method async

* Subscribe to background image event and change background in code behind

* Add the delete background image command

* Remove unused converters that we created

* Save background image in temporary folder

* Remove the temporary images and save the last one

* Implement the cancel and delete background

* Show the background image in terminal

* Split the image file methods in another interface

* Change the name of image saving method

* Prepare the object and methods to export encoded image

* Add import method to deserialize correctly the theme

* Remove the private from the set accessor

* Implememnt the import method

* Implement the conversion base64 image to real image

* Create the background image and import it

* Fix some actions bug

* Get the new image file name after import

* Fix some bugs when the image is deleted

* Move the background image in terminal view

* Translate the background image text

* Add the delete background command

* Fix the background image deletion if we click on cancel

* Add background opacity property to terminal view model

* Need to create a theme changed event

If we don't notify the view that the selected theme has changed with
another theme that has the same background but a different background image
we'll never see this image appear.
In fact the background change is triggered by the background color change.

* Remove the background from the theme settings page

* Fix error of last edited image

* Fix image background aspect ratio
2019-10-20 10:30:52 +02:00

142 lines
4.8 KiB
C#

using FluentTerminal.App.ViewModels;
using System;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using FluentTerminal.Models.Messages;
using GalaSoft.MvvmLight.Messaging;
using FluentTerminal.Models;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using FluentTerminal.App.Utilities;
namespace FluentTerminal.App.Views
{
public sealed partial class TerminalView : UserControl
{
private ITerminalView _terminalView;
public TerminalView(TerminalViewModel viewModel)
{
Messenger.Default.Register<KeyBindingsChangedMessage>(this, OnKeyBindingsChanged);
Messenger.Default.Register<TerminalOptionsChangedMessage>(this, OnTerminalOptionsChanged);
ViewModel = viewModel;
ViewModel.SearchStarted += OnSearchStarted;
ViewModel.Activated += OnActivated;
ViewModel.ThemeChanged += OnThemeChanged;
ViewModel.FindNextRequested += OnFindNextRequested;
ViewModel.FindPreviousRequested += OnFindPreviousRequested;
InitializeComponent();
_terminalView = new XtermTerminalView();
TerminalContainer.Children.Add((UIElement)_terminalView);
_terminalView.Initialize(ViewModel);
ViewModel.TerminalView = _terminalView;
ViewModel.Initialized = true;
SetGridBackgroundTheme(ViewModel.TerminalTheme);
}
public void DisposalPrepare()
{
Bindings.StopTracking();
TerminalContainer.Children.Remove((UIElement)_terminalView);
_terminalView?.Dispose();
_terminalView = null;
Messenger.Default.Unregister(this);
ViewModel.SearchStarted -= OnSearchStarted;
ViewModel.Activated -= OnActivated;
ViewModel.ThemeChanged -= OnThemeChanged;
ViewModel.FindNextRequested -= OnFindNextRequested;
ViewModel.FindPreviousRequested -= OnFindPreviousRequested;
ViewModel = null;
}
public TerminalViewModel ViewModel { get; private set; }
private async void OnActivated(object sender, EventArgs e)
{
if (_terminalView != null)
{
await _terminalView.FocusTerminal().ConfigureAwait(true);
}
}
private async void OnFindNextRequested(object sender, string e)
{
await _terminalView.FindNext(e).ConfigureAwait(true);
}
private async void OnFindPreviousRequested(object sender, string e)
{
await _terminalView.FindPrevious(e).ConfigureAwait(true);
}
private async void OnKeyBindingsChanged(KeyBindingsChangedMessage message)
{
await _terminalView.ChangeKeyBindings().ConfigureAwait(true);
}
private async void OnTerminalOptionsChanged(TerminalOptionsChangedMessage message)
{
await _terminalView.ChangeOptions(message.TerminalOptions).ConfigureAwait(true);
}
private void OnSearchStarted(object sender, EventArgs e)
{
SearchTextBox.Focus(FocusState.Programmatic);
}
private void OnSearchTextBoxKeyUp(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Escape)
{
ViewModel.CloseSearchPanelCommand.Execute(null);
}
else if (e.Key == VirtualKey.Enter)
{
ViewModel.FindNextCommand.Execute(null);
}
}
private async void OnThemeChanged(object sender, TerminalTheme e)
{
await _terminalView.ChangeTheme(e).ConfigureAwait(true);
SetGridBackgroundTheme(e);
}
private void SetGridBackgroundTheme(TerminalTheme terminalTheme)
{
var color = terminalTheme.Colors.Background;
var imageFile = terminalTheme.BackgroundImage;
Brush backgroundBrush;
if (imageFile != null && System.IO.File.Exists(imageFile.Path))
{
backgroundBrush = new ImageBrush()
{
ImageSource = new BitmapImage(new Uri(
imageFile.Path,
UriKind.Absolute)),
Stretch = Stretch.UniformToFill
};
}
else
{
backgroundBrush = new AcrylicBrush
{
BackgroundSource = AcrylicBackgroundSource.HostBackdrop,
FallbackColor = color.FromString(),
TintColor = color.FromString(),
TintOpacity = ViewModel.BackgroundOpacity
};
}
TerminalContainer.Background = backgroundBrush;
}
}
}