Skip to content

Commit

Permalink
fixed bugs with the appbar
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcadeRenegade committed Feb 13, 2016
1 parent cdd8c79 commit 849c26f
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 69 deletions.
4 changes: 1 addition & 3 deletions SidebarDiagnostics/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,13 @@ private void Close_Click(object sender, EventArgs e)
{
Shutdown();
}

#if !DEBUG

private static void AppDomain_Error(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = (Exception)e.ExceptionObject;

MessageBox.Show(ex.ToString(), Constants.Generic.ERRORTITLE, MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
}
#endif

public Sidebar GetSidebar
{
Expand Down
5 changes: 1 addition & 4 deletions SidebarDiagnostics/Dummy.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ public void Reposition()

Monitor.GetWorkArea(this, out _screen, out _edge, out _windowWA, out _appbarWA);

Left = _windowWA.Left;
Top = _windowWA.Top;
Width = _windowWA.Width;
Height = _windowWA.Height;
Move(_windowWA);
}

private void Window_Loaded(object sender, RoutedEventArgs e)
Expand Down
5 changes: 1 addition & 4 deletions SidebarDiagnostics/Sidebar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ private void BindPosition(Action callback)

Monitor.GetWorkArea(this, out _screen, out _edge, out _windowWA, out _appbarWA);

Left = _windowWA.Left;
Top = _windowWA.Top;
Width = _windowWA.Width;
Height = _windowWA.Height;
Move(_windowWA);

if (Framework.Settings.Instance.UseAppBar)
{
Expand Down
189 changes: 131 additions & 58 deletions SidebarDiagnostics/Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,40 +430,48 @@ private static IntPtr KeyHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam
break;

case KeyAction.CycleEdge:
switch (Framework.Settings.Instance.DockEdge)
if (_window.Visibility == Visibility.Visible)
{
case DockEdge.Right:
Framework.Settings.Instance.DockEdge = DockEdge.Left;
break;

default:
case DockEdge.Left:
Framework.Settings.Instance.DockEdge = DockEdge.Right;
break;
}
switch (Framework.Settings.Instance.DockEdge)
{
case DockEdge.Right:
Framework.Settings.Instance.DockEdge = DockEdge.Left;
break;

default:
case DockEdge.Left:
Framework.Settings.Instance.DockEdge = DockEdge.Right;
break;
}

Framework.Settings.Instance.Save();
Framework.Settings.Instance.Save();

_window.Reposition();
_window.Reposition();
}
break;

case KeyAction.CycleScreen:
Monitor[] _monitors = Monitor.GetMonitors();

if (Framework.Settings.Instance.ScreenIndex < (_monitors.Length - 1))
{
Framework.Settings.Instance.ScreenIndex++;
}
else
if (_window.Visibility == Visibility.Visible)
{
Framework.Settings.Instance.ScreenIndex = 0;
}
Monitor[] _monitors = Monitor.GetMonitors();

if (Framework.Settings.Instance.ScreenIndex < (_monitors.Length - 1))
{
Framework.Settings.Instance.ScreenIndex++;
}
else
{
Framework.Settings.Instance.ScreenIndex = 0;
}

Framework.Settings.Instance.Save();
Framework.Settings.Instance.Save();

_window.Reposition();
_window.Reposition();
}
break;
}

handled = true;
}
}

Expand All @@ -482,6 +490,22 @@ public struct RECT
public int Top;
public int Right;
public int Bottom;

public int Width
{
get
{
return Right - Left;
}
}

public int Height
{
get
{
return Bottom - Top;
}
}
}

public class WorkArea
Expand Down Expand Up @@ -670,7 +694,12 @@ public static void GetWorkArea(AppBarWindow window, out int screen, out DockEdge

double _modifyX = 0d;

if (window.IsAppBar && window.Screen == screen && window.DockEdge == edge)
if (
window.IsAppBar &&
window.Screen == screen &&
window.DockEdge == edge &&
(_screen.WorkArea.Width + window.AppBarWidth) <= _screen.Size.Width
)
{
_modifyX = window.AppBarWidth;
}
Expand Down Expand Up @@ -939,6 +968,70 @@ internal struct WINDOWPOS
public uint flags;
}

protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);

