Ejemplo n.º 1
0
        internal static void ShowTipWindow()
        {
            var tipLoader = new TipLoader();

            if (!tipLoader.LoadTips().Result)
            {
                return;
            }

            var oldMainWindow = NSApplication.SharedApplication.KeyWindow
                                ?? NSApplication.SharedApplication.MainWindow
                                ?? GtkQuartz.GetWindow(MessageService.RootWindow);

            using (var tipWindow = InflateFromNibResource <TipWindow> ("__xammac_content_TipWindow.nib")) {
                tipWindow.ParentWindow = oldMainWindow;
                tipWindow.MakeKeyAndOrderFront(tipWindow);
                tipWindow.ShowAtStartup = StartupHandler.ShowAtStartup;

                FilePath sharedAssetsPath = Mono.Addins.AddinManager.CurrentAddin.GetFilePath("content");

                tipWindow.HandleUrlOpen = s => {
                    if (s.IsFile)
                    {
                        return(false);
                    }
                    DesktopService.ShowUrl(s.ToString());
                    return(true);
                };

                void nextMessage(object sender, EventArgs e)
                {
                    string html;
                    string basePath = sharedAssetsPath;

                    try {
                        var tip = tipLoader.GetNextTip();
                        basePath = System.IO.Path.GetDirectoryName(tip.Filename);
                        html     = new TipView(tip, sharedAssetsPath).GenerateString();
                    } catch (Exception ex) {
                        html = $"<h1>Error</h1><p>{ex.ToString ()}</p>";
                    }
                    tipWindow.LoadHtml(html, basePath);
                }

                tipWindow.NextTipClicked += nextMessage;
                nextMessage(null, null);

                tipWindow.Run();

                StartupHandler.ShowAtStartup = tipWindow.ShowAtStartup;
            }

            GtkQuartz.FocusWindow(GtkQuartz.GetGtkWindow(oldMainWindow) ?? MessageService.RootWindow);
        }
Ejemplo n.º 2
0
        public bool Run(SelectFileDialogData data)
        {
            NSSavePanel panel = null;

            try {
                bool directoryMode = data.Action != Gtk.FileChooserAction.Open;

                if (data.Action == Gtk.FileChooserAction.Save)
                {
                    panel = new NSSavePanel();
                }
                else
                {
                    panel = new NSOpenPanel()
                    {
                        CanChooseDirectories = directoryMode,
                        CanChooseFiles       = !directoryMode,
                    };
                }

                SetCommonPanelProperties(data, panel);

                if (!directoryMode)
                {
                    var popup = CreateFileFilterPopup(data, panel);
                    if (popup != null)
                    {
                        panel.AccessoryView = popup;
                    }
                }

                var action = RunPanel(data, panel);

                if (action)
                {
                    data.SelectedFiles = GetSelectedFiles(panel);
                    GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                }
                else
                {
                    GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                }
                return(action);
            } finally {
                if (panel != null)
                {
                    panel.Dispose();
                }
            }
        }
        public bool Run(SelectFileDialogData data)
        {
            using (var panel = CreatePanel(data, out var saveState)) {
                if (panel.RunModal() == 0)
                {
                    GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                    return(false);
                }

                data.SelectedFiles = GetSelectedFiles(panel);
                GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                return(true);
            }
        }
        public bool Run(SelectFileDialogData data)
        {
            NSSavePanel panel = null;

            try {
                if (data.Action == FileChooserAction.Save)
                {
                    panel = new NSSavePanel();
                }
                else
                {
                    panel = new NSOpenPanel {
                        CanChooseDirectories = (data.Action & FileChooserAction.FolderFlags) != 0,
                        CanChooseFiles       = (data.Action & FileChooserAction.FileFlags) != 0,
                        CanCreateDirectories = (data.Action & FileChooserAction.CreateFolder) != 0,
                        ResolvesAliases      = false,
                    };
                }

                SetCommonPanelProperties(data, panel);

                if ((data.Action & FileChooserAction.FileFlags) != 0)
                {
                    var popup = CreateFileFilterPopup(data, panel);
                    if (popup != null)
                    {
                        panel.AccessoryView = popup;
                    }
                }

                if (panel.RunModal() == 0)
                {
                    GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                    return(false);
                }

                data.SelectedFiles = GetSelectedFiles(panel);
                GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                return(true);
            } finally {
                if (panel != null)
                {
                    panel.Dispose();
                }
            }
        }
Ejemplo n.º 5
0
        public bool Run(ExceptionDialogData data)
        {
            using (var alert = new NSAlert()) {
                alert.AlertStyle = NSAlertStyle.Critical;

                var    pix = ImageService.GetPixbuf(Gtk.Stock.DialogError, Gtk.IconSize.Dialog);
                byte[] buf = pix.SaveToBuffer("tiff");
                alert.Icon = new NSImage(NSData.FromArray(buf));

                alert.MessageText     = data.Title ?? "Some Message";
                alert.InformativeText = data.Message ?? "Some Info";

                if (data.Exception != null)
                {
                    var text = new NSTextView(new RectangleF(0, 0, float.MaxValue, float.MaxValue));
                    text.HorizontallyResizable             = true;
                    text.TextContainer.ContainerSize       = new SizeF(float.MaxValue, float.MaxValue);
                    text.TextContainer.WidthTracksTextView = false;
                    text.InsertText(new NSString(data.Exception.ToString()));
                    text.Editable = false;

                    var scrollView = new NSScrollView(new RectangleF(0, 0, 450, 150))
                    {
                        HasHorizontalScroller = true,
                        DocumentView          = text,
                    };
                    ;
                    alert.AccessoryView = scrollView;
                }

                // Hack up a slightly wider than normal alert dialog. I don't know how to do this in a nicer way
                // as the min size constraints are apparently ignored.
                var frame = ((NSPanel)alert.Window).Frame;
                ((NSPanel)alert.Window).SetFrame(new RectangleF(frame.X, frame.Y, Math.Max(frame.Width, 600), frame.Height), true);

                int result = alert.RunModal() - (int)NSAlertButtonReturn.First;

                GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
            }

            return(true);
        }
Ejemplo n.º 6
0
        public bool Run(AddFileDialogData data)
        {
            using (var panel = CreatePanel(data, out NSPopUpButton popup)) {
                if (panel.RunModal() == 0)
                {
                    GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                    return(false);
                }

                data.SelectedFiles = MacSelectFileDialogHandler.GetSelectedFiles(panel);

                var idx = popup.IndexOfSelectedItem - 2;
                if (idx >= 0)
                {
                    data.OverrideAction = data.BuildActions[idx];
                }

                GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                return(true);
            }
        }
