Skip to content

Commit 464d288

Browse files
committed
refactoring
1 parent b99314d commit 464d288

File tree

4 files changed

+99
-40
lines changed

4 files changed

+99
-40
lines changed

sources/RevitDBExplorer/MainWindow.xaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
xmlns:controls="clr-namespace:RevitDBExplorer.WPF.Controls"
1717
xmlns:ext="clr-namespace:RevitDBExplorer.WPF.MarkupExtensions"
1818
xmlns:wpf="clr-namespace:RevitDBExplorer.WPF"
19+
xmlns:ap="clr-namespace:RevitDBExplorer.WPF.AttachedProperties"
1920
mc:Ignorable="d"
2021
WindowStartupLocation="CenterOwner"
2122
Title="Revit database explorer and editor"
@@ -133,7 +134,7 @@
133134
<TextBlock Margin="0 2 0 0">Face</TextBlock>
134135
</StackPanel>
135136
</Button>-->
136-
<Button Click="ButtonWithSubMenu_Click" ToolTip="Snoop by picking given object type in Revit">
137+
<Button ap:ButtonAP.OpenSubMenuOnClick="True" ToolTip="Snoop by picking given object type in Revit">
137138
<StackPanel>
138139
<Path Style="{StaticResource IconPickEdge}" />
139140
<TextBlock Margin="0 2 0 0">Pick</TextBlock>
@@ -165,7 +166,7 @@
165166
<TextBlock Margin="0 0 0 0">Categories</TextBlock>
166167
</StackPanel>
167168
</Button>
168-
<Button Click="ButtonWithSubMenu_Click" ToolTip="Snoop Forge types">
169+
<Button ap:ButtonAP.OpenSubMenuOnClick="True" ToolTip="Snoop Forge types">
169170
<StackPanel>
170171
<Path Style="{StaticResource IconForge}" />
171172
<TextBlock Margin="0 6 0 0">Forge</TextBlock>
@@ -186,8 +187,8 @@
186187
<Path Style="{StaticResource IconEvent}" SnapsToDevicePixels="True" />
187188
<TextBlock Margin="0 0 0 0">Events</TextBlock>
188189
</StackPanel>
189-
</Button>
190-
<Button Click="ButtonWithSubMenu_Click" Margin="2 0 0 0" ToolTip="Snoop addins">
190+
</Button>
191+
<Button ap:ButtonAP.OpenSubMenuOnClick="True" Margin="2 0 0 0" ToolTip="Snoop addins">
191192
<StackPanel>
192193
<Path Style="{StaticResource IconAddin}" />
193194
<TextBlock Margin="0 2 0 0">Addins</TextBlock>
@@ -320,7 +321,7 @@
320321
<Grid Grid.ColumnSpan="2" Background="{DynamicResource StatusBar.Background}" HorizontalAlignment="Center">
321322
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="{Binding IsNewVerAvailable, Converter={StaticResource BoolToVisibilityConverterCollapsed}}">
322323
<Run Foreground="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}">a new version is available :</Run>
323-
<Hyperlink NavigateUri="{Binding NewVersionLink}" RequestNavigate="Hyperlink_RequestNavigate" Foreground="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" TextDecorations="None" >
324+
<Hyperlink NavigateUri="{Binding NewVersionLink}" ap:HyperlinkAP.OpenOnClick="True" Foreground="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" TextDecorations="None" >
324325
<Run Text="{Binding NewVersionLink}" />
325326
</Hyperlink>
326327
</TextBlock>

sources/RevitDBExplorer/MainWindow.xaml.cs

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.ComponentModel;
4-
using System.Diagnostics;
53
using System.Linq;
64
using System.Runtime.CompilerServices;
75
using System.Threading.Tasks;
@@ -205,6 +203,14 @@ public bool IsBoundingBoxVisualizerEnabled
205203
public RelayCommand SaveQueryAsFavoriteCommand { get; }
206204

207205

206+
public MainWindow(SourceOfObjects sourceOfObjects, IntPtr? parentWindowHandle = null) : this()
207+
{
208+
if (parentWindowHandle.HasValue)
209+
{
210+
new WindowInteropHelper(this).Owner = parentWindowHandle.Value;
211+
}
212+
PopulateExplorerTree(sourceOfObjects);
213+
}
208214
public MainWindow()
209215
{
210216
Dispatcher.UnhandledException += Dispatcher_UnhandledException;
@@ -241,16 +247,6 @@ private void IsRevitBusyDispatcher_Tick(object sender, EventArgs e)
241247
IsRevitBusy = Application.IsRevitBussy();
242248
MouseStatus = Application.GetMouseStatus();
243249
}
244-
public MainWindow(SourceOfObjects sourceOfObjects, IntPtr? parentWindowHandle = null) : this()
245-
{
246-
if (parentWindowHandle.HasValue)
247-
{
248-
new WindowInteropHelper(this).Owner = parentWindowHandle.Value;
249-
}
250-
PopulateExplorerTree(sourceOfObjects);
251-
}
252-
253-
254250
private void Dispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
255251
{
256252
e.Exception.ShowErrorMsg("MainWindow::UnhandledException");
@@ -378,18 +374,15 @@ void IAmQueryExecutor.Query(string query)
378374

379375
private void PopulateExplorerTree(SourceOfObjects sourceOfObjects)
380376
{
381-
382377
ExplorerTree.PopulateTreeView(sourceOfObjects);
383378
Breadcrumbs.Set(sourceOfObjects.Title);
384379
}
385-
386-
387380
private void ResetDatabaseQuery()
388381
{
389382
databaseQuery = "";
390383
OnPropertyChanged(nameof(DatabaseQuery));
391384
DatabaseQueryToolTip = "";
392-
queryVisualizationVM.Update(Enumerable.Empty<RDQCommand>()).Forget();
385+
QueryVisualization.Update(Enumerable.Empty<RDQCommand>()).Forget();
393386
}
394387

395388

@@ -409,18 +402,7 @@ private void SaveQueryAsFavorite(object parameter)
409402
FavoritesManager.Add(DatabaseQuery);
410403
}
411404

