コード例 #1
0
ファイル: LayoutService.cs プロジェクト: holzkeil/Hawkeye2
        /// <summary>
        ///     Updates the bounds and state information of the form identified by
        ///     the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        private void UpdateForm(string key)
        {
            if (EventsLocked)
            {
                return;
            }

            if (!IsLoaded(key))
            {
                return;
            }

            using (SuspendEvents())
            {
                if (!_forms.ContainsKey(key))
                {
                    return;
                }

                Form           form   = _forms[key];
                FormLayoutData layout = _layouts[key];

                layout.WindowState = form.WindowState;
                layout.Bounds      = form.Bounds;
                layout.ScreenName  = Screen.FromControl(form).DeviceName;
            }
        }
コード例 #2
0
ファイル: LayoutService.cs プロジェクト: holzkeil/Hawkeye2
        /// <summary>
        ///     Saves the bounds and state information of the form identified by the
        ///     specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        private void UnloadForm(string key)
        {
            UpdateForm(key);

            if (_layouts.ContainsKey(key))
            {
                FormLayoutData layout = _layouts[key];
                Form           form   = _forms[key];
                if (form is IAdditionalLayoutDataProvider provider)
                {
                    layout.AdditionalData = provider.GetAdditionalLayoutData();
                }
            }

            SaveLayouts();

            _loadedList.Remove(key);
        }
コード例 #3
0
ファイル: LayoutService.cs プロジェクト: holzkeil/Hawkeye2
        private void SaveLayouts(XmlNode rootLayoutsNode)
        {
            XmlDocument doc = rootLayoutsNode is XmlDocument document ? document : rootLayoutsNode.OwnerDocument;

            XmlNode FindLayoutNode(string k)
            {
                IEnumerable <XmlNode> layoutNodes =
                    rootLayoutsNode.ChildNodes.Cast <XmlNode>().Where(n => n.Name == "layout");

                foreach (XmlNode node in layoutNodes)
                {
                    XmlAttribute xaKey = node.Attributes.Cast <XmlAttribute>().SingleOrDefault(a => a.Name == "key");
                    if (xaKey.Value == k)
                    {
                        return(node);
                    }
                }

                return(null);
            }

            foreach (string key in _layouts.Keys)
            {
                XmlNode xn = FindLayoutNode(key);
                if (xn == null)
                {
                    xn = doc.CreateElement("layout");
                    XmlAttribute xa = doc.CreateAttribute("key");
                    xa.Value = key;
                    xn.Attributes.Append(xa);
                }

                // mandatory, if not present, skip this node
                FormLayoutData layout = _layouts[key];
                if (layout == null)
                {
                    continue;
                }

                if (layout.Bounds.HasValue)
                {
                    string  boundsValue = ConvertToString(layout.Bounds);
                    XmlNode xnBounds    = xn.ChildNodes.Cast <XmlNode>().SingleOrDefault(n => n.Name == "bounds");
                    if (xnBounds != null)
                    {
                        xnBounds.Attributes["value"].Value = boundsValue;
                    }
                    else
                    {
                        xnBounds = xn.AppendChild(doc.CreateElement("bounds"));
                        XmlAttribute xaBoundsValue = doc.CreateAttribute("value");
                        xaBoundsValue.Value = boundsValue;
                        xnBounds.Attributes.Append(xaBoundsValue);
                    }
                }

                if (layout.WindowState.HasValue)
                {
                    string  stateValue = ConvertToString(layout.WindowState);
                    XmlNode xnState    = xn.ChildNodes.Cast <XmlNode>().SingleOrDefault(n => n.Name == "state");
                    if (xnState != null)
                    {
                        xnState.Attributes["value"].Value = stateValue;
                    }
                    else
                    {
                        xnState = xn.AppendChild(doc.CreateElement("state"));
                        XmlAttribute xaStateValue = doc.CreateAttribute("value");
                        xaStateValue.Value = stateValue;
                        xnState.Attributes.Append(xaStateValue);
                    }
                }

                if (!string.IsNullOrEmpty(layout.ScreenName))
                {
                    string  screenValue = layout.ScreenName;
                    XmlNode xnScreen    = xn.ChildNodes.Cast <XmlNode>().SingleOrDefault(n => n.Name == "screen");
                    if (xnScreen != null)
                    {
                        xnScreen.Attributes["value"].Value = screenValue;
                    }
                    else
                    {
                        xnScreen = xn.AppendChild(doc.CreateElement("screen"));
                        XmlAttribute xaScreenValue = doc.CreateAttribute("value");
                        xaScreenValue.Value = screenValue;
                        xnScreen.Attributes.Append(xaScreenValue);
                    }
                }

                if (layout.AdditionalData != null)
                {
                    XmlNode xnAdditionalData =
                        xn.ChildNodes.Cast <XmlNode>().SingleOrDefault(n => n.Name == "additionalData") ??
                        xn.AppendChild(doc.CreateElement("additionalData"));
                    xnAdditionalData.InnerText = layout.AdditionalData;
                }

                rootLayoutsNode.AppendChild(xn);
            }

            // Regular settings have already been saved, but we happen after that. So, save once again
            SettingsManager.Save();
        }