Ejemplo n.º 7
0
        public bool Run(AlertDialogData data)
        {
            using (var alert = new NSAlert()) {
                alert.Window.Title = data.Title ?? BrandingService.ApplicationName;
                IdeTheme.ApplyTheme(alert.Window);

                bool stockIcon;
                if (data.Message.Icon == MonoDevelop.Ide.Gui.Stock.Error || data.Message.Icon == Gtk.Stock.DialogError)
                {
                    alert.AlertStyle = NSAlertStyle.Critical;
                    stockIcon        = true;
                }
                else if (data.Message.Icon == MonoDevelop.Ide.Gui.Stock.Warning || data.Message.Icon == Gtk.Stock.DialogWarning)
                {
                    alert.AlertStyle = NSAlertStyle.Critical;
                    stockIcon        = true;
                }
                else
                {
                    alert.AlertStyle = NSAlertStyle.Informational;
                    stockIcon        = data.Message.Icon == MonoDevelop.Ide.Gui.Stock.Information;
                }

                if (!stockIcon && !string.IsNullOrEmpty(data.Message.Icon))
                {
                    var img = ImageService.GetIcon(data.Message.Icon, Gtk.IconSize.Dialog);
                    // HACK: VK The icon is not rendered in dark style correctly
                    //       Use light variant and reder it here
                    //       as long as NSAppearance.NameVibrantDark is broken
                    if (IdeTheme.UserInterfaceSkin == Skin.Dark)
                    {
                        alert.Icon = img.WithStyles("-dark").ToBitmap(GtkWorkarounds.GetScaleFactor()).ToNSImage();
                    }
                    else
                    {
                        alert.Icon = img.ToNSImage();
                    }
                }
                else
                {
                    //for some reason the NSAlert doesn't pick up the app icon by default
                    alert.Icon = NSApplication.SharedApplication.ApplicationIconImage;
                }

                alert.MessageText     = data.Message.Text;
                alert.InformativeText = data.Message.SecondaryText ?? "";

                var buttons = data.Buttons.Reverse().ToList();

                for (int i = 0; i < buttons.Count - 1; i++)
                {
                    if (i == data.Message.DefaultButton)
                    {
                        var next = buttons[i];
                        for (int j = buttons.Count - 1; j >= i; j--)
                        {
                            var tmp = buttons[j];
                            buttons[j] = next;
                            next       = tmp;
                        }
                        break;
                    }
                }

                var wrappers = new List <AlertButtonWrapper> (buttons.Count);
                foreach (var button in buttons)
                {
                    var label = button.Label;
                    if (button.IsStockButton)
                    {
                        label = Gtk.Stock.Lookup(label).Label;
                    }
                    label = label.Replace("_", "");

                    //this message seems to be a standard Mac message since alert handles it specially
                    if (button == AlertButton.CloseWithoutSave)
                    {
                        label = GettextCatalog.GetString("Don't Save");
                    }

                    var nsbutton      = alert.AddButton(label);
                    var wrapperButton = new AlertButtonWrapper(nsbutton, data.Message, button, alert);
                    wrappers.Add(wrapperButton);
                    nsbutton.Target = wrapperButton;
                    nsbutton.Action = new ObjCRuntime.Selector("buttonActivatedAction");
                }


                NSButton[] optionButtons = null;
                if (data.Options.Count > 0)
                {
                    var box = new MDBox(LayoutDirection.Vertical, 2, 2);
                    optionButtons = new NSButton[data.Options.Count];

                    for (int i = data.Options.Count - 1; i >= 0; i--)
                    {
                        var option = data.Options[i];
                        var button = new NSButton {
                            Title = option.Text,
                            Tag   = i,
                            State = option.Value? NSCellStateValue.On : NSCellStateValue.Off,
                        };
                        button.SetButtonType(NSButtonType.Switch);
                        optionButtons[i] = button;
                        box.Add(new MDAlignment(button, true)
                        {
                            XAlign = LayoutAlign.Begin
                        });
                    }

                    box.Layout();
                    alert.AccessoryView = box.View;
                }

                NSButton applyToAllCheck = null;
                if (data.Message.AllowApplyToAll)
                {
                    alert.ShowsSuppressionButton = true;
                    applyToAllCheck       = alert.SuppressionButton;
                    applyToAllCheck.Title = GettextCatalog.GetString("Apply to all");
                }

                // Hack up a slightly wider than normal alert dialog. I don't know how to do this in a nicer way
                // as the min size constraints are apparently ignored.
                var frame = alert.Window.Frame;
                alert.Window.SetFrame(new CGRect(frame.X, frame.Y, NMath.Max(frame.Width, 600), frame.Height), true);
                alert.Layout();

                bool completed = false;
                if (data.Message.CancellationToken.CanBeCanceled)
                {
                    data.Message.CancellationToken.Register(delegate {
                        alert.InvokeOnMainThread(() => {
                            if (!completed)
                            {
                                NSApplication.SharedApplication.AbortModal();
                            }
                        });
                    });
                }

                if (!data.Message.CancellationToken.IsCancellationRequested)
                {
                    var result = (int)alert.RunModal() - (long)(int)NSAlertButtonReturn.First;

                    completed = true;
                    if (result >= 0 && result < buttons.Count)
                    {
                        data.ResultButton = buttons [(int)result];
                    }
                    else
                    {
                        data.ResultButton = null;
                    }
                }

                if (data.ResultButton == null || data.Message.CancellationToken.IsCancellationRequested)
                {
                    data.SetResultToCancelled();
                }

                if (optionButtons != null)
                {
                    foreach (var button in optionButtons)
                    {
                        var option = data.Options[(int)button.Tag];
                        data.Message.SetOptionValue(option.Id, button.State != 0);
                    }
                }

                if (applyToAllCheck != null && applyToAllCheck.State != 0)
                {
                    data.ApplyToAll = true;
                }



                GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
            }

            return(true);
        }