412-
413-
private void ButtonWithSubMenu_Click(object sender, RoutedEventArgs e)
414-
{
415-
var contextMenu = ContextMenuService.GetContextMenu(sender as DependencyObject);
416-
if (contextMenu == null)
417-
{
418-
return;
419-
}
420-
contextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
421-
contextMenu.PlacementTarget = sender as UIElement;
422-
contextMenu.IsOpen = true;
423-
}
405+
424406
private void Window_Closed(object sender, EventArgs e)
425407
{
426408
rdvController.Dispose();
@@ -430,7 +412,7 @@ private void Window_Closed(object sender, EventArgs e)
430412
ExplorerTree.SelectedItemChanged -= Tree_SelectedItemChanged;
431413
ExplorerTree.ScriptWasGenerated -= RDSOpenWithCommand;
432414
UtilityTree.SelectedItemChanged -= Tree_SelectedItemChanged;
433-
UtilityTree.ScriptWasGenerated -= RDSOpenWithCommand;
415+
UtilityTree.ScriptWasGenerated -= RDSOpenWithCommand;
434416
}
435417
private void Window_Closing(object sender, EventArgs e)
436418
{
@@ -474,12 +456,7 @@ private void Window_MenuItem_ConfigurationClick(object sender, RoutedEventArgs e
474456
skinDict.UpdateSource();
475457
}
476458
}
477-
478-
private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
479-
{
480-
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
481-
e.Handled = true;
482-
}
459+
483460

484461
private void RDS_Click(object sender, RoutedEventArgs e)
485462
{
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Windows;
2+
using System.Windows.Controls;
3+
4+
// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md
5+
6+
namespace RevitDBExplorer.WPF.AttachedProperties
7+
{
8+
internal class ButtonAP
9+
{
10+
public static readonly DependencyProperty OpenSubMenuOnClickProperty = DependencyProperty.RegisterAttached("OpenSubMenuOnClick", typeof(bool), typeof(ButtonAP), new PropertyMetadata(false, OpenSubMenuOnClickPropertyChanged));
11+
12+
13+
public static bool GetOpenSubMenuOnClick(DependencyObject element)
14+
{
15+
return (bool)element.GetValue(OpenSubMenuOnClickProperty);
16+
}
17+
public static void SetOpenSubMenuOnClick(DependencyObject element, bool value)
18+
{
19+
element.SetValue(OpenSubMenuOnClickProperty, value);
20+
}
21+
22+
23+
private static void OpenSubMenuOnClickPropertyChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
24+
{
25+
if (element is Button button)
26+
{
27+
button.Click += Button_Click;
28+
}
29+
}
30+
31+
private static void Button_Click(object sender, RoutedEventArgs e)
32+
{
33+
var contextMenu = ContextMenuService.GetContextMenu(sender as DependencyObject);
34+
if (contextMenu == null)
35+
{
36+
return;
37+
}
38+
contextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
39+
contextMenu.PlacementTarget = sender as UIElement;
40+
contextMenu.IsOpen = true;
41+
}
42+
}
43+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Diagnostics;
2+
using System.Windows;
3+
using System.Windows.Documents;
4+
5+
// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md
6+
7+
namespace RevitDBExplorer.WPF.AttachedProperties
8+
{
9+
internal class HyperlinkAP
10+
{
11+
public static readonly DependencyProperty OpenOnClickProperty = DependencyProperty.RegisterAttached("OpenOnClick", typeof(bool), typeof(ButtonAP), new PropertyMetadata(false, OpenOnClickkPropertyChanged));
12+
13+
14+
public static bool GetOpenOnClick(DependencyObject element)
15+
{
16+
return (bool)element.GetValue(OpenOnClickProperty);
17+
}
18+
public static void SetOpenOnClick(DependencyObject element, bool value)
19+
{
20+
element.SetValue(OpenOnClickProperty, value);
21+
}
22+
23+
24+
private static void OpenOnClickkPropertyChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
25+
{
26+
if (element is Hyperlink hyperlink)
27+
{
28+
hyperlink.RequestNavigate += Hyperlink_RequestNavigate;
29+
}
30+
}
31+
32+
private static void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
33+
{
34+
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
35+
e.Handled = true;
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)