Skip to content

Commit

Permalink
リファクタリング
Browse files Browse the repository at this point in the history
  • Loading branch information
yuto-trd committed Jun 24, 2024
1 parent eff2d8a commit d459831
Showing 1 changed file with 80 additions and 138 deletions.
218 changes: 80 additions & 138 deletions src/Beutl/Views/ElementView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,35 @@ public ElementView()
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (DataContext is ElementViewModel viewModel)
if (DataContext is not ElementViewModel viewModel) return;

if (e.Key == Key.F2)
{
if (e.Key == Key.F2)
{
Rename_Click(null, null!);
e.Handled = true;
return;
}
else if (e.Key == Key.LeftCtrl)
{
_resizeBehavior?.OnLeftCtrlPressed(e);
return;
}
Rename_Click(null, null!);
e.Handled = true;
return;
}
else if (e.Key == Key.LeftCtrl)
{
_resizeBehavior?.OnLeftCtrlPressed(e);
return;
}

// KeyBindingsは変更してはならない。
foreach (KeyBinding binding in viewModel.KeyBindings)
{
if (e.Handled)
break;
binding.TryHandle(e);
}
// KeyBindingsは変更してはならない。
foreach (KeyBinding binding in viewModel.KeyBindings)
{
if (e.Handled)
break;
binding.TryHandle(e);
}
}

private void OnContextFlyoutOpening(object? sender, EventArgs e)
{
if (DataContext is ElementViewModel viewModel)
{
change2OriginalLength.IsEnabled = viewModel.HasOriginalLength();
splitByCurrent.IsEnabled = viewModel.Model.Range.Contains(viewModel.Timeline.EditorContext.CurrentTime.Value);
}
if (DataContext is not ElementViewModel viewModel) return;

change2OriginalLength.IsEnabled = viewModel.HasOriginalLength();
splitByCurrent.IsEnabled = viewModel.Model.Range.Contains(viewModel.Timeline.EditorContext.CurrentTime.Value);
}

