示例#1
0
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (IOSource != null)
            {
                IOSource.Dispose();
                IOSource = null;
            }
        }
示例#2
0
        public TerminalWidget(Func <bool> focusProvider, bool isMonitorWindow)
        {
            this.isMonitorWindow = isMonitorWindow;
            var shortcutDictionary = new Dictionary <KeyEventArgs, Action>
            {
                { CreateKey(Key.C, ModifierKeys.Shift | ModifierKeys.Control), CopyMarkedField },
                { CreateKey(Key.V, ModifierKeys.Shift | ModifierKeys.Control), PasteMarkedField },
                { CreateKey(Key.Insert, ModifierKeys.Shift), PasteMarkedField },
                { CreateKey(Key.PageUp, ModifierKeys.Shift), () => terminal.PageUp() },
                { CreateKey(Key.PageDown, ModifierKeys.Shift), () => terminal.PageDown() },
                { CreateKey(Key.Plus, ModifierKeys.Shift | ModifierKeys.Control), FontSizeUp },
                { CreateKey(Key.Minus, ModifierKeys.Control), FontSizeDown },
                { CreateKey(Key.NumPadAdd, ModifierKeys.Control), FontSizeUp },
                { CreateKey(Key.NumPadSubtract, ModifierKeys.Control), FontSizeDown },
                { CreateKey(Key.K0, ModifierKeys.Control), SetDefaultFontSize },
                { CreateKey(Key.NumPad0, ModifierKeys.Control), SetDefaultFontSize }
            };

            modifyLineEndings     = ConfigurationManager.Instance.Get("termsharp", "append-CR-to-LF", true);
            terminal              = new Terminal(focusProvider);
            IOSource              = new TerminalIOSource(terminal);
            IOSource.BeforeWrite += b =>
            {
                // we do not check if previous byte was '\r', because it should not cause any problem to
                // send it twice
                if (modifyLineEndings && b == '\n')
                {
                    IOSource.Write((byte)'\r');
                }
            };

            terminal.InnerMargin    = new WidgetSpacing(5, 5, 5, 5);
            terminal.Cursor.Enabled = true;
            terminal.ContextMenu    = CreatePopupMenu();

            // We set the default font as a fall-back option.
            terminal.CurrentFont = Xwt.Drawing.Font.SystemMonospaceFont;
#if !PLATFORM_OSX
            // Here we try to load the robot font; unfortunately it doesn't work on OSX. Moreover, on some versions of
            // OSX it passes with no error (and no effect), on the others - it hangs. That's why we try to set the font and then
            // we check if we succeeded.
            var fontFile = typeof(TerminalWidget).Assembly.FromResourceToTemporaryFile("RobotoMono-Regular.ttf");
            Xwt.Drawing.Font.RegisterFontFromFile(fontFile);
#endif
            var fontFace = ConfigurationManager.Instance.Get("termsharp", "font-face", "Roboto Mono");
            defaultFontSize = ConfigurationManager.Instance.Get("termsharp", "font-size", (int)PredefinedFontSize, x => x >= MinFontSize);
            var font = Xwt.Drawing.Font.FromName(fontFace);
            if (!font.Family.Contains(fontFace))
            {
                Logger.Log(LogLevel.Warning, "The font '{0}' defined in the config file cannot be loaded.", fontFace);
                font = terminal.CurrentFont;
            }
            terminal.CurrentFont = font.WithSize(defaultFontSize);

            terminal.AppendRow(isMonitorWindow ? new LogoRow() : new MonospaceTextRow(""));

            var encoder = new TermSharp.Vt100.Encoder(x =>
            {
                terminal.ClearSelection();
                terminal.MoveScrollbarToEnd();
                IOSource.HandleInput(x);
            });

            terminal.KeyPressed += (s, a) =>
            {
                a.Handled = true;

                var modifiers = a.Modifiers;
                if (!Misc.IsOnOsX)
                {
                    modifiers &= ~(ModifierKeys.Command);
                }

                foreach (var entry in shortcutDictionary)
                {
                    if (modifiers == entry.Key.Modifiers)
                    {
                        if (a.Key == entry.Key.Key)
                        {
                            entry.Value();
                            return;
                        }
                    }
                }
                encoder.Feed(a.Key, modifiers);
            };
            Content = terminal;
        }