Ejemplo n.º 8
0
        public bool Run(OpenFileDialogData data)
        {
            NSSavePanel panel = null;

            try {
                if (data.Action == FileChooserAction.Save)
                {
                    panel = new NSSavePanel();
                }
                else
                {
                    panel = new NSOpenPanel {
                        CanChooseDirectories = (data.Action & FileChooserAction.FolderFlags) != 0,
                        CanChooseFiles       = (data.Action & FileChooserAction.FileFlags) != 0,
                    };
                }
                bool pathAlreadySet = false;
                panel.DidChangeToDirectory += (sender, e) => {
                    var directoryPath = e.NewDirectoryUrl?.AbsoluteString;
                    if (string.IsNullOrEmpty(directoryPath))
                    {
                        return;
                    }
                    var selectedPath = data.OnDirectoryChanged(this, directoryPath);
                    if (selectedPath.IsNull)
                    {
                        return;
                    }
                    data.SelectedFiles = new FilePath [] { selectedPath };
                    pathAlreadySet     = true;

                    // We need to call Cancel on 1ms delay so it's executed after DidChangeToDirectory event handler is finished
                    // this is needed because it's possible that DidChangeToDirectory event is executed while dialog is opening
                    // in that case calling .Cancel() leaves dialog in weird state...
                    // Fun fact: DidChangeToDirectory event is called from Open on 10.12 but not on 10.13
                    System.Threading.Tasks.Task.Delay(1).ContinueWith(delegate { panel.Cancel(panel); }, Runtime.MainTaskScheduler);
                };
                MacSelectFileDialogHandler.SetCommonPanelProperties(data, panel);

                SelectEncodingPopUpButton encodingSelector = null;
                NSPopUpButton             viewerSelector   = null;
                NSButton closeSolutionButton = null;

                var box = new MDBox(LayoutDirection.Vertical, 2, 2);

                List <FileViewer> currentViewers = null;
                var labels   = new List <MDAlignment> ();
                var controls = new List <MDAlignment> ();

                if ((data.Action & FileChooserAction.FileFlags) != 0)
                {
                    var filterPopup = MacSelectFileDialogHandler.CreateFileFilterPopup(data, panel);

                    if (filterPopup != null)
                    {
                        var filterLabel = new MDAlignment(new MDLabel(GettextCatalog.GetString("Show Files:"))
                        {
                            Alignment = NSTextAlignment.Right
                        }, true);
                        var filterPopupAlignment = new MDAlignment(filterPopup, true)
                        {
                            MinWidth = 200
                        };
                        var filterBox = new MDBox(LayoutDirection.Horizontal, 2, 0)
                        {
                            { filterLabel },
                            { filterPopupAlignment }
                        };
                        labels.Add(filterLabel);
                        controls.Add(filterPopupAlignment);
                        box.Add(filterBox);
                    }

                    if (data.ShowEncodingSelector)
                    {
                        encodingSelector = new SelectEncodingPopUpButton(data.Action != FileChooserAction.Save);
                        encodingSelector.SelectedEncodingId = data.Encoding != null ? data.Encoding.CodePage : 0;

                        var encodingLabel = new MDAlignment(new MDLabel(GettextCatalog.GetString("Encoding:"))
                        {
                            Alignment = NSTextAlignment.Right
                        }, true);
                        var encodingSelectorAlignment = new MDAlignment(encodingSelector, true)
                        {
                            MinWidth = 200
                        };
                        var encodingBox = new MDBox(LayoutDirection.Horizontal, 2, 0)
                        {
                            { encodingLabel },
                            { encodingSelectorAlignment }
                        };
                        labels.Add(encodingLabel);
                        controls.Add(encodingSelectorAlignment);
                        box.Add(encodingBox);
                    }

                    if (data.ShowViewerSelector && panel is NSOpenPanel)
                    {
                        currentViewers = new List <FileViewer> ();
                        viewerSelector = new NSPopUpButton {
                            Enabled = false,
                        };

                        if (encodingSelector != null || IdeApp.Workspace.IsOpen)
                        {
                            viewerSelector.Activated += delegate {
                                var  idx = viewerSelector.IndexOfSelectedItem;
                                bool workbenchViewerSelected = idx == 0 && currentViewers [0] == null;
                                if (encodingSelector != null)
                                {
                                    encodingSelector.Enabled = !workbenchViewerSelected;
                                }
                                if (closeSolutionButton != null)
                                {
                                    if (closeSolutionButton.Enabled != workbenchViewerSelected)
                                    {
                                        closeSolutionButton.Enabled = workbenchViewerSelected;
                                        closeSolutionButton.State   = workbenchViewerSelected ? NSCellStateValue.On : NSCellStateValue.Off;
                                    }
                                }
                            };
                        }

                        if (IdeApp.Workspace.IsOpen)
                        {
                            closeSolutionButton = new NSButton {
                                Title   = GettextCatalog.GetString("Close current workspace"),
                                Enabled = false,
                                State   = NSCellStateValue.Off,
                            };

                            closeSolutionButton.SetButtonType(NSButtonType.Switch);
                            closeSolutionButton.SizeToFit();

                            var closeSolutionLabelBox        = new MDAlignment(new MDLabel(string.Empty), true);
                            var closeSolutionButtonAlignment = new MDAlignment(closeSolutionButton, true);
                            var closeSolutionBox             = new MDBox(LayoutDirection.Horizontal, 2, 0)
                            {
                                { closeSolutionLabelBox },
                                { closeSolutionButtonAlignment }
                            };

                            labels.Add(closeSolutionLabelBox);
                            controls.Add(closeSolutionButtonAlignment);
                            box.Add(closeSolutionBox);
                        }

                        var viewSelLabel = new MDAlignment(new MDLabel(GettextCatalog.GetString("Open With:"))
                        {
                            Alignment = NSTextAlignment.Right
                        }, true);
                        var viewSelectorAlignemnt = new MDAlignment(viewerSelector, true)
                        {
                            MinWidth = 200
                        };
                        var viewSelBox = new MDBox(LayoutDirection.Horizontal, 2, 0)
                        {
                            { viewSelLabel },
                            { viewSelectorAlignemnt }
                        };

                        labels.Add(viewSelLabel);
                        controls.Add(viewSelectorAlignemnt);
                        box.Add(viewSelBox);
                    }
                }

                if (labels.Count > 0)
                {
                    float w = labels.Max(l => l.MinWidth);
                    foreach (var l in labels)
                    {
                        l.MinWidth = w;
                        l.XAlign   = LayoutAlign.Begin;
                    }
                }

                if (controls.Count > 0)
                {
                    float w = controls.Max(c => c.MinWidth);
                    foreach (var c in controls)
                    {
                        c.MinWidth = w;
                        c.XAlign   = LayoutAlign.Begin;
                    }
                }

                if (box.Count > 0)
                {
                    box.Layout();
                    panel.AccessoryView = box.View;
                }

                panel.SelectionDidChange += delegate {
                    var  selection         = MacSelectFileDialogHandler.GetSelectedFiles(panel);
                    bool slnViewerSelected = false;
                    if (viewerSelector != null)
                    {
                        slnViewerSelected = FillViewers(currentViewers, viewerSelector, closeSolutionButton, selection);
                        if (closeSolutionButton != null)
                        {
                            closeSolutionButton.Enabled = slnViewerSelected;
                            closeSolutionButton.State   = slnViewerSelected ? NSCellStateValue.On : NSCellStateValue.Off;
                        }
                    }
                    if (encodingSelector != null)
                    {
                        encodingSelector.Enabled = !slnViewerSelected;
                    }
                };

                if (panel.RunModal() == 0 && !pathAlreadySet)
                {
                    GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                    return(false);
                }
                if (!pathAlreadySet)
                {
                    data.SelectedFiles = MacSelectFileDialogHandler.GetSelectedFiles(panel);
                }

                if (encodingSelector != null)
                {
                    data.Encoding = encodingSelector.SelectedEncodingId > 0 ? Encoding.GetEncoding(encodingSelector.SelectedEncodingId) : null;
                }

                if (viewerSelector != null)
                {
                    if (closeSolutionButton != null)
                    {
                        data.CloseCurrentWorkspace = closeSolutionButton.State != NSCellStateValue.Off;
                    }
                    data.SelectedViewer = viewerSelector.IndexOfSelectedItem >= 0 ?
                                          currentViewers[(int)viewerSelector.IndexOfSelectedItem] : null;
                }

                GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
            } catch (Exception ex) {
                LoggingService.LogInternalError("Error in Open File dialog", ex);
            } finally {
                if (panel != null)
                {
                    panel.Dispose();
                }
            }
            return(true);
        }
Ejemplo n.º 9
0
        public bool Run(AlertDialogData data)
        {
            using (var alert = new NSAlert()) {
                if (data.Message.Icon == MonoDevelop.Ide.Gui.Stock.Information)
                {
                    alert.AlertStyle = NSAlertStyle.Critical;
                }
                else if (data.Message.Icon == MonoDevelop.Ide.Gui.Stock.Warning)
                {
                    alert.AlertStyle = NSAlertStyle.Warning;
                }
                else if (data.Message.Icon == MonoDevelop.Ide.Gui.Stock.Information)
                {
                    alert.AlertStyle = NSAlertStyle.Informational;
                }

                //FIXME: use correct size so we don't get horrible scaling?
                if (!string.IsNullOrEmpty(data.Message.Icon))
                {
                    var    pix = ImageService.GetPixbuf(data.Message.Icon, Gtk.IconSize.Dialog);
                    byte[] buf = pix.SaveToBuffer("tiff");
                    unsafe
                    {
                        fixed(byte *b = buf)
                        {
                            alert.Icon = new NSImage(NSData.FromBytes((IntPtr)b, (uint)buf.Length));
                        }
                    }
                }

                alert.MessageText     = data.Message.Text;
                alert.InformativeText = data.Message.SecondaryText ?? "";

                var buttons = data.Buttons.Reverse().ToList();

                for (int i = 0; i < buttons.Count - 1; i++)
                {
                    if (i == data.Message.DefaultButton)
                    {
                        var next = buttons[i];
                        for (int j = buttons.Count - 1; j >= i; j--)
                        {
                            var tmp = buttons[j];
                            buttons[j] = next;
                            next       = tmp;
                        }
                        break;
                    }
                }

                foreach (var button in buttons)
                {
                    var label = button.Label;
                    if (button.IsStockButton)
                    {
                        label = Gtk.Stock.Lookup(label).Label;
                    }
                    label = label.Replace("_", "");

                    //this message seems to be a standard Mac message since alert handles it specially
                    if (button == AlertButton.CloseWithoutSave)
                    {
                        label = GettextCatalog.GetString("Don't Save");
                    }

                    alert.AddButton(label);
                }


                NSButton[] optionButtons = null;
                if (data.Options.Count > 0)
                {
                    var box = new MDBox(LayoutDirection.Vertical, 2, 2);
                    optionButtons = new NSButton[data.Options.Count];

                    for (int i = data.Options.Count - 1; i >= 0; i--)
                    {
                        var option = data.Options[i];
                        var button = new NSButton()
                        {
                            Title = option.Text,
                            Tag   = i,
                            State = option.Value? NSCellStateValue.On : NSCellStateValue.Off,
                        };
                        button.SetButtonType(NSButtonType.Switch);
                        optionButtons[i] = button;
                        box.Add(new MDAlignment(button, true)
                        {
                            XAlign = LayoutAlign.Begin
                        });
                    }

                    box.Layout();
                    alert.AccessoryView = box.View;
                }

                NSButton applyToAllCheck = null;
                if (data.Message.AllowApplyToAll)
                {
                    alert.ShowsSuppressionButton = true;
                    applyToAllCheck       = alert.SuppressionButton;
                    applyToAllCheck.Title = GettextCatalog.GetString("Apply to all");
                }

                alert.Layout();

                int result = alert.RunModal() - (int)NSAlertButtonReturn.First;

                data.ResultButton = buttons [result];

                if (optionButtons != null)
                {
                    foreach (var button in optionButtons)
                    {
                        var option = data.Options[button.Tag];
                        data.Message.SetOptionValue(option.Id, button.State != 0);
                    }
                }

                if (applyToAllCheck != null && applyToAllCheck.State != 0)
                {
                    data.ApplyToAll = true;
                }

                GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
            }

            return(true);
        }
