optimize<*>: remove dragdrop adorner; redesign bookmark style on welcome page

This commit is contained in:
leo 2022-10-18 14:50:48 +08:00
parent bdd147f737
commit 787cf5bb02
9 changed files with 36 additions and 151 deletions

View file

@ -24,7 +24,6 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<AdornerDecorator>
<Border
Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
@ -32,7 +31,6 @@
BorderBrush="{TemplateBinding BorderBrush}">
<ContentPresenter/>
</Border>
</AdornerDecorator>
</ControlTemplate>
</Setter.Value>
</Setter>

View file

@ -1,49 +0,0 @@
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
namespace SourceGit.Views.Controls {
/// <summary>
/// 展示正在拖拽的视图
/// </summary>
public class DragDropAdorner : Adorner {
private FrameworkElement renderElem;
public struct PInPoint {
public int X;
public int Y;
public PInPoint(int x, int y) { X = x; Y = y; }
public PInPoint(double x, double y) { X = (int)x; Y = (int)y; }
}
[DllImport("user32.dll")]
static extern void GetCursorPos(ref PInPoint p);
public DragDropAdorner(FrameworkElement elem) : base(elem) {
renderElem = elem;
IsHitTestVisible = false;
Window.AddAdorner(elem, this);
}
public void Remove() {
Window.RemoveAdorner(renderElem, this);
}
protected override void OnRender(DrawingContext dc) {
base.OnRender(dc);
PInPoint p = new PInPoint();
GetCursorPos(ref p);
Point pos = PointFromScreen(new Point(p.X, p.Y));
Rect rect = new Rect(pos.X, pos.Y, renderElem.RenderSize.Width, renderElem.RenderSize.Height);
dc.PushOpacity(1);
dc.DrawRectangle(FindResource("Brush.Window") as Brush, null, rect);
dc.DrawRectangle(new VisualBrush(renderElem), null, rect);
dc.DrawRectangle(null, new Pen(Brushes.DeepSkyBlue, 2), rect);
}
}
}

View file

