aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/MachineStudio.Dispensers/Contracts/IMainView.cs
blob: ea0744c84e844ddfe98918bf60e63859e774e611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.SharedUI;

namespace Tango.MachineStudio.Dispensers.Contracts
{
    public enum DispensersNavigationView
    {
        DispensersView,
        DispenserView,
    }

    public interface IMainView : IView
    {
        void NavigateTo(DispensersNavigationView view);
    }
}
.s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Tango.Logging
{
    /// <summary>
    /// Represents a debugging (write-only) console emulator.
    /// </summary>
    public partial class ConsoleWindow : Window
    {
        private bool _closing;
        private SolidColorBrush _currentBrush;
        private ConsoleColor _current_color;

        /// <summary>
        /// Initializes a new instance of the <see cref="ConsoleWindow"/> class.
        /// </summary>
        public ConsoleWindow()
        {
            InitializeComponent();

            txtLog.Document.LineHeight = 12;
            txtLog.Document.PageWidth = 4000;
            _currentBrush = Brushes.Gainsboro;

            AppendLog("--------------------- Twine Console Emulator ---------------------" + Environment.NewLine + Environment.NewLine);

            Closing += ConsoleWindow_Closing;
        }

        /// <summary>
        /// Handles the Closing event of the ConsoleWindow control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void ConsoleWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _closing = true;
        }

        /// <summary>
        /// Appends the specified .
        /// </summary>
        /// <param name="obj">The object.</param>
        public void WriteLine(object obj)
        {
            AppendLog(obj.ToString() + Environment.NewLine);
        }

        /// <summary>
        /// Writes the specified object.
        /// </summary>
        /// <param name="obj">The object.</param>
        public void Write(object obj)
        {
            AppendLog(obj.ToString());
        }

        /// <summary>
        /// Sets the console foreground color.
        /// </summary>
        /// <param name="color">The color.</param>
        public void SetColor(ConsoleColor color)
        {
            if (_current_color != color)
            {
                _current_color = color;
                InvokeUI(() => _currentBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(color.ToString())));
            }
        }

        private void AppendLog(String log)
        {
            InvokeUI(() =>
            {
                TextRange tr = new TextRange(txtLog.Document.ContentEnd, txtLog.Document.ContentEnd);
                tr.Text = log;
                tr.ApplyPropertyValue(TextElement.ForegroundProperty, _currentBrush);
                Rect r = txtLog.Document.ContentEnd.GetCharacterRect(LogicalDirection.Forward);
                txtLog.ScrollToVerticalOffset(r.Y);
            });
        }

        /// <summary>
        /// Opens the console.
        /// </summary>
        public void Open()
        {
            Show();
        }

        /// <summary>
        /// Closes the console.
        /// </summary>
        public new void Close()
        {
            Close();
        }

        /// <summary>
        /// Waits until user closes the window.
        /// </summary>
        /// <returns></returns>
        public async Task WaitForUserClose()
        {
            WriteLine("");
            WriteLine("Close this window to exit...");

            await Task.Factory.StartNew(() =>
            {
                while (!_closing)
                {
                    Thread.Sleep(10);
                }
            });
        }

        /// <summary>
        /// Invokes the UI.
        /// </summary>
        /// <param name="action">The action.</param>
        private void InvokeUI(Action action)
        {
            Dispatcher.Invoke(action);
            DoEvents();
        }

        /// <summary>
        /// Forces the next rendering thread frame.
        /// </summary>
        private void DoEvents()
        {
            if (Application.Current == null) //Using this to enable processing outside a WPF application.
            {
                new Application { ShutdownMode = ShutdownMode.OnExplicitShutdown };
            }

            Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));
        }
    }
}