private void OnDataContextDetached(ElementViewModel obj)
Expand All @@ -93,58 +91,8 @@ private void OnDataContextAttached(ElementViewModel obj)
{
await Dispatcher.UIThread.InvokeAsync(async () =>
{
var animation1 = new Avalonia.Animation.Animation
{
Easing = new SplineEasing(0.1, 0.9, 0.2, 1.0),
Duration = TimeSpan.FromSeconds(0.25),
FillMode = FillMode.Forward,
Children =
{
new KeyFrame()
{
Cue = new Cue(0),
Setters =
{
new Setter(MarginProperty, border.Margin),
new Setter(WidthProperty, border.Width),
}
},
new KeyFrame()
{
Cue = new Cue(1),
Setters =
{
new Setter(MarginProperty, args.BorderMargin),
new Setter(WidthProperty, args.Width)
}
}
}
};
var animation2 = new Avalonia.Animation.Animation
{
Easing = new SplineEasing(0.1, 0.9, 0.2, 1.0),
Duration = TimeSpan.FromSeconds(0.25),
FillMode = FillMode.Forward,
Children =
{
new KeyFrame()
{
Cue = new Cue(0),
Setters =
{
new Setter(MarginProperty, obj.Margin.Value)
}
},
new KeyFrame()
{
Cue = new Cue(1),
Setters =
{
new Setter(MarginProperty, args.Margin)
}
}
}
};
var animation1 = new Avalonia.Animation.Animation { Easing = new SplineEasing(0.1, 0.9, 0.2, 1.0), Duration = TimeSpan.FromSeconds(0.25), FillMode = FillMode.Forward, Children = { new KeyFrame() { Cue = new Cue(0), Setters = { new Setter(MarginProperty, border.Margin), new Setter(WidthProperty, border.Width), } }, new KeyFrame() { Cue = new Cue(1), Setters = { new Setter(MarginProperty, args.BorderMargin), new Setter(WidthProperty, args.Width) } } } };
var animation2 = new Avalonia.Animation.Animation { Easing = new SplineEasing(0.1, 0.9, 0.2, 1.0), Duration = TimeSpan.FromSeconds(0.25), FillMode = FillMode.Forward, Children = { new KeyFrame() { Cue = new Cue(0), Setters = { new Setter(MarginProperty, obj.Margin.Value) } }, new KeyFrame() { Cue = new Cue(1), Setters = { new Setter(MarginProperty, args.Margin) } } } };

Task task1 = animation1.RunAsync(border, token);
Task task2 = animation2.RunAsync(this, token);
Expand Down Expand Up @@ -222,28 +170,27 @@ private void Rename_Click(object? sender, RoutedEventArgs e)

private void ChangeColor_Click(object? sender, RoutedEventArgs e)
{
if (DataContext is ElementViewModel viewModel)
if (DataContext is not ElementViewModel viewModel) return;

// ContextMenuから開いているので、閉じるのを待つ
s_colorPickerFlyout ??= new ColorPickerFlyout();
s_colorPickerFlyout.ColorPicker.Color = viewModel.Color.Value;
s_colorPickerFlyout.ColorPicker.IsAlphaEnabled = false;
s_colorPickerFlyout.ColorPicker.UseColorPalette = true;
s_colorPickerFlyout.ColorPicker.IsCompact = true;
s_colorPickerFlyout.ColorPicker.IsMoreButtonVisible = true;
s_colorPickerFlyout.Placement = PlacementMode.Top;

if (this.TryFindResource("PaletteColors", out object? colors)
&& colors is IEnumerable<Color> tcolors)
{
// ContextMenuから開いているので、閉じるのを待つ
s_colorPickerFlyout ??= new ColorPickerFlyout();
s_colorPickerFlyout.ColorPicker.Color = viewModel.Color.Value;
s_colorPickerFlyout.ColorPicker.IsAlphaEnabled = false;
s_colorPickerFlyout.ColorPicker.UseColorPalette = true;
s_colorPickerFlyout.ColorPicker.IsCompact = true;
s_colorPickerFlyout.ColorPicker.IsMoreButtonVisible = true;
s_colorPickerFlyout.Placement = PlacementMode.Top;

if (this.TryFindResource("PaletteColors", out object? colors)
&& colors is IEnumerable<Color> tcolors)
{
s_colorPickerFlyout.ColorPicker.CustomPaletteColors = tcolors;
}
s_colorPickerFlyout.ColorPicker.CustomPaletteColors = tcolors;
}

s_colorPickerFlyout.Confirmed += OnColorPickerFlyoutConfirmed;
s_colorPickerFlyout.Closed += OnColorPickerFlyoutClosed;
s_colorPickerFlyout.Confirmed += OnColorPickerFlyoutConfirmed;
s_colorPickerFlyout.Closed += OnColorPickerFlyoutClosed;

s_colorPickerFlyout.ShowAt(border);
}
s_colorPickerFlyout.ShowAt(border);
}

private void OnColorPickerFlyoutClosed(object? sender, EventArgs e)
Expand Down Expand Up @@ -325,13 +272,12 @@ public void OnLeftCtrlPressed(KeyEventArgs e)
protected override void OnAttached()
{
base.OnAttached();
if (AssociatedObject != null)
{
AssociatedObject.AddHandler(PointerMovedEvent, OnPointerMoved);
AssociatedObject.border.AddHandler(PointerPressedEvent, OnBorderPointerPressed);
AssociatedObject.border.AddHandler(PointerReleasedEvent, OnBorderPointerReleased);
AssociatedObject.border.AddHandler(PointerMovedEvent, OnBorderPointerMoved);
}
if (AssociatedObject == null) return;

AssociatedObject.AddHandler(PointerMovedEvent, OnPointerMoved);
AssociatedObject.border.AddHandler(PointerPressedEvent, OnBorderPointerPressed);
AssociatedObject.border.AddHandler(PointerReleasedEvent, OnBorderPointerReleased);
AssociatedObject.border.AddHandler(PointerMovedEvent, OnBorderPointerMoved);
}

protected override void OnDetaching()
Expand Down Expand Up @@ -398,11 +344,11 @@ private void OnPointerMoved(object? sender, PointerEventArgs e)

private void OnBorderPointerPressed(object? sender, PointerPressedEventArgs e)
{
if (AssociatedObject is { _timeline: { }, ViewModel: { } viewModel } view)
if (AssociatedObject is { _timeline: not null, ViewModel: { } viewModel } view)
{
PointerPoint point = e.GetCurrentPoint(view.border);
if (point.Properties.IsLeftButtonPressed && e.KeyModifiers is KeyModifiers.None or KeyModifiers.Alt
&& view.Cursor != Cursors.Arrow && view.Cursor is { })
&& view.Cursor != Cursors.Arrow && view.Cursor is not null)
{
_before = viewModel.Model.GetBefore(viewModel.Model.ZIndex, viewModel.Model.Start);
_after = viewModel.Model.GetAfter(viewModel.Model.ZIndex, viewModel.Model.Range.End);
Expand Down Expand Up @@ -483,29 +429,26 @@ private sealed class _MoveBehavior : Behavior<ElementView>
protected override void OnAttached()
{
base.OnAttached();
if (AssociatedObject != null)
{
AssociatedObject.AddHandler(PointerMovedEvent, OnPointerMoved);
AssociatedObject.border.AddHandler(PointerPressedEvent, OnBorderPointerPressed);
AssociatedObject.border.AddHandler(PointerReleasedEvent, OnBorderPointerReleased);
}
if (AssociatedObject == null) return;

AssociatedObject.AddHandler(PointerMovedEvent, OnPointerMoved);
AssociatedObject.border.AddHandler(PointerPressedEvent, OnBorderPointerPressed);
AssociatedObject.border.AddHandler(PointerReleasedEvent, OnBorderPointerReleased);
}

protected override void OnDetaching()
{
base.OnDetaching();
if (AssociatedObject != null)
{
AssociatedObject.RemoveHandler(PointerMovedEvent, OnPointerMoved);
AssociatedObject.border.RemoveHandler(PointerPressedEvent, OnBorderPointerPressed);
AssociatedObject.border.RemoveHandler(PointerReleasedEvent, OnBorderPointerReleased);
}
if (AssociatedObject == null) return;

AssociatedObject.RemoveHandler(PointerMovedEvent, OnPointerMoved);
AssociatedObject.border.RemoveHandler(PointerPressedEvent, OnBorderPointerPressed);
AssociatedObject.border.RemoveHandler(PointerReleasedEvent, OnBorderPointerReleased);
}

private void OnPointerMoved(object? sender, PointerEventArgs e)
{
if (AssociatedObject is { ViewModel: { } viewModel } view
&& view._timeline is { } timeline && _pressed)
if (AssociatedObject is { ViewModel: { } viewModel, _timeline: { } timeline } view && _pressed)
{
Point point = e.GetPosition(view);
float scale = viewModel.Timeline.Options.Value.Scale;
Expand Down Expand Up @@ -601,39 +544,36 @@ private sealed class _SelectBehavior : Behavior<ElementView>
protected override void OnAttached()
{
base.OnAttached();
if (AssociatedObject != null)
{
AssociatedObject.AddHandler(PointerPressedEvent, OnPointerPressed, RoutingStrategies.Tunnel);
AssociatedObject.border.AddHandler(PointerPressedEvent, OnBorderPointerPressed);
AssociatedObject.border.AddHandler(PointerReleasedEvent, OnBorderPointerReleased);
}
if (AssociatedObject == null) return;

AssociatedObject.AddHandler(PointerPressedEvent, OnPointerPressed, RoutingStrategies.Tunnel);
AssociatedObject.border.AddHandler(PointerPressedEvent, OnBorderPointerPressed);
AssociatedObject.border.AddHandler(PointerReleasedEvent, OnBorderPointerReleased);
}

protected override void OnDetaching()
{
base.OnDetaching();
if (AssociatedObject != null)
{
AssociatedObject.AddHandler(PointerPressedEvent, OnPointerPressed, RoutingStrategies.Tunnel);
AssociatedObject.border.RemoveHandler(PointerPressedEvent, OnBorderPointerPressed);
AssociatedObject.border.RemoveHandler(PointerReleasedEvent, OnBorderPointerReleased);
}
if (AssociatedObject == null) return;

AssociatedObject.AddHandler(PointerPressedEvent, OnPointerPressed, RoutingStrategies.Tunnel);
AssociatedObject.border.RemoveHandler(PointerPressedEvent, OnBorderPointerPressed);
AssociatedObject.border.RemoveHandler(PointerReleasedEvent, OnBorderPointerReleased);
}

private void OnPointerPressed(object? sender, PointerPressedEventArgs e)
{
if (AssociatedObject is { } obj)
if (AssociatedObject is not { } obj) return;

if (!obj.textBox.IsFocused)
{
if (!obj.textBox.IsFocused)
{
obj.Focus();
}
obj.Focus();
}
}

private void OnBorderPointerPressed(object? sender, PointerPressedEventArgs e)
{
if (AssociatedObject is { _timeline: { } } obj)
if (AssociatedObject is { _timeline.ViewModel: not null } obj)
{
PointerPoint point = e.GetCurrentPoint(obj.border);
if (point.Properties.IsLeftButtonPressed)
Expand All @@ -657,7 +597,7 @@ private void OnBorderPointerPressed(object? sender, PointerPressedEventArgs e)
{
Thickness margin = obj.ViewModel.Margin.Value;
Thickness borderMargin = obj.ViewModel.BorderMargin.Value;
_snapshot = new(borderMargin.Left, margin.Top, 0, 0);
_snapshot = new Thickness(borderMargin.Left, margin.Top, 0, 0);
_pressedWithModifier = true;
}

Expand All @@ -669,17 +609,19 @@ private void OnBorderPointerPressed(object? sender, PointerPressedEventArgs e)

private void OnBorderPointerReleased(object? sender, PointerReleasedEventArgs e)
{
if (AssociatedObject is { _timeline: { } } obj)
if (AssociatedObject is { _timeline: not null } obj)
{
if (_pressedWithModifier)
{
Thickness margin = obj.ViewModel.Margin.Value;
Thickness borderMargin = obj.ViewModel.BorderMargin.Value;
// ReSharper disable CompareOfFloatsByEqualityOperator
if (borderMargin.Left == _snapshot.Left
&& margin.Top == _snapshot.Top)
{
obj.ViewModel.IsSelected.Value = !obj.ViewModel.IsSelected.Value;
}
// ReSharper restore CompareOfFloatsByEqualityOperator

_pressedWithModifier = false;
}
Expand Down

0 comments on commit d459831

Please sign in to comment.