Skip to content

Commit

Permalink
fix window resize when switching; fix invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
xupefei committed May 24, 2017
1 parent 8238da2 commit 8e02828
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class BooleanToResizeBorderThicknessConverter : DependencyObject,
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return Visibility.Visible;
return 0;

var v = (bool) value;

Expand Down
25 changes: 25 additions & 0 deletions QuickLook/Converters/BooleanToResizeModeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace QuickLook.Converters
{
public sealed class BooleanToResizeModeConverter : DependencyObject, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return ResizeMode.CanResize;

var v = (bool) value;

return v ? ResizeMode.CanResize : ResizeMode.NoResize;
}

object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
5 changes: 5 additions & 0 deletions QuickLook/MainWindowTransparent.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
ShowActivated="False" ShowInTaskbar="False"
FontFamily="Segoe UI,Microsoft Yahei UI">
<Window.Resources>
<converters:BooleanToResizeModeConverter x:Key="BooleanToResizeModeConverter" />
<converters:BooleanToResizeBorderThicknessConverter x:Key="BooleanToResizeBorderThicknessConverter" />
</Window.Resources>
<Window.ResizeMode>
<Binding Converter="{StaticResource BooleanToResizeModeConverter}" ElementName="mainWindow"
Path="ContextObject.CanResize" />
</Window.ResizeMode>
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="{Binding Height, ElementName=titlebar}"
ResizeBorderThickness="{Binding ContextObject.CanResize, Converter={StaticResource BooleanToResizeBorderThicknessConverter}, ElementName=mainWindow}"
Expand Down
64 changes: 27 additions & 37 deletions QuickLook/MainWindowTransparent.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,6 @@ private void OpenWithAssocApp()
BeginHide(true);
}

private new void Show()
{
// revert UI changes
ContextObject.IsBusy = true;

var newHeight = ContextObject.PreferredSize.Height + titlebar.Height + windowBorder.BorderThickness.Top +
windowBorder.BorderThickness.Bottom;
var newWidth = ContextObject.PreferredSize.Width + windowBorder.BorderThickness.Left +
windowBorder.BorderThickness.Right;

ResizeAndCenter(new Size(newWidth, newHeight));

base.Show();

//if (!ContextObject.CanFocus)
// WindowHelper.SetNoactivate(new WindowInteropHelper(this));
}

private void ResizeAndCenter(Size size)
{
if (!IsLoaded)
Expand Down Expand Up @@ -103,21 +85,6 @@ internal void UnloadPlugin()
ContextObject.ViewerPlugin?.Cleanup();
}

private new void Hide()
{
UnloadPlugin();
ContextObject.Reset();

GC.Collect();

// revert UI changes
ContextObject.IsBusy = true;

base.Hide();
//Left -= 10000;
//Dispatcher.Delay(100, _ => base.Hide());
}

internal void BeginShow(IViewer matchedPlugin, string path)
{
ContextObject.CurrentContentContainer = container;
Expand All @@ -129,8 +96,20 @@ internal void BeginShow(IViewer matchedPlugin, string path)

SetOpenWithButtonAndPath(path);

// revert UI changes
ContextObject.IsBusy = true;

var newHeight = ContextObject.PreferredSize.Height + titlebar.Height + windowBorder.BorderThickness.Top +
windowBorder.BorderThickness.Bottom;
var newWidth = ContextObject.PreferredSize.Width + windowBorder.BorderThickness.Left +
windowBorder.BorderThickness.Right;

ResizeAndCenter(new Size(newWidth, newHeight));

Show();

//WindowHelper.SetActivate(new WindowInteropHelper(this), ContextObject.CanFocus);

// load plugin, do not block UI
Exception thrown = null;
Dispatcher.BeginInvoke(new Action(() =>
Expand Down Expand Up @@ -159,9 +138,9 @@ private void SetOpenWithButtonAndPath(string path)
buttonOpenWith.Content = isExe == true ? $"Run {appFriendlyName}" : $"Open with {appFriendlyName}";
}

internal bool BeginHide(bool quit = false)
internal bool BeginHide(bool quitIfViewer = false, bool disposePluginOnly = false)
{
if (quit && App.RunningAsViewer)
if (quitIfViewer && App.RunningAsViewer)
{
Application.Current.Shutdown();
return true;
Expand All @@ -170,9 +149,20 @@ internal bool BeginHide(bool quit = false)
if (Visibility != Visibility.Visible)
return false;

Hide();
UnloadPlugin();
ContextObject.Reset();
GC.Collect();

// revert UI changes
ContextObject.IsBusy = true;

if (!disposePluginOnly)
{
Hide();
return true;
}

return true;
return false;
}
}
}
1 change: 1 addition & 0 deletions QuickLook/QuickLook.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<Compile Include="..\GitVersion.cs">
<Link>Properties\GitVersion.cs</Link>
</Compile>
<Compile Include="Converters\BooleanToResizeModeConverter.cs" />
<Compile Include="Converters\BooleanToVisibilityCollapsedConverter.cs" />
<Compile Include="Converters\BooleanToResizeBorderThicknessConverter.cs" />
<Compile Include="Helpers\AutoStartupHelper.cs" />
Expand Down
7 changes: 4 additions & 3 deletions QuickLook/ViewWindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ internal void InvokeRoutine(Keys key)
// do we need switch to another file?
var replaceView = key == Keys.Up || key == Keys.Down || key == Keys.Left || key == Keys.Right;

if (replaceView && _currentMainWindow.IsLoaded && _currentMainWindow.Visibility != Visibility.Visible)
if (replaceView && _currentMainWindow.Visibility != Visibility.Visible)
return;

if (!WindowHelper.IsFocusedControlExplorerItem())
if (!WindowHelper.IsFocusedWindowSelf())
if (replaceView || !WindowHelper.IsFocusedWindowSelf())
return;

if (!replaceView && _currentMainWindow.BeginHide())
// should the window be closed (replaceView == false), return without showing new one
if (_currentMainWindow.BeginHide(disposePluginOnly: replaceView))
return;

var path = GetCurrentSelection();
Expand Down

0 comments on commit 8e02828

Please sign in to comment.