Skip to content

Commit 2dbab30

Browse files
committed
Improve WPF logic; misc
1 parent a0e191b commit 2dbab30

13 files changed

+186
-44
lines changed

SmartImage.Lib 3/Engines/Impl/Search/IqdbEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public sealed class IqdbEngine : BaseSearchEngine, IEndpoint
3434
public IqdbEngine() : base("https://iqdb.org/?url=")
3535
{
3636
MaxSize = MAX_FILE_SIZE; // NOTE: assuming IQDB uses kilobytes instead of kibibytes
37-
37+
Timeout = TimeSpan.FromSeconds(10);
3838
}
3939

4040
private SearchResultItem ParseResult(IHtmlCollection<IElement> tr, SearchResult r)
File renamed without changes.
File renamed without changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Read S SmartImage.UI InvertableBooleanToVisibilityConverter.cs
2+
// 2023-09-02 @ 3:46 AM
3+
4+
using System;
5+
using System.Globalization;
6+
using System.Windows;
7+
using System.Windows.Data;
8+
9+
namespace SmartImage.UI.Controls;
10+
11+
[ValueConversion(typeof(bool), typeof(Visibility))]
12+
public class InvertableBooleanToVisibilityConverter : IValueConverter
13+
{
14+
public enum Parameters
15+
{
16+
Normal, Inverted
17+
}
18+
19+
public object Convert(object value, Type targetType,
20+
object parameter, CultureInfo culture)
21+
{
22+
var boolValue = (bool)value;
23+
var direction = (Parameters)Enum.Parse(typeof(Parameters), (string)parameter);
24+
25+
if (direction == Parameters.Inverted)
26+
return !boolValue ? Visibility.Visible : Visibility.Hidden;
27+
28+
return boolValue ? Visibility.Visible : Visibility.Hidden;
29+
}
30+
31+
public object ConvertBack(object value, Type targetType,
32+
object parameter, CultureInfo culture)
33+
{
34+
return null;
35+
}
36+
}

SmartImage.UI/MainWindow.Handlers.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -301,35 +301,25 @@ private void Lv_Results_SelectionChanged(object sender, SelectionChangedEventArg
301301

302302
if (ri is UniResultItem uri) {
303303
UpdatePreview(uri);
304+
CheckMedia();
304305
}
305306
else if (ri.Result.Root.Engine.EngineOption != SearchEngineOptions.TraceMoe){
306307
UpdatePreview(ri);
308+
CheckMedia();
307309
}
308310
else {
309311
UpdatePreview(m_image);
310312
if (ri.Result.Metadata is TraceMoeEngine.TraceMoeDoc doc) {
311-
Br_Preview.Visibility = Visibility.Hidden;
312-
Img_Preview.Visibility = Visibility.Hidden;
313-
Br_Preview2.Visibility = Visibility.Visible;
314-
Me_Preview.Visibility = Visibility.Visible;
313+
315314
Me_Preview.ScrubbingEnabled = false;
316315
Me_Preview.UnloadedBehavior = MediaState.Close;
317316
Me_Preview.LoadedBehavior = MediaState.Manual;
318317
Me_Preview.Source = new Uri(doc.video, UriKind.Absolute);
319318
Me_Preview.Play();
320-
m_me = true;
319+
ShowMedia = true;
321320
}
322321
else {
323-
if (m_me) {
324-
Br_Preview.Visibility = Visibility.Visible;
325-
Img_Preview.Visibility = Visibility.Visible;
326-
Br_Preview2.Visibility = Visibility.Hidden;
327-
Me_Preview.Visibility = Visibility.Hidden;
328-
Me_Preview.Stop();
329-
Me_Preview.Close();
330-
Me_Preview.Source = null;
331-
m_me = false;
332-
}
322+
CheckMedia();
333323
}
334324

335325
}
@@ -339,6 +329,17 @@ private void Lv_Results_SelectionChanged(object sender, SelectionChangedEventArg
339329
}
340330

341331
e.Handled = true;
332+
return;
333+
334+
void CheckMedia()
335+
{
336+
if (ShowMedia) {
337+
Me_Preview.Stop();
338+
Me_Preview.Close();
339+
Me_Preview.Source = null;
340+
ShowMedia = false;
341+
}
342+
}
342343
}
343344