Ejemplo n.º 10
0
        public bool Run(AlertDialogData data)
        {
            using (var alert = new NSAlert()) {
                alert.Window.Title = data.Title ?? BrandingService.ApplicationName;

                if (data.Message.Icon == MonoDevelop.Ide.Gui.Stock.Information)
                {
                    alert.AlertStyle = NSAlertStyle.Critical;
                }
                else if (data.Message.Icon == MonoDevelop.Ide.Gui.Stock.Warning)
                {
                    alert.AlertStyle = NSAlertStyle.Warning;
                }
                else                     //if (data.Message.Icon == MonoDevelop.Ide.Gui.Stock.Information) {
                {
                    alert.AlertStyle = NSAlertStyle.Informational;
                }

                //FIXME: use correct size so we don't get horrible scaling?
                if (!string.IsNullOrEmpty(data.Message.Icon))
                {
                    var    pix = ImageService.GetPixbuf(data.Message.Icon, Gtk.IconSize.Dialog);
                    byte[] buf = pix.SaveToBuffer("tiff");
                    unsafe
                    {
                        fixed(byte *b = buf)
                        {
                            alert.Icon = new NSImage(NSData.FromBytes((IntPtr)b, (uint)buf.Length));
                        }
                    }
                }
                else
                {
                    //for some reason the NSAlert doesn't pick up the app icon by default
                    alert.Icon = NSApplication.SharedApplication.ApplicationIconImage;
                }

                alert.MessageText     = data.Message.Text;
                alert.InformativeText = data.Message.SecondaryText ?? "";

                var buttons = data.Buttons.Reverse().ToList();

                for (int i = 0; i < buttons.Count - 1; i++)
                {
                    if (i == data.Message.DefaultButton)
                    {
                        var next = buttons[i];
                        for (int j = buttons.Count - 1; j >= i; j--)
                        {
                            var tmp = buttons[j];
                            buttons[j] = next;
                            next       = tmp;
                        }
                        break;
                    }
                }

                foreach (var button in buttons)
                {
                    var label = button.Label;
                    if (button.IsStockButton)
                    {
                        label = Gtk.Stock.Lookup(label).Label;
                    }
                    label = label.Replace("_", "");

                    //this message seems to be a standard Mac message since alert handles it specially
                    if (button == AlertButton.CloseWithoutSave)
                    {
                        label = GettextCatalog.GetString("Don't Save");
                    }

                    alert.AddButton(label);
                }


                NSButton[] optionButtons = null;
                if (data.Options.Count > 0)
                {
                    var box = new MDBox(LayoutDirection.Vertical, 2, 2);
                    optionButtons = new NSButton[data.Options.Count];

                    for (int i = data.Options.Count - 1; i >= 0; i--)
                    {
                        var option = data.Options[i];
                        var button = new NSButton()
                        {
                            Title = option.Text,
                            Tag   = i,
                            State = option.Value? NSCellStateValue.On : NSCellStateValue.Off,
                        };
                        button.SetButtonType(NSButtonType.Switch);
                        optionButtons[i] = button;
                        box.Add(new MDAlignment(button, true)
                        {
                            XAlign = LayoutAlign.Begin
                        });
                    }

                    box.Layout();
                    alert.AccessoryView = box.View;
                }

                NSButton applyToAllCheck = null;
                if (data.Message.AllowApplyToAll)
                {
                    alert.ShowsSuppressionButton = true;
                    applyToAllCheck       = alert.SuppressionButton;
                    applyToAllCheck.Title = GettextCatalog.GetString("Apply to all");
                }

                // Hack up a slightly wider than normal alert dialog. I don't know how to do this in a nicer way
                // as the min size constraints are apparently ignored.
                var frame = ((NSPanel)alert.Window).Frame;
                ((NSPanel)alert.Window).SetFrame(new RectangleF(frame.X, frame.Y, Math.Max(frame.Width, 600), frame.Height), true);
                alert.Layout();

                bool completed = false;
                if (data.Message.CancellationToken.CanBeCanceled)
                {
                    data.Message.CancellationToken.Register(delegate {
                        alert.InvokeOnMainThread(() => {
                            if (!completed)
                            {
                                NSApplication.SharedApplication.AbortModal();
                            }
                        });
                    });
                }

                if (!data.Message.CancellationToken.IsCancellationRequested)
                {
                    int result = alert.RunModal() - (int)NSAlertButtonReturn.First;
                    completed = true;
                    if (result >= 0 && result < buttons.Count)
                    {
                        data.ResultButton = buttons [result];
                    }
                    else
                    {
                        data.ResultButton = null;
                    }
                }

                if (data.ResultButton == null || data.Message.CancellationToken.IsCancellationRequested)
                {
                    data.SetResultToCancelled();
                }

                if (optionButtons != null)
                {
                    foreach (var button in optionButtons)
                    {
                        var option = data.Options[button.Tag];
                        data.Message.SetOptionValue(option.Id, button.State != 0);
                    }
                }

                if (applyToAllCheck != null && applyToAllCheck.State != 0)
                {
                    data.ApplyToAll = true;
                }

                GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
            }

            return(true);
        }
        public bool Run(AlertDialogData data)
        {
            using (var alert = new NSAlert()) {
                alert.Window.Title = data.Title ?? BrandingService.ApplicationName;
                IdeTheme.ApplyTheme(alert.Window);

                bool stockIcon;
                if (data.Message.Icon == MonoDevelop.Ide.Gui.Stock.Error || data.Message.Icon == Gtk.Stock.DialogError)
                {
                    alert.AlertStyle = NSAlertStyle.Critical;
                    stockIcon        = true;
                }
                else if (data.Message.Icon == MonoDevelop.Ide.Gui.Stock.Warning || data.Message.Icon == Gtk.Stock.DialogWarning)
                {
                    alert.AlertStyle = NSAlertStyle.Critical;
                    stockIcon        = true;
                }
                else
                {
                    alert.AlertStyle = NSAlertStyle.Informational;
                    stockIcon        = data.Message.Icon == MonoDevelop.Ide.Gui.Stock.Information;
                }

                if (!stockIcon && !string.IsNullOrEmpty(data.Message.Icon))
                {
                    var img = ImageService.GetIcon(data.Message.Icon, Gtk.IconSize.Dialog);
                    // HACK: The icon is not rendered in dark mode (VibrantDark or DarkAqua) correctly.
                    //       Use light variant and reder it here.
                    // TODO: Recheck rendering issues with DarkAqua on final Mojave
                    if (IdeTheme.UserInterfaceTheme == Theme.Dark)
                    {
                        alert.Icon = img.WithStyles("-dark").ToBitmap(GtkWorkarounds.GetScaleFactor()).ToNSImage();
                    }
                    else
                    {
                        alert.Icon = img.ToNSImage();
                    }
                }
                else
                {
                    //for some reason the NSAlert doesn't pick up the app icon by default
                    alert.Icon = NSApplication.SharedApplication.ApplicationIconImage;
                }

                alert.MessageText = data.Message.Text;

                int accessoryViewItemsCount = data.Options.Count;

                string secondaryText = data.Message.SecondaryText ?? string.Empty;
                if (TryGetMessageView(secondaryText, out NSView messageView))
                {
                    accessoryViewItemsCount++;
                }
                else
                {
                    alert.InformativeText = secondaryText;
                }

                var accessoryViews      = accessoryViewItemsCount > 0 ? new NSView [accessoryViewItemsCount] : null;
                int accessoryViewsIndex = 0;

                if (messageView != null)
                {
                    accessoryViews [accessoryViewsIndex++] = messageView;
                }

                var buttons = data.Buttons.Reverse().ToList();

                for (int i = 0; i < buttons.Count - 1; i++)
                {
                    if (i == data.Message.DefaultButton)
                    {
                        var next = buttons[i];
                        for (int j = buttons.Count - 1; j >= i; j--)
                        {
                            var tmp = buttons[j];
                            buttons[j] = next;
                            next       = tmp;
                        }
                        break;
                    }
                }

                var wrappers = new List <AlertButtonWrapper> (buttons.Count);
                foreach (var button in buttons)
                {
                    var label = button.Label;
                    if (button.IsStockButton)
                    {
                        label = Gtk.Stock.Lookup(label).Label;
                    }
                    label = label.Replace("_", "");

                    //this message seems to be a standard Mac message since alert handles it specially
                    if (button == AlertButton.CloseWithoutSave)
                    {
                        label = GettextCatalog.GetString("Don't Save");
                    }

                    var nsbutton      = alert.AddButton(label);
                    var wrapperButton = new AlertButtonWrapper(nsbutton, data.Message, button, alert);
                    wrappers.Add(wrapperButton);
                    nsbutton.Target = wrapperButton;
                    nsbutton.Action = new ObjCRuntime.Selector("buttonActivatedAction");
                }

                NSButton [] optionButtons = null;
                if (data.Options.Count > 0)
                {
                    optionButtons = new NSButton [data.Options.Count];

                    for (int i = data.Options.Count - 1; i >= 0; i--)
                    {
                        var option = data.Options[i];
                        var button = new NSButton {
                            Title = option.Text,
                            Tag   = i,
                            State = option.Value? NSCellStateValue.On : NSCellStateValue.Off,
                        };
                        button.SetButtonType(NSButtonType.Switch);
                        button.SizeToFit();
                        optionButtons [i] = button;
                        accessoryViews [accessoryViewsIndex++] = button;
                    }
                }

                var accessoryView = ArrangeAccessoryViews(accessoryViews);
                if (accessoryView != null)
                {
                    alert.AccessoryView = accessoryView;
                }

                NSButton applyToAllCheck = null;
                if (data.Message.AllowApplyToAll)
                {
                    alert.ShowsSuppressionButton = true;
                    applyToAllCheck       = alert.SuppressionButton;
                    applyToAllCheck.Title = GettextCatalog.GetString("Apply to all");
                }

                // Hack up a slightly wider than normal alert dialog. I don't know how to do this in a nicer way
                // as the min size constraints are apparently ignored.
                var frame = alert.Window.Frame;
                alert.Window.SetFrame(new CGRect(frame.X, frame.Y, NMath.Max(frame.Width, 600), frame.Height), true);
                alert.Layout();

                bool completed = false;
                if (data.Message.CancellationToken.CanBeCanceled)
                {
                    data.Message.CancellationToken.Register(delegate {
                        alert.InvokeOnMainThread(() => {
                            if (!completed)
                            {
                                if (alert.Window.IsSheet && alert.Window.SheetParent != null)
                                {
                                    alert.Window.SheetParent.EndSheet(alert.Window);
                                }
                                else
                                {
                                    NSApplication.SharedApplication.AbortModal();
                                }
                            }
                        });
                    });
                }

                int response = -1000;

                var      parent = data.TransientFor ?? IdeApp.Workbench.RootWindow;
                NSWindow nativeParent;
                try {
                    nativeParent = parent;
                } catch (NotSupportedException) {
                    nativeParent = null;
                }
                if (!data.Message.CancellationToken.IsCancellationRequested)
                {
                    // sheeting is broken on High Sierra with dark NSAppearance
                    var sheet = IdeTheme.UserInterfaceTheme != Theme.Dark || MacSystemInformation.OsVersion != MacSystemInformation.HighSierra;
                    if (!sheet || nativeParent == null)
                    {
                        response = (int)alert.RunModal();
                    }
                    else
                    {
                        alert.BeginSheet(nativeParent, (modalResponse) => {
                            response = (int)modalResponse;
                            NSApplication.SharedApplication.StopModal();
                        });

                        NSApplication.SharedApplication.RunModalForWindow(alert.Window);
                    }
                }

                var result = response - (long)(int)NSAlertButtonReturn.First;

                completed = true;

                if (result >= 0 && result < buttons.Count)
                {
                    data.ResultButton = buttons [(int)result];
                }
                else
                {
                    data.ResultButton = null;
                }

                if (data.ResultButton == null || data.Message.CancellationToken.IsCancellationRequested)
                {
                    data.SetResultToCancelled();
                }

                if (optionButtons != null)
                {
                    foreach (var button in optionButtons)
                    {
                        var option = data.Options[(int)button.Tag];
                        data.Message.SetOptionValue(option.Id, button.State != 0);
                    }
                }

                if (applyToAllCheck != null && applyToAllCheck.State != 0)
                {
                    data.ApplyToAll = true;
                }

                if (nativeParent != null)
                {
                    nativeParent.MakeKeyAndOrderFront(nativeParent);
                }
                else
                {
                    GtkQuartz.FocusWindow(parent);
                }
            }

            return(true);
        }
        public bool Run(OpenFileDialogData data)
        {
            NSSavePanel panel = null;

            try {
                if (data.Action == FileChooserAction.Save)
                {
                    panel = new NSSavePanel();
                }
                else
                {
                    panel = new NSOpenPanel {
                        CanChooseDirectories = (data.Action & FileChooserAction.FolderFlags) != 0,
                        CanChooseFiles       = (data.Action & FileChooserAction.FileFlags) != 0,
                    };
                }

                MacSelectFileDialogHandler.SetCommonPanelProperties(data, panel);

                SelectEncodingPopUpButton encodingSelector = null;
                NSPopUpButton             viewerSelector   = null;
                NSButton closeSolutionButton = null;

                var box = new MDBox(LayoutDirection.Vertical, 2, 2);

                List <FileViewer> currentViewers = null;
                var labels = new List <MDAlignment> ();

                if ((data.Action & FileChooserAction.FileFlags) != 0)
                {
                    var filterPopup = MacSelectFileDialogHandler.CreateFileFilterPopup(data, panel);

                    if (filterPopup != null)
                    {
                        var filterLabel = new MDAlignment(new MDLabel(GettextCatalog.GetString("Show files:")), true);
                        var filterBox   = new MDBox(LayoutDirection.Horizontal, 2, 0)
                        {
                            { filterLabel },
                            { new MDAlignment(filterPopup, true)
                              {
                                  MinWidth = 200
                              } }
                        };
                        labels.Add(filterLabel);
                        box.Add(filterBox);
                    }

                    if (data.ShowEncodingSelector)
                    {
                        encodingSelector = new SelectEncodingPopUpButton(data.Action != FileChooserAction.Save);
                        encodingSelector.SelectedEncodingId = data.Encoding != null ? data.Encoding.CodePage : 0;

                        var encodingLabel = new MDAlignment(new MDLabel(GettextCatalog.GetString("Encoding:")), true);
                        var encodingBox   = new MDBox(LayoutDirection.Horizontal, 2, 0)
                        {
                            { encodingLabel },
                            { new MDAlignment(encodingSelector, true)
                              {
                                  MinWidth = 200
                              } }
                        };
                        labels.Add(encodingLabel);
                        box.Add(encodingBox);
                    }

                    if (data.ShowViewerSelector && panel is NSOpenPanel)
                    {
                        currentViewers = new List <FileViewer> ();
                        viewerSelector = new NSPopUpButton {
                            Enabled = false,
                        };

                        if (encodingSelector != null || IdeApp.Workspace.IsOpen)
                        {
                            viewerSelector.Activated += delegate {
                                var  idx = viewerSelector.IndexOfSelectedItem;
                                bool workbenchViewerSelected = idx == 0 && currentViewers [0] == null;
                                if (encodingSelector != null)
                                {
                                    encodingSelector.Enabled = !workbenchViewerSelected;
                                }
                                if (closeSolutionButton != null)
                                {
                                    if (closeSolutionButton.Hidden == workbenchViewerSelected)
                                    {
                                        closeSolutionButton.Hidden = !workbenchViewerSelected;
                                        CenterAccessoryView(box);
                                    }
                                }
                            };
                        }

                        var viewSelLabel = new MDLabel(GettextCatalog.GetString("Open with:"));
                        var viewSelBox   = new MDBox(LayoutDirection.Horizontal, 2, 0)
                        {
                            { viewSelLabel, true },
                            { new MDAlignment(viewerSelector, true)
                              {
                                  MinWidth = 200
                              } }
                        };

                        if (IdeApp.Workspace.IsOpen)
                        {
                            closeSolutionButton = new NSButton {
                                Title  = GettextCatalog.GetString("Close current workspace"),
                                Hidden = true,
                                State  = NSCellStateValue.On,
                            };

                            closeSolutionButton.SetButtonType(NSButtonType.Switch);
                            closeSolutionButton.SizeToFit();

                            viewSelBox.Add(closeSolutionButton, true);
                        }

                        box.Add(viewSelBox);
                    }
                }

                if (labels.Count > 0)
                {
                    float w = labels.Max(l => l.MinWidth);
                    foreach (var l in labels)
                    {
                        l.MinWidth = w;
                        l.XAlign   = LayoutAlign.Begin;
                    }
                }

                if (box.Count > 0)
                {
                    box.Layout();
                    panel.AccessoryView = box.View;
                }

                panel.SelectionDidChange += delegate {
                    var  selection         = MacSelectFileDialogHandler.GetSelectedFiles(panel);
                    bool slnViewerSelected = false;
                    if (viewerSelector != null)
                    {
                        slnViewerSelected = FillViewers(currentViewers, viewerSelector, closeSolutionButton, selection);
                        if (closeSolutionButton != null)
                        {
                            closeSolutionButton.Hidden = !slnViewerSelected;
                        }
                        CenterAccessoryView(box);
                    }
                    if (encodingSelector != null)
                    {
                        encodingSelector.Enabled = !slnViewerSelected;
                    }
                };

                if (panel.RunModal() == 0)
                {
                    GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                    return(false);
                }

                data.SelectedFiles = MacSelectFileDialogHandler.GetSelectedFiles(panel);

                if (encodingSelector != null)
                {
                    data.Encoding = encodingSelector.SelectedEncodingId > 0 ? Encoding.GetEncoding(encodingSelector.SelectedEncodingId) : null;
                }

                if (viewerSelector != null)
                {
                    if (closeSolutionButton != null)
                    {
                        data.CloseCurrentWorkspace = closeSolutionButton.State != NSCellStateValue.Off;
                    }
                    data.SelectedViewer = viewerSelector.IndexOfSelectedItem >= 0 ?
                                          currentViewers[(int)viewerSelector.IndexOfSelectedItem] : null;
                }

                GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
            } catch (Exception ex) {
                LoggingService.LogInternalError("Error in Open File dialog", ex);
            } finally {
                if (panel != null)
                {
                    panel.Dispose();
                }
            }
            return(true);
        }
