Skip to content

Commit 9245457

Browse files
committed
Add option for opening text box in large editor window
1 parent c8c7e24 commit 9245457

3 files changed

Lines changed: 246 additions & 12 deletions

File tree

src/ScriptRunner/ScriptRunner.GUI/Parameters/ParamsPanelFactory.cs

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,75 @@ public ParamsPanel Create(ScriptConfig action, Dictionary<string, string> values
8484
bool _isResizing = false;
8585
Point _lastPointerPosition = default;
8686

87-
var resizeHandle = new Border()
87+
// Create toolbar
88+
var toolbar = new StackPanel()
8889
{
89-
Height = 10,
90-
Width = 10,
91-
Background = Brushes.Transparent,
90+
Orientation = Orientation.Horizontal,
91+
Height = 24,
9292
HorizontalAlignment = HorizontalAlignment.Right,
93+
//Background = new SolidColorBrush(Color.FromArgb(50, 128, 128, 128)),
94+
Spacing = 5,
9395
ZIndex = 1,
94-
Margin = new Thickness(0,-10,0,0),
95-
VerticalAlignment = VerticalAlignment.Bottom,
96-
Cursor = new Cursor(StandardCursorType.BottomRightCorner)
96+
Margin = new Thickness(0,-25,0,0),
97+
};
98+
99+
// Expand button to open overlay
100+
var expandButton = new Button()
101+
{
102+
Width = 24,
103+
Height = 24,
104+
Padding = new Thickness(0),
105+
VerticalContentAlignment = VerticalAlignment.Center,
106+
HorizontalContentAlignment = HorizontalAlignment.Center,
107+
Background = Brushes.Transparent,
108+
BorderThickness = new Thickness(0)
97109
};
98-
resizeHandle.Child = new Icon()
110+
111+
var expandIcon = new Icon()
99112
{
100-
Value = "fas fa-signal",
101-
HorizontalAlignment = HorizontalAlignment.Right
113+
Value = "fas fa-expand",
114+
FontSize = 12
115+
};
116+
expandButton.Content = expandIcon;
117+
ToolTip.SetTip(expandButton, "Open in larger editor");
118+
119+
expandButton.Click += async (sender, e) =>
120+
{
121+
var overlay = new Views.TextEditorOverlay();
122+
overlay.SetEditorControl(controlRecord.Control);
123+
124+
// Find the parent window
125+
var parentWindow = TopLevel.GetTopLevel(controlRecord.Control as Visual) as Window;
126+
if (parentWindow != null)
127+
{
128+
await overlay.ShowDialog(parentWindow);
129+
}
130+
else
131+
{
132+
overlay.Show();
133+
}
102134
};
103135

136+
// Resize handle
137+
var resizeHandle = new Border()
138+
{
139+
Width = 24,
140+
Height = 24,
141+
Background = Brushes.Transparent,
142+
Cursor = new Cursor(StandardCursorType.BottomRightCorner),
143+
VerticalAlignment = VerticalAlignment.Center,
144+
Margin = new Thickness(2,5,0,0),
145+
};
146+
147+
var resizeIcon = new Icon()
148+
{
149+
Value = "fas fa-signal",
150+
FontSize = 12,
151+
HorizontalAlignment = HorizontalAlignment.Center,
152+
VerticalAlignment = VerticalAlignment.Center
153+
};
154+
resizeHandle.Child = resizeIcon;
155+
ToolTip.SetTip(resizeHandle, "Drag to resize");
104156

105157
resizeHandle.PointerPressed += (sender, e) =>
106158
{
@@ -133,14 +185,16 @@ public ParamsPanel Create(ScriptConfig action, Dictionary<string, string> values
133185
{
134186
_isResizing = false;
135187
};
136-
//paramsPanel.Children.Add(resizeHandle);
188+
189+
toolbar.Children.Add(expandButton);
190+
toolbar.Children.Add(resizeHandle);
137191

138192
var panel = new StackPanel()
139193
{
140194
Orientation = Orientation.Vertical
141195
};
142196
panel.Children.Add(controlRecord.Control);
143-
panel.Children.Add(resizeHandle);
197+
panel.Children.Add(toolbar);
144198
controlForEdit = panel;
145199

146200

@@ -585,6 +639,10 @@ private static TextEditor CreateAvaloniaEdit(string? value, int index, string sy
585639
CornerRadius = new CornerRadius(3)
586640
};
587641
textEditor.TextArea.TextView.Margin = new Thickness(10, 0);
642+
643+
// Store syntax as Tag so it can be retrieved later
644+
textEditor.Tag = syntax;
645+
588646
var registry = new RegistryOptions(ThemeName.DarkPlus);
589647
TextMate.Installation textMateInstallation = textEditor.InstallTextMate(registry);
590648
if (registry.GetLanguageByExtension("." + syntax) is { } languageByExtension)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<Window xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:i="clr-namespace:Projektanker.Icons.Avalonia;assembly=Projektanker.Icons.Avalonia"
6+
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="600"
7+
x:Class="ScriptRunner.GUI.Views.TextEditorOverlay"
8+
Title="Edit Text"
9+
Width="900"
10+
Height="700"
11+
WindowStartupLocation="CenterOwner"
12+
CanResize="True">
13+
14+
<Grid RowDefinitions="*,Auto">
15+
<!-- Content area -->
16+
<Border Grid.Row="0"
17+
Padding="10"
18+
Background="{DynamicResource SystemChromeLowColor}">
19+
<ContentPresenter Name="EditorContentPresenter" />
20+
</Border>
21+
22+
<!-- Button bar -->
23+
<Border Grid.Row="1"
24+
Background="{DynamicResource SystemChromeMediumColor}"
25+
BorderBrush="{DynamicResource SystemControlForegroundBaseMediumLowBrush}"
26+
BorderThickness="0,1,0,0"
27+
Padding="10">
28+
<StackPanel Orientation="Horizontal"
29+
HorizontalAlignment="Right"
30+
Spacing="10">
31+
<Button Name="OkButton"
32+
Content="OK"
33+
Width="80"
34+
HorizontalContentAlignment="Center"
35+
Classes="accent"/>
36+
<Button Name="CancelButton"
37+
HorizontalContentAlignment="Center"
38+
Content="Cancel"
39+
Width="80"/>
40+
</StackPanel>
41+
</Border>
42+
</Grid>
43+
</Window>
44+
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
using System;
2+
using Avalonia;
3+
using Avalonia.Controls;
4+
using Avalonia.Controls.Presenters;
5+
using Avalonia.Interactivity;
6+
using Avalonia.Layout;
7+
using Avalonia.Markup.Xaml;
8+
using Avalonia.Media;
9+
using AvaloniaEdit;
10+
using AvaloniaEdit.Document;
11+
using AvaloniaEdit.TextMate;
12+
using TextMateSharp.Grammars;
13+
14+
namespace ScriptRunner.GUI.Views;
15+
16+
public partial class TextEditorOverlay : Window
17+
{
18+
private Control? _originalControl;
19+
private Control? _clonedControl;
20+
21+
public bool WasConfirmed { get; private set; }
22+
23+
public TextEditorOverlay()
24+
{
25+
InitializeComponent();
26+
27+
#if DEBUG
28+
this.AttachDevTools();
29+
#endif
30+
31+
var okButton = this.FindControl<Button>("OkButton");
32+
var cancelButton = this.FindControl<Button>("CancelButton");
33+
34+
if (okButton != null)
35+
{
36+
okButton.Click += OkButton_Click;
37+
}
38+
39+
if (cancelButton != null)
40+
{
41+
cancelButton.Click += CancelButton_Click;
42+
}
43+
}
44+
45+
private void InitializeComponent()
46+
{
47+
AvaloniaXamlLoader.Load(this);
48+
}
49+
50+
public void SetEditorControl(Control originalControl)
51+
{
52+
_originalControl = originalControl;
53+
54+
// Clone the control based on its type
55+
if (originalControl is TextBox textBox)
56+
{
57+
var clonedTextBox = new TextBox
58+
{
59+
Text = textBox.Text,
60+
TextWrapping = textBox.TextWrapping,
61+
AcceptsReturn = true,
62+
VerticalAlignment = VerticalAlignment.Stretch,
63+
HorizontalAlignment = HorizontalAlignment.Stretch
64+
};
65+
_clonedControl = clonedTextBox;
66+
}
67+
else if (originalControl is TextEditor textEditor)
68+
{
69+
var clonedEditor = new TextEditor
70+
{
71+
Document = new TextDocument(textEditor.Text ?? string.Empty),
72+
ShowLineNumbers = textEditor.ShowLineNumbers,
73+
FontFamily = textEditor.FontFamily,
74+
Background = textEditor.Background,
75+
BorderBrush = textEditor.BorderBrush,
76+
BorderThickness = textEditor.BorderThickness,
77+
CornerRadius = textEditor.CornerRadius,
78+
VerticalAlignment = VerticalAlignment.Stretch,
79+
HorizontalAlignment = HorizontalAlignment.Stretch
80+
};
81+
82+
// Copy TextArea settings
83+
if (textEditor.TextArea?.TextView?.Margin != null)
84+
{
85+
clonedEditor.TextArea.TextView.Margin = textEditor.TextArea.TextView.Margin;
86+
}
87+
88+
// Set up TextMate syntax highlighting from the Tag property
89+
if (textEditor.Tag is string syntax && !string.IsNullOrWhiteSpace(syntax))
90+
{
91+
var registry = new RegistryOptions(ThemeName.DarkPlus);
92+
var textMateInstallation = clonedEditor.InstallTextMate(registry);
93+
94+
if (registry.GetLanguageByExtension("." + syntax) is { } languageByExtension)
95+
{
96+
textMateInstallation.SetGrammar(registry.GetScopeByLanguageId(languageByExtension.Id));
97+
}
98+
}
99+
100+
_clonedControl = clonedEditor;
101+
}
102+
103+
var presenter = this.FindControl<ContentPresenter>("EditorContentPresenter");
104+
if (presenter != null && _clonedControl != null)
105+
{
106+
presenter.Content = _clonedControl;
107+
}
108+
}
109+
110+
private void OkButton_Click(object? sender, RoutedEventArgs e)
111+
{
112+
WasConfirmed = true;
113+
114+
// Copy the text back to the original control
115+
if (_originalControl is TextBox originalTextBox && _clonedControl is TextBox clonedTextBox)
116+
{
117+
originalTextBox.Text = clonedTextBox.Text;
118+
}
119+
else if (_originalControl is TextEditor originalEditor && _clonedControl is TextEditor clonedEditor)
120+
{
121+
originalEditor.Text = clonedEditor.Text;
122+
}
123+
124+
Close();
125+
}
126+
127+
private void CancelButton_Click(object? sender, RoutedEventArgs e)
128+
{
129+
WasConfirmed = false;
130+
Close();
131+
}
132+
}

0 commit comments

Comments
 (0)