344345
private void Lv_Results_KeyDown(object sender, KeyEventArgs e)
@@ -508,7 +509,8 @@ private void Wnd_Main_Loaded(object sender, RoutedEventArgs e)
508509
m_cbDispatch.Start();
509510
}
510511

511-
m_trDispatch.Start();
512+
// todo: not used for now
513+
// m_trDispatch.Start();
512514
e.Handled = true;
513515
Debug.WriteLine("Main loaded");
514516

@@ -606,7 +608,8 @@ private void Img_Preview_MouseLeftButtonDown(object sender, MouseButtonEventArgs
606608
};
607609
this.m_popup.Show();
608610
}*/
609-
FileSystem.Open(CurrentQueueItem);
611+
var s = Img_Preview.Source.ToString();
612+
FileSystem.Open(s);
610613
}
611614

612615
e.Handled = true;

SmartImage.UI/MainWindow.State.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,17 @@ private int FindIndex(Predicate<ResultItem> f)
6666

6767
return Results.IndexOf(r);
6868
}
69+
70+
private bool m_showMedia;
71+
72+
public bool ShowMedia
73+
{
74+
get => m_showMedia;
75+
set
76+
{
77+
if (value == m_showMedia) return;
78+
m_showMedia = value;
79+
OnPropertyChanged();
80+
}
81+
}
6982
}

SmartImage.UI/MainWindow.xaml

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
88
xmlns:ui="clr-namespace:SmartImage.UI"
99
xmlns:converters="clr-namespace:SmartImage.UI"
10+
xmlns:controls="clr-namespace:SmartImage.UI.Controls"
1011
DataContext="{Binding RelativeSource={RelativeSource Self}}"
1112
mc:Ignorable="d" Height="600" Width="1300" MinWidth="1300" MinHeight="300" Loaded="Wnd_Main_Loaded"
1213
Closed="Wnd_Main_Closed"
1314
Closing="Wnd_Main_Closing" Title="SmartImage" Unloaded="Wnd_Main_Unloaded" Background="#FF191919"
1415
KeyDown="Wnd_Main_KeyDown">
1516
<Window.Resources>
17+
<!-- <ui:SharedViewModel x:Key="SharedViewModel" /> -->
18+
<!-- <ui:SharedImageControl x:Key="SharedImage" /> -->
19+
1620
<converters:BooleanToBrushConverter x:Key="BooleanToBrushConverter">
1721
<converters:BooleanToBrushConverter.TrueBrush>
18-
<SolidColorBrush Color="Green"/>
22+
<SolidColorBrush Color="Green" />
1923
</converters:BooleanToBrushConverter.TrueBrush>
2024
<converters:BooleanToBrushConverter.FalseBrush>
21-
<SolidColorBrush Color="Red"/>
25+
<SolidColorBrush Color="Red" />
2226
</converters:BooleanToBrushConverter.FalseBrush>
2327
</converters:BooleanToBrushConverter>
2428
<Style x:Key="CustomContextMenuStyle" TargetType="{x:Type ContextMenu}">
@@ -57,7 +61,10 @@
5761
<MenuItem Header="Retry" Click="RetryItem_Click" />
5862
<MenuItem Header="Enqueue" Click="EnqueueItem_Click" IsEnabled="{Binding CanDownload}" />
5963
</ContextMenu>
64+
6065
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
66+
<controls:InvertableBooleanToVisibilityConverter x:Key="BooleanVisConverter"/>
67+
6168
<Style x:Key="BoldGridViewCellStyle" TargetType="{x:Type TextBlock}">
6269
<Setter Property="FontWeight" Value="Bold" />
6370
<Setter Property="HorizontalAlignment" Value="Center" />
@@ -294,14 +301,17 @@
294301
SelectedItem="{Binding CurrentQueueItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, IsAsync=True}"
295302
IsSynchronizedWithCurrentItem="True"
296303
KeyDown="Lb_Queue_KeyDown" Margin="0,2,174,0" HorizontalAlignment="Right"
297-
VerticalAlignment="Top" Width="320" Height="150" Grid.Column="1" Grid.RowSpan="2"/>
304+
VerticalAlignment="Top" Width="320" Height="150" Grid.Column="1" Grid.RowSpan="2" />
305+
298306
<Border x:Name="Br_Preview" Height="150" Margin="0,3,11,0" Width="150"
299307
Background="{DynamicResource Black2}"
300308
VerticalAlignment="Top" HorizontalAlignment="Right" Grid.RowSpan="2" Panel.ZIndex="1"
301-
Grid.Column="1">
309+
Grid.Column="1"
310+
Visibility="{Binding ShowMedia, Converter={StaticResource BooleanVisConverter}, ConverterParameter=Inverted}">
302311

