Skip to content

Commit

Permalink
Finalize v1.1.0
Browse files Browse the repository at this point in the history
Localization setting for user.
folder.png is now embedded resource.
  • Loading branch information
ChiNoel-osu committed Nov 30, 2022
1 parent e33c40b commit d4bb750
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 79 deletions.
4 changes: 2 additions & 2 deletions App.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</configSections>
<applicationSettings>
<FolderThumbnailExplorer.Properties.Settings>
<setting name="TestSetting" serializeAs="String">
<value>DefaultTestString123123123 I suck at coding.</value>
<setting name="Locale" serializeAs="String">
<value>zh-CN</value>
</setting>
</FolderThumbnailExplorer.Properties.Settings>
</applicationSettings>
Expand Down
13 changes: 12 additions & 1 deletion App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Windows;
using System.Globalization;
using System.Threading.Tasks;

namespace FolderThumbnailExplorer
{
Expand All @@ -10,7 +11,17 @@ public partial class App : Application
{
public App()
{
CultureInfo.CurrentUICulture = new CultureInfo("zh-CN");
#region Load settings.
try
{
CultureInfo.CurrentUICulture = new CultureInfo(FolderThumbnailExplorer.Properties.Settings.Default.Locale);
}
catch (System.Exception ex)
{
Task.Run(() => MessageBox.Show($"{ex.Message}\nThe app will now use en-US as default language.", "Yo Fucked UP!", MessageBoxButton.OK, MessageBoxImage.Asterisk));
CultureInfo.CurrentUICulture = new CultureInfo("en-US");
}
#endregion
}
}
}
8 changes: 8 additions & 0 deletions FolderThumbnailExplorer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
</PropertyGroup>

<ItemGroup>
<None Remove="folder.png" />
</ItemGroup>

<ItemGroup>
<Content Include="icon.ico" />
</ItemGroup>