Ejemplo n.º 13
0
        public bool Run(AddFileDialogData data)
        {
            using (var panel = new NSOpenPanel {
                CanChooseDirectories = false,
                CanChooseFiles = true,
            }) {
                MacSelectFileDialogHandler.SetCommonPanelProperties(data, panel);

                var popup       = new NSPopUpButton(new CGRect(0, 0, 200, 28), false);
                var dropdownBox = new MDBox(LayoutDirection.Horizontal, 2, 0)
                {
                    { new MDLabel(GettextCatalog.GetString("Override build action:")), true },
                    { new MDAlignment(popup, true)
                      {
                          MinWidth = 200
                      } }
                };

                var filterPopup = MacSelectFileDialogHandler.CreateFileFilterPopup(data, panel);
                if (filterPopup != null)
                {
                    dropdownBox.Layout();
                    var box = new MDBox(LayoutDirection.Vertical, 2, 2)
                    {
                        dropdownBox.View,
                        filterPopup,
                    };
                    box.Layout();
                    panel.AccessoryView = box.View;
                    box.Layout(box.View.Superview.Frame.Size);
                }
                else
                {
                    dropdownBox.Layout();
                    panel.AccessoryView = dropdownBox.View;
                }

                popup.AddItem(GettextCatalog.GetString("(Default)"));
                popup.Menu.AddItem(NSMenuItem.SeparatorItem);

                foreach (var b in data.BuildActions)
                {
                    if (b == "--")
                    {
                        popup.Menu.AddItem(NSMenuItem.SeparatorItem);
                    }
                    else
                    {
                        popup.AddItem(b);
                    }
                }

                if (panel.RunModal() == 0)
                {
                    GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                    return(false);
                }

                data.SelectedFiles = MacSelectFileDialogHandler.GetSelectedFiles(panel);

                var idx = popup.IndexOfSelectedItem - 2;
                if (idx >= 0)
                {
                    data.OverrideAction = data.BuildActions[idx];
                }

                GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                return(true);
            }
        }