303312
<Image x:Name="Img_Preview" MouseLeftButtonDown="Img_Preview_MouseLeftButtonDown"
304-
Width="150" HorizontalAlignment="Right" VerticalAlignment="Top" Panel.ZIndex="0">
313+
Width="150" HorizontalAlignment="Right" VerticalAlignment="Top" Panel.ZIndex="0"
314+
Visibility="{Binding ShowMedia, Converter={StaticResource BooleanVisConverter}, ConverterParameter=Inverted}">
305315

306316
<Image.RenderTransformOrigin>
307317
<Point X="1" Y="0"></Point>
@@ -346,9 +356,9 @@
346356
<Border x:Name="Br_Preview2" Height="150" Margin="0,2,11,0" Width="150"
347357
Background="{DynamicResource Black2}"
348358
VerticalAlignment="Top" HorizontalAlignment="Right" Grid.RowSpan="2" Panel.ZIndex="1"
349-
Grid.Column="1" Visibility="Hidden">
359+
Grid.Column="1" Visibility="{Binding ShowMedia, Converter={StaticResource BooleanVisConverter}, ConverterParameter=Normal}">
350360
<MediaElement x:Name="Me_Preview" HorizontalAlignment="Right" VerticalAlignment="Top"
351-
Width="150" MouseDown="Me_Preview_MouseDown">
361+
Width="150" MouseDown="Me_Preview_MouseDown" Visibility="{Binding ShowMedia, Converter={StaticResource BooleanVisConverter}, ConverterParameter=Normal}">
352362
<MediaElement.RenderTransformOrigin>
353363
<Point X="1" Y="0"></Point>
354364
</MediaElement.RenderTransformOrigin>
@@ -398,7 +408,7 @@
398408
MouseDoubleClick="Tb_Upload_MouseDoubleClick" Foreground="White" Background="Black" />
399409

400410
<Button x:Name="Btn_Clear" Content="Clear" HorizontalAlignment="Left" Margin="501,0,0,0"
401-
VerticalAlignment="Center" Height="20" Click="Btn_Clear_Click"/>
411+
VerticalAlignment="Center" Height="20" Click="Btn_Clear_Click" />
402412

403413
<Button x:Name="Btn_Restart" Content="Restart" HorizontalAlignment="Left" Margin="537,0,0,0"
404414
VerticalAlignment="Center" Click="Btn_Restart_Click"
@@ -632,15 +642,16 @@
632642
<Label x:Name="Lbl_UploadEngines" Content="Upload Engine" HorizontalAlignment="Left"
633643
Margin="600,3,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" />
634644
<TextBox x:Name="Tb_HyEndpoint" HorizontalAlignment="Left" Margin="527,297,0,0" TextWrapping="Wrap"
635-
VerticalAlignment="Top" Width="120"
636-
Text="{Binding Config.HydrusEndpoint, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
645+
VerticalAlignment="Top" Width="120"
646+
Text="{Binding Config.HydrusEndpoint, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
637647
<TextBox x:Name="Tb_HyKey" HorizontalAlignment="Left" Margin="527,326,0,0" TextWrapping="Wrap"
638-
VerticalAlignment="Top" Width="120"
639-
Text="{Binding Config.HydrusKey, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
640-
<Label x:Name="Lb_HyEndpoint" Content="Hydrus Endpoint" HorizontalAlignment="Left" Margin="424,293,0,0"
641-
VerticalAlignment="Top" Foreground="White" />
648+
VerticalAlignment="Top" Width="120"
649+
Text="{Binding Config.HydrusKey, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
650+
<Label x:Name="Lb_HyEndpoint" Content="Hydrus Endpoint" HorizontalAlignment="Left"
651+
Margin="424,293,0,0"
652+
VerticalAlignment="Top" Foreground="White" />
642653
<Label x:Name="Lb_HyKey" Content="Hydrus Key" HorizontalAlignment="Left" Margin="424,322,0,0"
643-
VerticalAlignment="Top" Foreground="White" />
654+
VerticalAlignment="Top" Foreground="White" />
644655

