First taskbar position iteration

- Simplified code
- Overlapping is prevented for the very first instead of the first two opened menues
- When taskbar position was changed while STM is running, it will take updated position into account
This commit is contained in:
Peter Kirmeier 2020-09-20 17:26:45 +02:00
parent f2c20c5a5e
commit d4bf10f174
2 changed files with 53 additions and 25 deletions

View file

@ -27,11 +27,6 @@ namespace SystemTrayMenu.Business
private readonly BackgroundWorker workerMainMenu = new BackgroundWorker(); private readonly BackgroundWorker workerMainMenu = new BackgroundWorker();
private readonly List<BackgroundWorker> workersSubMenu = new List<BackgroundWorker>(); private readonly List<BackgroundWorker> workersSubMenu = new List<BackgroundWorker>();
private readonly int screenHeight = Screen.PrimaryScreen.Bounds.Height;
private readonly int screenWidth = Screen.PrimaryScreen.Bounds.Width;
private readonly int screenRight = Screen.PrimaryScreen.Bounds.Right;
private readonly int taskbarHeight = new WindowsTaskbar().Size.Height;
private readonly DgvMouseRow dgvMouseRow = new DgvMouseRow(); private readonly DgvMouseRow dgvMouseRow = new DgvMouseRow();
private readonly WaitToLoadMenu waitToOpenMenu = new WaitToLoadMenu(); private readonly WaitToLoadMenu waitToOpenMenu = new WaitToLoadMenu();
private readonly KeyboardInput keyboardInput = null; private readonly KeyboardInput keyboardInput = null;
@ -41,6 +36,7 @@ namespace SystemTrayMenu.Business
private OpenCloseState openCloseState = OpenCloseState.Default; private OpenCloseState openCloseState = OpenCloseState.Default;
private RowData loadingRowData = null; private RowData loadingRowData = null;
private bool showingMessageBox = false; private bool showingMessageBox = false;
private TaskbarPosition taskbarPosition = new WindowsTaskbar().Position;
public Menus() public Menus()
{ {
@ -513,7 +509,7 @@ namespace SystemTrayMenu.Business
menus[0] = Create( menus[0] = Create(
GetData(workerMainMenu, Config.Path, 0), GetData(workerMainMenu, Config.Path, 0),
Path.GetFileName(Config.Path)); Path.GetFileName(Config.Path));
menus[0].AdjustSizeAndLocation(screenHeight, screenRight, taskbarHeight); AdjustMenusSizeAndLocation();
DisposeMenu(menus[0]); DisposeMenu(menus[0]);
} }
@ -859,23 +855,54 @@ namespace SystemTrayMenu.Business
private void AdjustMenusSizeAndLocation() private void AdjustMenusSizeAndLocation()
{ {
WindowsTaskbar taskbar = new WindowsTaskbar();
Menu menuPredecessor = null; Menu menuPredecessor = null;
int widthPredecessors = -1; // -1 padding int widthPredecessors = -1; // -1 padding
bool directionToRight = false;
List<Menu> list = AsList; List<Menu> list = AsList;
bool directionToRight;
Menu menu; Menu menu;
Rectangle screenBounds = Screen.PrimaryScreen.Bounds;
// Only apply taskbar position change when no menu is currently open
if (list.Count == 1)
{
taskbarPosition = taskbar.Position;
}
// Shrink the usable space depending on taskbar location
switch (taskbarPosition)
{
case TaskbarPosition.Left:
screenBounds.X += taskbar.Size.Width;
screenBounds.Width -= taskbar.Size.Width;
break;
case TaskbarPosition.Right:
screenBounds.Width -= taskbar.Size.Width;
break;
case TaskbarPosition.Top:
screenBounds.Y += taskbar.Size.Height;
screenBounds.Height -= taskbar.Size.Height;
break;
case TaskbarPosition.Bottom:
default:
screenBounds.Height -= taskbar.Size.Height;
break;
}
// For all taskbars at Bottom/Right/Top, go left
directionToRight = taskbarPosition == TaskbarPosition.Left ? true : false;
for (int i = 0; i < list.Count; i++) for (int i = 0; i < list.Count; i++)
{ {
menu = list[i]; menu = list[i];
// skip calculation based on the predecessor for the very first menu // Skip calculation based on the predecessor for the very first menu
if (menuPredecessor != null) if (menuPredecessor != null)
{ {
int newWidth = menu.Width - menu.Padding.Horizontal + menuPredecessor.Width; int newWidth = menu.Width - menu.Padding.Horizontal + menuPredecessor.Width;
if (directionToRight) if (directionToRight)
{ {
if (widthPredecessors - menus[0].Width - menu.Width < 0) if (widthPredecessors - menu.Width < 0)
{ {
directionToRight = false; directionToRight = false;
} }
@ -884,7 +911,7 @@ namespace SystemTrayMenu.Business
widthPredecessors -= newWidth; widthPredecessors -= newWidth;
} }
} }
else if (screenWidth < widthPredecessors + menus[0].Width + menu.Width) else if (screenBounds.Width < widthPredecessors + menu.Width)
{ {
directionToRight = true; directionToRight = true;
widthPredecessors -= newWidth; widthPredecessors -= newWidth;
@ -893,10 +920,16 @@ namespace SystemTrayMenu.Business
widthPredecessors += menu.Width - menu.Padding.Left; widthPredecessors += menu.Width - menu.Padding.Left;
} }
// only last one has to be updated as all previous one were already updated in the past // Only last one has to be updated as all previous one were already updated in the past
if (list.Count - 1 == i) if (list.Count - 1 == i)
{ {
menu.AdjustSizeAndLocation(screenHeight, screenRight, taskbarHeight, menuPredecessor, directionToRight); menu.AdjustSizeAndLocation(screenBounds, menuPredecessor, directionToRight);
}
if (i == 0)
{
// Remember width of the initial menu as we don't want to overlap with it
screenBounds.Width -= menu.Width - menu.Padding.Horizontal;
} }
menuPredecessor = menu; menuPredecessor = menu;

View file

@ -279,11 +279,9 @@ namespace SystemTrayMenu.UserInterface
} }
internal void AdjustSizeAndLocation( internal void AdjustSizeAndLocation(
int screenHeight, Rectangle bounds,
int screenRight, Menu menuPredecessor,
int taskbarHeight, bool directionToRight)
Menu menuPredecessor = null,
bool directionToRight = false)
{ {
CheckForAutoResizeRowDone(); CheckForAutoResizeRowDone();
void CheckForAutoResizeRowDone() void CheckForAutoResizeRowDone()
@ -310,8 +308,7 @@ namespace SystemTrayMenu.UserInterface
} }
else else
{ {
dgv.RowTemplate.Height = menuPredecessor.GetDataGridView(). dgv.RowTemplate.Height = menuPredecessor.GetDataGridView().RowTemplate.Height;
RowTemplate.Height;
foreach (DataGridViewRow row in dgv.Rows) foreach (DataGridViewRow row in dgv.Rows)
{ {
row.Height = dgv.RowTemplate.Height; row.Height = dgv.RowTemplate.Height;
@ -321,12 +318,10 @@ namespace SystemTrayMenu.UserInterface
} }
} }
int dgvHeightNeeded = dgv.Rows.GetRowsHeight( int dgvHeightNeeded = dgv.Rows.GetRowsHeight(DataGridViewElementStates.None);
DataGridViewElementStates.None);
int menuRestNeeded = Height - dgv.Height; int menuRestNeeded = Height - dgv.Height;
int dgvHeightMax = screenHeight - taskbarHeight - int dgvHeightMax = bounds.Height - menuRestNeeded;
menuRestNeeded;
if (dgvHeightNeeded > dgvHeightMax) if (dgvHeightNeeded > dgvHeightMax)
{ {
@ -344,7 +339,7 @@ namespace SystemTrayMenu.UserInterface
int x; int x;
if (menuPredecessor == null) if (menuPredecessor == null)
{ {
x = screenRight - Width; x = bounds.Width - Width;
} }
else else
{ {
@ -371,7 +366,7 @@ namespace SystemTrayMenu.UserInterface
int y; int y;
if (menuPredecessor == null) if (menuPredecessor == null)
{ {
y = screenHeight - taskbarHeight - Height; y = bounds.Height - Height;
} }
else else
{ {