コード例 #4
0
ファイル: LayoutService.cs プロジェクト: holzkeil/Hawkeye2
        /// <summary>
        ///     Restores the state and bounds of the form identified by the
        ///     specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        private void LoadForm(string key)
        {
            if (IsLoaded(key))
            {
                return;
            }

            using (SuspendEvents())
            {
                if (!_forms.ContainsKey(key))
                {
                    return;
                }

                Form   form          = _forms[key];
                Screen defaultScreen = Screen.PrimaryScreen;

                if (_layouts.ContainsKey(key))
                {
                    FormLayoutData layout = _layouts[key];

                    // Determine screen
                    string screenName = layout.ScreenName;
                    Screen screen     = defaultScreen;
                    if (!string.IsNullOrEmpty(screenName))
                    {
                        screen =
                            Screen.AllScreens.SingleOrDefault(s => s.DeviceName == screenName) ?? defaultScreen;
                    }

                    if (layout.Bounds.HasValue)
                    {
                        form.StartPosition = FormStartPosition.Manual;
                        Rectangle bounds = layout.Bounds.Value;
                        if (!screen.WorkingArea.Contains(bounds))
                        {
                            SetDefaultLayout(form);
                        }
                        else
                        {
                            form.Bounds = bounds;
                        }
                    }
                    else
                    {
                        Log.Verbose($"Could not find a 'bounds' value for layout key {key}");
                    }

                    if (layout.WindowState.HasValue)
                    {
                        FormWindowState state = layout.WindowState.Value;
                        if (state == FormWindowState.Minimized)
                        {
                            form.WindowState = FormWindowState.Normal;
                        }
                        else
                        {
                            form.WindowState = state;
                        }
                    }
                    else
                    {
                        // default to Normal
                        form.WindowState = FormWindowState.Normal;
                        Log.Verbose($"Could not find a 'state' value for layout key {key}");
                    }

                    // If state is Normal and we have no bounds, use DefaultBounds and center on screen
                    if (!layout.Bounds.HasValue && form.WindowState == FormWindowState.Normal)
                    {
                        SetDefaultLayout(form);
                    }
                }
                else
                {
                    SetDefaultLayout(form);
                    _layouts.Add(key, new FormLayoutData(form));
                }

                form.SizeChanged     += (s, e) => UpdateForm(key);
                form.LocationChanged += (s, e) => UpdateForm(key);
                form.FormClosed      += (s, e) => UnloadForm(key);

                _loadedList.Add(key);

                UpdateForm(key);

                // Even if no additional data was saved, call SetData.
                if (form is IAdditionalLayoutDataProvider)
                {
                    ((IAdditionalLayoutDataProvider)form).SetAdditionalLayoutData(_layouts[key].AdditionalData);
                }
            }
        }
コード例 #5
0
ファイル: LayoutService.cs プロジェクト: holzkeil/Hawkeye2
        private void LoadLayout(XmlNode layoutNode, string key)
        {
            if (_layouts.ContainsKey(key))
            {
                return;
            }

            var layoutData = new FormLayoutData();

            foreach (XmlNode xn in layoutNode.ChildNodes)
            {
                switch (xn.Name)
                {
                case "bounds":
                    string bounds = GetNodeValue(xn);
                    if (bounds != null)
                    {
                        try
                        {
                            layoutData.Bounds = ConvertTo <Rectangle>(bounds);
                        }
                        catch (Exception ex)
                        {
                            Log.Warning($"Invalid 'bounds' value for key {key}: {bounds}", ex);
                            layoutData.Bounds = null;
                        }
                    }
                    else
                    {
                        Log.Warning($"'bounds' value for key {key} was not found.");
                        layoutData.Bounds = null;
                    }

                    break;

                case "state":
                    string state = GetNodeValue(xn);
                    if (state != null)
                    {
                        try
                        {
                            layoutData.WindowState = ConvertTo <FormWindowState>(state);
                        }
                        catch (Exception ex)
                        {
                            Log.Warning($"Invalid 'state' value for key {key}: {2}", ex);
                            layoutData.WindowState = null;
                        }
                    }
                    else
                    {
                        Log.Warning($"'state' value for key {key} was not found.");
                        layoutData.WindowState = null;
                    }

                    break;

                case "screen":
                    layoutData.ScreenName = GetNodeValue(xn);
                    break;

                case "additionalData":
                    layoutData.AdditionalData = xn.InnerText;
                    break;
                }
            }

            _layouts.Add(key, layoutData);
        }