Skip to content

Commit

Permalink
Merge pull request #50 from Dirkster99/LoadLayoutAsync
Browse files Browse the repository at this point in the history
Load layout async
  • Loading branch information
Dirkster99 authored Jul 14, 2019
2 parents bfe2dce + 94325a4 commit 1b818e8
Show file tree
Hide file tree
Showing 11 changed files with 435 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
Expand Down
36 changes: 35 additions & 1 deletion source/MLibTest/MLibTest/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

/// <summary>
Expand All @@ -28,11 +29,20 @@ static App()
// Create service model to ensure available services
ServiceInjector.InjectServices();
}

public App()
{
LayoutLoaded = new LayoutLoader(@".\AvalonDock.Layout.config");
}
#endregion constructors

internal LayoutLoader LayoutLoaded { get; }

#region methods
private void Application_Startup(object sender, StartupEventArgs e)
{
LayoutLoaded.LoadLayout();

try
{
// Set shutdown mode here (and reset further below) to enable showing custom dialogs (messageboxes)
Expand Down Expand Up @@ -141,7 +151,8 @@ private void MainWindow_Loaded(object sender, RoutedEventArgs e)
Debug.WriteLine(exp);
}

_mainWindow.OnLoadLayout();
// Load and layout AvalonDock elements when MainWindow has loaded
_mainWindow.OnLoadLayoutAsync();

/***
try
Expand Down Expand Up @@ -266,6 +277,29 @@ private void OnClosed(AppViewModel appVM, IViewSize win)
}
}

private LayoutLoaderResult LoadAvalonDockLayoutToString()
{
string path = System.IO.Path.GetFullPath(@".\AvalonDock.Layout.config");

if (System.IO.File.Exists(path) == false)
return null;

try
{
//Thread.Sleep(2000);
return new LayoutLoaderResult(System.IO.File.ReadAllText(path), true, null);
}
catch (Exception exc)
{
return new LayoutLoaderResult(null, false, exc);
}
}

internal async Task<LayoutLoaderResult> GetLayoutString(EventHandler<LayoutLoadedEventArgs> loadEventHandler)
{
return await LayoutLoaded.GetLayoutString(loadEventHandler);
}

/// <summary>
/// This method gets the service locator instance
/// that is used in turn to get an application specific service instance.
Expand Down
93 changes: 49 additions & 44 deletions source/MLibTest/MLibTest/Demos/ViewModels/AD/FileViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,25 @@

internal class FileViewModel : PaneViewModel
{
#region fields
private static ImageSourceConverter ISC = new ImageSourceConverter();
private IWorkSpaceViewModel _workSpaceViewModel = null;

private string _textContent = string.Empty;
private string _filePath = null;
private bool _isDirty = false;

ICommand _closeCommand = null;
ICommand _saveAsCommand = null;
ICommand _saveCommand = null;
#endregion fields

#region ctors
/// <summary>
/// Class constructor
/// </summary>
/// <param name="filePath"></param>
/// <param name="workSpaceViewModel"></param>
public FileViewModel(string filePath, IWorkSpaceViewModel workSpaceViewModel)
: this(workSpaceViewModel)
{
Expand All @@ -21,15 +37,19 @@ public FileViewModel(string filePath, IWorkSpaceViewModel workSpaceViewModel)
IconSource = ISC.ConvertFromInvariantString(@"pack://application:,,/Demos/Images/document.png") as ImageSource;
}

/// <summary>
/// class constructor
/// </summary>
/// <param name="workSpaceViewModel"></param>
public FileViewModel(IWorkSpaceViewModel workSpaceViewModel)
{
_workSpaceViewModel = workSpaceViewModel;
IsDirty = true;
IsDirty = false;
Title = FileName;
}
#endregion ctors

#region FilePath
private string _filePath = null;
#region Properties
public string FilePath
{
get { return _filePath; }
Expand All @@ -50,23 +70,19 @@ public string FilePath
}
}
}
#endregion


public string FileName
{
get
get
{
if (FilePath == null)
return "Noname" + (IsDirty ? "*" : "");

return System.IO.Path.GetFileName(FilePath) + (IsDirty ? "*" : "");
return System.IO.Path.GetFileName(FilePath) + (IsDirty ? "*" : "");
}
}

#region TextContent

private string _textContent = string.Empty;
public string TextContent
{
get { return _textContent; }
Expand All @@ -83,9 +99,6 @@ public string TextContent

#endregion

#region IsDirty

private bool _isDirty = false;
public bool IsDirty
{
get { return _isDirty; }
Expand All @@ -100,10 +113,6 @@ public bool IsDirty
}
}

#endregion

#region SaveCommand
ICommand _saveCommand = null;
public ICommand SaveCommand
{
get
Expand All @@ -117,20 +126,6 @@ public ICommand SaveCommand
}
}

private bool CanSave(object parameter)
{
return IsDirty;
}

private void OnSave(object parameter)
{
_workSpaceViewModel.Save(this, false);
}

#endregion

#region SaveAsCommand
ICommand _saveAsCommand = null;
public ICommand SaveAsCommand
{
get
Expand All @@ -144,20 +139,7 @@ public ICommand SaveAsCommand
}
}

private bool CanSaveAs(object parameter)
{
return IsDirty;
}

private void OnSaveAs(object parameter)
{
_workSpaceViewModel.Save(this, true);
}

#endregion

#region CloseCommand
ICommand _closeCommand = null;
public ICommand CloseCommand
{
get
Expand All @@ -170,7 +152,10 @@ public ICommand CloseCommand
return _closeCommand;
}
}
#endregion
#endregion Properties

#region methods
private bool CanClose()
{
return true;
Expand All @@ -180,6 +165,26 @@ private void OnClose()
{
_workSpaceViewModel.Close(this);
}
#endregion

private bool CanSave(object parameter)
{
return IsDirty;
}

private void OnSave(object parameter)
{
_workSpaceViewModel.Save(this, false);
}

private bool CanSaveAs(object parameter)
{
return IsDirty;
}

private void OnSaveAs(object parameter)
{
_workSpaceViewModel.Save(this, true);
}
#endregion methods
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ internal interface IWorkSpaceViewModel
/// <param name="fileToSave"></param>
/// <param name="saveAsFlag"></param>
void Save(FileViewModel fileToSave, bool saveAsFlag = false);

/// <summary>
/// Open a file and return its content in a viewmodel.
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
FileViewModel Open(string filepath);
#endregion methods
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ private void OnOpen(object parameter)
}
}

/// <summary>
/// Open a file and return its content in a viewmodel.
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
public FileViewModel Open(string filepath)
{
var fileViewModel = _files.FirstOrDefault(fm => fm.FilePath == filepath);
Expand All @@ -346,6 +351,7 @@ public FileViewModel Open(string filepath)

fileViewModel = new FileViewModel(filepath, this as IWorkSpaceViewModel);
_files.Add(fileViewModel);

return fileViewModel;
}
#endregion OpenCommand
Expand Down
3 changes: 3 additions & 0 deletions source/MLibTest/MLibTest/MLibTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
<DependentUpon>ColorSelectionView.xaml</DependentUpon>
</Compile>
<Compile Include="Models\AppCore.cs" />
<Compile Include="Models\LayoutLoadedEventArgs.cs" />
<Compile Include="Models\LayoutLoader.cs" />
<Compile Include="Models\LayoutLoaderResult.cs" />
<Compile Include="Models\SettingDefaults.cs" />
<Compile Include="ServiceInjector.cs" />
<Compile Include="ViewModels\AppLifeCycleViewModel.cs" />
Expand Down
13 changes: 10 additions & 3 deletions source/MLibTest/MLibTest/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,23 @@
AnchorablesSource="{Binding Tools}"
DocumentsSource="{Binding Files}"
ActiveContent="{Binding ActiveDocument, Mode=TwoWay, Converter={StaticResource ActiveDocumentConverter}}"
Grid.Row="1">
Grid.Row="1"
Visibility="Hidden"
>
<!-- avalonDock:DockingManager.Theme>
<avalonDock:Vs2013LightTheme/>
</avalonDock:DockingManager.Theme -->
<avalonDock:DockingManager.LayoutItemTemplateSelector>
<demos:PanesTemplateSelector>
<demos:PanesTemplateSelector.FileViewTemplate>
<DataTemplate>
<TextBox Text="{Binding TextContent, UpdateSourceTrigger=PropertyChanged}"
BorderThickness="0" />
<Grid>
<TextBox
Text="{Binding TextContent, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"
AcceptsReturn="True"
BorderThickness="0" />
</Grid>
</DataTemplate>
</demos:PanesTemplateSelector.FileViewTemplate>
<demos:PanesTemplateSelector.FileStatsViewTemplate>
Expand Down
Loading

0 comments on commit 1b818e8

Please sign in to comment.