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

Code improvements #136

Merged
merged 6 commits into from
Jul 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: 3 additions & 3 deletions Source/Hurl.BrowserSelector/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Hurl.BrowserSelector"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml">
xmlns:wpfui="http://schemas.lepo.co/wpfui/2022/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ui:ControlsDictionary />
<ui:ThemesDictionary Theme="Dark" />
<wpfui:ControlsDictionary />
<wpfui:ThemesDictionary Theme="Dark" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Expand Down
14 changes: 6 additions & 8 deletions Source/Hurl.BrowserSelector/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public partial class App : Application

public App()
{
this.Dispatcher.UnhandledException += Dispatcher_UnhandledException;
Dispatcher.UnhandledException += Dispatcher_UnhandledException;
}

private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
Expand Down Expand Up @@ -72,7 +72,7 @@ protected override void OnStartup(StartupEventArgs e)
{
if (Current.MainWindow is { } window)
{
_mainWindow.MaximizeWindow();
_mainWindow.ShowWindow();
}
else
{
Expand Down Expand Up @@ -129,12 +129,10 @@ public void PipeServer()

try
{
using (StreamReader sr = new(pipeserver))
{
string args = sr.ReadToEnd();
string[] argsArray = JsonSerializer.Deserialize<string[]>(args) ?? [];
OnInstanceInvoked(argsArray);
}
using StreamReader sr = new(pipeserver);
string args = sr.ReadToEnd();
string[] argsArray = JsonSerializer.Deserialize<string[]>(args) ?? [];
OnInstanceInvoked(argsArray);
}
catch (Exception e)
{
Expand Down
4 changes: 1 addition & 3 deletions Source/Hurl.BrowserSelector/Controls/BrowsersList.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
CornerRadius="5"
Cursor="Hand"
Focusable="True"
MouseLeftButtonUp="BtnArea_MouseLeftButtonUp"
MouseLeftButtonUp="BrowserButton_Click"
Tag="{Binding}"
ToolTip="{Binding Path=Name}">
<Border.Style>
Expand Down Expand Up @@ -130,6 +130,4 @@
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>


</UserControl>
12 changes: 6 additions & 6 deletions Source/Hurl.BrowserSelector/Controls/BrowsersList.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial class BrowsersList : UserControl
{
public BrowsersList()
{
DataContext = Globals.SettingsGlobal.GetBrowsers();
DataContext = SettingsGlobal.GetBrowsers();
InitializeComponent();
Loaded += (s, e) =>
{
Expand All @@ -41,9 +41,9 @@ private void OnPreviewKeyUp(object sender, KeyEventArgs e)
//if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control)) { }
}

private void BtnArea_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
private void BrowserButton_Click(object sender, MouseButtonEventArgs e)
{
var tag = (sender as Border).Tag as Browser;
var tag = ((Border)sender).Tag as Browser;
OpenLink(tag);
MinimizeWindow();
}
Expand All @@ -66,12 +66,12 @@ private void AdditionalBtn_Click(object sender, RoutedEventArgs e)

private void MenuItem_Click(object sender, RoutedEventArgs e)
{
var alt = (sender as MenuItem).Tag as AltLaunchParentConverter.AltLaunchParent;
var alt = ((MenuItem)sender).Tag as AltLaunchParentConverter.AltLaunchParent;
OpenAltLaunch(alt.AltLaunch, alt.Browser);
MinimizeWindow();
}

public void OpenLink(Browser clickedbrowser)
public static void OpenLink(Browser clickedbrowser)
{
var browser = clickedbrowser;
var Link = UriGlobal.Value;
Expand All @@ -88,7 +88,7 @@ public void OpenLink(Browser clickedbrowser)
}
}

public void OpenAltLaunch(AlternateLaunch alt, Browser browser)
public static void OpenAltLaunch(AlternateLaunch alt, Browser browser)
{
if (alt.LaunchArgs.Contains("%URL%"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return new object[] {
return [
(value as AltLaunchParent).AltLaunch,
(value as AltLaunchParent).Browser
};
];
}
}
}
12 changes: 6 additions & 6 deletions Source/Hurl.BrowserSelector/Globals/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace Hurl.BrowserSelector.Globals
{
public class SettingsGlobal
{
private static SettingsGlobal _instance = new();
private static readonly SettingsGlobal _instance = new();

private Library.Models.Settings _settings = SettingsFile.GetSettings();
private Settings _settings = SettingsFile.GetSettings();

public static Library.Models.Settings Value
public static Settings Value
{
get
{
Expand All @@ -27,7 +27,7 @@ public static void AddBrowserRule(List<string> rules, string name)
{
if (Value?.Rulesets == null)
{
Value.Rulesets = new List<Ruleset> { };
Value.Rulesets = [];
}
Value.Rulesets.Add(new Ruleset() { Rules = rules, BrowserName = name });

Expand All @@ -40,11 +40,11 @@ public static void AdjustWindowSize(SizeChangedEventArgs e)
{
if (Value.AppSettings != null)
{
Value.AppSettings.WindowSize = new int[] { (int)e.NewSize.Width, (int)e.NewSize.Height };
Value.AppSettings.WindowSize = [(int)e.NewSize.Width, (int)e.NewSize.Height];
}
else
{
Value.AppSettings = new AppSettings() { WindowSize = new int[] { (int)e.NewSize.Width, (int)e.NewSize.Height } };
Value.AppSettings = new AppSettings() { WindowSize = [(int)e.NewSize.Width, (int)e.NewSize.Height] };
}

Save();
Expand Down
19 changes: 5 additions & 14 deletions Source/Hurl.BrowserSelector/Globals/Uri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ namespace Hurl.BrowserSelector.Globals
{
public sealed class UriGlobal : INotifyPropertyChanged
{
private UriGlobal(string Url)
{
this.Url = Url;
}
private UriGlobal(string Url) => this.Url = Url;
private string _url;

private string Url
Expand All @@ -24,31 +21,25 @@ private string Url
}
}

private static UriGlobal _instance = null;
private static UriGlobal? _instance;

public static string Value
{
get
{
if (_instance == null)
{
_instance = new UriGlobal(string.Empty);
}
_instance ??= new UriGlobal(string.Empty);
return _instance.Url;
}
set
{
if (_instance == null)
{
_instance = new UriGlobal(value);
}
_instance ??= new UriGlobal(value);
_instance.Url = value;
}
}

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged([CallerMemberName] string name = null)
protected void OnPropertyChanged([CallerMemberName] string? name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
Expand Down
6 changes: 3 additions & 3 deletions Source/Hurl.BrowserSelector/Helpers/CursorPosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace Hurl.BrowserSelector.Helpers
{
public static class CursorPosition
public static partial class CursorPosition
{
[DllImport("user32.dll")]
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetCursorPos(ref Win32Point pt);
internal static partial bool GetCursorPos(ref Win32Point pt);

[StructLayout(LayoutKind.Sequential)]
internal struct Win32Point
Expand Down
1 change: 1 addition & 0 deletions Source/Hurl.BrowserSelector/Hurl.BrowserSelector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<ApplicationIcon>Assets\internet.ico</ApplicationIcon>
<StartupObject>Hurl.BrowserSelector.App</StartupObject>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
Expand Down
4 changes: 2 additions & 2 deletions Source/Hurl.BrowserSelector/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Deactivated="Window_Deactivated"
ExtendsContentIntoTitleBar="True"
PreviewKeyUp="Window_KeyEvents"
SizeChanged="UiWindow_SizeChanged"
SizeChanged="Window_SizeChanged"
Topmost="True"
WindowBackdropType="Mica"
WindowStartupLocation="CenterScreen"
Expand Down Expand Up @@ -129,7 +129,7 @@
Padding="7,3"
Background="{x:Null}"
BorderBrush="{x:Null}"
Click="linkpreview_Click"
Click="Linkpreview_Click"
Cursor="Hand"
Foreground="AliceBlue"
ToolTip="Edit URL (E)" />
Expand Down
41 changes: 20 additions & 21 deletions Source/Hurl.BrowserSelector/Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class MainWindow : FluentWindow

public MainWindow()
{
var settings = Globals.SettingsGlobal.Value;
var settings = SettingsGlobal.Value;

InitializeComponent();

Expand Down Expand Up @@ -56,17 +56,17 @@ public void Init(CliArgs data)

if (data.IsRunAsMin || isRuleCheckSuccess)
{
Show(); // TODO: try to remove the flashing
MinimizeWindow();
Show();
}
else
{
Show();
PositionWindowUnderTheMouse();
if (data.IsSecondInstance)
{
WindowState = WindowState.Normal;
}
Show();
}

linkpreview.Content = string.IsNullOrEmpty(UriGlobal.Value) ? "No Url Opened" : UriGlobal.Value;
Expand Down Expand Up @@ -127,33 +127,33 @@ private void LinkCopyBtnClick(object sender, RoutedEventArgs e)
}

private void SettingsBtnClick(object sender, RoutedEventArgs e) => Process.Start("explorer", "\"" + Constants.APP_SETTINGS_MAIN + "\"");
private void Draggable(object sender, MouseButtonEventArgs e) => this.DragMove();
private void Draggable(object sender, MouseButtonEventArgs e) => DragMove();
private void CloseBtnClick(object sender, RoutedEventArgs e) => MinimizeWindow();

private void MinimizeWindow()
{
this.WindowState = WindowState.Minimized;
this.Hide();
WindowState = WindowState.Minimized;
Hide();
}

public void MaximizeWindow()
public void ShowWindow()
{
this.Show();
Show();
PositionWindowUnderTheMouse();
this.WindowState = WindowState.Normal;
WindowState = WindowState.Normal;
}

private void TrayMenuItem_OnClick(object sender, RoutedEventArgs e)
{
var settings = Globals.SettingsGlobal.Value;
_ = SettingsGlobal.Value;

string tag = (sender as Wpf.Ui.Controls.MenuItem).Tag as string;
string tag = ((MenuItem)sender).Tag as string;
try
{
switch (tag)
{
case "open":
MaximizeWindow();
ShowWindow();
break;
case "settings":
Process.Start("explorer", "\"" + Constants.APP_SETTINGS_MAIN + "\"");
Expand All @@ -176,7 +176,7 @@ private void TrayMenuItem_OnClick(object sender, RoutedEventArgs e)
}
}

private void NotifyIcon_LeftClick(object sender, RoutedEventArgs e) => MaximizeWindow();
private void NotifyIcon_LeftClick(object sender, RoutedEventArgs e) => ShowWindow();

private void Window_Deactivated(object sender, EventArgs e)
{
Expand All @@ -189,7 +189,7 @@ private void Window_Deactivated(object sender, EventArgs e)

private void PositionWindowUnderTheMouse()
{
var settings = Globals.SettingsGlobal.Value;
var settings = SettingsGlobal.Value;

if (settings.AppSettings != null && settings.AppSettings.LaunchUnderMouse)
{
Expand All @@ -198,29 +198,29 @@ private void PositionWindowUnderTheMouse()
Left = mouse.X;
Top = mouse.Y;

Debug.WriteLine($"{Left}x{Top} while screen res: {SystemParameters.FullPrimaryScreenWidth}x{SystemParameters.FullPrimaryScreenHeight}");
Debug.WriteLine($"{Left}{Top} with screen resolution: {SystemParameters.FullPrimaryScreenWidth}{SystemParameters.FullPrimaryScreenHeight}");
}
}

private void Button_Click(object sender, RoutedEventArgs e)
{
forcePreventWindowDeactivationEvent = true;
new TimeSelectWindow(Globals.SettingsGlobal.Value.Browsers).ShowDialog();
new TimeSelectWindow(SettingsGlobal.Value.Browsers).ShowDialog();
forcePreventWindowDeactivationEvent = false;
}

private void UiWindow_SizeChanged(object sender, SizeChangedEventArgs e) => Globals.SettingsGlobal.AdjustWindowSize(e);
private void Window_SizeChanged(object sender, SizeChangedEventArgs e) => SettingsGlobal.AdjustWindowSize(e);

async private void linkpreview_Click(object sender, RoutedEventArgs e)
async private void Linkpreview_Click(object sender, RoutedEventArgs e)
{
forcePreventWindowDeactivationEvent = true;

var NewUrl = await URLEditor.ShowInputAsync(this, UriGlobal.Value);
if (!string.IsNullOrEmpty(NewUrl))
{
UriGlobal.Value = NewUrl;
(sender as Wpf.Ui.Controls.Button).Content = NewUrl;
(sender as Wpf.Ui.Controls.Button).ToolTip = NewUrl;
((Button)sender).Content = NewUrl;
((Button)sender).ToolTip = NewUrl;
}

forcePreventWindowDeactivationEvent = false;
Expand All @@ -232,4 +232,3 @@ private void Button_Click_1(object sender, RoutedEventArgs e)
Process.Start(Constants.SETTINGS_APP);
}
}

4 changes: 2 additions & 2 deletions Source/Hurl.BrowserSelector/Windows/TimeSelectWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
ItemsSource="{Binding}" />
</wpfui:CardControl>
<Grid Margin="0,10">
<wpfui:Button Padding="40,15" Click="Button_Click_1">
<wpfui:Button Padding="40,15" Click="CancelBtn_Click">
Cancel Current
</wpfui:Button>
<wpfui:Button
Padding="40,15"
HorizontalAlignment="Right"
Click="Button_Click"
Click="SetBtn_Click"
Content="Set" />
</Grid>
</StackPanel>
Expand Down
Loading