//
// Copyright (c) PlaceholderCompany. All rights reserved.
//
namespace SystemTrayMenu.UserInterface
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using SystemTrayMenu.DataClasses;
using SystemTrayMenu.DllImports;
using SystemTrayMenu.Helper;
using SystemTrayMenu.Utilities;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
///
/// Logic of Menu window.
///
public partial class Menu : Window
{
#if TODO // SEARCH
public const string RowFilterShowAll = "[SortIndex] LIKE '%0%'";
#endif
private const int CornerRadius = 10;
private readonly Fading fading = new();
private bool isShowing;
private bool directionToRight;
private bool mouseDown;
private Point lastLocation;
#if TODO // SEARCH
private bool isSetSearchText;
private bool dgvHeightSet;
#endif
private bool isClosed = false; // TODO WPF Replace Forms wrapper
private DispatcherTimer timerUpdateIcons = new DispatcherTimer(DispatcherPriority.Render, Dispatcher.CurrentDispatcher);
internal Menu()
{
timerUpdateIcons.Tick += TimerUpdateIcons_Tick;
Closed += (_, _) =>
{
fading.Fade(Fading.FadingState.Idle);
timerUpdateIcons.Stop();
isClosed = true; // TODO WPF Replace Forms wrapper
};
Opacity = 0D;
fading.ChangeOpacity += Fading_ChangeOpacity;
void Fading_ChangeOpacity(object? sender, double newOpacity)
{
if (newOpacity != Opacity && !IsDisposed && !Disposing)
{
Opacity = newOpacity;
}
}
fading.Show += Fading_Show;
void Fading_Show()
{
try
{
isShowing = true;
Visibility = Visibility.Visible;
isShowing = false;
timerUpdateIcons.Start();
}
catch (ObjectDisposedException)
{
Visibility = Visibility.Hidden;
isShowing = false;
Log.Info($"Could not open menu, old menu was disposing," +
$" IsDisposed={IsDisposed}");
}
if (Visibility == Visibility.Visible)
{
if (Level == 0)
{
Activate();
Show();
}
else
{
ShowActivated = false;
Show();
}
}
}
fading.Hide += Hide;
InitializeComponent();
Assembly myassembly = Assembly.GetExecutingAssembly();
string myname = myassembly.GetName().Name ?? string.Empty;
txtTitle.Text = myname;
foreach (FrameworkElement control in
new List()
{
buttonMenuAlwaysOpen,
buttonOpenFolder,
buttonSettings,
buttonRestart,
pictureBoxSearch,
pictureBoxMenuAlwaysOpen,
pictureBoxOpenFolder,
pictureBoxSettings,
pictureBoxRestart,
pictureBoxLoading,
})
{
control.Width = Scaling.Scale(control.Width);
control.Height = Scaling.Scale(control.Height);
}
labelTitle.FontSize = Scaling.ScaleFontByPoints(8.25F);
textBoxSearch.FontSize = Scaling.ScaleFontByPoints(8.25F);
labelItems.FontSize = Scaling.ScaleFontByPoints(7F);
dgv.FontSize = Scaling.ScaleFontByPoints(9F);
MouseDown += Menu_MouseDown;
MouseUp += Menu_MouseUp;
MouseMove += Menu_MouseMove;
SolidColorBrush foreColor = new(Colors.Black);
SolidColorBrush backColor = AppColors.Background.ToSolidColorBrush();
SolidColorBrush backColorSearch = AppColors.SearchField.ToSolidColorBrush();
SolidColorBrush backgroundBorder = AppColors.BackgroundBorder.ToSolidColorBrush();
if (Config.IsDarkMode())
{
foreColor = new (Colors.White);
backColor = AppColors.DarkModeBackground.ToSolidColorBrush();
backColorSearch = AppColors.DarkModeSearchField.ToSolidColorBrush();
backgroundBorder = AppColors.DarkModeBackgroundBorder.ToSolidColorBrush();
}
labelTitle.Foreground = foreColor;
textBoxSearch.Foreground = foreColor;
dgv.Foreground = foreColor;
labelItems.Foreground = MenuDefines.ColorIcons.ToSolidColorBrush();
windowFrame.BorderBrush = backgroundBorder;
windowFrame.Background = backColor;
searchPanel.Background = backColorSearch;
panelLine.Background = AppColors.Icons.ToSolidColorBrush();
dgv.GotFocus += (_, _) => FocusTextBox();
#if TODO // Misc MouseEvents
dgv.MouseEnter += ControlsMouseEnter;
labelTitle.MouseEnter += ControlsMouseEnter;
textBoxSearch.MouseEnter += ControlsMouseEnter;
pictureBoxOpenFolder.MouseEnter += ControlsMouseEnter;
pictureBoxMenuAlwaysOpen.MouseEnter += ControlsMouseEnter;
pictureBoxSettings.MouseEnter += ControlsMouseEnter;
pictureBoxRestart.MouseEnter += ControlsMouseEnter;
pictureBoxSearch.MouseEnter += ControlsMouseEnter;
tableLayoutPanelMenu.MouseEnter += ControlsMouseEnter;
tableLayoutPanelDgvAndScrollbar.MouseEnter += ControlsMouseEnter;
tableLayoutPanelBottom.MouseEnter += ControlsMouseEnter;
labelItems.MouseEnter += ControlsMouseEnter;
void ControlsMouseEnter(object sender, EventArgs e)
{
MouseEnter?.Invoke();
}
dgv.MouseLeave += ControlsMouseLeave;
labelTitle.MouseLeave += ControlsMouseLeave;
textBoxSearch.MouseLeave += ControlsMouseLeave;
pictureBoxMenuAlwaysOpen.MouseLeave += ControlsMouseLeave;
pictureBoxOpenFolder.MouseLeave += ControlsMouseLeave;
pictureBoxSettings.MouseLeave += ControlsMouseLeave;
pictureBoxRestart.MouseLeave += ControlsMouseLeave;
pictureBoxSearch.MouseLeave += ControlsMouseLeave;
tableLayoutPanelMenu.MouseLeave += ControlsMouseLeave;
tableLayoutPanelDgvAndScrollbar.MouseLeave += ControlsMouseLeave;
tableLayoutPanelBottom.MouseLeave += ControlsMouseLeave;
labelItems.MouseLeave += ControlsMouseLeave;
void ControlsMouseLeave(object sender, EventArgs e)
{
MouseLeave?.Invoke();
}
#endif
#if TODO // TOUCH
bool isTouchEnabled = NativeMethods.IsTouchEnabled();
if ((isTouchEnabled && Properties.Settings.Default.DragDropItemsEnabledTouch) ||
(!isTouchEnabled && Properties.Settings.Default.DragDropItemsEnabled))
{
AllowDrop = true;
DragEnter += DragDropHelper.DragEnter;
DragDrop += DragDropHelper.DragDrop;
}
#endif
Loaded += (sender, e) =>
{
NativeMethods.HideFromAltTab(this);
// TODO WPF Replace Forms wrapper
IsHandleCreated = true;
HandleCreated?.Invoke(sender, e);
};
Closed += (sender, e) =>
{
foreach (ListViewItemData item in dgv.Items)
{
item.data.SubMenu?.Close();
}
};
}
internal new event Action MenuScrolled;
#if TODO // Misc MouseEvents
internal new event Action MouseEnter;
internal new event Action MouseLeave;
#endif
internal event Action? UserClickedOpenFolder;
internal event Action