Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enable closing of dock tabs #1110

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Beutl.Language/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Beutl.Language/Strings.ja.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1249,4 +1249,7 @@ b-editorがダウンロードURLを管理します。</value>
<data name="DockArea_TopRight" xml:space="preserve">
<value>上 右</value>
</data>
<data name="MoveTo" xml:space="preserve">
<value>移動</value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/Beutl.Language/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1249,4 +1249,7 @@ and b-editor maintains the download URL.</value>
<data name="DockArea_TopRight" xml:space="preserve">
<value>Top Right</value>
</data>
<data name="MoveTo" xml:space="preserve">
<value>Move to</value>
</data>
</root>
8 changes: 8 additions & 0 deletions src/Beutl/ViewModels/DockHostViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,19 @@ public void CloseToolTab(IToolContext item)
_logger.LogInformation("CloseToolTab {ToolName}", item.Extension.Name);
try
{
item.IsSelected.Value = false;
foreach (ReactiveCollection<ToolTabViewModel> tools in GetNestedTools())
{
if (tools.FirstOrDefault(x => x.Context == item) is { } found)
{
int index = tools.IndexOf(found);
tools.Remove(found);
index = Math.Min(index, tools.Count - 1);
if (0 <= index && index < tools.Count)
{
tools[index].Context.IsSelected.Value = true;
}

break;
}
}
Expand Down
105 changes: 105 additions & 0 deletions src/Beutl/Views/CustomSideBarButtonMenuFlyout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Avalonia.Interactivity;
using Avalonia.VisualTree;
using Beutl.ViewModels;
using ReDocking;

namespace Beutl.Views;

public class CustomSideBarButtonMenuFlyout : MenuFlyout
{
private readonly ReDockHost _dockHost;

public CustomSideBarButtonMenuFlyout(ReDockHost dockHost)
{
_dockHost = dockHost;
var list = new List<Control>();

{
var moveMenu = new MenuItem();
moveMenu.Header = Strings.MoveTo;
moveMenu.ItemsSource = dockHost.DockAreas;
moveMenu.DataTemplates.Add(new FuncDataTemplate<DockArea>(_ => true,
o => new TextBlock
{
[!TextBlock.TextProperty] = o.GetObservable(DockArea.LocalizedNameProperty).ToBinding(),
}));

moveMenu.AddHandler(MenuItem.ClickEvent, OnMoveToSubItemClick);
list.Add(moveMenu);
}

{
var closeMenu = new MenuItem();
closeMenu.Header = Strings.Close;
closeMenu.AddHandler(MenuItem.ClickEvent, OnCloseClick);
list.Add(closeMenu);
}

if (dockHost.IsFloatingEnabled)
{
var displayMenu = new MenuItem();
displayMenu.Header = "Display mode";
displayMenu.ItemsSource = new List<Control>
{
new MenuItem { Header = "Docked", Tag = DockableDisplayMode.Docked },
new MenuItem { Header = "Floating", Tag = DockableDisplayMode.Floating },
};
displayMenu.AddHandler(MenuItem.ClickEvent, OnDisplayModeClick);
list.Add(displayMenu);
}

ItemsSource = list;
}

private void OnCloseClick(object? sender, RoutedEventArgs e)
{
if (Target is not SideBarButton button) return;
if (button is not { DataContext: ToolTabViewModel tabViewModel }) return;
if (button.FindAncestorOfType<EditView>() is not { DataContext: EditViewModel viewModel }) return;

viewModel.CloseToolTab(tabViewModel.Context);
}

private void OnDisplayModeClick(object? sender, RoutedEventArgs e)
{
if (e.Source is MenuItem { Tag: DockableDisplayMode mode } &&
Target is SideBarButton button)
{
var args = new SideBarButtonDisplayModeChangedEventArgs(ReDockHost.ButtonDisplayModeChangedEvent, this)
{
DisplayMode = mode, Item = button.DataContext, Button = button
};
_dockHost.RaiseEvent(args);
}
}

private void OnMoveToSubItemClick(object? sender, RoutedEventArgs e)
{
if (e.Source is MenuItem { DataContext: DockArea area } &&
Target is SideBarButton button)
{
// Target
var oldSideBar = button.FindAncestorOfType<SideBar>();
var newSideBar = area.SideBar;
if (oldSideBar is null || newSideBar is null) return;
var oldLocation = button.DockLocation;
var newLocation = area.Location;
if (oldLocation is null || oldLocation == newLocation) return;

var args = new SideBarButtonMoveEventArgs(ReDockHost.ButtonMoveEvent, this)
{
Item = button.DataContext,
Button = button,
SourceSideBar = oldSideBar,
SourceLocation = oldLocation,
DestinationSideBar = newSideBar,
DestinationLocation = newLocation,
DestinationIndex = 0
};
_dockHost.RaiseEvent(args);
}
}
}
1 change: 1 addition & 0 deletions src/Beutl/Views/EditView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
</UserControl.Resources>
<dock:ReDockHost Name="DockHost"
ButtonDisplayModeChanged="OnSideBarButtonDisplayModeChanged"
ButtonFlyoutRequested="OnSideBarButtonFlyoutRequested"
ButtonMove="OnSideBarButtonDrop"
IsFloatingEnabled="False">
<dock:ReDockHost.DockAreas>
Expand Down
18 changes: 17 additions & 1 deletion src/Beutl/Views/EditView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void OnSideBarButtonDisplayModeChanged(object? sender, SideBarButtonDisp
{
}

private ReactiveCollection<ToolTabViewModel> GetItemsSource(DockAreaLocation location)
internal ReactiveCollection<ToolTabViewModel> GetItemsSource(DockAreaLocation location)
{
var sideBar = DockHost.DockAreas.FirstOrDefault(i => i.SideBar?.Location == location.LeftRight)?.SideBar;

Expand Down Expand Up @@ -156,4 +156,20 @@ private void OnSideBarButtonDrop(object? sender, SideBarButtonMoveEventArgs e)

e.Handled = true;
}

private void OnSideBarButtonFlyoutRequested(object? sender, SideBarButtonFlyoutRequestedEventArgs e)
{
e.Handled = true;
var flyout = new CustomSideBarButtonMenuFlyout(e.DockHost);
if (e.Button.DockLocation?.LeftRight == SideBarLocation.Left)
{
flyout.Placement = PlacementMode.Right;
}
else
{
flyout.Placement = PlacementMode.Left;
}

flyout.ShowAt(e.Button);
}
}
Loading