645656
</Grid>
646657
</TabItem>

SmartImage.UI/MainWindow.xaml.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ namespace SmartImage.UI;
6767
/// </summary>
6868
public partial class MainWindow : Window, IDisposable, INotifyPropertyChanged
6969
{
70-
static MainWindow()
71-
{
72-
}
70+
static MainWindow() { }
7371

7472
public MainWindow()
7573
{
@@ -161,8 +159,6 @@ public MainWindow()
161159

162160
private readonly object m_lock = new();
163161

164-
private bool m_me;
165-
166162
private readonly WindowInteropHelper m_wndInterop;
167163

168164
private readonly SemaphoreSlim m_us;
@@ -313,6 +309,7 @@ private async Task UpdateQueryAsync(string query)
313309
}
314310

315311
}
312+
316313
Tb_Upload.Text = upload;
317314
Pb_Status.IsIndeterminate = false;
318315

@@ -1071,6 +1068,10 @@ private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
10711068
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
10721069
}
10731070

1071+
/*public delegate bool PreviewChangedCallback(ResultItem? ri);
1072+
1073+
public event PreviewChangedCallback? PreviewChanged;*/
1074+
10741075
private void UpdatePreview(ResultItem ri)
10751076
{
10761077
/*if (ri.Load()) {
@@ -1082,8 +1083,8 @@ private void UpdatePreview(ResultItem ri)
10821083

10831084
if (ri.LoadImage()) {
10841085
Img_Preview.Source = ri.Image;
1085-
Debug.WriteLine($"updated image {ri.Image}");
1086-
1086+
// Debug.WriteLine($"updated image {ri.Image}");
1087+
// PreviewChanged?.Invoke(ri);
10871088
}
10881089
});
10891090
}
@@ -1093,7 +1094,8 @@ private void UpdatePreview(ImageSource x)
10931094
Application.Current.Dispatcher.Invoke(() =>
10941095
{
10951096
Img_Preview.Source = x;
1096-
Debug.WriteLine($"updated image {x}");
1097+
// Debug.WriteLine($"updated image {x}");
1098+
// PreviewChanged?.Invoke(null);
10971099
});
10981100
}
10991101

SmartImage.UI/PopupWindow.xaml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,28 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6-
xmlns:local="clr-namespace:SmartImage.UI"
6+
xmlns:ui="clr-namespace:SmartImage.UI"
77
mc:Ignorable="d"
8-
Title="PopupWindow" Height="450" Width="800" DataContext="{RelativeSource Self}">
8+
Title="PopupWindow" Height="350" Width="650" DataContext="{RelativeSource Self}">
9+
<Window.Resources>
10+
<!-- <ui:SharedViewModel x:Key="SharedViewModel" /> -->
11+
<ui:SharedImageControl x:Key="SharedImage" />
12+
</Window.Resources>
913
<Grid>
10-
<Image x:Name="Img_Preview" HorizontalAlignment="Center" Margin="10,10,0,0" VerticalAlignment="Center"/>
14+
<Border x:Name="Br_Preview"
15+
Width="300" Height="300" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0"
16+
17+
Background="{DynamicResource Black2}" >
18+
<Image x:Name="Img_Preview" MouseLeftButtonDown="Img_Preview_OnMouseLeftButtonDown"
19+
Panel.ZIndex="0" d:IsLocked="True" />
20+
</Border>
21+
<Border x:Name="Br_Compare"
22+
Width="300" Height="300" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,10,10,0"
23+
Background="{DynamicResource Black2}" >
24+
<Image x:Name="Img_Compare" MouseLeftButtonDown="Img_Compare_OnMouseLeftButtonDown"
25+
Panel.ZIndex="0" d:IsLocked="True">
26+
</Image>
1127

28+
</Border>
1229
</Grid>
1330
</Window>

0 commit comments

Comments
 (0)