Esempio n. 1
0
        /// <summary>Re-applies the src.</summary>
        public void UpdateSrc()
        {
            string src = null;

            // Do we have an srcset?
            if (SrcSet != null)
            {
                // Select best src from the srcset (by density only for now):
                ContentEntry ce = SrcSet.BestByDensity;

                if (ce != null)
                {
                    src = ce.Src;
                }
            }
            else
            {
                // Use src:
                src = this["src"];
            }

            if (src != null)
            {
                src = src.Trim();
            }

            if (!string.IsNullOrEmpty(src))
            {
                // Build it as a CSS url:
                src = "url(\"" + src.Replace("\"", "\\\"") + "\")";
            }

            // Set it now:
            Style.Computed.ChangeTagProperty("background-image", src);
        }
        /// <summary>Adds the given location to the group with an optional descriptor.</summary>
        public int Add(string src, string descriptor)
        {
            // 1x by default:
            float descriptorValue = 1f;
            int   descriptorType  = ContentEntry.DENSITY_DESCRIPTOR;

            if (!string.IsNullOrEmpty(descriptor))
            {
                // First, trim it:
                descriptor = descriptor.Trim();

                if (descriptor.Length > 0)
                {
                    // Get the last character - either a 'w' or 'x':
                    char end = descriptor[descriptor.Length - 1];

                    // Chop it off:
                    descriptor = descriptor.Substring(0, descriptor.Length - 1);

                    // Parse it:
                    float.TryParse(descriptor, out descriptorValue);

                    if (end == 'w')
                    {
                        // Width descriptor:
                        descriptorType = ContentEntry.WIDTH_DESCRIPTOR;
                    }
                }
            }

            // Create the entry and add it:
            ContentEntry ce = new ContentEntry();

            ce.Src        = src;
            ce.Type       = descriptorType;
            ce.Descriptor = descriptorValue;

            Entries.Add(ce);

            return(Entries.Count - 1);
        }
        /// <summary>Selects the most suitable entry for the given density/ width descriptor.</summary>
        public ContentEntry MostSuitable(float descriptor)
        {
            ContentEntry current       = null;
            float        acceptedDelta = 0f;

            // For each entry..
            for (int i = 0; i < Entries.Count; i++)
            {
                // Get the entry:
                ContentEntry entry = Entries[i];

                // The difference between this device and the entry:
                float ratioDelta = entry.Descriptor - descriptor;

                if (ratioDelta == 0f)
                {
                    // Insta-win:
                    return(entry);
                }

                // We want the ratioDelta that is closest to zero and ideally positive.

                // So, this one 'wins' if any of these is true:
                // - It's the first one
                // - ratioDelta is +ve and acceptedDelta is -ve
                // - ratioDelta is closer to zero than acceptedDelta

                if (
                    current == null ||
                    (ratioDelta > 0f && acceptedDelta < 0f) ||
                    Math.Abs(ratioDelta) < Math.Abs(acceptedDelta)
                    )
                {
                    current       = entry;
                    acceptedDelta = ratioDelta;
                }
            }

            return(current);
        }