Skip to content

Commit

Permalink
Fixed max opened recently not working and apply correct recently open…
Browse files Browse the repository at this point in the history
…ed after chaning max
  • Loading branch information
CPKreu committed Mar 21, 2021
1 parent 06d12c3 commit 1e0c4e7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
2 changes: 1 addition & 1 deletion PixiEditor/Models/DataHolders/Document/Document.IO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void UpdateRecentlyOpened(string newPath)
recentlyOpened.Move(index, 0);
}

if (recentlyOpened.Count > IPreferences.Current.GetPreference("maxOpenedRecently", 8))
if (recentlyOpened.Count > IPreferences.Current.GetPreference("MaxOpenedRecently", 8))
{
for (int i = 4; i < recentlyOpened.Count; i++)
{
Expand Down
28 changes: 21 additions & 7 deletions PixiEditor/Models/UserPreferences/PreferencesSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,21 @@ public void AddCallback(string setting, Action<object> action)
Init();
}

return Preferences.ContainsKey(name)
? (T)Preferences[name]
: fallbackValue;
try
{
return Preferences.ContainsKey(name)
? (T)Convert.ChangeType(Preferences[name], typeof(T))
: fallbackValue;
}
catch (InvalidCastException)
{
return fallbackValue;
}
}

public T? GetLocalPreference<T>(string name)
{
return GetPreference(name, default(T));
return GetLocalPreference(name, default(T));
}

public T? GetLocalPreference<T>(string name, T? fallbackValue)
Expand All @@ -136,9 +143,16 @@ public void AddCallback(string setting, Action<object> action)
Init();
}

return LocalPreferences.ContainsKey(name)
? (T)LocalPreferences[name]
: fallbackValue;
try
{
return LocalPreferences.ContainsKey(name)
? (T)Convert.ChangeType(LocalPreferences[name], typeof(T))
: fallbackValue;
}
catch (InvalidCastException)
{
return fallbackValue;
}
}

#nullable disable
Expand Down
24 changes: 23 additions & 1 deletion PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public FileViewModel(ViewModelMain owner)
{
HasRecent = true;
}

IPreferences.Current.AddCallback("MaxOpenedRecently", UpdateMaxRecentlyOpened);
}

public void OpenRecent(object parameter)
Expand Down Expand Up @@ -286,9 +288,29 @@ private bool CanSave(object property)
return Owner.BitmapManager.ActiveDocument != null;
}

private void UpdateMaxRecentlyOpened(object parameter)
{
int newAmount = (int)parameter;

if (newAmount >= RecentlyOpened.Count)
{
return;
}

var recentlyOpeneds = new List<RecentlyOpenedDocument>(RecentlyOpened.Take(newAmount));

RecentlyOpened.Clear();

foreach (var recent in recentlyOpeneds)
{
RecentlyOpened.Add(recent);
}
}

private List<RecentlyOpenedDocument> GetRecentlyOpenedDocuments()
{
var paths = IPreferences.Current.GetLocalPreference(nameof(RecentlyOpened), new JArray()).ToObject<string[]>();
var paths = IPreferences.Current.GetLocalPreference(nameof(RecentlyOpened), new JArray()).ToObject<string[]>()
.Take(IPreferences.Current.GetPreference("MaxOpenedRecently", 8));

List<RecentlyOpenedDocument> documents = new List<RecentlyOpenedDocument>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ public long DefaultNewFileHeight
public int MaxOpenedRecently
{
get => maxOpenedRecently;
set
{
maxOpenedRecently = value;
RaiseAndUpdatePreference(nameof(MaxOpenedRecently), value);
}
set => RaiseAndUpdatePreference(ref maxOpenedRecently, value);
}
}
}
4 changes: 2 additions & 2 deletions PixiEditor/Views/Dialogs/SettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
<CheckBox Content="Show Startup Window"
IsChecked="{Binding SettingsSubViewModel.File.ShowStartupWindow}"/>
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<Label Content="Max Saved Opened Recently:" ToolTip="How many documents are shown under File > Recent. Default: 10" Style="{StaticResource BaseLabel}"/>
<views:NumberInput FontSize="16" Value="{Binding SettingsSubViewModel.File.MaxOpenedRecently}" Width="40"/>
<Label Content="Max Saved Opened Recently:" ToolTip="How many documents are shown under File > Recent. Default: 8" Style="{StaticResource BaseLabel}"/>
<views:NumberInput FontSize="16" Value="{Binding SettingsSubViewModel.File.MaxOpenedRecently, Mode=TwoWay}" Width="40"/>
</StackPanel>
<Label Content="Default new file size:" Style="{StaticResource Header2}" Margin="0 20 0 20"/>
<StackPanel Orientation="Horizontal" Margin="40,0,0,0">
Expand Down

0 comments on commit 1e0c4e7

Please sign in to comment.