Пример #1
0
        public MyTaskbar(DesktopEnvironment parent, Color color) : base(
                parent,
                color,
                position: new Vector2i(0, parent.Size.Y - 30),
                size: new Vector2i(parent.Size.X, 30))
        {
            int      borderLength = 5;
            Vector2i iconsSize    = new Vector2i(Size.Y, Size.Y);

            var gErc = new ExtensibleRowContainer(this, borderLength);

            var startMenuButton = new SimpleRectControl(gErc, Color.Blue,
                                                        position: new Vector2i(0, 0),
                                                        size: new Vector2i(Size.Y, Size.Y));

            startMenuButton.Click += (s, e) => ToggleStartMenu(s, EventArgs.Empty);

            var erc = new ExtensibleRowContainer(gErc, borderLength);
            //myTaskbar.Controls.Add(new TaskbarExecutable(myTaskbar, @"C:\Program Files (x86)\Minecraft\MinecraftLauncher.exe"));
            var cmdExec = new Executable(erc, @"c:\windows\system32\cmd.exe")
            {
                Size = iconsSize
            };
            var notepadExec = new Executable(erc, @"c:\windows\notepad.exe")
            {
                Size = iconsSize
            };

            Load(startMenuButton);
            Load(gErc);
            Load(erc);
            Load(cmdExec);
            Load(notepadExec);
        }
        public static CustomDesktopEnvironment ParseFile(string filePath)
        {
            CustomDesktopEnvironment desktopEnvironment;
            XmlReaderSettings        settings = new XmlReaderSettings();
            XmlReader reader = XmlReader.Create(filePath, settings);

            try
            {
                desktopEnvironment = ReadDesktopEnvironment();

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (!Enum.TryParse(reader.Name, out ControlType type))
                        {
                            throw new FileLoadException(reader.BaseURI);
                        }

                        switch (type)
                        {
                        case ControlType.SimpleRect:
                            Control.Load(ReadSimpleRect());
                            break;

                        default:
                            throw new FileLoadException(reader.BaseURI);
                        }
                    }
                }
            }
            catch (XmlException)
            {
                throw new FileLoadException(reader.BaseURI);
            }
            finally
            {
                reader.Close();
            }

            return(desktopEnvironment);


            void ReadSizeAttribute(Control control)
            {
                int x = int.Parse(reader.GetAttribute($"{nameof(control.Size)}{nameof(control.Size.X)}"));
                int y = int.Parse(reader.GetAttribute($"{nameof(control.Size)}{nameof(control.Size.Y)}"));

                control.Size = new Vector2i(x, y);

                if (x == ControlLayout.ScreenSize)
                {
                    control.FillParentWidth();
                }
                if (y == ControlLayout.ScreenSize)
                {
                    control.FillParentHeight();
                }
            }

            void ReadPositionAttribute(Control control)
            {
                int x = int.Parse(reader.GetAttribute($"{nameof(control.Position)}{nameof(control.Position.X)}"));
                int y = int.Parse(reader.GetAttribute($"{nameof(control.Position)}{nameof(control.Position.Y)}"));

                control.Position = new Vector2i(Math.Abs(x), Math.Abs(y));

                if (ControlLayout.IsStickedToRightOrBottom(x))
                {
                    control.StickToRight();
                }
                if (ControlLayout.IsStickedToRightOrBottom(y))
                {
                    control.StickToBottom();
                }
            }

            ControlNo ReadIdAttribute()
            {
                try
                {
                    return(uint.Parse(reader.GetAttribute($"{nameof(Control.Id)}")));
                }
                catch (Exception)
                {
                    return(0);
                }
            }

            CustomDesktopEnvironment ReadDesktopEnvironment()
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (!Enum.TryParse(reader.Name, out ControlType type) || type != ControlType.DesktopEnvironment)
                        {
                            throw new FileLoadException(reader.BaseURI);
                        }

                        // BBHERE: initialize the desktopEnvironments attributes from XML

                        return(new CustomDesktopEnvironment(new KeyboardShortcutCollection()));
                    }
                }

                throw new FileLoadException(reader.BaseURI);
            }

            SimpleRectControl ReadSimpleRect()
            {
                Color color   = DesktopEnvironmentStorage.GetColorOrDefault(reader.GetAttribute($"{nameof(SimpleRectControl.Color)}"));
                var   control = new SimpleRectControl(desktopEnvironment, color, id: ReadIdAttribute());

                ReadPositionAttribute(control);
                ReadSizeAttribute(control);

                return(control);
            }
        }