Skip to content

Commit b2660ca

Browse files
committed
移动端布局优化
1 parent a0e3165 commit b2660ca

7 files changed

Lines changed: 290 additions & 43 deletions

File tree

DirectorPrompt/Services/RemoteWindowService.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,20 @@ private Control CreateModal(Window window, Control content)
216216
Child = content
217217
};
218218

219-
if (!double.IsNaN(window.Width) && window.Width > 0)
220-
frame.MaxWidth = window.Width;
219+
if (window is SettingsWindow or ProjectEditWindow)
220+
{
221+
frame.Margin = new Thickness(8);
222+
frame.HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch;
223+
frame.VerticalAlignment = Avalonia.Layout.VerticalAlignment.Stretch;
224+
}
225+
else
226+
{
227+
if (!double.IsNaN(window.Width) && window.Width > 0)
228+
frame.MaxWidth = window.Width;
221229

222-
if (!double.IsNaN(window.Height) && window.Height > 0)
223-
frame.MaxHeight = window.Height;
230+
if (!double.IsNaN(window.Height) && window.Height > 0)
231+
frame.MaxHeight = window.Height;
232+
}
224233

225234
modal.Children.Add(frame);
226235
return modal;

DirectorPrompt/Views/Components/PathComboBox.cs

Lines changed: 78 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
using Avalonia.Controls;
33
using Avalonia.Controls.Templates;
44
using Avalonia.Data;
5-
using Avalonia.Controls.Primitives;
6-
using Avalonia.VisualTree;
5+
using Avalonia.Input;
6+
using Avalonia.Media;
77
using Avalonia.Threading;
88
using DirectorPrompt.Services;
99

1010
namespace DirectorPrompt.Views.Components;
1111

1212
public sealed class PathComboBox : ComboBox
1313
{
14-
private Popup? popup;
15-
private Control? remotePopupContent;
14+
private Border? remotePopupContent;
15+
private ListBox? remoteList;
16+
private bool suppressDropDownClosed;
1617

1718
protected override Type StyleKeyOverride => typeof(ComboBox);
1819

@@ -80,7 +81,7 @@ private void OnDropDownOpened(object? sender, EventArgs e)
8081

8182
private void OnDropDownClosed(object? sender, EventArgs e)
8283
{
83-
if (!RemotePopupHost.IsRemote(this))
84+
if (!RemotePopupHost.IsRemote(this) || suppressDropDownClosed)
8485
return;
8586

8687
HideRemoteDropdown();
@@ -91,18 +92,50 @@ private void ShowRemoteDropdown()
9192
if (remotePopupContent is not null)
9293
return;
9394

94-
popup = this.GetVisualDescendants().OfType<Popup>().FirstOrDefault();
95+
suppressDropDownClosed = true;
96+
IsDropDownOpen = false;
97+
suppressDropDownClosed = false;
9598

96-
if (popup?.Child is not Control content)
97-
return;
99+
var list = new ListBox
100+
{
101+
ItemTemplate = ItemTemplate
102+
};
98103

99-
popup.Child = null;
100-
remotePopupContent = content;
104+
if (ItemsSource is not null)
105+
{
106+
list.ItemsSource = ItemsSource;
107+
list.SelectedItem = SelectedItem;
108+
}
109+
else
110+
{
111+
list.ItemsSource = Items
112+
.Cast<object>()
113+
.Select(static item => item is ComboBoxItem comboItem ? comboItem.Content : item)
114+
.ToArray();
115+
list.SelectedIndex = SelectedIndex;
116+
}
117+
118+
list.SelectionChanged += OnRemoteSelectionChanged;
119+
list.KeyDown += OnRemoteListKeyDown;
120+
121+
var content = new Border
122+
{
123+
MinHeight = 36,
124+
MaxHeight = 320,
125+
Padding = new Thickness(4),
126+
Background = new SolidColorBrush(Color.FromRgb(32, 32, 32)),
127+
BorderBrush = new SolidColorBrush(Color.FromRgb(92, 92, 92)),
128+
BorderThickness = new Thickness(1),
129+
Child = list
130+
};
131+
132+
remoteList = list;
133+
remotePopupContent = content;
101134

102135
if (!RemotePopupHost.Show(this, content, Bounds.Width, RestoreRemotePopupContent))
103136
{
104137
remotePopupContent = null;
105-
popup.Child = content;
138+
remoteList = null;
106139
}
107140
}
108141

@@ -111,18 +144,47 @@ private void HideRemoteDropdown()
111144
if (remotePopupContent is null)
112145
return;
113146

114-
var content = RemotePopupHost.Hide(this) ?? remotePopupContent;
147+
RemotePopupHost.Hide(this);
115148
remotePopupContent = null;
149+
remoteList = null;
150+
}
116151