Ejemplo n.º 14
0
        public bool Run(ExceptionDialogData data)
        {
            using (var alert = new NSAlert {
                AlertStyle = NSAlertStyle.Critical
            }) {
                IdeTheme.ApplyTheme(alert.Window);
                alert.Icon = NSApplication.SharedApplication.ApplicationIconImage;

                alert.MessageText = data.Title ?? GettextCatalog.GetString("Error");

                if (!string.IsNullOrEmpty(data.Message))
                {
                    alert.InformativeText = data.Message;
                }

                List <AlertButton> buttons = null;
                if (data.Buttons != null && data.Buttons.Length > 0)
                {
                    buttons = data.Buttons.Reverse().ToList();
                }

                if (buttons != null)
                {
                    foreach (var button in buttons)
                    {
                        var label = button.Label;
                        if (button.IsStockButton)
                        {
                            label = Gtk.Stock.Lookup(label).Label;
                        }
                        label = label.Replace("_", "");

                        //this message seems to be a standard Mac message since alert handles it specially
                        if (button == AlertButton.CloseWithoutSave)
                        {
                            label = GettextCatalog.GetString("Don't Save");
                        }

                        alert.AddButton(label);
                    }
                }

                if (data.Exception != null)
                {
                    var         scrollSize = new CGSize(400, 130);
                    const float spacing    = 4;

                    string title    = GettextCatalog.GetString("View details");
                    string altTitle = GettextCatalog.GetString("Hide details");

                    var buttonFrame = new CGRect(0, 0, 0, 0);
                    var button      = new NSButton(buttonFrame)
                    {
                        BezelStyle     = NSBezelStyle.Disclosure,
                        Title          = "",
                        AlternateTitle = "",
                    };
                    button.SetButtonType(NSButtonType.OnOff);
                    button.SizeToFit();

                    var label = new MDClickableLabel(title)
                    {
                        Alignment = NSTextAlignment.Left,
                    };
                    label.SizeToFit();

                    button.SetFrameSize(new CGSize(button.Frame.Width, NMath.Max(button.Frame.Height, label.Frame.Height)));
                    label.SetFrameOrigin(new CGPoint(button.Frame.Width + 5, button.Frame.Y));

                    var text = new MyTextView(new CGRect(0, 0, float.MaxValue, float.MaxValue))
                    {
                        HorizontallyResizable = true,
                    };
                    text.TextContainer.ContainerSize       = new CGSize(float.MaxValue, float.MaxValue);
                    text.TextContainer.WidthTracksTextView = true;
                    text.InsertText(new NSString(data.Exception.ToString()));
                    text.Editable = false;

                    var scrollView = new NSScrollView(new CGRect(CGPoint.Empty, CGSize.Empty))
                    {
                        HasHorizontalScroller = true,
                        HasVerticalScroller   = true,
                    };

                    var accessory = new NSView(new CGRect(0, 0, scrollSize.Width, button.Frame.Height));
                    accessory.AddSubview(scrollView);
                    accessory.AddSubview(button);
                    accessory.AddSubview(label);

                    alert.AccessoryView = accessory;

                    button.Activated += delegate {
                        nfloat change;
                        if (button.State == NSCellStateValue.On)
                        {
                            change                  = scrollSize.Height + spacing;
                            label.StringValue       = altTitle;
                            scrollView.Hidden       = false;
                            scrollView.Frame        = new CGRect(CGPoint.Empty, scrollSize);
                            scrollView.DocumentView = text;
                        }
                        else
                        {
                            change            = -(scrollSize.Height + spacing);
                            label.StringValue = title;
                            scrollView.Hidden = true;
                            scrollView.Frame  = new CGRect(CGPoint.Empty, CGSize.Empty);
                        }
                        var f = accessory.Frame;
                        f.Height       += change;
                        accessory.Frame = f;
                        var lf = label.Frame;
                        lf.Y       += change;
                        label.Frame = lf;
                        var bf = button.Frame;
                        bf.Y        += change;
                        button.Frame = bf;
                        label.SizeToFit();
                        var panel = alert.Window;
                        var pf    = panel.Frame;
                        pf.Height += change;
                        pf.Y      -= change;
                        panel.SetFrame(pf, true, true);
                        //unless we assign the icon again, it starts nesting old icon into the warning icon
                        alert.Icon = NSApplication.SharedApplication.ApplicationIconImage;
                        alert.Layout();
                    };
                    label.OnMouseUp += (sender, e) => button.PerformClick(e.Event);
                }

                var result = (int)(nint)alert.RunModal() - (int)(long)NSAlertButtonReturn.First;
                data.ResultButton = buttons != null ? buttons [result] : null;
                GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
            }

            return(true);
        }
