Example #1
0
        protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
        {
            base.OnApplyTemplate(e);

            _captionButtons?.Detach();

            _captionButtons = e.NameScope.Get <PleasantCaptionButtons>("PART_CaptionButtons");
            _image          = e.NameScope.Get <Image>("PART_Icon");
            _title          = e.NameScope.Get <TextBlock>("PART_Title");
            _logo           = e.NameScope.Get <Avalonia.Controls.Shapes.Path>("PART_Logo");
            _titleBarMenu   = e.NameScope.Get <ContentPresenter>("PART_TitleBarMenu");
            _dragWindow     = e.NameScope.Get <Border>("PART_DragWindow");
            _contextMenu    = e.NameScope.Get <ContextMenu>("PART_ContextMenu");
            _titlePanel     = e.NameScope.Get <StackPanel>("PART_TitlePanel");

            _closeMenuItem       = e.NameScope.Get <MenuItem>("PART_CloseMenuItem");
            _collapseMenuItem    = e.NameScope.Get <MenuItem>("PART_CollapseMenuItem");
            _expandMenuItem      = e.NameScope.Get <MenuItem>("PART_ExpandMenuItem");
            _reestablishMenuItem = e.NameScope.Get <MenuItem>("PART_ReestablishMenuItem");
            _separator           = e.NameScope.Get <Separator>("PART_Separator");

            _pinButton = e.NameScope.Get <ToggleButton>("PART_PinButton");

            if (VisualRoot is PleasantWindow window)
            {
                _captionButtons.HostWindow = window;

                _closeMenuItem.Click       += (_, e1) => window.Close();
                _reestablishMenuItem.Click += (_, e1) => window.WindowState = WindowState.Normal;
                _expandMenuItem.Click      += (_, e1) => window.WindowState = WindowState.Maximized;
                _collapseMenuItem.Click    += (_, e1) => window.WindowState = WindowState.Minimized;

                _pinButton.Click += (_, e1) => window.Topmost = (_pinButton.IsChecked is null) == false && (bool)_pinButton.IsChecked;

                _dragWindow.PointerPressed += (_, e1) =>
                {
                    if (window.WindowState != WindowState.FullScreen)
                    {
                        _contextMenu.Close();
                        window.PlatformImpl?.BeginMoveDrag(e1);
                    }
                };
                _dragWindow.DoubleTapped += (_, e1) =>
                {
                    if (window.WindowButtons == WindowButtons.CloseAndExpand ||
                        window.WindowButtons == WindowButtons.All)
                    {
                        window.WindowState = window.WindowState == WindowState.Maximized
                            ? WindowState.Normal
                            : WindowState.Maximized;
                    }
                };
                window.PointerReleased += (_, e1) => _contextMenu.Close();
            }

            Attach();
        }
Example #2
0
        protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
        {
            base.OnDetachedFromVisualTree(e);

            _disposables?.Dispose();

            _captionButtons?.Detach();
            _captionButtons = null;
        }