Exemplo n.º 1
0
        public static CGColor ToCG(this NSColor color)
        {
            if (color == null)
            {
                return(null);
            }
            CGColor cgColor;

            // try getting the CGColor and ensure it isn't null..
            if (MacVersion.IsAtLeast(10, 8) && (cgColor = color.CGColor) != null)
            {
                return(cgColor);
            }

            // try to convert to RGB colorspace so we can convert it to CGColor
            var converted = color.UsingColorSpaceFast(NSColorSpace.DeviceRGB);

            if (converted == null)
            {
                return(new CGColor(0, 0, 0, 1f));
            }

            // try getting the CGColor again..
            if (MacVersion.IsAtLeast(10, 8) && (cgColor = converted.CGColor) != null)
            {
                return(cgColor);
            }

            converted.GetComponents(out var components);
            return(new CGColor(converted.ColorSpace.ColorSpace, components));
        }
Exemplo n.º 2
0
        public override void AttachEvent(string id)
        {
            switch (id)
            {
            case Application.UnhandledExceptionEvent:
                AppDomain.CurrentDomain.UnhandledException += OnCurrentDomainUnhandledException;
                break;

            case Application.TerminatingEvent:
                // handled by app delegate
                break;

            case Application.NotificationActivatedEvent:
                if (MacVersion.IsAtLeast(10, 8))
                {
                    NSUserNotificationCenter.DefaultUserNotificationCenter.DidActivateNotification += (sender, e) => DidActivateNotification(e.Notification);
                    NSApplication.Notifications.ObserveDidFinishLaunching(DidFinishLaunching);
                }
                break;

            default:
                base.AttachEvent(id);
                break;
            }
        }
Exemplo n.º 3
0
        public virtual void DrawTemplateImage(GraphicsHandler graphics, RectangleF source, RectangleF destination)
        {
            var imageSize = Size;
            // draw as a template image, and ignore color data
            var ctx = graphics.Control;

            ctx.SaveState();

            RectangleF destMask;

            if (destination.Size != source.Size)
            {
                // scale and position
                var scale = destination.Size / source.Size;
                destMask = new RectangleF(destination.Location - source.Location * scale, imageSize * scale);
            }
            else
            {
                // just position
                destMask = new RectangleF(destination.Location - source.Location, imageSize);
            }

            var destRect = destination.ToNS();
            var cgImage  = GetImage().AsCGImage(ref destRect, graphics.GraphicsContext, null);

            // clip to the image as a mask, using only alpha channel
            ctx.ClipToMask(destMask.ToNS(), cgImage);

            // set fill color based on current dark/light theme
            // this is the best approximation I can find to get it to draw the same as NSImageView
            // thus far..
            NSColor color;

            if (MacVersion.IsAtLeast(10, 14) && graphics.DisplayView.HasDarkTheme())
            {
                color = NSColor.FromWhite(1f, .55f);
            }
            else
            {
                color = NSColor.FromWhite(0, .5f);
            }
            color.SetFill();

            ctx.FillRect(destRect);
            ctx.RestoreState();
        }
Exemplo n.º 4
0
        public static bool HasDarkTheme(this NSView view)
        {
            if (!MacVersion.IsAtLeast(10, 14))
            {
                return(false);
            }

            var appearance = view?.EffectiveAppearance ?? NSAppearance.CurrentAppearance;

            var name = appearance.Name;

            if (name == NSAppearance.NameDarkAqua || name == NSAppearance.NameAccessibilityHighContrastDarkAqua)
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 5
0
        public static CGColor ToCG(this NSColor color)
        {
            if (color == null)
            {
                return(null);
            }
            if (MacVersion.IsAtLeast(10, 8))
            {
                return(color.CGColor);
            }

            var converted = color.UsingColorSpaceFast(NSColorSpace.DeviceRGB);

            if (converted == null)
            {
                return(new CGColor(0, 0, 0, 1f));
            }

            converted.GetComponents(out var components);
            return(new CGColor(converted.ColorSpace.ColorSpace, components));
        }