Loaded += AppBarWindow_Loaded;
}

private void AppBarWindow_Loaded(object sender, RoutedEventArgs e)
{
PreventMove();
}

public void Move(WorkArea workArea)
{
AllowMove();

Left = workArea.Left;
Top = workArea.Top;
Width = workArea.Width;
Height = workArea.Height;

PreventMove();
}

private void PreventMove()
{
if (!_canMove)
{
return;
}

_canMove = false;

HwndSource.AddHook(MoveHook);
}

private void AllowMove()
{
if (_canMove)
{
return;
}

_canMove = true;

HwndSource.RemoveHook(MoveHook);
}

private IntPtr MoveHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_WINDOWPOSCHANGING.MSG)
{
WINDOWPOS _pos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS));

_pos.flags |= WM_WINDOWPOSCHANGING.SWP_NOMOVE;

Marshal.StructureToPtr(_pos, lParam, true);

handled = true;
}

return IntPtr.Zero;
}

public void SetTop()
{
NativeMethods.SetWindowPos(
Expand Down Expand Up @@ -994,42 +1087,18 @@ public void ClearClickThrough()
NativeMethods.SetWindowLongPtr(_hwnd, WND_STYLE.GWL_EXSTYLE, _style & ~WND_STYLE.WS_EX_TRANSPARENT);
}

public void PreventMove()
{
HwndSource.AddHook(MoveHook);
}

public void AllowMove()
{
HwndSource.RemoveHook(MoveHook);
}

private IntPtr MoveHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_WINDOWPOSCHANGING.MSG)
{
WINDOWPOS _pos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS));

_pos.flags |= WM_WINDOWPOSCHANGING.SWP_NOMOVE;

Marshal.StructureToPtr(_pos, lParam, true);

handled = true;
}

return IntPtr.Zero;
}

public void SetAppBar(int screen, DockEdge edge, WorkArea windowWA, WorkArea appbarWA, Action callback)
{
if (edge == DockEdge.None)
{
throw new ArgumentException("This parameter cannot be set to 'none'.", "edge");
}

bool _init = false;

if (!IsAppBar)
{
IsAppBar = true;
IsAppBar = _init = true;

APPBARDATA _data = NewData();

Expand All @@ -1041,7 +1110,7 @@ public void SetAppBar(int screen, DockEdge edge, WorkArea windowWA, WorkArea app
Screen = screen;
DockEdge = edge;

DockAppBar(edge, windowWA, appbarWA, callback);
DockAppBar(edge, windowWA, appbarWA, _init, callback);
}

public void ClearAppBar()
Expand Down Expand Up @@ -1097,9 +1166,8 @@ private APPBARDATA NewData()
return _data;
}

private void DockAppBar(DockEdge edge, WorkArea windowWA, WorkArea appbarWA, Action callback)
private void DockAppBar(DockEdge edge, WorkArea windowWA, WorkArea appbarWA, bool init, Action callback)
{
PreventMove();

APPBARDATA _data = NewData();
_data.uEdge = (int)edge;
Expand All @@ -1122,11 +1190,14 @@ private void DockAppBar(DockEdge edge, WorkArea windowWA, WorkArea appbarWA, Act

AppBarWidth = appbarWA.Width;

Move(windowWA);

Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, (Action)(() =>
{
AllowMove();
HwndSource.AddHook(AppBarHook);
if (init)
{
HwndSource.AddHook(AppBarHook);
}
if (callback != null)
{
Expand All @@ -1149,7 +1220,7 @@ private IntPtr AppBarHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, re

_cancelReposition = new CancellationTokenSource();

Task.Delay(TimeSpan.FromMilliseconds(50), _cancelReposition.Token).ContinueWith(_ =>
Task.Delay(TimeSpan.FromMilliseconds(100), _cancelReposition.Token).ContinueWith(_ =>
{
if (_.IsCanceled)
{
Expand Down Expand Up @@ -1201,6 +1272,8 @@ private IntPtr AppBarHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, re

public double AppBarWidth { get; private set; } = 0;

private bool _canMove { get; set; } = true;

private int _callbackID { get; set; }

private int _prevZOrder { get; set; }
Expand Down

0 comments on commit 849c26f

Please sign in to comment.