Example #1
0
        public override void DrawRect(CGRect dirtyRect)
        {
            CGContext context = NSGraphicsContext.CurrentContext.GraphicsPort;

            context.SetFillColor(__BackgroundColor);
            context.FillRect(dirtyRect);

            base.DrawRect(dirtyRect);

            if (BorderLineWidth > 0)
            {
                NSBezierPath bounds = new NSBezierPath();
                bounds.AppendPathWithRect(dirtyRect);
                bounds.AddClip();

                bounds.LineWidth = BorderLineWidth;

                if (BorderColor != null)
                {
                    BorderColor.SetStroke();
                }

                bounds.Stroke();
            }
        }
        public override void DrawRect(CGRect dirtyRect)
        {
            base.DrawRect(dirtyRect);

            if (!Colors.IsDarkMode)
            {
                NSBezierPath betterBounds = new NSBezierPath();
                betterBounds.AppendPathWithRoundedRect(dirtyRect, CornerRadius, CornerRadius);
                betterBounds.AddClip();

                betterBounds.LineWidth = BorderLineWidth;

                if (BorderColor != null)
                {
                    BorderColor.SetStroke();
                }

                betterBounds.Stroke();
            }
        }
        public static void DrawUISwitch(CGRect dirtyRect, bool isEnabled, bool isSwitchOn,
                                        int BorderWidth,
                                        NSColor SwitchOnBackgroundColor,
                                        NSColor SwitchOffBackgroundColor,
                                        NSColor SwitchOffBorderColor,
                                        NSColor InternalSwitcherColor,
                                        NSColor InternalSwitcherShadowColor
                                        )
        {
            NSGraphicsContext context = NSGraphicsContext.CurrentContext;

            context.CGContext.SaveState();


            NSColor switchOnBackgroundColor     = SwitchOnBackgroundColor;
            NSColor switchOffBackgroundColor    = SwitchOffBackgroundColor;
            NSColor switchOffBorderColor        = SwitchOffBorderColor;
            NSColor internalSwitcherColor       = InternalSwitcherColor;
            NSColor internalSwitcherShadowColor = InternalSwitcherShadowColor;

            if (isEnabled == false)
            {
                // if disabled - set background color darker
                switchOnBackgroundColor     = SetColorDarker(switchOnBackgroundColor);
                switchOffBackgroundColor    = SetColorDarker(switchOffBackgroundColor);
                switchOffBorderColor        = SetColorDarker(switchOffBorderColor);
                internalSwitcherColor       = SetColorDarker(internalSwitcherColor);
                internalSwitcherShadowColor = SetColorDarker(internalSwitcherShadowColor);
            }

            nfloat offset = dirtyRect.Height * 0.1f;

            dirtyRect = new CGRect(dirtyRect.X + offset,
                                   dirtyRect.Y + offset,
                                   dirtyRect.Width - offset * 2,
                                   dirtyRect.Height - offset * 2);

            // set backgrund color
            NSColor bodyColor = (isSwitchOn)? switchOnBackgroundColor : switchOffBackgroundColor;

            bodyColor.SetFill();

            // draw background
            NSBezierPath bodyPath = NSBezierPath.FromRoundedRect(dirtyRect, dirtyRect.Height / 2, dirtyRect.Height / 2);

            bodyPath.Fill();

            // draw border
            if (!isSwitchOn)
            {
                bodyPath.AddClip();
                bodyPath.LineWidth = BorderWidth;

                if (switchOffBorderColor != null)
                {
                    switchOffBorderColor.SetStroke();
                }
                bodyPath.Stroke();
            }

            //restore \ save context status
            context.CGContext.RestoreState();
            context.CGContext.SaveState();

            // DRAW CIRCLE
            CGRect circleRect;

            if (!isSwitchOn)
            {
                circleRect = new CGRect(dirtyRect.X,
                                        dirtyRect.Y,
                                        dirtyRect.Height,
                                        dirtyRect.Height);
            }
            else
            {
                circleRect = new CGRect((dirtyRect.Width - dirtyRect.Height + dirtyRect.X),
                                        dirtyRect.Y,
                                        dirtyRect.Height,
                                        dirtyRect.Height);
            }

            // draw circle with shadow (no shadow for dark mode)
            if (!Colors.IsDarkMode)
            {
                CGRect circleShadowRect = new CGRect(circleRect.X + offset / 3, circleRect.Y + offset / 3, circleRect.Height - 2 * offset / 3, circleRect.Height - 2 * offset / 3);

                NSBezierPath circleShadowPath = NSBezierPath.FromRoundedRect(circleShadowRect, circleShadowRect.Height / 2, circleShadowRect.Height / 2);
                context.CGContext.SetShadow(new CGSize(offset / 3, -offset),
                                            offset * 1f,
                                            new CGColor(internalSwitcherShadowColor.RedComponent,
                                                        internalSwitcherShadowColor.GreenComponent,
                                                        internalSwitcherShadowColor.BlueComponent));
                circleShadowPath.Fill();
            }

            // restore context state
            context.CGContext.RestoreState();

            // set circle color
            NSColor circleColor = internalSwitcherColor;

            circleColor.SetFill();

            // draw circle without shadow to fill internal area filled by shadow
            NSBezierPath circlePath = NSBezierPath.FromRoundedRect(circleRect, circleRect.Height / 2, circleRect.Height / 2);

            circlePath.Fill();

            // circle border
            circlePath.AddClip();
            circlePath.LineWidth = 1;

            if (Colors.IsDarkMode)
            {
                // no border for Dark mode
                internalSwitcherColor.SetStroke();
            }
            else
            {
                if (switchOffBorderColor != null)
                {
                    switchOffBorderColor.SetStroke();
                }
            }
            circlePath.Stroke();
        }