<ItemGroup>
<Resource Include="folder.png" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="FolderThumbnailExplorer.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="TestSetting" Type="System.String" Scope="Application">
<Value Profile="(Default)">DefaultTestString123123123 I suck at coding.</Value>
<Setting Name="Locale" Type="System.String" Scope="Application">
<Value Profile="(Default)">zh-CN</Value>
</Setting>
</Settings>
</SettingsFile>
6 changes: 4 additions & 2 deletions View/AddNewFav.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
Topmost="True"
ShowInTaskbar="False">
ShowInTaskbar="False"
Loaded="Window_Loaded">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down Expand Up @@ -60,7 +61,8 @@
Margin="0,-1,53,23"
Grid.ColumnSpan="3"
Grid.RowSpan="2" />
<TextBox Grid.Row="1"
<TextBox Name="NameBox"
Grid.Row="1"
Background="Transparent"
BorderBrush="CornflowerBlue"
Margin="0,2,53,4"
Expand Down
5 changes: 5 additions & 0 deletions View/AddNewFav.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ public AddNewFav(string defaultPath = "")
InitializeComponent();
DataContext = new AddNewFavoriteViewModel(defaultPath);
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
NameBox.Focus();
}
}
}
1 change: 1 addition & 0 deletions View/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
Foreground="White"
Margin="0,1,1,0"
Padding="3,2,0,3"
SelectedIndex="{Binding MainPageViewModel.CBBoxSelectedIndex}"
ItemsSource="{Binding MainPageViewModel.ComboBoxItems}"
SelectedItem="{Binding MainPageViewModel.CBBoxSelected, Mode=OneWayToSource}">
<ComboBox.ItemContainerStyle>
Expand Down
115 changes: 46 additions & 69 deletions ViewModel/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void ReGetContent()
if (dirShit.ContentExistsInPath(_PATHtoShow))
{
string[] dirs = dirShit.DirInPath(_PATHtoShow);
if (dirs != null)
if (dirs is not null)
{
CustomContentItem lastAdded = null; //Mark the last added item.
List<string> unauthorizedFolders = new List<string>();
Expand All @@ -78,58 +78,53 @@ public void ReGetContent()
break;
}
FileAttributes dirAtt = new DirectoryInfo(dir).Attributes;
if (!(dirAtt.HasFlag(FileAttributes.System) || dirAtt.HasFlag(FileAttributes.Hidden))) //Actually hidden folders can be read now, it's handled.
if (!(dirAtt.HasFlag(FileAttributes.System)/* || dirAtt.HasFlag(FileAttributes.Hidden)*/)) //Actually hidden folders can be read now, it's handled below.
{
string[] allowedExt = { ".jpg", ".png", ".jpeg", ".gif" };
string firstFilePath;
try
{
//Get first image file
firstFilePath = Directory.EnumerateFiles(dir, "*.*").Where(s => allowedExt.Any(s.ToLower().EndsWith)).First();
}
catch (InvalidOperationException)
{ //No such image file, set default
firstFilePath = Directory.GetCurrentDirectory() + "\\folder.png";
}
try //Get first image file. Is EnumerateFiles faster than GetFiles? idk.
{ firstFilePath = Directory.EnumerateFiles(dir, "*.*").Where(s => allowedExt.Any(s.ToLower().EndsWith)).First(); }
catch (InvalidOperationException) //No such image file, set default
{ firstFilePath = "Bruh"; }
catch (UnauthorizedAccessException)
{ //Some top secret folder encounted
unauthorizedFolders.Add(dir);
continue; //Skip folder and continue with the next dir.
}
BitmapImage bitmap = new BitmapImage();
using (FileStream stream = File.OpenRead(firstFilePath))
{ //Use stream sourec instead of regular uri source to improve responsiveness.
if (firstFilePath == "Bruh")
{ //Default thumbnail.
bitmap.BeginInit();
//Use BitmapCacheOption.OnLoad to even make it display.
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.DecodePixelWidth = SliderValue + 128; //Make it sharper
try
{
bitmap.EndInit();
}
catch (NotSupportedException) //Bad image file, use default.
{
bitmap = new BitmapImage();
using (FileStream def = File.OpenRead(Directory.GetCurrentDirectory() + "\\folder.png"))
bitmap.DecodePixelWidth = SliderValue + 128;
bitmap.UriSource = new Uri("pack://application:,,,/folder.png");
bitmap.EndInit();
bitmap.Freeze();
}
else
using (FileStream stream = File.OpenRead(firstFilePath))
{ //Use stream sourec instead of regular uri source to improve responsiveness.
bitmap.BeginInit();
//Use BitmapCacheOption.OnLoad to even make it display.
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.DecodePixelWidth = SliderValue + 128; //Make it sharper
try { bitmap.EndInit(); }
catch (NotSupportedException) //Bad image file, use default.
{
bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = def;
bitmap.DecodePixelWidth = SliderValue + 128;
bitmap.UriSource = new Uri("pack://application:,,,/folder.png");
bitmap.EndInit();
}
finally //This is VITAL for it to be passed between threads.
{ bitmap.Freeze(); }
}
finally
{
//This is VITAL for it to be passed between threads.
bitmap.Freeze();
}
}
CustomContentItem newItem = new CustomContentItem { ThumbNail = bitmap, Header = dirShit.GetFileFolderName(dir) };
lastAdded = newItem;
//Items should be created in UI thread, and Dispatcher.Invoke does it.
Application.Current.Dispatcher.Invoke(() => Content.Add(lastAdded)); //This code can cause exception as Application.Current will be null when the app is closed. But user have to close it anyway so yeah ill just leave it there.
Application.Current.Dispatcher.Invoke(() => Content.Add(lastAdded));
//The above line can cause exception as Application.Current will be null when the app is closed. But user have to close it anyway so yeah ill just leave it there.
}
}
//Done adding stuff, notify user about unauthorized folder if any.
Expand All @@ -154,52 +149,38 @@ public void ThumbnailClicked(Image img)
Mouse.RemoveMouseUpHandler(img, ThumbnailMouseUpHandler);
Mouse.AddMouseUpHandler(img, ThumbnailMouseUpHandler);
}
[RelayCommand]
public void LabelDoubleClicked(Label label)
{ //Double click advance folder
string imageFolder = label.Content.ToString();
PATHtoShow = PATHtoShow.EndsWith('\\') ? string.Format("{0}{1}", PATHtoShow, imageFolder) : string.Format("{0}\\{1}", PATHtoShow, imageFolder);
}

