Exemple #1
0
        public MainWindow()
        {
            if (WpfUtil.IsInDesignMode)
            {
                return;
            }
            if (!NTMinerConsole.IsEnabled)
            {
                NTMinerConsole.Enable();
            }
            this.Vm          = new MainWindowViewModel();
            this.DataContext = Vm;
            this.MinHeight   = 430;
            this.MinWidth    = 640;
            this.Width       = AppRoot.MainWindowWidth;
            this.Height      = AppRoot.MainWindowHeight;
#if DEBUG
            NTStopwatch.Start();
#endif
            ConsoleWindow.Instance.MouseDown += (sender, e) => {
                MoveConsoleWindow();
            };
            SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
            this.Loaded += (sender, e) => {
                ConsoleTabItemTopBorder.Margin = new Thickness(0, ConsoleTabItem.ActualHeight - 1, 0, 0);
                MoveConsoleWindow();
                hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
                hwndSource.AddHook(new HwndSourceHook(Win32Proc.WindowProc));
            };
            InitializeComponent();
            _leftDrawerGripWidth    = LeftDrawerGrip.Width;
            _btnOverClockBackground = BtnOverClock.Background;
            // 下面几行是为了看见设计视图
            this.ResizeCursors.Visibility = Visibility.Visible;
            this.HideLeftDrawerGrid();
            // 上面几行是为了看见设计视图

            DateTime lastGetServerMessageOn = DateTime.MinValue;
            // 切换了主界面上的Tab时
            this.MainTabControl.SelectionChanged += (sender, e) => {
                // 延迟创建,以加快主界面的启动
                #region
                var selectedItem = MainTabControl.SelectedItem;
                if (selectedItem == TabItemSpeedTable)
                {
                    if (SpeedTableContainer.Child == null)
                    {
                        SpeedTableContainer.Child = GetSpeedTable();
                    }
                }
                else if (selectedItem == TabItemMessage)
                {
                    if (MessagesContainer.Child == null)
                    {
                        MessagesContainer.Child = new Messages();
                    }
                }
                else if (selectedItem == TabItemToolbox)
                {
                    if (ToolboxContainer.Child == null)
                    {
                        ToolboxContainer.Child = new Toolbox();
                    }
                }
                else if (selectedItem == TabItemMinerProfileOption)
                {
                    if (MinerProfileOptionContainer.Child == null)
                    {
                        MinerProfileOptionContainer.Child = new MinerProfileOption();
                    }
                }
                RpcRoot.SetIsServerMessagesVisible(selectedItem == TabItemMessage);
                if (selectedItem == TabItemMessage)
                {
                    if (lastGetServerMessageOn.AddSeconds(10) < DateTime.Now)
                    {
                        lastGetServerMessageOn = DateTime.Now;
                        VirtualRoot.Execute(new LoadNewServerMessageCommand());
                    }
                }
                if (selectedItem == ConsoleTabItem)
                {
                    ConsoleTabItemTopBorder.Visibility = Visibility.Visible;
                }
                else
                {
                    ConsoleTabItemTopBorder.Visibility = Visibility.Collapsed;
                }
                #endregion
            };
            this.IsVisibleChanged += (sender, e) => {
                #region
                if (this.IsVisible)
                {
                    NTMinerContext.IsUiVisible = true;
                }
                else
                {
                    NTMinerContext.IsUiVisible = false;
                }
                MoveConsoleWindow();
                #endregion
            };
            this.StateChanged += (s, e) => {
                #region
                if (Vm.MinerProfile.IsShowInTaskbar)
                {
                    ShowInTaskbar = true;
                }
                else
                {
                    if (WindowState == WindowState.Minimized)
                    {
                        ShowInTaskbar = false;
                    }
                    else
                    {
                        ShowInTaskbar = true;
                    }
                }
                if (WindowState == WindowState.Maximized)
                {
                    ResizeCursors.Visibility = Visibility.Collapsed;
                }
                else
                {
                    ResizeCursors.Visibility = Visibility.Visible;
                }
                MoveConsoleWindow();
                #endregion
            };
            bool isLeftClosed = false;
            this.ConsoleRectangle.IsVisibleChanged += (sender, e) => {
                if (this.ConsoleRectangle.IsVisible)
                {
                    if (isLeftClosed != (LeftDrawerGrip.Width == _leftDrawerGripWidth))
                    {
                        ConsoleWindowFit();
                    }
                }
                else
                {
                    isLeftClosed = LeftDrawerGrip.Width == _leftDrawerGripWidth;
                }
            };
            this.ConsoleRectangle.SizeChanged += (s, e) => {
                MoveConsoleWindow();
            };
            if (this.Width < 860)
            {
                NTMinerConsole.UserWarn("左侧面板已折叠,可点击侧边的'开始挖矿'按钮展开。");
            }
            this.SizeChanged += (s, e) => {
                #region
                if (this.Width < 860)
                {
                    this.CloseLeftDrawer();
                    this.BtnAboutNTMiner.Visibility = Visibility.Collapsed;
                }
                else
                {
                    this.OpenLeftDrawer(isSizeChanged: true);
                    this.BtnAboutNTMiner.Visibility = Visibility.Visible;
                }
                if (!this.ConsoleRectangle.IsVisible)
                {
                    if (e.WidthChanged)
                    {
                        ConsoleWindow.Instance.Width = e.NewSize.Width;
                    }
                    if (e.HeightChanged)
                    {
                        ConsoleWindow.Instance.Height = e.NewSize.Height;
                    }
                }
                #endregion
            };
            NotiCenterWindow.Bind(this, ownerIsTopmost: true);
            this.LocationChanged += (sender, e) => {
                MoveConsoleWindow();
            };
            VirtualRoot.BuildCmdPath <TopmostCommand>(path: message => {
                UIThread.Execute(() => {
                    if (!this.Topmost)
                    {
                        this.Topmost = true;
                    }
                });
            }, this.GetType());
            VirtualRoot.BuildCmdPath <UnTopmostCommand>(path: message => {
                UIThread.Execute(() => {
                    if (this.Topmost)
                    {
                        this.Topmost = false;
                    }
                });
            }, this.GetType());
            VirtualRoot.BuildCmdPath <CloseMainWindowCommand>(path: message => {
                UIThread.Execute(() => {
                    if (message.IsAutoNoUi)
                    {
                        SwitchToNoUi();
                    }
                    else
                    {
                        this.Close();
                    }
                });
            }, location: this.GetType());
            this.BuildEventPath <Per1MinuteEvent>("挖矿中时自动切换为无界面模式", LogEnum.DevConsole,
                                                  path: message => {
                if (NTMinerContext.IsUiVisible && NTMinerContext.Instance.MinerProfile.IsAutoNoUi && NTMinerContext.Instance.IsMining)
                {
                    if (NTMinerContext.MainWindowRendedOn.AddMinutes(NTMinerContext.Instance.MinerProfile.AutoNoUiMinutes) < message.BornOn)
                    {
                        VirtualRoot.ThisLocalInfo(nameof(MainWindow), $"挖矿中界面展示{NTMinerContext.Instance.MinerProfile.AutoNoUiMinutes}分钟后自动切换为无界面模式,可在选项页调整配置");
                        VirtualRoot.Execute(new CloseMainWindowCommand(isAutoNoUi: true));
                    }
                }
            }, location: this.GetType());
#if DEBUG
            var elapsedMilliseconds = NTStopwatch.Stop();
            if (elapsedMilliseconds.ElapsedMilliseconds > NTStopwatch.ElapsedMilliseconds)
            {
                NTMinerConsole.DevTimeSpan($"耗时{elapsedMilliseconds} {this.GetType().Name}.ctor");
            }
#endif
        }
        private MinerClientsWindow()
        {
            if (WpfUtil.IsInDesignMode)
            {
                return;
            }
            Width            = SystemParameters.FullPrimaryScreenWidth * 0.95;
            Height           = SystemParameters.FullPrimaryScreenHeight * 0.95;
            this.DataContext = Vm;
            ConsoleWindow.Instance.Show();
            ConsoleWindow.Instance.MouseDown += (sender, e) => {
                MoveConsoleWindow();
            };
            this.Owner   = ConsoleWindow.Instance;
            this.Loaded += (sender, e) => {
                MoveConsoleWindow();
                hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
                hwndSource.AddHook(new HwndSourceHook(Win32Proc.WindowProc));
                this.WindowState = WindowState.Maximized;
            };
            InitializeComponent();
            // 这一行是为了看见设计视图
            this.ResizeCursors.Visibility = Visibility.Visible;
            this.TbUcName.Text            = nameof(MinerClientsWindow);
            DateTime lastGetServerMessageOn = DateTime.MinValue;

            this.MinerStudioTabControl.SelectionChanged += (sender, e) => {
                // 延迟创建,以加快主界面的启动
                #region
                var selectedItem = MinerStudioTabControl.SelectedItem;
                if (selectedItem == MinerStudioTabItemMessage)
                {
                    if (MinerStudioMessagesContainer.Child == null)
                    {
                        MinerStudioMessagesContainer.Child = new Messages();
                    }
                    if (lastGetServerMessageOn.AddSeconds(10) < DateTime.Now)
                    {
                        lastGetServerMessageOn = DateTime.Now;
                        VirtualRoot.Execute(new LoadNewServerMessageCommand());
                    }
                }
                RpcRoot.SetIsServerMessagesVisible(selectedItem == MinerStudioTabItemMessage);
                #endregion
            };
            this.MinerClienTabControl.SelectionChanged += (sender, e) => {
                // 延迟创建,以加快主界面的启动
                #region
                var selectedItem = MinerClienTabControl.SelectedItem;
                if (selectedItem == MinerClientTabItemMessage)
                {
                    if (MinerClientMessagesContainer.Child == null)
                    {
                        MinerClientMessagesContainer.Child = new MinerClientMessages();
                    }
                    MinerStudioRoot.SetIsMinerClientMessagesVisible(true);
                }
                else
                {
                    MinerStudioRoot.SetIsMinerClientMessagesVisible(false);
                }
                #endregion
            };
            this.ConsoleRectangle.IsVisibleChanged += (sender, e) => {
                MoveConsoleWindow();
            };
            this.ConsoleRectangle.SizeChanged += (s, e) => {
                MoveConsoleWindow();
            };
            this.StateChanged += (s, e) => {
                #region
                if (WindowState == WindowState.Maximized)
                {
                    ResizeCursors.Visibility = Visibility.Collapsed;
                }
                else
                {
                    ResizeCursors.Visibility = Visibility.Visible;
                }
                MoveConsoleWindow();
                #endregion
            };
            this.SizeChanged += (s, e) => {
                #region
                if (!this.ConsoleRectangle.IsVisible)
                {
                    if (e.WidthChanged)
                    {
                        ConsoleWindow.Instance.Width = e.NewSize.Width;
                    }
                    if (e.HeightChanged)
                    {
                        ConsoleWindow.Instance.Height = e.NewSize.Height;
                    }
                }
                #endregion
            };
            this.LocationChanged += (sender, e) => {
                MoveConsoleWindow();
            };
            this.AddEventPath <Per1SecondEvent>("刷新倒计时秒表", LogEnum.None,
                                                action: message => {
                #region
                var minerClients = Vm.MinerClients.ToArray();
                if (Vm.CountDown > 0)
                {
                    Vm.CountDown = Vm.CountDown - 1;
                    foreach (var item in minerClients)
                    {
                        item.OnPropertyChanged(nameof(item.LastActivedOnText));
                    }
                    if (RpcRoot.IsOuterNet && Vm.CountDown == 5)
                    {
                        // 外网群控时在矿机列表页数据刷新前5秒通过Ws刷新矿机的算力数据
                        MinerStudioRoot.WsClient.SendAsync(new WsMessage(Guid.NewGuid(), WsMessage.GetSpeed)
                        {
                            Data = Vm.MinerClients.Select(a => a.ClientId).ToList()
                        });
                    }
                }
                else if (Vm.CountDown == 0)
                {
                    MinerStudioRoot.MinerClientsWindowVm.QueryMinerClients(isAuto: true);
                }
                #endregion
            }, location: this.GetType());
            NotiCenterWindow.Bind(this, ownerIsTopMost: true);
            MinerStudioRoot.MinerClientsWindowVm.QueryMinerClients();
        }