Пример #1
0
        private bool ExecuteLine(Rainmeter.Settings.InstanceSettings Instance, string sLine)
        {
            // If this line contains a $UserInput$ token, then we need to do some extra
            // parsing
            if (sLine.ToUpper().Contains("$USERINPUT$"))
            {
                try
                {
                    #region Handle in-line overrides
                    // Create a blank list of overrides
                    Dictionary <string, string> Overrides = new Dictionary <string, string>();

                    // Start looking for overridable settings and adjust the list accordingly,
                    // stripping out settings from the line if they are discovered.
                    //
                    // The supporting TagData() function allows for whitespace if quotes are
                    // used.  For example:
                    //
                    // DefaultValue="hello there, how are you"
                    sLine = ScanAndReplace(sLine, "DefaultValue", ref Overrides);
                    sLine = ScanAndReplace(sLine, "X", ref Overrides);
                    sLine = ScanAndReplace(sLine, "Y", ref Overrides);
                    sLine = ScanAndReplace(sLine, "W", ref Overrides);
                    sLine = ScanAndReplace(sLine, "H", ref Overrides);
                    sLine = ScanAndReplace(sLine, "StringStyle", ref Overrides);
                    sLine = ScanAndReplace(sLine, "StringAlign", ref Overrides);
                    sLine = ScanAndReplace(sLine, "FocusDismiss", ref Overrides);
                    sLine = ScanAndReplace(sLine, "FontColor", ref Overrides);
                    sLine = ScanAndReplace(sLine, "FontFace", ref Overrides);
                    sLine = ScanAndReplace(sLine, "SolidColor", ref Overrides);
                    sLine = ScanAndReplace(sLine, "Password", ref Overrides);
                    sLine = ScanAndReplace(sLine, "FontSize", ref Overrides);
                    sLine = ScanAndReplace(sLine, "TopMost", ref Overrides);
                    #endregion

                    // Get user input
                    string sInput = GetUserInput(Instance, Overrides);
                    if (sInput == null)
                    {
                        // Rainmeter.Log(Rainmeter.LogLevel.Debug, "InputText: Aborted, user cancelled text box");
                        return(false);
                    }

                    // Swap out the $UserInput$ token with what the user typed
                    sLine = Replace(sLine, "$USERINPUT$", sInput);
                }
                catch (Exception ex)
                {
                    // If there was an error doing any of the above, log it for debugging purposes.
                    Rainmeter.Log(Rainmeter.LogLevel.Warning, "InputText: Exception " + ex.GetType().ToString() + ": " + ex.Message);
                    return(false);
                }
            }

            // Execute the bang.  This will either be the original line from CommandX= (sLine variable) or
            // an adjusted line based on special parsing above.
            // Rainmeter.Log(Rainmeter.LogLevel.Debug, "InputText: Executing bang: " + sLine);
            Rainmeter.Bang(sLine);
            return(true);
        }
Пример #2
0
        // 'ExecuteBang' is a way of Rainmeter telling your plugin to do something *right now*.
        // What it wants to do can be defined by the 'Command' parameter.
        public void ExecuteBang(Rainmeter.Settings.InstanceSettings Instance, string Command)
        {
            #region Handle a single parameter

            // If our parameter list only contains a single word, then open a textbox immediately
            // and set a value.  This mode does not do any batching.
            if (!Command.Trim().Contains(" "))
            {
                // Assume that the parameter is the name of the variable
                string sVariableName = Command.Trim();

                // Ask for input
                string sInput = GetUserInput(Instance);

                // If the user cancelled out of the inputbox (ESC key, etc.), then abort
                if (sInput == null)
                {
                    return;
                }

                // Ask Rainmeter to set the variable using a bang (http://rainmeter.net/RainCMS/?q=Bangs)
                Rainmeter.Bang("!RainmeterSetVariable " + sVariableName + " \"" + sInput.Replace("\"", "\\\"") + "\"");

                // Note that the skin needs DynamicVariables=1 in the measure's settings or the above
                // code will have no effect.
                return;
            }

            #endregion
            #region Handle multiple parameters

            // Our parameter list contains at least two words, so split them up
            string[] sParts = Command.Trim().Split(new string[] { " " }, StringSplitOptions.None);

            // If the first parameter is 'ExecuteBatch' (not case sensitive)...
            if (sParts[0].Trim().ToUpper() == "EXECUTEBATCH")
            {
                // ExecuteBatch tells this plugin to go through the measure's settings to look
                // for lines beginning with "Command" and executing Rainmeter bangs for each one.
                // If a line contains $UserInput$, then an input textbox is opened and command
                // execution pauses until the user enters text or dismisses the textbox.  If the
                // textbox is dismissed (Escape key, for example), all processing ends, otherwise
                // it continues depending on the range of commands selected.
                //
                // Each "Command" line allows overriding all settings that the input textbox
                // supports, therefor some checking and substitution is performed, thus a
                // more complex parser has been implemented.
                //
                // ExecuteBatch expects this syntax:
                //       ExecuteBatch [All|#|#-#]
                //
                // This allows Rainmeter to call the plugin to execute a range including:
                //       All      All commands in the measure
                //       #        Only a single command in the measure
                //       #-#      A specific range of commands in the measure

                #region Determine range
                // Determine range.  Default is 1 to 1,000,000,000, although if processing finds
                // that a requested line is blank, it will stop all processing (so 'All' will
                // only parse 14 lines if "Command15" does not exist or is blank).
                int iMin = 1;
                int iMax = 1000000000;
                try
                {
                    if (sParts[1].Trim().ToUpper() != "ALL")
                    {
                        if (sParts[1].Contains("-"))
                        {
                            string[] sSubParts = sParts[1].Split(new string[] { "-" }, StringSplitOptions.None);
                            iMin = int.Parse(sSubParts[0]);
                            iMax = int.Parse(sSubParts[1]);
                        }
                        else
                        {
                            iMin = iMax = int.Parse(sParts[1]);
                        }
                    }
                }
                catch // handle all errors above
                {
                    // Any error above will be ignored and the default range used instead.
                    // This can occur if the measure asks to ExecuteBatch an invalid range
                    // or the range could not be translated to an acceptable format.
                    //
                    // For example:  ExecuteBatch asdf
                    iMin = 1;
                    iMax = 1000000000;
                }
                #endregion
                #region Parse commands in range
                // Parse each command in the range, aborting if any line returns 'false' or
                // the requested command line does not exist in the config for that measure.
                for (int i = iMin; i <= iMax; i++)
                {
                    // Read this command's line
                    string sCurrentLine = Instance.INI_Value("Command" + i.ToString());

                    // If empty/non-existent, abort
                    if (string.IsNullOrEmpty(sCurrentLine))
                    {
                        break;
                    }

                    // Execute the line, but if there's a problem (error or they cancel the
                    // input textbox), then abort
                    if (!ExecuteLine(Instance, sCurrentLine))
                    {
                        break;
                    }

                    // Continue to the next line, if there is any
                }
                #endregion
                return;
            }

            // Unhandled command, log the message but otherwise do nothing
            Rainmeter.Log(Rainmeter.LogLevel.Debug, "InputText: Received command \"" + sParts[0].Trim() + "\", left unhandled");

            #endregion

            return;
        }