Beispiel #1
0
        // --------------------------------------------------------------------------------------------------
        #region ctors & Factory


        /// <summary>
        /// Base control constructor
        /// </summary>
        public LimeControl()
        {
            LimeLib.LifeTrace(this);

            Base = this;

            // This Control will be defined when bound
            this.SetBinding(BoundDataContextProperty, new Binding());
        }
Beispiel #2
0
        // --------------------------------------------------------------------------------------------------
        #region ctors

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="skinName">Name of the skin</param>
        /// <param name="loadParam">Load the parameters from user-config</param>
        public Skin(string skinName, bool loadParam = true)
        {
            LimeMsg.Debug("Skin: constructor");
            LimeLib.LifeTrace(this);

            // Invalidate this skin
            Name = null;

            // Prepare to populate new set of Skin-parameters
            SkinParam.Clear();

            // Load Skin list
            LoadSkinList();

            // Load resource
            ResourceDictionary resources = null;

            if (!string.IsNullOrEmpty(skinName))
            {
                // Load Xaml here
                string dir = About.SkinsPath;
#if DEBUG
                // Debug Only: bypass local skins to use the one in the project directly
                if (!string.IsNullOrEmpty(Global.DebugProjectDir))
                {
                    dir = Path.Combine(Global.DebugProjectDir, "Skins");
                }
#endif

                dir = Path.Combine(dir, skinName);
                dir = LimeLib.ResolvePath(dir);

                string path = Path.Combine(dir, skinName + ".xaml");
                path = LimeLib.ResolvePath(path);

                // Parse theXaml. This may fail if there is a file/Xaml error
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    // Get the root element, which must be a ResourceDictionary
                    resources = (ResourceDictionary)XamlReader.Load(fs);
                }

                // File change monitoring
                if (Global.User.SkinAutoRefresh && Global.User.DevMode)
                {
                    LimeMsg.Debug("Skin: constructor: Watch changes on {0}", dir);
                    _Watch = new FileSystemWatcher
                    {
                        Path = dir,
                        IncludeSubdirectories = false,
                        Filter = ""
                    };

                    _Watch.Changed += new FileSystemEventHandler(OnXamlFileChanged);

                    _Watch.EnableRaisingEvents = true;
                }
                else
                {
                    _Watch = null;
                }
            }

            // Freeze the Parameters
            SkinParam.Lock();


            // Retrieve/check Required resources
            double version = 0.0;
            try
            {
                MetaAuthor      = (string)resources["MetaAuthor"];
                MetaContact     = (string)resources["MetaContact"];
                MetaWebsite     = (string)resources["MetaWebsite"];
                MetaDescription = (string)resources["MetaDescription"];
                version         = (double)resources["MetaLimeVersion"];
            }
            catch (ResourceReferenceKeyNotFoundException e)
            {
                LimeMsg.Error("ErrSkinParMiss", e.Key);
            }
            catch
            {
                LimeMsg.Error("ErrSkinFormat");
            }

            if (version > 0.5)
            {
                // Successfull
                if (version > About.versionNum)
                {
                    LimeMsg.Error("ErrSkinVersion", skinName, About.name, version);
                }
            }

            // Process the Skin parameters
            var parList = SkinParam.List.ToArray();
            SkinParam.Clear();

            // Assign the key of the resource-dictonary to the SkinParam Ident.
            foreach (var key in resources.Keys)
            {
                if (key is string skey && resources[key] is SkinParam res)
                {
                    res.Ident = skey;

                    // Exclude Empty elements
                    if (res.Content == null && res.Name == null)
                    {
                        res.Visible = false;
                    }
                }
            }

            // Retrieve special parameters
            IconBigSize   = Array.Find(parList, x => x.Ident == "ParamIconBigSize");
            IconSmallSize = Array.Find(parList, x => x.Ident == "ParamIconSmallSize");

            if (IconBigSize == null)
            {
                LimeMsg.Error("ErrSkinParMiss", nameof(IconBigSize));
                return;
            }

            if (IconSmallSize == null)
            {
                LimeMsg.Error("ErrSkinParMiss", nameof(IconSmallSize));
                return;
            }


            // Remove the non-visible parameters from the list of parameters
            parList = parList.Where(x => x.Visible).ToArray();

            // Retrieve (if exists) parameters from the Configuration (settings)
            SkinParam[] configParam = null;
            if (Global.User.SkinParams != null)
            {
                Global.User.SkinParams.TryGetValue(skinName, out configParam);
            }

            // Process every paramters
            foreach (var param in parList)
            {
                // Create, reference and retrieve the property from the config (settings)
                LimeMsg.Debug("Skin: Parameter: {0} ({1})", param.Ident, param.Type);
                if (param.Content != null)
                {
                    // If no explicit name, assign the type to the Name property
                    if (param.Name == null)
                    {
                        string name = param.Type.ToString();
                        if (name != null)
                        {
                            while (name.Contains("."))
                            {
                                name = name.Substring(name.IndexOf(".") + 1);
                            }
                            param.Name = name;
                        }
                    }

                    // Default description
                    if (param.Desc == null)
                    {
                        param.Desc = "Skin Parameter: " + param.Name;
                    }

                    // Load Setting
                    if (configParam != null && loadParam)
                    {
                        SkinParam match = Array.Find(configParam, x => x.Ident == param.Ident);
                        if (match != null)
                        {
                            string val = match.Serialize;
                            LimeMsg.Debug("Skin: Parameter: Load {0} = {1}", param.Ident, val);
                            try
                            {
                                param.Serialize = val;
                            }
                            catch
                            {
                                if (Global.User.DevMode)
                                {
                                    LimeMsg.Error("ErrSkinParValue", param.Ident, val);
                                }
                            }
                        }
                    }

                    // Monitor this property for modification
                    param.PropertyChangedWeak += SkinParamPropertyChanged;
                }
            }

            // Monitor the user-properties Scaled and EnableTypeScaled
            Global.User.PropertyChangedWeak  += IconSizePropertyChanged;
            Global.Local.PropertyChangedWeak += IconSizePropertyChanged;

            // Apply the skin
            Application.Current.Resources = resources;
            Commands.MainWindow.Style     = (Style)Commands.MainWindow.FindResource(typeof(Window));


            // Validate the skin
            Name = skinName;

            // Make the parameters visible (binding)
            Parameters = parList;

            // Parameter are considered as modified when reset to default
            if (!loadParam && configParam != null)
            {
                Global.User.Modified = true;
            }
        }