117-
if (popup is not null)
118-
popup.Child = content;
152+
private void OnRemoteSelectionChanged(object? sender, SelectionChangedEventArgs e)
153+
{
154+
CommitRemoteSelection();
155+
}
156+
157+
private void CommitRemoteSelection()
158+
{
159+
if (remoteList is null || remoteList.SelectedIndex < 0)
160+
return;
161+
162+
if (ItemsSource is not null)
163+
SelectedItem = remoteList?.SelectedItem;
164+
else
165+
SelectedIndex = remoteList?.SelectedIndex ?? -1;
166+
167+
HideRemoteDropdown();
119168
}
120169

121170
private void RestoreRemotePopupContent(Control content)
122171
{
123172
remotePopupContent = null;
173+
remoteList = null;
174+
}
124175

125-
if (popup is not null)
126-
popup.Child = content;
176+
private void OnRemoteListKeyDown(object? sender, KeyEventArgs e)
177+
{
178+
if (e.Key == Key.Escape)
179+
{
180+
HideRemoteDropdown();
181+
e.Handled = true;
182+
}
183+
else if (e.Key == Key.Enter)
184+
{
185+
if (remoteList is not null)
186+
CommitRemoteSelection();
187+
e.Handled = true;
188+
}
127189
}
128190
}

DirectorPrompt/Views/Components/SearchableComboBox.axaml.cs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Avalonia.Interactivity;
1111
using Avalonia.LogicalTree;
1212
using Avalonia.Markup.Xaml;
13+
using Avalonia.Media;
1314
using DirectorPrompt.Services;
1415

1516
namespace DirectorPrompt.Views.Components;
@@ -36,8 +37,8 @@ public sealed partial class SearchableComboBox : UserControl
3637

3738
private readonly List<object> allItems = [];
3839
private INotifyCollectionChanged? observedCollection;
39-
private Control? remotePopupContent;
40-
private Popup? remotePopup;
40+
private Border? remotePopupContent;
41+
private ListBox? remoteResults;
4142
private bool isUpdatingText;
4243

4344
private TextBox SearchInput =>
@@ -281,18 +282,32 @@ private void ShowRemoteDropdown()
281282
if (remotePopupContent is not null)
282283
return;
283284

284-
remotePopup = DropDownPopup;
285-
286-
if (remotePopup.Child is not Control content)
287-
return;
285+
var list = new ListBox
286+
{
287+
ItemsSource = FilteredItems,
288+
DisplayMemberBinding = new Binding(DisplayMemberPath)
289+
};
290+
list.SelectionChanged += OnRemoteResultSelectionChanged;
291+
list.KeyDown += OnRemoteResultKeyDown;
288292

289-
remotePopup.Child = null;
293+
var content = new Border
294+
{
295+
MinHeight = 36,
296+
MaxHeight = 320,
297+
Padding = new Thickness(4),
298+
Background = new SolidColorBrush(Color.FromRgb(32, 32, 32)),
299+
BorderBrush = new SolidColorBrush(Color.FromRgb(92, 92, 92)),
300+
BorderThickness = new Thickness(1),
301+
Child = list
302+
};
303+
304+
remoteResults = list;
290305
remotePopupContent = content;
291306