Example #4
0
        public override void DrawRect(CGRect dirtyRect)
        {
            if (Gradient != null)
            {
                Gradient.DrawInRect(dirtyRect, GradientAngle);
            }

            // -----------------------------------------
            nfloat textOffset   = 0f;
            nfloat imageXOffset = 0f;
            nfloat imageWidth   = 0f;

            if (Image != null)
            {
                if (Image.Size.Height > dirtyRect.Height)
                {
                    Image.Size = new CGSize(Image.Size.Height, dirtyRect.Height);
                }
                if (Image.Size.Width > dirtyRect.Width)
                {
                    Image.Size = new CGSize(dirtyRect.Width, Image.Size.Width);
                }

                imageWidth = Image.Size.Width;

                nfloat minXOffset = (dirtyRect.Height - Image.Size.Height) / 2f;

                imageXOffset = CornerRadius;
                if (imageXOffset < minXOffset)
                {
                    imageXOffset = minXOffset;
                }

                if (AttributedTitle == null)
                {
                    imageXOffset = dirtyRect.Width / 2f - Image.Size.Width / 2f;
                }
                else if (IconLocation == IconLocationEnum.Right_BeforeCenteredText)
                {
                    imageXOffset = dirtyRect.Width / 2f - AttributedTitle.Size.Width / 2f - Image.Size.Width - minXOffset / 2f;
                }
                else if (IconLocation == IconLocationEnum.Left_AfterCenteredText)
                {
                    imageXOffset = dirtyRect.Width / 2f + AttributedTitle.Size.Width / 2f + minXOffset / 2f;
                }
                else if (IconLocation == IconLocationEnum.Right_AfterCenteredText)
                {
                    imageXOffset = dirtyRect.Width - minXOffset / 2f - Image.Size.Width;
                }
                else if (IconLocation == IconLocationEnum.Right)
                {
                    nfloat space = (dirtyRect.Width - AttributedTitle.Size.Width - Image.Size.Width) / 3;
                    textOffset   = space;
                    imageXOffset = space + AttributedTitle.Size.Width + space;
                }
                else if (IconLocation == IconLocationEnum.Left)
                {
                    nfloat space = (dirtyRect.Width - AttributedTitle.Size.Width - Image.Size.Width) / 3;
                    imageXOffset = space;
                    textOffset   = space + Image.Size.Width + space;
                }

                CGRect imgRect = new CGRect(dirtyRect.X + imageXOffset,
                                            dirtyRect.Y + dirtyRect.Height / 2f - Image.Size.Height / 2f,
                                            Image.Size.Width,
                                            Image.Size.Height);
                Image.Draw(imgRect);
            }

            if (AttributedTitle != null)
            {
                if (IconLocation == IconLocationEnum.Right_BeforeCenteredText || IconLocation == IconLocationEnum.Left_BeforeCenteredText)
                {
                    if ((imageXOffset + imageWidth) > (dirtyRect.Width - AttributedTitle.Size.Width) / 2f)
                    {
                        textOffset = imageXOffset + imageWidth;
                    }
                }

                CGRect titleRect = new CGRect(dirtyRect.X + textOffset,
                                              dirtyRect.Y + dirtyRect.Height / 2f - AttributedTitle.Size.Height / 2f - 1f,
                                              dirtyRect.Width - textOffset,
                                              AttributedTitle.Size.Height);

                this.AttributedTitle.DrawInRect(titleRect);
            }

            if (Highlighted)
            {
                NSColor fillColor = HighlitedColorOverlay;
                if (fillColor == null)
                {
                    fillColor = NSColor.FromRgba(0, 0, 0, 0.1f);
                }
                fillColor.Set();

                NSBezierPath highlitedFill = new NSBezierPath();
                highlitedFill.AppendPathWithRoundedRect(dirtyRect, CornerRadius, CornerRadius);
                highlitedFill.Fill();
            }

            if (!this.Enabled && DoNotChangeColorWhenDisabled != true)
            {
                NSColor fillColor = HighlitedColorOverlay;
                if (fillColor == null)
                {
                    fillColor = NSColor.FromRgba(0, 0, 0, 0.1f);
                }
                fillColor.Set();

                NSBezierPath highlitedFill = new NSBezierPath();
                highlitedFill.AppendPathWithRoundedRect(dirtyRect, CornerRadius, CornerRadius);
                highlitedFill.Fill();
            }
            // -----------------------------------------
            //base.DrawRect (dirtyRect);

            if (!Highlighted && BorderShadow != null)
            {
                BorderShadow.Set();
            }


            NSBezierPath bounds = new NSBezierPath();

            bounds.AppendPathWithRoundedRect(dirtyRect, CornerRadius, CornerRadius);
            bounds.AddClip();

            bounds.LineWidth = BorderLineWidth;

            if (BorderColor != null)
            {
                BorderColor.SetStroke();
            }

            bounds.Stroke();
        }
Example #5
0
 public virtual void AddClip()
 {
     path.AddClip();
 }