private void ThumbnailMouseUpHandler(object sender, MouseButtonEventArgs e)
{
string imageFolder = ((Image)sender).ToolTip.ToString();
if (e.ChangedButton == MouseButton.Left)
{
string imagePath = ((FileStream)((BitmapImage)((Image)sender).Source).StreamSource).Name;
string imageFolder = ((Image)sender).ToolTip.ToString();
if (imagePath.EndsWith("folder.png"))
{ //No image found (default folder.png), advance path.
if (PATHtoShow.EndsWith('\\')) //Check drive root
PATHtoShow = string.Format("{0}{1}", PATHtoShow, imageFolder);
else
PATHtoShow = string.Format("{0}\\{1}", PATHtoShow, imageFolder);
}
if (((Image)sender).Source.ToString().EndsWith("folder.png")) //No image found (default folder.png), advance path.
PATHtoShow = PATHtoShow.EndsWith('\\') ? string.Format("{0}{1}", PATHtoShow, imageFolder) : string.Format("{0}\\{1}", PATHtoShow, imageFolder);
else
{ //Image found, start Photo Viewer.
if (imagePath.EndsWith("folder.png")) return; //No image in folder, return.
string imagePath = ((FileStream)((BitmapImage)((Image)sender).Source).StreamSource).Name;
imagePath = imagePath.Remove(imagePath.LastIndexOf('\\'));
PhotoViewer photoViewer = new PhotoViewer(imagePath);
photoViewer.Left = 0; photoViewer.Top = 0; //Spawns window at top left corner.
wnds.Push(photoViewer); //Add this to opened windows list to close it when mainwindows closes
photoViewer.Show();
}
}
else if (e.ChangedButton == MouseButton.Right)
{ //Right click to Start explorer.exe
string imageFolder = ((Image)sender).ToolTip.ToString();
string path;
if (PATHtoShow.EndsWith('\\'))
path = PATHtoShow + imageFolder;
else
path = PATHtoShow + '\\' + imageFolder;
Process.Start("explorer.exe", path);
}
}
[RelayCommand]
public void LabelDoubleClicked(Label label)
{ //Double click advance folder
string imageFolder = label.Content.ToString();
if (PATHtoShow.EndsWith('\\')) //Check drive root
PATHtoShow = string.Format("{0}{1}", PATHtoShow, imageFolder);
else
PATHtoShow = string.Format("{0}\\{1}", PATHtoShow, imageFolder);
else if (e.ChangedButton == MouseButton.Right) //Right click to Start explorer.exe
Process.Start("explorer.exe", PATHtoShow.EndsWith('\\') ? (PATHtoShow + imageFolder) : (PATHtoShow + '\\' + imageFolder));
}

#region AddNewFavorite
private Button addBtn;
[ObservableProperty]
int _CBBoxSelectedIndex = -1;
[RelayCommand]
public void AddNewFav(Button button)
{
Expand Down Expand Up @@ -241,7 +222,7 @@ public ComboBoxItem CBBoxSelected
public ObservableCollection<ComboBoxItem> ComboBoxItems
{
get
{ //Read Favorite file
{
Task.Run(() =>
{
string favPath = Directory.GetCurrentDirectory() + "\\Favorites.txt";
Expand Down Expand Up @@ -276,11 +257,7 @@ public ObservableCollection<ComboBoxItem> ComboBoxItems
{
FavItem.Add(tempName, str[(str.LastIndexOf('|') + 1)..]);
ComboBoxItem comboBoxItem = null;
Application.Current.Dispatcher.Invoke(() =>
{
comboBoxItem = new ComboBoxItem
{ Content = tempName, ToolTip = str[(str.LastIndexOf('|') + 1)..] };
});
Application.Current.Dispatcher.Invoke(() => comboBoxItem = new ComboBoxItem { Content = tempName, ToolTip = str[(str.LastIndexOf('|') + 1)..] });
_ComboBoxItems.Add(comboBoxItem);
}
isOddLine = !isOddLine;
Expand Down

0 comments on commit d4bb750

Please sign in to comment.