Esempio n. 1
0
        private static Gtk.AccelKey GetAcceleratorKey(this VisualRelayCommand command)
        {
            ErrorReporting.ReportErrorIf(command.KeyboardShortcutKey.Length != 1, "Invalid keyboard shortcut!");
            var modifiers = (Gdk.ModifierType)command.KeyboardShortcutModifiers;

            if (char.IsUpper(command.KeyboardShortcutKey[0]))
            {
                modifiers |= Gdk.ModifierType.ShiftMask;
            }
            var key            = (Gdk.Key)command.KeyboardShortcutKey[0]; // totes hacky!
            var acceleratorKey = new Gtk.AccelKey(key, modifiers, Gtk.AccelFlags.Visible);

            return(acceleratorKey);
        }
Esempio n. 2
0
 /// <summary>
 /// Adds an input binding given a command with keyboard shortcut information defined.
 /// </summary>
 /// <param name="command">The command containing a keyboard shortcut.</param>
 /// <param name="visual">The visual for which to register the binding.</param>
 /// <remarks>If the visual is null, the shortcut will be registered using the
 /// current application's MainWindow.</remarks>
 public static void AddInputBinding(this VisualRelayCommand command, UIElement visual)
 {
     if (!string.IsNullOrEmpty(command.KeyboardShortcutKey))
     {
         ErrorReporting.ReportErrorIf(command.KeyboardShortcutKey.Length != 1, "Invalid keyboard shortcut!");
         var modifiers = (ModifierKeys)command.KeyboardShortcutModifiers;
         if (char.IsUpper(command.KeyboardShortcutKey[0]))
         {
             modifiers |= ModifierKeys.Shift;
         }
         var keyConverter = new KeyConverter();
         var key          = (Key)keyConverter.ConvertFromInvariantString(command.KeyboardShortcutKey);
         if (visual == null)
         {
             visual = SingleInstanceApplication.Current.MainWindow;
         }
         visual.InputBindings.Add(new KeyBinding(command, key, modifiers));
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Creates a visual for a command.
        /// </summary>
        /// <param name="command">The command for which a visual must be created.</param>
        /// <param name="requiresParentCommand">If <c>true</c>, this command requires a parent visual.</param>
        /// <returns>The visual for the command.</returns>
        public static UIElement CreateVisualForCommand(this VisualRelayCommand command, bool requiresParentCommand)
        {
            System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(command.UniqueId), "Command's UniqueId is not defined.");
            UIElement visual        = null;
            var       parentCommand = (VisualRelayCommand)command.VisualParent;
            UIElement parentVisual  = null;

            if (parentCommand != null)
            {
                if (parentCommand.Visual.IsEmpty)
                {
                    var group = parentCommand.GetCommandGroup();
                    if (group != null)
                    {
                        parentCommand.Visual = group.CreateVisualForCommand(parentCommand);
                    }
                }
                ErrorReporting.ReportErrorIf(requiresParentCommand && parentCommand.Visual.IsEmpty, "Failed to create parent visual for command: " + command.Name + "(" + command.UniqueId + ")");
                parentVisual = parentCommand.Visual.AsType <UIElement>();
            }

            DebugOutputIf(requiresParentCommand && (parentCommand == null), "No parent visual for command: " + command.Name + "(" + command.UniqueId + ")");

            if ((visual == null) && command.UseXamlResource)
            {
                visual = command.CreateVisualFromResource();

                if (visual != null)
                {
                    visual.SetValue(VisualCommandProperty, command);
                    command.AddChildCommandVisual(visual);
                }
            }

            return(visual);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a visual for a command.
        /// </summary>
        /// <param name="command">The command for which a visual must be created.</param>
        /// <param name="requiresParentCommand">If <c>true</c>, this command requires a parent visual.</param>
        /// <returns>The visual for the command.</returns>
        public static Gtk.Widget CreateVisualForCommand(this VisualRelayCommand command, bool requiresParentCommand)
        {
            System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(command.UniqueId), "Command's UniqueId is not defined.");
            Gtk.Widget visual        = null;
            var        parentCommand = (VisualRelayCommand)command.VisualParent;

            if (parentCommand != null)
            {
                var parentVisual = parentCommand.Visual.NativeVisual;
                if (parentVisual is Gtk.MenuBar)
                {
                    throw new System.InvalidOperationException("This should be done by CreateMenuItem!");

                    // Make a submenu for the command, and add the menu item to the menubar.
                    ////var menu = new Gtk.Menu() { Name = command.UniqueId };
                    ////var menuItem = new Gtk.MenuItem(command.MenuItemName) { Name = command.UniqueId, Submenu = menu };
                    ////((Gtk.MenuBar)parentVisual).Add(menuItem);
                    ////visual = menuItem;
                }
                if (parentVisual is Gtk.Toolbar)
                {
                    // The default toolbar command visual will be a button. If a different visual is desired,
                    // the CommandGroup should override visual creation for that command in ICommandGroup.CreateVisualForCommand().
                    var          toolbar = parentVisual as Gtk.Toolbar;
                    Gtk.ToolItem toolbarItem;
                    if (command.UniqueId == RootCommandGroup.ToolbarSeparatorCommand.UniqueId)
                    {
                        toolbarItem = new Gtk.SeparatorToolItem();
                    }
                    else
                    {
                        var group = command.GetCommandGroup() as CommandGroup;
                        toolbarItem = group.CreateToolbarItemForCommand(command).AsType <Gtk.ToolItem>();
                    }
                    var insertLocation = FindInsertLocation(parentCommand, command, false);
                    toolbar.Insert(toolbarItem, insertLocation); // if <0, appends
                    visual = toolbarItem;
                }
                if (parentCommand.Visual.IsEmpty)
                {
                    var group = parentCommand.GetCommandGroup();
                    if (group != null)
                    {
                        parentCommand.Visual = group.CreateVisualForCommand(parentCommand);
                    }
                }

                ErrorReporting.ReportErrorIf(requiresParentCommand && parentCommand.Visual.IsEmpty, "Failed to create parent visual for command: " + command.Name + "(" + command.UniqueId + ")");
                parentVisual = parentCommand.Visual;
            }

            DebugOutputIf(requiresParentCommand && (parentCommand == null), "No parent visual for command: " + command.Name + "(" + command.UniqueId + ")");
            if (visual != null)
            {
                visual.SetValue(CommandGroup.AttachedCommandPropertyName, command);
                var group = command.GetCommandGroup() as CommandGroup;
                if (group != null)
                {
                    var context = group.GetCommandContext(command, null);
                    visual.SetValue(IFakeDependencyObjectHelpers.DataContextPropertyName, context);
                    group.AttachCanExecuteChangeHandler(command);
                }
            }
            return(visual);
        }
Esempio n. 5
0
 /// <summary>
 /// Performs one-time initialization for the Directory class.
 /// </summary>
 static Directory()
 {
     ErrorReporting.ReportErrorIf <System.InvalidOperationException>(FlatSizeInBytes != 512, ReportMechanism.Default, "Directory flat size is incorrect.", "Directory Sanity Check");
     InvalidDirectory = new Directory();
 }
Esempio n. 6
0
        /// <summary>
        /// Creates a visual for a command.
        /// </summary>
        /// <param name="command">The command for which a visual must be created.</param>
        /// <param name="requiresParentCommand">If <c>true</c>, this command requires a parent visual.</param>
        /// <returns>The visual for the command.</returns>
        public static NSObject CreateVisualForCommand(this VisualRelayCommand command, bool requiresParentCommand)
        {
            System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(command.UniqueId), "Command's UniqueId is not defined.");
            NSObject visual        = null;
            var      parentCommand = (VisualRelayCommand)command.VisualParent;
            NSObject parentVisual  = null;

            if (parentCommand != null)
            {
                ErrorReporting.ReportErrorIf(parentCommand.Visual == null, "Failed to create parent visual for command: " + command.Name + "(" + command.UniqueId + ")");
                parentVisual = parentCommand.Visual;
            }

            DebugOutputIf(parentCommand == null, "No parent visual for command: " + command.Name + "(" + command.UniqueId + ")");

            var insertLocation = parentCommand.FindInsertLocation(command);

            if (parentVisual is NSToolbar)
            {
                var toolbar     = (NSToolbar)parentVisual;
                var toolbarItem = new NSToolbarItem(command.UniqueId);
                toolbarItem.Autovalidates = false;
                toolbarItem.Tag           = (int)(command.Weight * WeightScaleFactor);
                toolbarItem.Label         = command.Name.TrimEnd(new char[] { '.' });
                toolbarItem.Image         = command.LargeIcon;
                toolbarItem.MinSize       = command.LargeIcon.Size;
                toolbarItem.MaxSize       = command.LargeIcon.Size;
                if (!string.IsNullOrEmpty(command.ToolTip))
                {
                    toolbarItem.ToolTip = command.ToolTip;
                }
                visual = toolbarItem;
#if false
                if (toolbar.DefaultItemIdentifiers == null)
                {
                    toolbar.DefaultItemIdentifiers = NSToolbarIdentifiers;
                }
                if (toolbar.AllowedItemIdentifiers == null)
                {
                    toolbar.AllowedItemIdentifiers = NSToolbarIdentifiers;
                }
                if (toolbar.SelectableItemIdentifiers == null)
                {
                    // WORKAROUND for bug https://bugzilla.xamarin.com/show_bug.cgi?id=21680
                    toolbar.SelectableItemIdentifiers = BugWorkaround;
                }
                var prevWillAdd = toolbar.WillInsertItem;
                toolbar.WillInsertItem = (t, i, b) => toolbarItem;
                toolbar.InsertItem(((VisualRelayCommand)command).UniqueId, insertLocation);
                toolbar.WillInsertItem = prevWillAdd;
#endif // false
                toolbar.AddToolbarItem(toolbarItem, ((VisualRelayCommand)command).UniqueId, insertLocation);
            }
            if (parentVisual is NSToolbarItem)
            {
                parentVisual = ((NSToolbarItem)parentVisual).View;
            }
            if (parentVisual is NSSegmentedControl)
            {
                var toolbarItem      = parentCommand.Visual as NSToolbarItem;
                var segmentedControl = (NSSegmentedControl)parentVisual;

                // We don't actually try to insert at correct location, since segmented control
                // will always add new cells at the end and it's just a PITA to deal w/ generally
                // solving the shuffling of cell data at this time. Instead, always stick the new
                // cell at the end.
                insertLocation = (int)segmentedControl.SegmentCount;
                segmentedControl.SegmentCount = insertLocation + 1;
                segmentedControl.SetImage(command.SmallIcon, insertLocation);
                segmentedControl.Cell.SetToolTip(command.Name, insertLocation);
                segmentedControl.Cell.SetTag(command.GetHashCode(), insertLocation);
                segmentedControl.SizeToFit();
                toolbarItem.MinSize = segmentedControl.Bounds.Size;
                visual = segmentedControl;
            }
            return(visual);
        }