Skip to content

Commit

Permalink
Give proper title bar text
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed May 21, 2020
1 parent 85f06a3 commit bb2714e
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 157 deletions.
325 changes: 169 additions & 156 deletions OpenKh.Tools.BarEditor/ViewModels/BarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,151 +16,156 @@
namespace OpenKh.Tools.BarEditor.ViewModels
{
public class BarViewModel : GenericListModel<BarEntryModel>
{
private static readonly List<FileDialogFilter> Filters = FileDialogFilterComposer.Compose().AddAllFiles();
{
private static string ApplicationName = Utilities.GetApplicationName();
private string _fileName;
private static readonly List<FileDialogFilter> Filters = FileDialogFilterComposer.Compose().AddAllFiles();
private readonly ToolInvokeDesc _toolInvokeDesc;

public string Title => $"{FileName ?? "untitled"} | {ApplicationName}";

public BarViewModel() : this((IEnumerable<BarEntryModel>)null) { }

public BarViewModel(ToolInvokeDesc desc) :
this(Bar.Read(desc.SelectedEntry.Stream))
public BarViewModel(ToolInvokeDesc desc) :
this(Bar.Read(desc.SelectedEntry.Stream))
{
_toolInvokeDesc = desc;
_toolInvokeDesc = desc;

NewCommand = new RelayCommand(x => { }, x => false);
OpenCommand = new RelayCommand(x => { }, x => false);
NewCommand = new RelayCommand(x => { }, x => false);
OpenCommand = new RelayCommand(x => { }, x => false);
SaveCommand = new RelayCommand(x =>
{
{
var stream = _toolInvokeDesc.SelectedEntry.Stream;
stream.Position = 0;
stream.SetLength(0);
stream.SetLength(0);
Bar.Write(stream, Items.Select(item => item.Entry));
});
SaveAsCommand = new RelayCommand(x => { }, x => false);
}

public BarViewModel(IEnumerable<Bar.Entry> list) :
this(list.Select(x => new BarEntryModel(x)))
{ }

public BarViewModel(IEnumerable<BarEntryModel> list) :
base(list)
{
Types = new EnumModel<Bar.EntryType>();

NewCommand = new RelayCommand(x =>
{
Items.Clear();
}, x => true);

OpenCommand = new RelayCommand(x =>
{
FileDialog.OnOpen(fileName =>
{
});
SaveAsCommand = new RelayCommand(x => { }, x => false);
}

public BarViewModel(IEnumerable<Bar.Entry> list) :
this(list.Select(x => new BarEntryModel(x)))
{ }

public BarViewModel(IEnumerable<BarEntryModel> list) :
base(list)
{
Types = new EnumModel<Bar.EntryType>();

NewCommand = new RelayCommand(x =>
{
FileName = "untitled.bar";
Items.Clear();
}, x => true);

OpenCommand = new RelayCommand(x =>
{
FileDialog.OnOpen(fileName =>
{
OpenFileName(fileName);
FileName = fileName;
}, Filters);
}, x => true);

SaveCommand = new RelayCommand(x =>
{
if (!string.IsNullOrEmpty(FileName))
{
using (var stream = File.Open(FileName, FileMode.Create))
{
Bar.Write(stream, Items.Select(item => item.Entry));
}
}
else
{
SaveAsCommand.Execute(x);
}
}, x => true);
FileName = fileName;
}, Filters);
}, x => true);

SaveCommand = new RelayCommand(x =>
{
if (!string.IsNullOrEmpty(FileName))
{
using (var stream = File.Open(FileName, FileMode.Create))
{
Bar.Write(stream, Items.Select(item => item.Entry));
}
}
else
{
SaveAsCommand.Execute(x);
}
}, x => true);

SaveAsCommand = new RelayCommand(x =>
{
FileDialog.OnSave(fileName =>
{
{
FileDialog.OnSave(fileName =>
{
SaveToFile(fileName);
}, Filters);
}, x => true);

ExitCommand = new RelayCommand(x =>
{
Window.Close();
}, x => true);

AboutCommand = new RelayCommand(x =>
{
new AboutDialog(Assembly.GetExecutingAssembly()).ShowDialog();
}, x => true);

OpenItemCommand = new RelayCommand(x =>
{
try
{
var tempFileName = SaveToTempraryFile();
switch (ToolsLoaderService.OpenTool(FileName, tempFileName, SelectedItem.Entry))
}, Filters, Path.GetFileName(FileName));
}, x => true);

ExitCommand = new RelayCommand(x =>
{
Window.Close();
}, x => true);

AboutCommand = new RelayCommand(x =>
{
new AboutDialog(Assembly.GetExecutingAssembly()).ShowDialog();
}, x => true);

OpenItemCommand = new RelayCommand(x =>
{
try
{
var tempFileName = SaveToTempraryFile();
switch (ToolsLoaderService.OpenTool(FileName, tempFileName, SelectedItem.Entry))
{
case Common.ToolInvokeDesc.ContentChangeInfo.File:
ReloadFromTemporaryFile(tempFileName);
break;
case Common.ToolInvokeDesc.ContentChangeInfo.File:
ReloadFromTemporaryFile(tempFileName);
break;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
OnPropertyChanged(nameof(SelectedItem));
}, x => true);

ExportCommand = new RelayCommand(x =>
{
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
OnPropertyChanged(nameof(SelectedItem));
}, x => true);

ExportCommand = new RelayCommand(x =>
{
var defaultFileName = GetSuggestedFileName(SelectedItem.Entry);
FileDialog.OnSave(fileName =>
{
using (var fStream = File.OpenWrite(fileName))
{
SelectedItem.Entry.Stream.Position = 0;
SelectedItem.Entry.Stream.CopyTo(fStream);
}
}, Filters, defaultFileName);
}, x => IsItemSelected);
FileDialog.OnSave(fileName =>
{
using (var fStream = File.OpenWrite(fileName))
{
SelectedItem.Entry.Stream.Position = 0;
SelectedItem.Entry.Stream.CopyTo(fStream);
}
}, Filters, defaultFileName);
}, x => IsItemSelected);

ExportAllCommand = new RelayCommand(x =>
{
FileDialog.OnFolder(folder =>
{
foreach (var item in Items.Select(item => item.Entry))
{
var fileName = GetSuggestedFileName(item);
using (var fStream = File.OpenWrite(Path.Combine(folder, fileName)))
{
item.Stream.Position = 0;
item.Stream.CopyTo(fStream);
}
}
});
FileDialog.OnFolder(folder =>
{
foreach (var item in Items.Select(item => item.Entry))
{
var fileName = GetSuggestedFileName(item);
using (var fStream = File.OpenWrite(Path.Combine(folder, fileName)))
{
item.Stream.Position = 0;
item.Stream.CopyTo(fStream);
}
}
});
}, x => true);

ImportCommand = new RelayCommand(x =>
{
FileDialog.OnOpen(fileName =>
{
using (var fStream = File.OpenRead(fileName))
{
var memStream = new MemoryStream((int)fStream.Length);
fStream.CopyTo(memStream);
SelectedItem.Entry.Stream = memStream;
}
OnPropertyChanged(nameof(SelectedItem.Size));
}, Filters);
}, x => IsItemSelected);
SearchCommand = new RelayCommand(x => { }, x => false);
}
{
FileDialog.OnOpen(fileName =>
{
using (var fStream = File.OpenRead(fileName))
{
var memStream = new MemoryStream((int)fStream.Length);
fStream.CopyTo(memStream);
SelectedItem.Entry.Stream = memStream;
}
OnPropertyChanged(nameof(SelectedItem.Size));
}, Filters);
}, x => IsItemSelected);
SearchCommand = new RelayCommand(x => { }, x => false);
}

public void OpenFileName(string fileName)
{
Expand All @@ -180,64 +185,72 @@ private void SaveToFile(string fileName)
Bar.Write(stream, Items.Select(item => item.Entry));
}

private string GetTemporaryFileName(string actualFileName)
private string GetTemporaryFileName(string actualFileName)
{
return Path.GetTempFileName();
return Path.GetTempFileName();
}

private string SaveToTempraryFile()
{
var tempFileName = GetTemporaryFileName(FileName);
SaveToFile(tempFileName);
{
var tempFileName = GetTemporaryFileName(FileName);
SaveToFile(tempFileName);

return tempFileName;
}
return tempFileName;
}

private void ReloadFromTemporaryFile(string tempFileName)
private void ReloadFromTemporaryFile(string tempFileName)
{
OpenFileName(tempFileName);
OpenFileName(tempFileName);
}

private static string GetSuggestedFileName(Bar.Entry item) =>
$"{item.Name}_{item.Index}.{Helpers.GetSuggestedExtension(item.Type)}";

private Window Window => Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);

public string FileName { get; set; }
public string FileName
{
get => _fileName;
set
{
_fileName = value;
OnPropertyChanged(nameof(Title));
}
}

public RelayCommand NewCommand { get; set; }
public RelayCommand OpenCommand { get; set; }
public RelayCommand SaveCommand { get; set; }
public RelayCommand SaveAsCommand { get; set; }
public RelayCommand ExitCommand { get; set; }
public RelayCommand AboutCommand { get; set; }
public RelayCommand NewCommand { get; set; }
public RelayCommand OpenCommand { get; set; }
public RelayCommand SaveCommand { get; set; }
public RelayCommand SaveAsCommand { get; set; }
public RelayCommand ExitCommand { get; set; }
public RelayCommand AboutCommand { get; set; }
public RelayCommand ExportCommand { get; set; }
public RelayCommand ExportAllCommand { get; set; }
public RelayCommand ImportCommand { get; set; }
public RelayCommand OpenItemCommand { get; set; }
public RelayCommand SearchCommand { get; set; }
public RelayCommand OpenItemCommand { get; set; }
public RelayCommand SearchCommand { get; set; }

public EnumModel<Bar.EntryType> Types { get; set; }
public EnumModel<Bar.EntryType> Types { get; set; }

public string ExportFileName => IsItemSelected ? $"{SelectedItem?.DisplayName}.bin" : "(no file selected)";
public string ExportFileName => IsItemSelected ? $"{SelectedItem?.DisplayName}.bin" : "(no file selected)";

protected override BarEntryModel OnNewItem()
{
return new BarEntryModel(new Bar.Entry()
{
Stream = new MemoryStream()
});
}
protected override BarEntryModel OnNewItem()
{
return new BarEntryModel(new Bar.Entry()
{
Stream = new MemoryStream()
});
}

protected override void OnSelectedItem(BarEntryModel item)
{
base.OnSelectedItem(item);
protected override void OnSelectedItem(BarEntryModel item)
{
base.OnSelectedItem(item);

ExportCommand.CanExecute(SelectedItem);
ImportCommand.CanExecute(SelectedItem);
OpenItemCommand.CanExecute(SelectedItem);
ExportCommand.CanExecute(SelectedItem);
ImportCommand.CanExecute(SelectedItem);
OpenItemCommand.CanExecute(SelectedItem);

OnPropertyChanged(nameof(ExportFileName));
}
}
OnPropertyChanged(nameof(ExportFileName));
}
}
}
2 changes: 1 addition & 1 deletion OpenKh.Tools.BarEditor/Views/BarView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns:local="clr-namespace:OpenKh.Tools.BarEditor.Views"
xmlns:controls="clr-namespace:Xe.Tools.Wpf.Controls;assembly=Xe.Tools.Wpf"
mc:Ignorable="d"
Title="BAR editor - OpenKH" Height="300" Width="400"
Title="{Binding Title}" Height="300" Width="400"
Drop="Window_Drop" AllowDrop="True">
<DockPanel>
<Menu DockPanel.Dock="Top">
Expand Down

0 comments on commit bb2714e

Please sign in to comment.