Beispiel #1
0
        public void Execute()
        {
            var builder = new StringBuilder();

            builder.AppendLine("Diagnostics.Execute()");
            builder.AppendLine(new string('-', 80));

            builder.AppendLine($"Addin path: {Assembly.GetExecutingAssembly().Location}");
            builder.AppendLine($"Data path.: {PathFactory.GetAppDataPath()}");
            builder.AppendLine($"Log path..: {(logger as Logger).LogPath}");
            builder.AppendLine();

            using (var manager = new ApplicationManager())
            {
                var(backupPath, defaultPath, unfiledPath) = manager.GetLocations();
                builder.AppendLine($"Default path: {defaultPath}");
                builder.AppendLine($"Backup  path: {backupPath}");
                builder.AppendLine($"Unfiled path: {unfiledPath}");
                builder.AppendLine();

                var(pageName, pagePath, pageLink) = manager.GetCurrentPageInfo();
                builder.AppendLine($"Page name: {pageName}");
                builder.AppendLine($"Page path: {pagePath}");
                builder.AppendLine($"Page link: {pageLink}");
                builder.AppendLine();

                var app = manager.Application;

                var win = app.Windows.CurrentWindow;

                builder.AppendLine($"CurrentNotebookId: {win.CurrentNotebookId}");
                builder.AppendLine($"CurrentPageId....: {win.CurrentPageId}");
                builder.AppendLine($"CurrentSectionId.: {win.CurrentSectionId}");
                builder.AppendLine($"CurrentSecGrpId..: {win.CurrentSectionGroupId}");
                builder.AppendLine($"DockedLocation...: {win.DockedLocation}");
                builder.AppendLine($"IsFullPageView...: {win.FullPageView}");
                builder.AppendLine($"IsSideNote.......: {win.SideNote}");
                builder.AppendLine();

                builder.AppendLine($"Windows ({app.Windows.Count})");
                var currentHandle = manager.Application.Windows.CurrentWindow.WindowHandle;

                var e = app.Windows.GetEnumerator();
                while (e.MoveNext())
                {
                    var window = e.Current as One.Window;

                    var threadId = Native.GetWindowThreadProcessId(
                        (IntPtr)window.WindowHandle,
                        out var processId);

                    builder.Append($"- window [processId:{processId}, threadId:{threadId}]");
                    builder.Append($" handle:{window.WindowHandle:x} active:{window.Active}");

                    if (window.WindowHandle == currentHandle)
                    {
                        builder.AppendLine(" (current)");
                    }
                }

                builder.AppendLine();

                var page           = new Page(manager.CurrentPage());
                var pageColor      = page.GetPageColor(out _, out _);
                var pageBrightness = pageColor.GetBrightness();

                builder.AppendLine($"Page background: {pageColor.ToRGBHtml()}");
                builder.AppendLine($"Page brightness: {pageBrightness}");
                builder.AppendLine($"Page is dark...: {pageBrightness < 0.5}");

                builder.AppendLine(new string('-', 80));

                logger.WriteLine(builder.ToString());
            }
        }
Beispiel #2
0
        public void AddFavorite()
        {
            XElement root;

            if (File.Exists(path))
            {
                root = XElement.Load(path, LoadOptions.None);
            }
            else
            {
                root = MakeMenuRoot();
            }

            using (var manager = new ApplicationManager())
            {
                var info = manager.GetCurrentPageInfo();

                var name = info.Name;
                if (name.Length > 50)
                {
                    name = name.Substring(0, 50) + "...";
                }

                // similar to mongo ObjectId, a random-enough identifier for our needs
                var id = ((DateTimeOffset.Now.ToUnixTimeSeconds() << 32)
                          + new Random().Next()).ToString("x");

                root.Add(new XElement(ns + "splitButton",
                                      new XAttribute("id", $"omFavorite{id}"),
                                      new XElement(ns + "button",
                                                   new XAttribute("id", $"omFavoriteLink{id}"),
                                                   new XAttribute("onAction", "NavigateToFavorite"),
                                                   new XAttribute("imageMso", "FileLinksToFiles"),
                                                   new XAttribute("label", name),
                                                   new XAttribute("tag", info.Link),
                                                   new XAttribute("screentip", info.Path)
                                                   ),
                                      new XElement(ns + "menu",
                                                   new XAttribute("id", $"omFavoriteMenu{id}"),
                                                   new XElement(ns + "button",
                                                                new XAttribute("id", $"omFavoriteRemoveButton{id}"),
                                                                new XAttribute("onAction", "RemoveFavorite"),
                                                                new XAttribute("label", "Remove this item"),
                                                                new XAttribute("imageMso", "HyperlinkRemove"),
                                                                new XAttribute("tag", $"omFavorite{id}")
                                                                )
                                                   )
                                      ));

                // sort by name/label
                var items =
                    from e in root.Elements(ns + "splitButton")
                    let key = e.Element(ns + "button").Attribute("label").Value
                              orderby key
                              select e;

                root = MakeMenuRoot();
                foreach (var item in items)
                {
                    root.Add(item);
                }

                logger.WriteLine($"Saving favorite '{info.Path}' ({info.Link})");
            }

            try
            {
                PathFactory.EnsurePathExists(PathFactory.GetAppDataPath());
                root.Save(path, SaveOptions.None);

                ribbon.InvalidateControl("OneMoreFavoritesMenu");
            }
            catch (Exception exc)
            {
                logger.WriteLine($"Cannot save {path}");
                logger.WriteLine(exc);
            }
        }