thexamlguy / NotificationFlyout

MIT License
0 stars 0 forks source link

Suggestion: Add a method to "Normalize" a minimized main window. #8

Open Noemata opened 3 years ago

Noemata commented 3 years ago

For those that want to implement a singleton type of main window, having a method to bring up a minimized window would be handy.

Example:

// NotificationFlyoutApplication.cs

        public void NormalizeWindowState<TUIElement>() where TUIElement : Windows.UI.Xaml.UIElement
        {
            var window = new XamlHost<TUIElement>();

            window.WindowState = WindowState.Normal;
            window.Show();
        }

// INotificationFlyoutApplication.cs

        void NormalizeWindowState<TUIElement>() where TUIElement : UIElement;
Noemata commented 3 years ago

Usage example:

        private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            var app = GetApplication();

            if (!App.IsWindowContentShown)
                app.OpenAsWindow<WindowContent>();
            else
                app.NormalizeWindowState<WindowContent>();
        }
Noemata commented 3 years ago

Well, that didn't work at all. Didn't realize all I was doing was opening new windows. DOH! Not sure how this has to be done.

Noemata commented 3 years ago

Sorry, was just being stupid. Here's the correct code.

In NotificationFlyoutApplication.cs add this code:

private static object _window;

...
        public void OpenAsWindow<TUIElement>(bool singleton = false) where TUIElement : Windows.UI.Xaml.UIElement
        {
            if (!singleton)
            {
                var window = new XamlHost<TUIElement>();
                window.Show();
            }
            else
            {
                var window = new XamlHost<TUIElement>();
                _window = window;
                ((XamlHost<TUIElement>)_window).Show();
            }
        }

        public void NormalizeWindowState<TUIElement>() where TUIElement : Windows.UI.Xaml.UIElement
        {
            if (_window != null)
                ((XamlHost<TUIElement>)_window).WindowState = WindowState.Normal;
        }

In INotificationFlyoutApplication.cs change to:

using Windows.UI.Xaml;

namespace NotificationFlyout.Uwp.UI
{
    public interface INotificationFlyoutApplication
    {
        void Exit();

        void OpenAsWindow<TUIElement>(bool singleton = false) where TUIElement : UIElement;
        void NormalizeWindowState<TUIElement>() where TUIElement : UIElement;
    }
}

Usage:

        private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            var app = GetApplication();

            if (!App.IsWindowContentShown)
                app.OpenAsWindow<WindowContent>(true);
            else
                app.NormalizeWindowState<WindowContent>();
        }

This way none of your existing code is affected by the use of the default param.

Noemata commented 3 years ago

Also need this method in NotificationFlyoutApplication.cs :

        public void CloseWindow<TUIElement>() where TUIElement : Windows.UI.Xaml.UIElement
        {
            if (_window != null)
            {
                ((XamlHost<TUIElement>)_window).Close();
                _window = null;
            }
        }

And in INotificationFlyoutApplication.cs:

using Windows.UI.Xaml;

namespace NotificationFlyout.Uwp.UI
{
    public interface INotificationFlyoutApplication
    {
        void Exit();

        void OpenAsWindow<TUIElement>(bool singleton = false) where TUIElement : UIElement;
        void NormalizeWindowState<TUIElement>() where TUIElement : UIElement;
        void CloseWindow<TUIElement>() where TUIElement : UIElement;
    }
}

To facilitate:

        private void MenuFlyoutItem_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            MenuFlyoutItem mfi = e.OriginalSource as MenuFlyoutItem;

            if (mfi != null && mfi.Text == "Alt")
                return;

            var app = GetApplication();
            app.CloseWindow<WindowContent>();
            app.Exit();
        }
Noemata commented 3 years ago

Very good progress! Any word on the above?

thexamlguy commented 3 years ago

I am currently working on a spec for windowing managements from within the flyout at the moment, it is on the radar for the next milestone though. The first approach is to introduce a WindowsOptions class which can be passed over to the ShowAsWindow method including options for managing single instance, windowstate and so on.

I'd like to keep the INotificationFlyoutApplication interface fairly lean given that the NotificationControl is what it is, it should not need to or know how to manage windows. I may be taking an Extensions approach to this one giving the ability for a developer to add their own extension methods without me overloading the INotificationFlyoutApplication with lots of different windowing methods.

Noemata commented 3 years ago

I mostly agree. That said, given the call(s) that are already present, these do align. I respect your forward vision, so you'll have to do what's best for where you're headed. I do think OpenAsWindow would be complemented by the other additions unless the plan is to hide that call also.