Ejemplo n.º 15
0
        public bool Run(OpenFileDialogData data)
        {
            try {
                using (var panel = CreatePanel(data, out var state)) {
                    bool pathAlreadySet = false;
                    panel.DidChangeToDirectory += (sender, e) => {
                        var directoryPath = e.NewDirectoryUrl?.AbsoluteString;
                        if (string.IsNullOrEmpty(directoryPath))
                        {
                            return;
                        }
                        var selectedPath = data.OnDirectoryChanged(this, directoryPath);
                        if (selectedPath.IsNull)
                        {
                            return;
                        }
                        data.SelectedFiles = new FilePath [] { selectedPath };
                        pathAlreadySet     = true;

                        // We need to call Cancel on 1ms delay so it's executed after DidChangeToDirectory event handler is finished
                        // this is needed because it's possible that DidChangeToDirectory event is executed while dialog is opening
                        // in that case calling .Cancel() leaves dialog in weird state...
                        // Fun fact: DidChangeToDirectory event is called from Open on 10.12 but not on 10.13
                        System.Threading.Tasks.Task.Delay(1).ContinueWith(delegate { panel.Cancel(panel); }, Runtime.MainTaskScheduler);
                    };

                    panel.SelectionDidChange += delegate {
                        var  selection         = MacSelectFileDialogHandler.GetSelectedFiles(panel);
                        bool slnViewerSelected = false;
                        if (state.ViewerSelector != null)
                        {
                            slnViewerSelected = FillViewers(state.CurrentViewers, state.ViewerSelector, state.CloseSolutionButton, selection);
                            if (state.CloseSolutionButton != null)
                            {
                                state.CloseSolutionButton.Enabled = slnViewerSelected;
                                state.CloseSolutionButton.State   = slnViewerSelected ? NSCellStateValue.On : NSCellStateValue.Off;
                            }
                        }
                        if (state.EncodingSelector != null)
                        {
                            state.EncodingSelector.Enabled = !slnViewerSelected;
                        }
                    };

                    if (panel.RunModal() == 0 && !pathAlreadySet)
                    {
                        GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                        return(false);
                    }
                    if (!pathAlreadySet)
                    {
                        data.SelectedFiles = MacSelectFileDialogHandler.GetSelectedFiles(panel);
                    }

                    if (state.EncodingSelector != null)
                    {
                        data.Encoding = state.EncodingSelector.SelectedEncodingId > 0 ? Encoding.GetEncoding(state.EncodingSelector.SelectedEncodingId) : null;
                    }

                    if (state.ViewerSelector != null)
                    {
                        if (state.CloseSolutionButton != null)
                        {
                            data.CloseCurrentWorkspace = state.CloseSolutionButton.State != NSCellStateValue.Off;
                        }
                        data.SelectedViewer = state.ViewerSelector.IndexOfSelectedItem >= 0 ?
                                              state.CurrentViewers [(int)state.ViewerSelector.IndexOfSelectedItem] : null;
                    }

                    GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                }
            } catch (Exception ex) {
                LoggingService.LogInternalError("Error in Open File dialog", ex);
            }
            return(true);
        }
