예제 #1
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                FileIcon?.Dispose();
                PreviewImage?.Dispose();
            }

            base.Dispose(disposing);
        }
예제 #2
0
        /// <summary>
        /// Returns the string name of the supplied icon.
        /// </summary>
        /// <param name="icon">
        /// A <see cref="GLib.Icon"/>
        /// </param>
        /// <returns>
        /// A <see cref="System.String"/>
        /// </returns>
        public string IconFromGIcon(GLib.Icon icon)
        {
            string name = "";

            if (icon is ThemedIcon)
            {
                ThemedIcon themeIcon = new ThemedIcon(icon.Handle);
                // if the icon exists in the theme, this will return the relevent icon
                if (themeIcon.Names.Any())
                {
                    name = themeIcon.Names.FirstOrDefault(n => IconTheme.Default.HasIcon(n));
                }
                themeIcon.Dispose();
            }
            else if (icon is FileIcon)
            {
                // in some cases, devices provide their own icon.  This will use the device icon.
                FileIcon iconFile = new FileIcon(icon.Handle);
                name = iconFile.File.Path;
                iconFile.Dispose();
            }
            return(name);
        }
        /// <summary>
        /// Shows a native Gtk notification. For best results, you shows initialize a
        /// <seealso cref="GLib.Application"/>
        /// with the correct application id. Also, gnome-shell requires a desktop file
        /// whose base name matches the application id for the notification to show.
        /// </summary>
        /// <param name="toast">ToastNotification as created in the native library.</param>
        /// <returns></returns>
        public static async Task Show(this ToastNotification toast)
        {
            var notification = new Notification(toast.Title);

            notification.Body = toast.Message;

            string path = null;

            if (toast.AppLogoOverride != null)
            {
                var stream = await toast.AppLogoOverride.GetStreamAsync().ConfigureAwait(false);

                path = Path.GetTempFileName();
                var fileStream = File.OpenWrite(path);
                stream.CopyTo(fileStream);
                stream.Dispose();
                fileStream.Dispose();

                var      fileInfo = FileFactory.NewForPath(path);
                FileIcon icon     = new FileIcon(fileInfo);

                notification.Icon = icon;

                icon.Dispose();
            }

            Guid id;

            if (ids.ContainsKey(toast))
            {
                id = ids[toast];
            }
            else
            {
                id = Guid.NewGuid();
                ids.Add(toast, id);
                notifications.Add(id, toast);
            }

            await InvokeAsync(() =>
            {
                if (Application.Default == null)
                {
                    var application = new Application(Windows.ApplicationModel.Package.Current.DisplayName + ".Skia.Gtk", ApplicationFlags.None);
                    application.Register(null);
                }
                Application.Default.SendNotification(id.ToString(), notification);
                notification.Dispose();

                // Gtk notifications do not automatically close, so we can decide the time here.
                var waitTime = toast.ToastDuration == ToastDuration.Short ? 7000 : 25000;
                Thread.Sleep(waitTime);
                Application.Default.WithdrawNotification(id.ToString());

                // Gnome uses this temporary file to display the icon. We must not delete this file
                // before the notification withdraws.
                if (path != null)
                {
                    File.Delete(path);
                }
            }).ConfigureAwait(false);
        }