Example #1
0
        public Taskbar()
        {
            var pinned = LoadPinnedItems().ToList();
            var visibleWindowsHandles = LoadTopLevelWindows().ToList();

            Items = new List <ApplicationDockItem>();

            foreach (var item in pinned)
            {
                var entry    = DesktopEntryManager.FromFile(item);
                var dockItem = new ApplicationDockItem(entry);
                dockItem.Pinned = true;
                Items.Add(dockItem);
            }

            foreach (var hWnd in visibleWindowsHandles)
            {
                var  id    = AppUserModelId.Find(hWnd).Where(a => File.Exists(a.DestinationList)).FirstOrDefault();
                bool match = false;

                if (id != null)
                {
                    foreach (var item in Items)
                    {
                        if (item.AppId != null && item.AppId.Id == id.Id)
                        {
                            item.RegisterWindowHandle(hWnd);
                            match = true;
                            break;
                        }
                    }
                }

                if (!match)
                {
                    var dockItem = new ApplicationDockItem(hWnd);
                    //Items.Add(dockItem);
                }
            }
        }
Example #2
0
        private void LoadIcons()
        {
            // Load application shortcuts from existing folder if possible
            if (Directory.Exists(PinnedIconsFolder) && Directory.GetFiles(PinnedIconsFolder).Where(s => new FileInfo(s).Attributes.HasFlag(FileAttributes.Hidden) == false).Count() > 0)
            {
                foreach (var appIconShortcutFile in Directory.GetFiles(PinnedIconsFolder))
                {
                    var entry = DesktopEntryManager.FromFile(appIconShortcutFile);
                    try
                    {
                        var possibleAppIds = AppUserModelId.Find(entry.TryExec);
                        var appId          = possibleAppIds.FirstOrDefault(a => File.Exists(a.DestinationList)) ?? possibleAppIds.First();

                        if (appId != null)
                        {
                            var item = new ApplicationDockItem(entry);
                            items.Add(item);
                            OnItemsChanged(this, ItemsChangedEventArgs <DockItem> .BuildAddedEvents(new List <DockItem> {
                                item
                            }));
                        }
                    }
                    catch { }
                }
            }
            else // No existing items found, try to load icons from the Taskbar
            {
                using (var taskbar = new Taskbar())
                {
                    items.AddRange(taskbar.Items);
                }
            }

            hook = new Hook();
            var timer = new System.Timers.Timer(100);

            timer.Elapsed += (object o, System.Timers.ElapsedEventArgs e) =>
            {
                while (hook.HasCreatedWindow())
                {
                    var hwnd    = hook.PopCreatedWindow();
                    var process = Process.GetProcesses().Where(p => p.MainWindowHandle == hwnd).SingleOrDefault();
                    if (process == null)
                    {
                        continue;
                    }
                    foreach (var item in Items)
                    {
                        if (((ApplicationDockItem)item).DesktopEntry.TryExec == process.MainModule.FileName)
                        {
                            ((ApplicationDockItem)item).RegisterWindowHandle(hwnd);
                            break;
                        }
                    }
                }

                while (hook.HasDestroyedWindow())
                {
                    var hwnd = hook.PopDestroyedWindow();
                    foreach (var item in Items)
                    {
                        if (((ApplicationDockItem)item).HasRegisteredWindowHandle(hwnd))
                        {
                            ((ApplicationDockItem)item).UnregisterWindowHandle(hwnd);
                            break;
                        }
                    }
                }
            };
            timer.Enabled = true;
        }