| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Input;
- namespace MuchInfo.Chart.Infrastructure.Helpers
- {
- public class PopupManager
- {
- #region Fields
- //public static Panel PopupRoot { get; set; }
- #endregion Fields
- #region Enumerations
- public enum RelativePositionHoriz
- {
- Left,
- FlushRights,
- FlushLefts,
- Right
- }
- public enum RelativePositionVert
- {
- Above,
- FlushBottoms,
- FlushTops,
- Below
- }
- #endregion Enumerations
- #region Methods
- private static Popup CreatePopup()
- {
- return new Popup
- {
- AllowsTransparency = true,
- Placement = PlacementMode.Relative,
- };
- }
- #region Public Static Methods
- public static Popup CreateAppOverlay(Panel popupRoot)
- {
- var popup = CreatePopup();
- var popupManager = new PopupManager.AppOverlayPopup();
- popupManager.BuildPopup(popup, popupRoot);
- return popup;
- }
- public static Popup CreateAppOverlay(MouseEventArgs position, Panel popupRoot)
- {
- var popup = CreatePopup();
- var popupManager = new PopupManager.AppOverlayPopup();
- popupManager.BuildPopup(popup, popupRoot);
- bool flag = position != null;
- if (flag)
- {
- var p1 = position.GetPosition(popupRoot);
- popup.HorizontalOffset = (p1.X);
- popup.VerticalOffset = (p1.Y);
- }
- return popup;
- }
- public static Popup CreateOverlay(Panel popupRoot)
- {
- var popup = CreatePopup();
- var popupManager = new PopupManager.OverlayPopup();
- popupManager.BuildPopup(popup, popupRoot);
- return popup;
- }
- public static Popup CreateOverlay(MouseEventArgs position, Panel popupRoot)
- {
- var popup = CreatePopup();
- var popupManager = new PopupManager.OverlayPopup();
- popupManager.BuildPopup(popup, popupRoot);
- bool flag = position != null;
- if (flag)
- {
- var p1 = position.GetPosition(popupRoot);
- popup.HorizontalOffset = (p1.X);
- popup.VerticalOffset = (p1.Y);
- }
- return popup;
- }
- //public static Popup CreatePopUp(MouseEventArgs position, Panel popupRoot)
- //{
- // var popup = new Popup();
- // bool flag = position != null;
- // if (flag)
- // {
- // var currentPosition = position.GetPosition(popupRoot);
- // popup.HorizontalOffset = (currentPosition.X);
- // popup.VerticalOffset = (currentPosition.Y);
- // }
- // popup.Closed += (new EventHandler(PopupManager.PopUpManager_2090));
- // popupRoot.Children.Add(popup);
- // return popup;
- //}
- //public static Popup CreatePopUp(Panel popupRoot)
- //{
- // var popup = new Popup();
- // popup.Closed += (new EventHandler(PopupManager.PopUpManager_2090));
- // popupRoot.Children.Add(popup);
- // return popup;
- //}
- public static void MovePopup(Popup aPop, FrameworkElement relativeTo, PopupManager.RelativePositionHoriz positionLeft, PopupManager.RelativePositionVert positionTop, Panel popupRoot)
- {
- aPop.UpdateLayout();
- bool flag = aPop.Child != null;
- if (flag)
- {
- aPop.Child.UpdateLayout();
- }
- Point point = GeometryHelper.PositionOfControlRelative(relativeTo, popupRoot);
- switch (positionLeft)
- {
- case PopupManager.RelativePositionHoriz.Left:
- point.X = (point.X - aPop.ActualWidth);
- break;
- case PopupManager.RelativePositionHoriz.FlushRights:
- point.X = (point.X - (aPop.ActualWidth - relativeTo.ActualWidth));
- break;
- case PopupManager.RelativePositionHoriz.Right:
- point.X = (point.X + relativeTo.ActualWidth);
- break;
- }
- switch (positionTop)
- {
- case PopupManager.RelativePositionVert.Above:
- point.Y = (point.Y - aPop.ActualHeight);
- break;
- case PopupManager.RelativePositionVert.FlushBottoms:
- point.Y = (point.Y - (aPop.ActualHeight - relativeTo.ActualHeight));
- break;
- case PopupManager.RelativePositionVert.Below:
- point.Y = (point.Y + relativeTo.ActualHeight);
- break;
- }
- aPop.HorizontalOffset = (point.X);
- aPop.VerticalOffset = (point.Y);
- }
- //public static void SetPopUpRoot(UIElement root)
- //{
- // bool flag = root is Panel;
- // if (flag)
- // {
- // PopupManager.PopupRoot = (Panel)root;
- // return;
- // }
- // throw new Exception("Root is not a panel");
- //}
- #endregion Public Static Methods
- #region Private Static Methods
- //private static void PopUpManager_2090(object sender, System.EventArgs e)
- //{
- // bool flag = sender is Popup;
- // if (flag)
- // {
- // var popup = (Popup)sender;
- // try
- // {
- // popup.Child = (null);
- // popup.Closed -= (new EventHandler(PopupManager.PopUpManager_2090));
- // PopupManager.PopupRoot.Children.Remove(popup);
- // }
- // catch (Exception expr_59)
- // {
- // ProjectData.SetProjectError(expr_59);
- // ProjectData.ClearProjectError();
- // }
- // }
- //}
- #endregion Private Static Methods
- #endregion Methods
- #region Nested Types
- private sealed class AppOverlayPopup
- {
- #region Fields
- private Popup _popup;
- private Panel _panel;
- #endregion Fields
- #region Properties
- #region Public Properties
- private Popup Popup
- {
- get
- {
- return this._popup;
- }
- set
- {
- var sizeChangedEventHandler = new SizeChangedEventHandler(this.Popup_SizeChanged);
- var eventHandler = new EventHandler(this.Popup_Closed);
- bool flag = this._popup != null;
- if (flag)
- {
- this._popup.SizeChanged -= (sizeChangedEventHandler);
- this._popup.Closed -= (eventHandler);
- }
- this._popup = value;
- flag = (this._popup != null);
- if (flag)
- {
- this._popup.SizeChanged += (sizeChangedEventHandler);
- this._popup.Closed += (eventHandler);
- }
- }
- }
- private Panel Root
- {
- get
- {
- return this._panel;
- }
- set
- {
- var mouseButtonEventHandler = new MouseButtonEventHandler(this.Panel_MouseLeftButtonDown);
- var routedEventHandler = new RoutedEventHandler(this.Panel_GotFocus);
- bool flag = this._panel != null;
- if (flag)
- {
- this._panel.MouseLeftButtonDown -= (mouseButtonEventHandler);
- this._panel.GotFocus -= (routedEventHandler);
- }
- this._panel = value;
- flag = (this._panel != null);
- if (flag)
- {
- this._panel.MouseLeftButtonDown += (mouseButtonEventHandler);
- this._panel.GotFocus += (routedEventHandler);
- }
- }
- }
- #endregion Public Properties
- #endregion Properties
- #region Methods
- #region Internal Methods
- internal void BuildPopup(Popup aPop, Panel root)
- {
- this.Root = root;
- this.Popup = aPop;
- root.Children.Add(this.Popup);
- }
- #endregion Internal Methods
- #region Private Methods
- private void Popup_Closed(object sender, System.EventArgs e)
- {
- this.Root.Children.Remove(this.Popup);
- this.Popup.Child = (null);
- this.Root = null;
- this.Popup = null;
- }
- private void Panel_GotFocus(object sender, RoutedEventArgs e)
- {
- this.Popup.IsOpen = false;
- }
- private void Panel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- this.Popup.IsOpen = false;
- }
- private void Popup_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- GeometryHelper.EnsureControlInApplication(this.Popup, this.Root);
- }
- #endregion Private Methods
- #endregion Methods
- }
- private sealed class OverlayPopup
- {
- #region Fields
- private Popup _popup;
- private Panel _panel;
- #endregion Fields
- #region Constructors
- public OverlayPopup()
- {
- //this.MainGrid = new Grid();
- }
- #endregion Constructors
- #region Properties
- #region Public Properties
- //private Grid MainGrid
- //{
- // get
- // {
- // return this._mainGrid;
- // }
- // set
- // {
- // var mouseButtonEventHandler = new MouseButtonEventHandler(MainGrid_MouseLeftButtonDown);
- // bool flag = this._mainGrid != null;
- // if (flag)
- // {
- // this._mainGrid.MouseLeftButtonDown -= (mouseButtonEventHandler);
- // }
- // this._mainGrid = value;
- // flag = (this._mainGrid != null);
- // if (flag)
- // {
- // this._mainGrid.MouseLeftButtonDown += (mouseButtonEventHandler);
- // }
- // }
- //}
- //private Popup BasePopup { get; set; }
- private Popup Popup
- {
- get
- {
- return this._popup;
- }
- set
- {
- var eventHandler = new EventHandler(this.Popup_Closed);
- bool flag = this._popup != null;
- if (flag)
- {
- this._popup.Closed -= (eventHandler);
- }
- this._popup = value;
- flag = (this._popup != null);
- if (flag)
- {
- this._popup.Closed += (eventHandler);
- }
- }
- }
- private Panel Root
- {
- get
- {
- return this._panel;
- }
- set
- {
- //var sizeChangedEventHandler = new SizeChangedEventHandler(this.Panel_SizeChanged);
- //bool flag = this._panel != null;
- //if (flag)
- //{
- // this._panel.SizeChanged -= (sizeChangedEventHandler);
- //}
- this._panel = value;
- //flag = (this._panel != null);
- //if (flag)
- //{
- // this._panel.SizeChanged += (sizeChangedEventHandler);
- //}
- }
- }
- #endregion Public Properties
- #endregion Properties
- #region Methods
- #region Internal Methods
- internal void BuildPopup(Popup popup, Panel root)
- {
- this.Root = root;
- this.Popup = popup;
- root.Children.Add(this.Popup);
- //this.BasePopup = new Popup()
- //{
- // AllowsTransparency = true,
- // Placement = PlacementMode.Relative
- //};
- //root.Children.Add(this.BasePopup);
- //this.BasePopup.IsOpen = true;
- //this.MainGrid.Background = new SolidColorBrush(Colors.Transparent);
- //this.MainGrid.Width = root.ActualWidth;
- //this.MainGrid.Height = root.ActualHeight;
- //this.BasePopup.Child = this.MainGrid;
- //父容器左击其它区域退出菜单
- this.Root.MouseLeftButtonDown -= Root_MouseLeftButtonDown;
- this.Root.MouseLeftButtonDown += Root_MouseLeftButtonDown;
- //父容器右击其它区域退出菜单(后面添加的)
- this.Root.MouseRightButtonDown -= Root_MouseLeftButtonDown;
- this.Root.MouseRightButtonDown += Root_MouseLeftButtonDown;
- }
- void Root_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- //popup不能弹出看这里:在popup父容器MouseLeftButtonDown set handled = true
- if (Popup != null) Popup.IsOpen = false;
- }
- #endregion Internal Methods
- #region Private Static Methods
- #endregion Private Static Methods
- #region Private Methods
- //private void MainGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- //{
- // this.Popup.IsOpen = false;
- //}
- private void Popup_Closed(object sender, System.EventArgs e)
- {
- //this.BasePopup.IsOpen = false;
- this.Root.Children.Remove(this.Popup);
- //this.Root.Children.Remove(this.BasePopup);
- this.Root.MouseLeftButtonDown -= Root_MouseLeftButtonDown;
- this.Root.MouseRightButtonDown -= Root_MouseLeftButtonDown;
- this.Popup.Child = null;
- //this.BasePopup = null;
- this.Root = null;
- //this.MainGrid = null;
- this.Popup = null;
- }
- //private void Panel_SizeChanged(object sender, SizeChangedEventArgs e)
- //{
- // this.MainGrid.Width = this.Root.ActualWidth;
- // this.MainGrid.Height = this.Root.ActualHeight;
- //}
- #endregion Private Methods
- #endregion Methods
- }
- #endregion Nested Types
- }
- }
|