5using System.Threading.Tasks;
7using System.Windows.Controls;
8using System.Windows.Input;
9using System.Windows.Markup;
10using System.Windows.Media;
11using System.Windows.Media.Imaging;
35 private readonly StackPanel historyPanel;
47 this.historyPanel = HistoryPanel;
59 get => this.referenceUri.Value;
60 set => this.referenceUri.Value = value;
68 get => this.input.Value;
69 set => this.input.Value = value;
79 if (e.Key == Key.Enter)
81 if (!Keyboard.Modifiers.HasFlag(ModifierKeys.Control) && !Keyboard.Modifiers.HasFlag(ModifierKeys.Shift))
83 this.ExecuteButton_Click(
this, e);
95 private void ExecuteButton_Click(
object sender, RoutedEventArgs e)
98 TextBlock ScriptBlock;
99 UIElement ResultBlock =
null;
105 ScriptBlock =
new TextBlock()
108 FontFamily =
new FontFamily(
"Courier New"),
109 TextWrapping = TextWrapping.Wrap,
113 ScriptBlock.PreviewMouseDown += this.TextBlock_PreviewMouseDown;
115 this.historyPanel.Children.Add(ScriptBlock);
117 if (this.historyPanel.Parent is ScrollViewer ScrollViewer)
118 ScrollViewer.ScrollToBottom();
120 this.Input =
string.Empty;
137 MainWindow.UpdateGui(async () =>
139 ResultBlock = await this.ShowResult(ResultBlock, e2.
Preview, ScriptBlock);
142 return Task.CompletedTask;
145 this.variables.OnPreview += Preview;
148 Ans = await Exp.
Root.EvaluateAsync(this.variables);
160 this.variables.OnPreview -= Preview;
163 this.variables[
"Ans"] = Ans;
165 MainWindow.UpdateGui(async () =>
167 ResultBlock = await this.ShowResult(ResultBlock, Ans, ScriptBlock);
173 await MainWindow.MessageBox(ex.Message,
"Unable to parse script.", MessageBoxButton.OK, MessageBoxImage.Error);
178 private async Task<UIElement> ShowResult(UIElement ResultBlock,
IElement Ans, TextBlock ScriptBlock)
184 PixelInformation Pixels = G.CreatePixels(this.variables, out
object[] States);
185 return this.AddImageBlock(ScriptBlock, Pixels, G, States, ResultBlock);
191 UIElement Last = ResultBlock ?? ScriptBlock;
195 if (ex is AggregateException ex2)
197 foreach (Exception ex3
in ex2.InnerExceptions)
198 Last = this.AddTextBlock(Last, ex3.Message, Colors.Red, FontWeights.Bold,
null, ex3);
201 Last = this.AddTextBlock(ScriptBlock, ex.Message, Colors.Red, FontWeights.Bold, ResultBlock, ex);
207 StringBuilder Markdown =
new();
209 foreach (
string s2
in M.ColumnNames)
211 Markdown.Append(
"| ");
215 Markdown.AppendLine(
" |");
217 foreach (
string s2
in M.ColumnNames)
218 Markdown.Append(
"|---");
220 Markdown.AppendLine(
"|");
224 for (y = 0; y < M.Rows; y++)
226 for (x = 0; x < M.Columns; x++)
228 Markdown.Append(
"| ");
230 object Item = M.GetElement(x, y).AssociatedObjectValue;
233 if (Item is not
string s2)
236 s2 = s2.Replace(
"\r\n",
"\n").Replace(
'\r',
'\n').Replace(
"\n",
"<br/>");
241 Markdown.AppendLine(
" |");
245 string XAML = await Doc.GenerateXAML(GetXamlSettings());
247 if (XamlReader.Parse(XAML) is UIElement Parsed)
248 return this.AddBlock(ScriptBlock, Parsed);
253 return this.AddTextBlock(ScriptBlock, Ans.ToString(), Colors.Red, FontWeights.Normal, ResultBlock, Ans);
259 this.variables[
"Ans"] = Ans;
261 if (ex is AggregateException ex2)
263 foreach (Exception ex3
in ex2.InnerExceptions)
264 ScriptBlock = this.AddTextBlock(ScriptBlock, ex3.Message, Colors.Red, FontWeights.Bold,
null, ex3);
267 this.AddTextBlock(ScriptBlock, ex.Message, Colors.Red, FontWeights.Bold, ResultBlock, ex);
282 XmlEntitiesOnly =
true
290 TableCellRowBackgroundColor1 =
"#20404040",
291 TableCellRowBackgroundColor2 =
"#10808080"
295 private TextBlock AddTextBlock(UIElement ScriptBlock,
string s, Color cl, FontWeight Weight, UIElement ResultBlock,
object Tag)
297 if (ResultBlock is TextBlock TextBlock)
304 TextBlock =
new TextBlock()
307 FontFamily =
new FontFamily(
"Courier New"),
308 Foreground =
new SolidColorBrush(cl),
309 TextWrapping = TextWrapping.Wrap,
314 TextBlock.PreviewMouseDown += this.TextBlock_PreviewMouseDown;
316 this.AddBlock(ScriptBlock, TextBlock);
322 private UIElement AddBlock(UIElement ScriptBlock, UIElement ResultBlock)
324 if (ScriptBlock is
null)
325 this.historyPanel.Children.Add(ResultBlock);
327 this.historyPanel.Children.Insert(this.historyPanel.Children.IndexOf(ScriptBlock) + 1, ResultBlock);
332 private void TextBlock_PreviewMouseDown(
object sender, MouseButtonEventArgs e)
334 this.Input = ((TextBlock)sender).Text;
338 private UIElement AddImageBlock(UIElement ScriptBlock,
PixelInformation Pixels,
Graph Graph,
object[] States, UIElement ResultBlock)
340 BitmapImage BitmapImage;
343 using (MemoryStream ms =
new(Bin))
345 BitmapImage =
new BitmapImage();
346 BitmapImage.BeginInit();
347 BitmapImage.CacheOption = BitmapCacheOption.OnLoad;
348 BitmapImage.StreamSource = ms;
349 BitmapImage.EndInit();
352 if (ResultBlock is Image ImageBlock)
354 ImageBlock.Source = BitmapImage;
355 ImageBlock.Width = Pixels.
Width;
356 ImageBlock.Height = Pixels.
Height;
357 ImageBlock.Tag =
new Tuple<byte[], int, int, Graph, object[]>(Bin, Pixels.
Width, Pixels.
Height,
Graph, States);
361 ImageBlock =
new Image()
363 Source = BitmapImage,
364 Width = Pixels.
Width,
366 Tag =
new Tuple<byte[], int, int, Graph, object[]>(Bin, Pixels.
Width, Pixels.
Height,
Graph, States)
369 ImageBlock.PreviewMouseDown += this.ImageBlock_PreviewMouseDown;
371 this.historyPanel.Children.Insert(this.historyPanel.Children.IndexOf(ScriptBlock) + 1, ImageBlock);
377 private void ImageBlock_PreviewMouseDown(
object sender, MouseButtonEventArgs e)
379 Image ImageBlock = (Image)sender;
383 Point P = e.GetPosition(ImageBlock);
386 if (ImageBlock.Tag is Tuple<
byte[],
int,
int,
Graph,
object[]> Image && Image.Item4 !=
null && Image.Item5 !=
null)
388 double X = ((double)P.X) * Image.Item2 / ImageBlock.ActualWidth;
389 double Y = ((double)P.Y) * Image.Item3 / ImageBlock.ActualHeight;
391 Script = Image.Item4.GetBitmapClickScript(X, Y, Image.Item5);
394 Script =
"[" + P.X.ToString() +
"," + P.Y.ToString() +
"]";
397 this.ExecuteButton_Click(
this, e);
401 internal void Print(
string Output)
403 MainWindow.UpdateGui(() =>
405 this.AddTextBlock(
null, Output, Colors.Blue, FontWeights.Normal,
null,
false);
406 return Task.CompletedTask;
413 this.historyPanel.Children.Clear();
423 MainWindow.currentInstance.ScriptTab.DataContext =
this;
424 return Task.CompletedTask;
Interaction logic for MainWindow.xaml
static void ErrorBox(string ErrorMessage)
Displays an error message on the screen, on the main UI thread.
static void UpdateGui(GuiDelegate Method)
Calls a method from the Main UI thread.
Abstract base class for view models
Generic class for properties
Redirects script print output to the script tab output. From the IoTGateway project,...
Interaction logic for the script view. From the IoTGateway project, with permission.
ScriptModel(StackPanel HistoryPanel)
Interaction logic for the script view. From the IoTGateway project, with permission.
string ReferenceUri
URI to reference documentation.
override async Task Start()
Starts the model.
void InputPreviewKeyDown(KeyEventArgs e)
Event handler for the input PreviewKeyDown event.
Contains a markdown document. This markdown document class supports original markdown,...
static string Encode(string s)
Encodes all special characters in a string so that it can be included in a markdown document without ...
static Task< MarkdownDocument > CreateAsync(string MarkdownText, params Type[] TransparentExceptionTypes)
Contains a markdown document. This markdown document class supports original markdown,...
Contains settings that the Markdown parser uses to customize its behavior.
Contains settings that the HTML export uses to customize HTML output.
Contains settings that the XAML export uses to customize XAML output.
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
static Exception UnnestException(Exception Exception)
Unnests an exception, to extract the relevant inner exception.
Script runtime exception.
IElement ReturnValue
Return value.
Class managing a script expression.
ScriptNode Root
Root script node.
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
string[] ColumnNames
Contains optional column names.
Event arguments for preview events.
IElement Preview
Preview of result.
Basic interface for all types of elements.
object AssociatedObjectValue
Associated object value.
MouseButton
Enumeration identifying mouse button being used.