@ -20,37 +20,9 @@ namespace SourceGit.Views.Controls {
set { SetValue(IsMaximizedProperty, value); }
}
private AdornerLayer adornerLayer = null;
private List<Adorner> adorners = new List<Adorner>();
public Window() {
Style = FindResource("Style.Window") as Style;
Loaded += (_, __) => {
adornerLayer = AdornerLayer.GetAdornerLayer(Content as FrameworkElement);
OnStateChanged(null);
};
}
public static void AddAdorner(FrameworkElement windowContext, Adorner adorner) {
var wnd = GetWindow(windowContext) as Window;
if (wnd != null && wnd.adornerLayer != null) {
wnd.adorners.Add(adorner);
wnd.adornerLayer.Add(adorner);
}
}
public static void RemoveAdorner(FrameworkElement windowContext, Adorner adorner) {
var wnd = GetWindow(windowContext) as Window;
if (wnd != null && wnd.adornerLayer != null) {
wnd.adorners.Remove(adorner);
wnd.adornerLayer.Remove(adorner);
}
}
protected override void OnPreviewGiveFeedback(GiveFeedbackEventArgs e) {
base.OnPreviewGiveFeedback(e);
if (adornerLayer != null && adorners.Count > 0) adornerLayer.Update();
Loaded += (_, __) => OnStateChanged(null);
}
protected override void OnStateChanged(EventArgs e) {

View file

@ -19,7 +19,7 @@ namespace SourceGit.Views.Converters {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
var index = (int)value;
return index == 0 ? (App.Current.FindResource("Brush.FG1") as Brush) : COLORS[index];
return COLORS[index];
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {

View file

@ -144,7 +144,6 @@ namespace SourceGit.Views {
#region TAB_OPERATION
private void OnTabAdding(object sender, Widgets.PageTabBar.TabEventArgs e) {
var page = new Widgets.Welcome();
page.OnBookmarkChanged += repo => tabs.Update(repo.Path, repo.Bookmark, repo.Name);
container.Add(e.TabId, page);
Controls.PopupWidget.RegisterContainer(e.TabId, page);
}

View file

@ -64,6 +64,11 @@
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="ContextMenuOpening" Handler="OnTabContextMenuOpening" />
<EventSetter Event="MouseMove" Handler="OnMouseMove"/>
<EventSetter Event="DragOver" Handler="OnDragOver"/>
<EventSetter Event="Drop" Handler="OnDrop"/>
<EventSetter Event="GiveFeedback" Handler="OnGiveFeedback"/>
<Setter Property="AllowDrop" Value="True"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
@ -125,9 +130,6 @@
</ControlTemplate>
</Setter.Value>
</Setter>
<EventSetter Event="MouseMove" Handler="OnMouseMove"/>
<EventSetter Event="Drop" Handler="OnDrop"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>

View file

@ -269,12 +269,25 @@ namespace SourceGit.Views.Widgets {
if (tab == null || tab != container.SelectedItem) return;
if (e.LeftButton == MouseButtonState.Pressed) {
var dragging = new Controls.DragDropAdorner(item);
DragDrop.DoDragDrop(item, item.DataContext, DragDropEffects.Move);
dragging.Remove();
}
}
private void OnGiveFeedback(object sender, GiveFeedbackEventArgs e) {
if (e.Effects == DragDropEffects.Move) {
e.UseDefaultCursors = false;
Mouse.SetCursor(Cursors.Hand);
} else {
e.UseDefaultCursors = true;
}
e.Handled = true;
}
private void OnDragOver(object sender, DragEventArgs e) {
OnDrop(sender, e);
}
private void OnDrop(object sender, DragEventArgs e) {
var tabSrc = e.Data.GetData(typeof(Tab)) as Tab;
if (tabSrc == null) return;
@ -323,13 +336,12 @@ namespace SourceGit.Views.Widgets {
menu.Items.Add(closeRight);
if (tab.IsRepository) {
var iconBookmark = FindResource("Icon.Git") as Geometry;
var bookmark = new MenuItem();
bookmark.Header = App.Text("PageTabBar.Tab.Bookmark");
for (int i = 0; i < Converters.IntToBookmarkBrush.COLORS.Length; i++) {
var icon = new System.Windows.Shapes.Path();
icon.Data = iconBookmark;
icon.Fill = i == 0 ? (FindResource("Brush.FG1") as Brush) : Converters.IntToBookmarkBrush.COLORS[i];
icon.Data = new EllipseGeometry(new Point(0, 0), 12, 12);
icon.Fill = Converters.IntToBookmarkBrush.COLORS[i];
icon.Width = 12;
var mark = new MenuItem();

View file

@ -124,25 +124,20 @@
Height="36" Margin="0,2"
BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}"
Background="{DynamicResource Brush.Contents}">
<Grid Margin="8,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="8"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="32"/>
<ColumnDefinition Width="32"/>
<ColumnDefinition Width="32"/>
<ColumnDefinition Width="32"/>
<ColumnDefinition Width="8"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Background="Transparent" Width="16" BorderThickness="0" ToolTip="{DynamicResource Text.RepoCM.Bookmark}" Click="OnChangeRepositoryBookmark">
<Path Width="8" Fill="{Binding Bookmark, Converter={StaticResource IntToBookmarkBrush}}">
<Path.Data>
<EllipseGeometry Center="0,0" RadiusX="14" RadiusY="14"/>
</Path.Data>
</Path>
</Button>
<Border Grid.Column="0" Background="{Binding Bookmark, Converter={StaticResource IntToBookmarkBrush}}"/>
<StackPanel Grid.Column="1" Margin="8,0" Orientation="Horizontal">
<StackPanel Grid.Column="1" Margin="4,0" Orientation="Horizontal">
<TextBlock Text="{Binding Name}" FontSize="12pt" FontFamily="{Binding Source={x:Static models:Preference.Instance}, Path=General.FontFamilyContent, Mode=OneWay}"/>
<TextBlock Text="{Binding Path}" FontSize="10pt" FontFamily="{Binding Source={x:Static models:Preference.Instance}, Path=General.FontFamilyContent, Mode=OneWay}" Margin="8,0" Foreground="{DynamicResource Brush.FG2}"/>
</StackPanel>

View file

@ -16,11 +16,6 @@ namespace SourceGit.Views.Widgets {
/// </summary>
public partial class Welcome : UserControl, Controls.IPopupContainer {
/// <summary>
/// 修改仓库标签颜色的回调
/// </summary>
public event Action<Models.Repository> OnBookmarkChanged;
public Welcome() {
InitializeComponent();
UpdateVisibles();
@ -135,45 +130,6 @@ namespace SourceGit.Views.Widgets {
e.Handled = true;
}
private void OnChangeRepositoryBookmark(object sender, RoutedEventArgs e) {
var btn = (sender as Button);
var repo = btn.DataContext as Models.Repository;
if (repo == null) return;
var menu = new ContextMenu();
menu.Placement = PlacementMode.Bottom;
menu.PlacementTarget = btn;
menu.StaysOpen = false;
menu.Focusable = true;
for (int i = 0; i < Converters.IntToBookmarkBrush.COLORS.Length; i++) {
var icon = new System.Windows.Shapes.Path();
icon.Data = new EllipseGeometry(new Point(0, 0), 8, 8);
icon.Fill = i == 0 ? (FindResource("Brush.FG1") as Brush) : Converters.IntToBookmarkBrush.COLORS[i];
icon.Width = 12;
var mark = new MenuItem();
mark.Icon = icon;
mark.Header = $"{i}";
var refIdx = i;
mark.Click += (o, ev) => {
if (repo != null) {
repo.Bookmark = refIdx;
UpdateVisibles();
OnBookmarkChanged?.Invoke(repo);
}
ev.Handled = true;
};
menu.Items.Add(mark);
}
btn.ContextMenu = menu;
btn.ContextMenu.IsOpen = true;
e.Handled = true;
}
private void OnOpenRepositoryTerminal(object sender, RoutedEventArgs e) {
var repo = (sender as Button).DataContext as Models.Repository;
if (repo == null) return;