292307
if (!RemotePopupHost.Show(this, content, Bounds.Width, RestoreRemotePopupContent))
293308
{
294309
remotePopupContent = null;
295-
remotePopup.Child = content;
310+
remoteResults = null;
296311
}
297312
}
298313

@@ -301,18 +316,37 @@ private void HideRemoteDropdown()
301316
if (remotePopupContent is null)
302317
return;
303318

304-
var content = RemotePopupHost.Hide(this) ?? remotePopupContent;
319+
RemotePopupHost.Hide(this);
305320
remotePopupContent = null;
321+
remoteResults = null;
322+
}
306323

307-
if (remotePopup is not null)
308-
remotePopup.Child = content;
324+
private void OnRemoteResultSelectionChanged(object? sender, SelectionChangedEventArgs e)
325+
{
326+
if (remoteResults?.SelectedItem is { } selectedItem)
327+
CommitSelection(selectedItem);
328+
}
329+
330+
private void OnRemoteResultKeyDown(object? sender, KeyEventArgs e)
331+
{
332+
if (e.Key == Key.Escape)
333+
{
334+
SetDropDownOpen(false);
335+
SearchInput.Focus();
336+
e.Handled = true;
337+
}
338+
else if (e.Key == Key.Enter)
339+
{
340+
if (remoteResults?.SelectedItem is { } selectedItem)
341+
CommitSelection(selectedItem);
342+
SearchInput.Focus();
343+
e.Handled = true;
344+
}
309345
}
310346

311347
private void RestoreRemotePopupContent(Control content)
312348
{
313349
remotePopupContent = null;
314-
315-
if (remotePopup is not null)
316-
remotePopup.Child = content;
350+
remoteResults = null;
317351
}
318352
}

DirectorPrompt/Views/ProjectEditWindow.axaml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,22 @@
1717
MinHeight="600"
1818
Icon="/Assets/Images/Icon-NoBackground.ico"
1919
TransparencyLevelHint="Mica">
20-
<Grid RowDefinitions="*,Auto">
20+
<Grid x:Name="RootLayout" RowDefinitions="Auto,*,Auto">
2121

22-
<Grid Grid.Row="0">
22+
<components:PathComboBox x:Name="RemoteNavComboBox"
23+
Grid.Row="0"
24+
Margin="12,8"
25+
HorizontalAlignment="Stretch"
26+
IsVisible="False"
27+
SelectionChanged="OnRemoteNavSelectionChanged"
28+
SelectedIndex="0">
29+
<ComboBoxItem Content="{Binding [Project.Basic.Title], Source={x:Static loc:Loc.Instance}}" />
30+
<ComboBoxItem Content="{Binding [Knowledge.Title], Source={x:Static loc:Loc.Instance}}" />
31+
<ComboBoxItem Content="{Binding [State.Title], Source={x:Static loc:Loc.Instance}}" />
32+
<ComboBoxItem Content="{Binding [Character.Title], Source={x:Static loc:Loc.Instance}}" />
33+
</components:PathComboBox>
34+
35+
<Grid x:Name="ContentLayout" Grid.Row="1">
2336
<Grid.ColumnDefinitions>
2437
<ColumnDefinition Width="220" MinWidth="180" />
2538
<ColumnDefinition Width="*" />
@@ -62,7 +75,8 @@
6275
</ListBoxItem>
6376
</ListBox>
6477

65-
<Border Grid.Column="1"
78+
<Border x:Name="ContentPanel"
79+
Grid.Column="1"
6680
BorderBrush="{DynamicResource ControlElevationBorderBrush}"
6781
BorderThickness="1,0,0,0">
6882
<ScrollViewer VerticalScrollBarVisibility="Auto"
@@ -1140,7 +1154,7 @@
11401154
</Border>
11411155
</Grid>
11421156

1143-
<Border Grid.Row="1"
1157+
<Border Grid.Row="2"
11441158
BorderBrush="{DynamicResource ControlElevationBorderBrush}"
11451159
BorderThickness="0,1,0,0"
11461160
Padding="24,12">

0 commit comments

Comments
 (0)