Skip to content

Commit

Permalink
Merge pull request #25 from atticus-lv/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
atticus-lv authored Dec 4, 2024
2 parents 7b81ae3 + 01174db commit 8fd121c
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 166 deletions.
4 changes: 3 additions & 1 deletion src/VirtualStreetSnap/Assets/i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@
"HardLight": "Hard Light",
"SelectDirectory": "Select Directory",
"Factor": "Factor",
"ConfigChanged": "Configuration Changed"
"ConfigChanged": "Configuration Changed",
"SelectAnImage": "Select An Image",
"ThumbSize": "Thumb Size"
}
4 changes: 3 additions & 1 deletion src/VirtualStreetSnap/Assets/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@
"HardLight": "硬光",
"SelectDirectory": "选择目录",
"Factor": "系数",
"ConfigChanged": "配置已更改"
"ConfigChanged": "配置已更改",
"SelectAnImage": "选择一个图像",
"ThumbSize": "缩略图大小"
}
2 changes: 1 addition & 1 deletion src/VirtualStreetSnap/Services/ImageEditHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static Bitmap ConvertToBitmap<TPixel>(Image<TPixel> image) where TPixel :
return new Bitmap(ms);
}

// Convert a Avalonia Bitmap to ImageSharp Image
// Convert an Avalonia Bitmap to ImageSharp Image
public static Image<Rgba32> ConvertToImageSharp(Bitmap bitmap)
{
using var ms = new MemoryStream();
Expand Down
2 changes: 1 addition & 1 deletion src/VirtualStreetSnap/Services/ImageResizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static async Task<Bitmap> ResizeImageAsync(string inputPath, int targetWi
return await Task.Run(() => ResizeImage(inputPath, targetWidth, targetHeight));
}

public static Bitmap ResizeImage(string inputPath, int targetWidth, int targetHeight)
private static Bitmap ResizeImage(string inputPath, int targetWidth, int targetHeight)
{
using var srcImage = Image.FromFile(inputPath);
var srcWidth = srcImage.Width;
Expand Down
19 changes: 8 additions & 11 deletions src/VirtualStreetSnap/ViewModels/ImageEditorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using CommunityToolkit.Mvvm.ComponentModel;
Expand Down Expand Up @@ -34,7 +33,7 @@ public partial class ImageEditorViewModel : ViewModelBase
private LayerBaseViewModel? _selectedLayer;

[ObservableProperty]
private string _dragItemText;
private string? _dragItemText;

public ObservableCollection<LayerTypeItem> LayerTypes { get; set; }

Expand Down Expand Up @@ -84,6 +83,7 @@ public void SetupImage(ImageBase? image)
[RelayCommand]
public void RemoveLayer()
{
if (SelectedLayer == null) return;
var index = LayerManager.Layers.IndexOf(SelectedLayer);
LayerManager.RemoveLayer(SelectedLayer);
// If the removed layer was the selected layer, select the nearest layer
Expand Down Expand Up @@ -112,19 +112,16 @@ public void AddLayer(string? layerType)
var layer = value();
LayerManager.AddLayer(layer);
// Move the layer to above the selected layer, if no layer is selected, move to the top
var index = LayerManager.Layers.IndexOf(layer);
if (SelectedLayer != null)
{
index = LayerManager.Layers.IndexOf(SelectedLayer) + 1;
if (index == LayerManager.Layers.Count) index--;
LayerManager.MoveLayer(LayerManager.Layers.Last(), index);
SelectedLayer = LayerManager.Layers.ElementAtOrDefault(index);
}
if (SelectedLayer == null) return;
var index = LayerManager.Layers.IndexOf(SelectedLayer) + 1;
if (index == LayerManager.Layers.Count) index--;
LayerManager.MoveLayer(LayerManager.Layers.Last(), index);
SelectedLayer = LayerManager.Layers.ElementAtOrDefault(index);
}

public event EventHandler? ImageSaved;

internal void OnImageSaved()
private void OnImageSaved()
{
ImageSaved?.Invoke(this, EventArgs.Empty);
}
Expand Down
24 changes: 13 additions & 11 deletions src/VirtualStreetSnap/ViewModels/ImageGalleryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Media.Imaging;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using VirtualStreetSnap.Models;
Expand Down Expand Up @@ -39,7 +38,7 @@ public void Initialize(string saveDirectory)
_lastCheckedDirectory = saveDirectory;

_allImagePaths = Directory.GetFiles(saveDirectory, "*.png")
.ToDictionary(file => file, file => File.GetLastWriteTime(file));
.ToDictionary(file => file, File.GetLastWriteTime);
_allImagePaths = _allImagePaths.OrderByDescending(kv => kv.Value).ToDictionary(kv => kv.Key, kv => kv.Value);
Thumbnails.Clear();
_currentBatchIndex = 0;
Expand All @@ -57,7 +56,7 @@ public void LoadNextBatch()
public void LoadNecessary()
{
var currentImagePaths = Directory.GetFiles(_lastCheckedDirectory, "*.png")
.ToDictionary(file => file, file => File.GetLastWriteTime(file));
.ToDictionary(file => file, File.GetLastWriteTime);

// Detect and remove missing files
var missingFiles = _allImagePaths.Keys.Except(currentImagePaths.Keys).ToList();
Expand Down Expand Up @@ -97,13 +96,17 @@ public partial class ImageGalleryViewModel : ViewModelBase
private AppConfig _config = ConfigService.Instance;

[ObservableProperty]
private ImageViewerViewModel _selectedImageViewer = new ImageViewerViewModel();

private ImageViewerView _selectedImageViewer;

[ObservableProperty]
private int _thumbDisplaySize = 80;

public ObservableCollection<ImageBase> Thumbnails => _lazyLoadManager.Thumbnails;

public ImageGalleryViewModel()
{
UpdateThumbnails();
SelectedImageViewer = new ImageViewerView();
UpdateThumbnails(selectFirst:false);
Config.Settings.PropertyChanged += OnSettingsPropertyChanged;
}

Expand All @@ -115,11 +118,10 @@ private void OnSettingsPropertyChanged(object? sender, PropertyChangedEventArgs
partial void OnSelectedThumbnailChanged(ImageBase value)
{
value.LoadImage();
SelectedImageViewer.ViewImage = value;
if (SelectedImageViewer.DataContext is ImageViewerViewModel viewModel) viewModel.ViewImage = value;
}

[RelayCommand]
public void UpdateThumbnails(bool reload = false)
public void UpdateThumbnails(bool reload = false,bool selectFirst = true)
{
if (_lazyLoadManager.IsInitialized && !reload)
{
Expand All @@ -130,7 +132,7 @@ public void UpdateThumbnails(bool reload = false)
_lazyLoadManager.Initialize(Config.Settings.SaveDirectory);
}

if (Thumbnails.Count > 0 && SelectedThumbnail == null) SelectedThumbnail = Thumbnails.First();
if (Thumbnails.Count > 0 && SelectedThumbnail == null && selectFirst) SelectedThumbnail = Thumbnails.First();
}


Expand Down
7 changes: 2 additions & 5 deletions src/VirtualStreetSnap/ViewModels/ImageViewerViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System.Threading.Tasks;
using Avalonia.Media.Imaging;
using Avalonia.Media.Imaging;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using VirtualStreetSnap.Models;
using VirtualStreetSnap.Services;

namespace VirtualStreetSnap.ViewModels;

Expand All @@ -16,7 +13,7 @@ public partial class ImageViewerViewModel : ViewModelBase
private ImageBase? _viewImage;

[ObservableProperty]
private Bitmap? _image = null;
private Bitmap? _image;

public ImageViewerViewModel()
{
Expand Down
46 changes: 26 additions & 20 deletions src/VirtualStreetSnap/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,17 @@

namespace VirtualStreetSnap.ViewModels;

public class PagesModel
{
public PagesModel(string name, UserControl page, string? iconKey = null)
{
Name = name;
Page = page;
if (iconKey == null) return;
Application.Current!.TryFindResource(iconKey, out var res);
ItemIcon = (StreamGeometry)res!;
}

public string Name { get; set; }
public UserControl Page { get; set; }
public StreamGeometry? ItemIcon { get; }
}

public partial class MainWindowViewModel : ViewModelBase
{
[ObservableProperty]
private PagesModel? _currentPage;

[ObservableProperty]
private bool _isPaneOpen;

[RelayCommand]
public void TogglePane() => IsPaneOpen = !IsPaneOpen;

public ObservableCollection<PagesModel> Pages { get; } =
[
new("SnapShot", new SnapShotView(), "CameraRegular"),
Expand All @@ -49,7 +39,7 @@ partial void OnCurrentPageChanged(PagesModel value)
case "Gallery":
{
var viewModel = value.Page.DataContext as ImageGalleryViewModel;
viewModel?.UpdateThumbnails();
viewModel?.UpdateThumbnails(selectFirst: false);
Console.WriteLine($"Update thumbnails for {value.Name}");
break;
}
Expand Down Expand Up @@ -81,21 +71,37 @@ public void OnCloseButtonClick()

private void StartFixWindowSizeTimer()
{
var maxTry = 20;
const int maxTry = 20;
var tryCount = 0;
var timer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(0.2)
};
timer.Tick += (sender, args) =>
{
var snapView = Pages[0].Page as SnapShotView;
if (snapView.FixWindowSize() || tryCount >= 10)
if (Pages[0].Page is SnapShotView snapView && (snapView.FixWindowSize() || tryCount >= maxTry))
{
timer.Stop();
}

tryCount++;
};
timer.Start();
}
}

public class PagesModel
{
public PagesModel(string name, UserControl page, string? iconKey = null)
{
Name = name;
Page = page;
if (iconKey == null) return;
Application.Current!.TryFindResource(iconKey, out var res);
ItemIcon = (StreamGeometry)res!;
}

public string Name { get; set; }
public UserControl Page { get; set; }
public StreamGeometry? ItemIcon { get; }
}
2 changes: 1 addition & 1 deletion src/VirtualStreetSnap/Views/ImageEditorView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
</ContextMenu>
</Button.ContextMenu>
</Button>
<Button Grid.Column="2" Background="Transparent" Command="{Binding RemoveLayer}">
<Button Grid.Column="1" Background="Transparent" Command="{Binding RemoveLayer}">
<PathIcon Data="{StaticResource Trash}" Width="14" />
</Button>
</Grid>
Expand Down
90 changes: 54 additions & 36 deletions src/VirtualStreetSnap/Views/ImageGalleryView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:VirtualStreetSnap.ViewModels"
xmlns:views="clr-namespace:VirtualStreetSnap.Views"
xmlns:i18n="clr-namespace:VirtualStreetSnap.Localizer"
xmlns:models="clr-namespace:VirtualStreetSnap.Models"
mc:Ignorable="d" d:DesignWidth="1280" d:DesignHeight="720"
Expand All @@ -13,9 +12,13 @@
<UserControl.DataContext>
<viewModels:ImageGalleryViewModel />
</UserControl.DataContext>

<Grid RowDefinitions="*,10,0.2*" Background="#111111">
<Panel Grid.Row="0">

<Grid ColumnDefinitions="3*,10,1*" Background="#111111">
<TextBlock Grid.Column="0" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"
Text="{i18n:Localize SelectAnImage}"
FontSize="24"
IsVisible="{Binding SelectedThumbnail, Converter={x:Static ObjectConverters.IsNull}}" />
<Panel Grid.Column="0" IsVisible="{Binding SelectedThumbnail, Converter={x:Static ObjectConverters.IsNotNull}}">
<ContentControl Content="{Binding SelectedImageViewer}" />
<Button Command="{Binding SelectPreviousThumbnail}"
Content="" FontSize="24" Opacity="0.5" Margin="10" Height="50"
Expand All @@ -32,42 +35,57 @@
<Button Content="{i18n:Localize SendToEditor}"
Command="{Binding EditSelectedImageCommand} " CommandParameter="{Binding $parent[Window]}" />
</StackPanel>

</Panel>
<GridSplitter Grid.Row="1" Background="Black"></GridSplitter>
<GridSplitter Grid.Column="1" Background="Black"></GridSplitter>
<!-- Thumbnails Display -->
<ScrollViewer x:Name="ThumbnailsScrollViewer" Grid.Row="2"
<ScrollViewer Grid.Column="2"
x:Name="ThumbnailsScrollViewer"
VerticalScrollBarVisibility="Auto"
Margin="0 0 10 0 "
ScrollChanged="ThumbnailsScrollViewer_ScrollChanged">
<ListBox ItemsSource="{Binding Thumbnails}"
SelectedItem="{Binding SelectedThumbnail}"
HorizontalAlignment="Left" Background="#111111"
SelectionMode="Single">
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="{i18n:Localize Delete}" Command="{Binding DeleteSelectedThumbnail}" />
<MenuItem Header="{i18n:Localize OpenInFolder}"
Command="{Binding OpenSelectedThumbnailFolder}" />
</ContextMenu>
</ListBox.ContextMenu>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type models:ImageBase}">
<Panel>
<Image Source="{Binding ImageThumb}" Stretch="UniformToFill" ToolTip.ShowDelay="500"
Width="100" Height="100">
<ToolTip.Tip>
<TextBlock Text="{Binding ImgName}" />
</ToolTip.Tip>
</Image>
</Panel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Spacing="5" HorizontalAlignment="Center" Margin="10 0 10 0">
<TextBlock Text="{i18n:Localize ThumbSize}" VerticalAlignment="Center" FontSize="16"></TextBlock>
<Slider Value="{Binding ThumbDisplaySize}" Maximum="200" Minimum="20" Width="100" TickFrequency="10"></Slider>
</StackPanel>
<ListBox ItemsSource="{Binding Thumbnails}"
SelectedItem="{Binding SelectedThumbnail}"
HorizontalAlignment="Left" Background="#111111"
SelectionMode="Single">
<ListBox.Styles>
<Style Selector="ListBoxItem">
<Setter Property="Padding" Value="4" />
<Setter Property="CornerRadius" Value="4"></Setter>
</Style>
</ListBox.Styles>
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="{i18n:Localize Delete}" Command="{Binding DeleteSelectedThumbnail}" />
<MenuItem Header="{i18n:Localize OpenInFolder}"
Command="{Binding OpenSelectedThumbnailFolder}" />
</ContextMenu>
</ListBox.ContextMenu>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type models:ImageBase}">
<Panel>
<Image Source="{Binding ImageThumb}" Stretch="UniformToFill" ToolTip.ShowDelay="500"
Width="{Binding $parent[UserControl].((viewModels:ImageGalleryViewModel)DataContext).ThumbDisplaySize}"
Height="{Binding $parent[UserControl].((viewModels:ImageGalleryViewModel)DataContext).ThumbDisplaySize}">
<ToolTip.Tip>
<TextBlock Text="{Binding ImgName}" />
</ToolTip.Tip>
</Image>
</Panel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</ScrollViewer>

</Grid>
Expand Down
Loading

0 comments on commit 8fd121c

Please sign in to comment.