Ejemplo n.º 16
0
        public bool Run(OpenFileDialogData data)
        {
            NSSavePanel panel = null;

            try {
                bool directoryMode = data.Action != Gtk.FileChooserAction.Open &&
                                     data.Action != Gtk.FileChooserAction.Save;

                if (data.Action == Gtk.FileChooserAction.Save)
                {
                    panel = new NSSavePanel();
                }
                else
                {
                    panel = new NSOpenPanel()
                    {
                        CanChooseDirectories = directoryMode,
                        CanChooseFiles       = !directoryMode,
                    };
                }

                MacSelectFileDialogHandler.SetCommonPanelProperties(data, panel);

                SelectEncodingPopUpButton encodingSelector = null;
                NSPopUpButton             viewerSelector   = null;
                NSButton closeSolutionButton = null;

                var box = new MDBox(LayoutDirection.Vertical, 2, 2);

                List <FileViewer>  currentViewers = null;
                List <MDAlignment> labels         = new List <MDAlignment> ();

                if (!directoryMode)
                {
                    var filterPopup = MacSelectFileDialogHandler.CreateFileFilterPopup(data, panel);

                    if (filterPopup != null)
                    {
                        var filterLabel = new MDAlignment(new MDLabel(GettextCatalog.GetString("Show files:")), true);
                        var filterBox   = new MDBox(LayoutDirection.Horizontal, 2, 0)
                        {
                            { filterLabel },
                            { new MDAlignment(filterPopup, true)
                              {
                                  MinWidth = 200
                              } }
                        };
                        labels.Add(filterLabel);
                        box.Add(filterBox);
                    }

                    if (data.ShowEncodingSelector)
                    {
                        encodingSelector = new SelectEncodingPopUpButton(data.Action != Gtk.FileChooserAction.Save);
                        encodingSelector.SelectedEncodingId = data.Encoding != null ? data.Encoding.CodePage : 0;

                        var encodingLabel = new MDAlignment(new MDLabel(GettextCatalog.GetString("Encoding:")), true);
                        var encodingBox   = new MDBox(LayoutDirection.Horizontal, 2, 0)
                        {
                            { encodingLabel },
                            { new MDAlignment(encodingSelector, true)
                              {
                                  MinWidth = 200
                              } }
                        };
                        labels.Add(encodingLabel);
                        box.Add(encodingBox);
                    }

                    if (data.ShowViewerSelector && panel is NSOpenPanel)
                    {
                        currentViewers = new List <FileViewer> ();
                        viewerSelector = new NSPopUpButton()
                        {
                            Enabled = false,
                        };

                        if (encodingSelector != null)
                        {
                            viewerSelector.Activated += delegate {
                                var idx = viewerSelector.IndexOfSelectedItem;
                                encodingSelector.Enabled = !(idx == 0 && currentViewers [0] == null);
                            };
                        }

                        var viewSelLabel = new MDLabel(GettextCatalog.GetString("Open with:"));
                        var viewSelBox   = new MDBox(LayoutDirection.Horizontal, 2, 0)
                        {
                            { viewSelLabel, true },
                            { new MDAlignment(viewerSelector, true)
                              {
                                  MinWidth = 200
                              } }
                        };

                        if (IdeApp.Workspace.IsOpen)
                        {
                            closeSolutionButton = new NSButton()
                            {
                                Title  = GettextCatalog.GetString("Close current workspace"),
                                Hidden = true,
                                State  = NSCellStateValue.On,
                            };

                            closeSolutionButton.SetButtonType(NSButtonType.Switch);
                            closeSolutionButton.SizeToFit();

                            viewSelBox.Add(closeSolutionButton, true);
                        }

                        box.Add(viewSelBox);
                    }
                }

                if (labels.Count > 0)
                {
                    float w = labels.Max(l => l.MinWidth);
                    foreach (var l in labels)
                    {
                        l.MinWidth = w;
                        l.XAlign   = LayoutAlign.Begin;
                    }
                }

                if (box.Count > 0)
                {
                    box.Layout();
                    panel.AccessoryView = box.View;
                }

                panel.SelectionDidChange += delegate(object sender, EventArgs e) {
                    var  selection         = MacSelectFileDialogHandler.GetSelectedFiles(panel);
                    bool slnViewerSelected = false;
                    if (viewerSelector != null)
                    {
                        FillViewers(currentViewers, viewerSelector, closeSolutionButton, selection);
                        if (currentViewers.Count == 0 || currentViewers [0] != null)
                        {
                            if (closeSolutionButton != null)
                            {
                                closeSolutionButton.Hidden = true;
                            }
                            slnViewerSelected = false;
                        }
                        else
                        {
                            if (closeSolutionButton != null)
                            {
                                closeSolutionButton.Hidden = false;
                            }
                            slnViewerSelected = true;
                        }
                        box.Layout();

                        //re-center the accessory view in its parent, Cocoa does this for us initially and after
                        //resizing the window, but we need to do it again after altering its layout
                        var superFrame = box.View.Superview.Frame;
                        var frame      = box.View.Frame;
                        //not sure why it's ceiling, but this matches the Cocoa layout
                        frame.X        = (float)Math.Ceiling((superFrame.Width - frame.Width) / 2);
                        frame.Y        = (float)Math.Ceiling((superFrame.Height - frame.Height) / 2);
                        box.View.Frame = frame;
                    }
                    if (encodingSelector != null)
                    {
                        encodingSelector.Enabled = !slnViewerSelected;
                    }
                };

                if (panel.RunModal() == 0)
                {
                    GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                    return(false);
                }

                data.SelectedFiles = MacSelectFileDialogHandler.GetSelectedFiles(panel);

                if (encodingSelector != null)
                {
                    data.Encoding = encodingSelector.SelectedEncodingId > 0 ? Encoding.GetEncoding(encodingSelector.SelectedEncodingId) : null;
                }

                if (viewerSelector != null)
                {
                    if (closeSolutionButton != null)
                    {
                        data.CloseCurrentWorkspace = closeSolutionButton.State != NSCellStateValue.Off;
                    }
                    data.SelectedViewer = currentViewers[viewerSelector.IndexOfSelectedItem];
                }

                GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
            } catch (Exception ex) {
                LoggingService.LogError("Error in Open File dialog", ex);
                MessageService.ShowException(ex);
            } finally {
                if (panel != null)
                {
                    panel.Dispose();
                }
            }
            return(true);
        }
        public bool Run(OpenFileDialogData data)
        {
            NSSavePanel panel = null;

            try {
                bool directoryMode = data.Action != Gtk.FileChooserAction.Open &&
                                     data.Action != Gtk.FileChooserAction.Save;

                if (data.Action == Gtk.FileChooserAction.Save)
                {
                    panel = new NSSavePanel();
                }
                else
                {
                    panel = new NSOpenPanel()
                    {
                        CanChooseDirectories = directoryMode,
                        CanChooseFiles       = !directoryMode,
                    };
                }

                MacSelectFileDialogHandler.SetCommonPanelProperties(data, panel);

                SelectEncodingPopUpButton encodingSelector = null;
                NSPopUpButton             viewerSelector   = null;
                NSButton closeSolutionButton = null;

                var box = new MDBox(LayoutDirection.Vertical, 2, 2);

                List <FileViewer>  currentViewers = null;
                List <MDAlignment> labels         = new List <MDAlignment> ();

                if (!directoryMode)
                {
                    var filterPopup = MacSelectFileDialogHandler.CreateFileFilterPopup(data, panel);

                    var filterLabel = new MDAlignment(new MDLabel(GettextCatalog.GetString("Show files:")), true);
                    var filterBox   = new MDBox(LayoutDirection.Horizontal, 2, 0)
                    {
                        { filterLabel },
                        { new MDAlignment(filterPopup, true)
                          {
                              MinWidth = 200
                          } }
                    };
                    labels.Add(filterLabel);
                    box.Add(filterBox);

                    if (data.ShowEncodingSelector)
                    {
                        encodingSelector = new SelectEncodingPopUpButton(data.Action != Gtk.FileChooserAction.Save);
                        encodingSelector.SelectedEncodingId = data.Encoding;

                        var encodingLabel = new MDAlignment(new MDLabel(GettextCatalog.GetString("Encoding:")), true);
                        var encodingBox   = new MDBox(LayoutDirection.Horizontal, 2, 0)
                        {
                            { encodingLabel },
                            { new MDAlignment(encodingSelector, true)
                              {
                                  MinWidth = 200
                              } }
                        };
                        labels.Add(encodingLabel);
                        box.Add(encodingBox);
                    }

                    if (data.ShowViewerSelector && panel is NSOpenPanel)
                    {
                        currentViewers = new List <FileViewer> ();
                        viewerSelector = new NSPopUpButton()
                        {
                            Enabled = false,
                        };

                        if (encodingSelector != null)
                        {
                            viewerSelector.Activated += delegate {
                                var idx = viewerSelector.IndexOfSelectedItem;
                                encodingSelector.Enabled = !(idx == 0 && currentViewers[0] == null);
                            };
                        }

                        var viewSelLabel = new MDLabel(GettextCatalog.GetString("Open with:"));
                        var viewSelBox   = new MDBox(LayoutDirection.Horizontal, 2, 0)
                        {
                            { viewSelLabel, true },
                            { new MDAlignment(viewerSelector, true)
                              {
                                  MinWidth = 200
                              } }
                        };

                        if (IdeApp.Workspace.IsOpen)
                        {
                            closeSolutionButton = new NSButton()
                            {
                                Title  = GettextCatalog.GetString("Close current workspace"),
                                Hidden = true,
                                State  = NSCellStateValue.On,
                            };

                            closeSolutionButton.SetButtonType(NSButtonType.Switch);
                            closeSolutionButton.SizeToFit();

                            viewSelBox.Add(closeSolutionButton, true);
                        }

                        box.Add(viewSelBox);
                    }
                }

                if (labels.Count > 0)
                {
                    float w = labels.Max(l => l.MinWidth);
                    foreach (var l in labels)
                    {
                        l.MinWidth = w;
                        l.XAlign   = LayoutAlign.Begin;
                    }
                }

                if (box.Count > 0)
                {
                    box.Layout();
                    panel.AccessoryView = box.View;
                }

                panel.SelectionDidChange += delegate(object sender, EventArgs e) {
                    var  selection         = MacSelectFileDialogHandler.GetSelectedFiles(panel);
                    bool slnViewerSelected = false;
                    if (viewerSelector != null)
                    {
                        FillViewers(currentViewers, viewerSelector, selection);
                        if (currentViewers.Count == 0 || currentViewers[0] != null)
                        {
                            if (closeSolutionButton != null)
                            {
                                closeSolutionButton.Hidden = true;
                            }
                            slnViewerSelected = false;
                        }
                        else
                        {
                            if (closeSolutionButton != null)
                            {
                                closeSolutionButton.Hidden = false;
                            }
                            slnViewerSelected = true;
                        }
                        box.Layout();
                    }
                    if (encodingSelector != null)
                    {
                        encodingSelector.Enabled = !slnViewerSelected;
                    }
                };

                try {
                    var action = MacSelectFileDialogHandler.RunPanel(data, panel);
                    if (!action)
                    {
                        GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
                        return(false);
                    }
                } catch (Exception ex) {
                    System.Console.WriteLine(ex);
                    throw;
                }

                data.SelectedFiles = MacSelectFileDialogHandler.GetSelectedFiles(panel);

                if (encodingSelector != null)
                {
                    data.Encoding = encodingSelector.SelectedEncodingId;
                }

                if (viewerSelector != null)
                {
                    if (closeSolutionButton != null)
                    {
                        data.CloseCurrentWorkspace = closeSolutionButton.State != NSCellStateValue.Off;
                    }
                    data.SelectedViewer = currentViewers[viewerSelector.IndexOfSelectedItem];
                }

                GtkQuartz.FocusWindow(data.TransientFor ?? MessageService.RootWindow);
            } catch (Exception ex) {
                MessageService.ShowException(ex);
            } finally {
                if (panel != null)
                {
                    panel.Dispose();
                }
            }
            return(true);
        }