Skip to content

Commit f6de309

Browse files
committed
(#191) MainWindow: formatting
1 parent 6691b11 commit f6de309

File tree

2 files changed

+101
-102
lines changed

2 files changed

+101
-102
lines changed

WpfMath.sln.DotSettings

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=infty/@EntryIndexedValue">True</s:Boolean>
23
<s:Boolean x:Key="/Default/UserDictionary/Words/=lbrack/@EntryIndexedValue">True</s:Boolean>
4+
<s:Boolean x:Key="/Default/UserDictionary/Words/=mathrm/@EntryIndexedValue">True</s:Boolean>
35
<s:Boolean x:Key="/Default/UserDictionary/Words/=pmatrix/@EntryIndexedValue">True</s:Boolean>
46
<s:Boolean x:Key="/Default/UserDictionary/Words/=rbrack/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/WpfMath.Example/MainWindow.xaml.cs

Lines changed: 99 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -8,124 +8,121 @@
88
using Microsoft.Win32;
99
using WpfMath.Converters;
1010

11-
namespace WpfMath.Example
11+
namespace WpfMath.Example;
12+
13+
public partial class MainWindow
1214
{
13-
public partial class MainWindow : Window
14-
{
15-
private readonly TexFormulaParser _formulaParser = new TexFormulaParser();
15+
private readonly TexFormulaParser _formulaParser = new();
1616

17-
private static ComboBoxItem DemoFormula(string name, string text) =>
18-
new ComboBoxItem { Content = name, DataContext = text };
17+
private static ComboBoxItem DemoFormula(string name, string text) =>
18+
new ComboBoxItem { Content = name, DataContext = text };
1919

20-
private readonly IList<ComboBoxItem> _testFormulas = new[]
21-
{
22-
DemoFormula("Integral 1", @"\int_0^{\infty}{x^{2n} e^{-a x^2} \, dx} = \frac{2n-1}{2a} \int_0^{\infty}{x^{2(n-1)} e^{-a x^2} \, dx} = \frac{(2n-1)!!}{2^{n+1}} \sqrt{\frac{\pi}{a^{2n+1}}}"),
23-
DemoFormula("Integral 2", @"\int_a^b{f(x) \, dx} = (b - a) \sum_{n = 1}^{\infty} {\sum_{m = 1}^{2^n - 1} { ( { - 1} )^{m + 1} } } 2^{ - n} f(a + m ( {b - a} )2^{-n} )"),
24-
DemoFormula("Integral 3", @"L = \int_a^\infty \sqrt[4]{ \left\vert \sum_{i,j=1}^ng_{ij}\left\(\gamma(t)\right\) \left\[\frac{d}{dt}x^i\circ\gamma(t) \right\] \left\{\frac{d}{dt}x^j\circ\gamma(t) \right\} \right\|} \, dt"),
25-
DemoFormula("Number matrix", @"\matrix{4&78&3 \\ 5 & 9 & 82 }"),
26-
DemoFormula("Nested matrix", @"\matrix{4&78&3\\ 57 & {\matrix{78 \\ 12}} & 20782 }"),
27-
DemoFormula("Cases", @"f(x) = \cases{1/3 & \mathrm{if} \thinspace 0\le x\le 1;\cr 2/3 & \mathrm{if} \thinspace 3\le x \le 4; \cr 0 & \mathrm{elsewhere.}\cr}"),
28-
DemoFormula("Matrix and new lines", @"v \times w = \left( \matrix{v_2 w_3 - v_3 w_2 \\ v_3 w_1 - v_1 w_3 \\ v_1 w_2 - v_2 w_1} \right) \\ \matrix{\mathrm{where} & v= \left(\matrix{ v_1 \\ v_2 \\ v_3 }\right), \\ & w= \left( \matrix{w_1 \\ w_2 \\ w_3} \right)}"),
29-
DemoFormula("Big matrix", @"\Gamma_{\mu \rho} ^{\sigma}= \pmatrix{\pmatrix{0 & 0 & 0 \\ 0 & -r & 0 \\ 0 & 0 & -r \sin^2(\theta)} \\ \pmatrix{0 & \frac{1}{r} & 0 \\ \frac{1}{r} & 0 & 0 \\ 0 & 0 & -\sin(\theta) \cos(\theta)} \\ \pmatrix{0 & 0 & \frac{1}{r} \\ 0 & 0 & \frac{1}{\tan(\theta)} \\ \frac{1}{r} & \frac{1}{\tan(\theta)} & 0 }}"),
30-
DemoFormula("Environment with Matrix", @"\begin{pmatrix} a_1 & a_2 & a_3 \\ b_1 & b_2 & b_3 \\ c_1 & c_2 & c_3 \end{pmatrix}")
31-
};
20+
private readonly IList<ComboBoxItem> _testFormulas = new[]
21+
{
22+
DemoFormula("Integral 1", @"\int_0^{\infty}{x^{2n} e^{-a x^2} \, dx} = \frac{2n-1}{2a} \int_0^{\infty}{x^{2(n-1)} e^{-a x^2} \, dx} = \frac{(2n-1)!!}{2^{n+1}} \sqrt{\frac{\pi}{a^{2n+1}}}"),
23+
DemoFormula("Integral 2", @"\int_a^b{f(x) \, dx} = (b - a) \sum_{n = 1}^{\infty} {\sum_{m = 1}^{2^n - 1} { ( { - 1} )^{m + 1} } } 2^{ - n} f(a + m ( {b - a} )2^{-n} )"),
24+
DemoFormula("Integral 3", @"L = \int_a^\infty \sqrt[4]{ \left\vert \sum_{i,j=1}^ng_{ij}\left\(\gamma(t)\right\) \left\[\frac{d}{dt}x^i\circ\gamma(t) \right\] \left\{\frac{d}{dt}x^j\circ\gamma(t) \right\} \right\|} \, dt"),
25+
DemoFormula("Number matrix", @"\matrix{4&78&3 \\ 5 & 9 & 82 }"),
26+
DemoFormula("Nested matrix", @"\matrix{4&78&3\\ 57 & {\matrix{78 \\ 12}} & 20782 }"),
27+
DemoFormula("Cases", @"f(x) = \cases{1/3 & \mathrm{if} \thinspace 0\le x\le 1;\cr 2/3 & \mathrm{if} \thinspace 3\le x \le 4; \cr 0 & \mathrm{elsewhere.}\cr}"),
28+
DemoFormula("Matrix and new lines", @"v \times w = \left( \matrix{v_2 w_3 - v_3 w_2 \\ v_3 w_1 - v_1 w_3 \\ v_1 w_2 - v_2 w_1} \right) \\ \matrix{\mathrm{where} & v= \left(\matrix{ v_1 \\ v_2 \\ v_3 }\right), \\ & w= \left( \matrix{w_1 \\ w_2 \\ w_3} \right)}"),
29+
DemoFormula("Big matrix", @"\Gamma_{\mu \rho} ^{\sigma}= \pmatrix{\pmatrix{0 & 0 & 0 \\ 0 & -r & 0 \\ 0 & 0 & -r \sin^2(\theta)} \\ \pmatrix{0 & \frac{1}{r} & 0 \\ \frac{1}{r} & 0 & 0 \\ 0 & 0 & -\sin(\theta) \cos(\theta)} \\ \pmatrix{0 & 0 & \frac{1}{r} \\ 0 & 0 & \frac{1}{\tan(\theta)} \\ \frac{1}{r} & \frac{1}{\tan(\theta)} & 0 }}"),
30+
DemoFormula("Environment with Matrix", @"\begin{pmatrix} a_1 & a_2 & a_3 \\ b_1 & b_2 & b_3 \\ c_1 & c_2 & c_3 \end{pmatrix}")
31+
};
32+
33+
public MainWindow()
34+
{
35+
InitializeComponent();
3236

33-
public MainWindow()
34-
{
35-
InitializeComponent();
37+
FormulaSelector.ItemsSource = _testFormulas;
38+
FormulaSelector.SelectedIndex = 0;
39+
}
3640

37-
FormulaSelector.ItemsSource = _testFormulas;
38-
FormulaSelector.SelectedIndex = 0;
41+
private TexFormula? ParseFormula(string input)
42+
{
43+
// Create formula object from input text.
44+
TexFormula? formula = null;
45+
try
46+
{
47+
formula = this._formulaParser.Parse(input);
3948
}
40-
41-
private TexFormula? ParseFormula(string input)
49+
catch (Exception ex)
4250
{
43-
// Create formula object from input text.
44-
TexFormula? formula = null;
45-
try
46-
{
47-
formula = this._formulaParser.Parse(input);
48-
}
49-
catch (Exception ex)
50-
{
51-
MessageBox.Show("An error occurred while parsing the given input:" + Environment.NewLine +
52-
Environment.NewLine + ex.Message, "WPF-Math Example", MessageBoxButton.OK, MessageBoxImage.Error);
53-
}
54-
55-
return formula;
51+
MessageBox.Show("An error occurred while parsing the given input:" + Environment.NewLine +
52+
Environment.NewLine + ex.Message, "WPF-Math Example", MessageBoxButton.OK, MessageBoxImage.Error);
5653
}
5754

58-
private void saveButton_Click(object sender, RoutedEventArgs e)
55+
return formula;
56+
}
57+
58+
private void saveButton_Click(object sender, RoutedEventArgs e)
59+
{
60+
// Choose file
61+
SaveFileDialog saveFileDialog = new SaveFileDialog()
62+
{
63+
Filter = "SVG Files (*.svg)|*.svg|PNG Files (*.png)|*.png"
64+
};
65+
var result = saveFileDialog.ShowDialog();
66+
if (result == false) return;
67+
68+
// Create formula object from input text.
69+
var formula = ParseFormula(InputTextBox.Text);
70+
if (formula == null) return;
71+
var renderer = formula.GetRenderer(TexStyle.Display, Formula.Scale, "Arial");
72+
73+
// Open stream
74+
var filename = saveFileDialog.FileName;
75+
using var stream = new FileStream(filename, FileMode.Create);
76+
switch (saveFileDialog.FilterIndex)
5977
{
60-
// Choose file
61-
SaveFileDialog saveFileDialog = new SaveFileDialog()
62-
{
63-
Filter = "SVG Files (*.svg)|*.svg|PNG Files (*.png)|*.png"
64-
};
65-
var result = saveFileDialog.ShowDialog();
66-
if (result == false) return;
67-
68-
// Create formula object from input text.
69-
var formula = ParseFormula(InputTextBox.Text);
70-
if (formula == null) return;
71-
var renderer = formula.GetRenderer(TexStyle.Display, this.Formula.Scale, "Arial");
72-
73-
// Open stream
74-
var filename = saveFileDialog.FileName;
75-
using (var stream = new FileStream(filename, FileMode.Create))
76-
{
77-
switch (saveFileDialog.FilterIndex)
78+
case 1:
79+
var geometry = renderer.RenderToGeometry(0, 0);
80+
var converter = new SVGConverter();
81+
var svgPathText = converter.ConvertGeometry(geometry);
82+
var svgText = AddSvgHeader(svgPathText);
83+
using (var writer = new StreamWriter(stream))
84+
writer.WriteLine(svgText);
85+
break;
86+
87+
case 2:
88+
var bitmap = renderer.RenderToBitmap(0, 0, 300);
89+
var encoder = new PngBitmapEncoder
7890
{
79-
case 1:
80-
var geometry = renderer.RenderToGeometry(0, 0);
81-
var converter = new SVGConverter();
82-
var svgPathText = converter.ConvertGeometry(geometry);
83-
var svgText = AddSVGHeader(svgPathText);
84-
using (var writer = new StreamWriter(stream))
85-
writer.WriteLine(svgText);
86-
break;
87-
88-
case 2:
89-
var bitmap = renderer.RenderToBitmap(0, 0, 300);
90-
var encoder = new PngBitmapEncoder
91-
{
92-
Frames = { BitmapFrame.Create(bitmap) }
93-
};
94-
encoder.Save(stream);
95-
break;
96-
97-
default:
98-
return;
99-
}
100-
}
91+
Frames = { BitmapFrame.Create(bitmap) }
92+
};
93+
encoder.Save(stream);
94+
break;
95+
96+
default:
97+
return;
10198
}
99+
}
102100

103-
private string AddSVGHeader(string svgText)
104-
{
105-
var builder = new StringBuilder();
106-
builder.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>")
107-
.AppendLine("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" >")
108-
.AppendLine(svgText)
109-
.AppendLine("</svg>");
101+
private string AddSvgHeader(string svgText)
102+
{
103+
var builder = new StringBuilder();
104+
builder.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>")
105+
.AppendLine("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" >")
106+
.AppendLine(svgText)
107+
.AppendLine("</svg>");
110108

111-
return builder.ToString();
112-
}
109+
return builder.ToString();
110+
}
113111

114-
private void Window_Closed(object sender, EventArgs e)
115-
{
116-
//
117-
}
112+
private void Window_Closed(object sender, EventArgs e)
113+
{
114+
//
115+
}
118116

119-
private void inputTextBox_SelectionChanged(object sender, RoutedEventArgs e)
120-
{
121-
Formula.SelectionStart = InputTextBox.SelectionStart;
122-
Formula.SelectionLength = InputTextBox.SelectionLength;
123-
}
117+
private void inputTextBox_SelectionChanged(object sender, RoutedEventArgs e)
118+
{
119+
Formula.SelectionStart = InputTextBox.SelectionStart;
120+
Formula.SelectionLength = InputTextBox.SelectionLength;
121+
}
124122

125-
private void FormulaTextBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
126-
{
127-
var item = (ComboBoxItem) ((ComboBox) sender).SelectedItem;
128-
InputTextBox.Text = (string)item.DataContext;
129-
}
123+
private void FormulaTextBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
124+
{
125+
var item = (ComboBoxItem) ((ComboBox) sender).SelectedItem;
126+
InputTextBox.Text = (string)item.DataContext;
130127
}
131128
}

0 commit comments

Comments
 (0)