Change icon and name
This commit is contained in:
1 parent
81d1ed8379
commit
79c1d88b58
6 files changed
+313
-20
No files matched your search
@@ -0,0 +1,34 @@
|
||||
<Window x:Class="FoxBrowser.UI.AppSettingsWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
Title="Назва та іконка FoxBrowser" Width="540" Height="270"
|
||||
MinWidth="480" Background="#15171A" Foreground="#F2F4F7"
|
||||
WindowStartupLocation="CenterOwner" ResizeMode="NoResize">
|
||||
<Grid Margin="20">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<TextBlock Text="Назва та іконка застосунку" FontSize="18" FontWeight="SemiBold" Margin="0,0,0,18" />
|
||||
<Grid Grid.Row="1" Margin="0,0,0,12">
|
||||
<Grid.ColumnDefinitions><ColumnDefinition Width="140" /><ColumnDefinition Width="*" /></Grid.ColumnDefinitions>
|
||||
<TextBlock Text="Назва:" VerticalAlignment="Center" />
|
||||
<TextBox x:Name="NameBox" Grid.Column="1" />
|
||||
</Grid>
|
||||
<Grid Grid.Row="2" Margin="0,0,0,8">
|
||||
<Grid.ColumnDefinitions><ColumnDefinition Width="140" /><ColumnDefinition Width="*" /><ColumnDefinition Width="Auto" /></Grid.ColumnDefinitions>
|
||||
<TextBlock Text="Іконка:" VerticalAlignment="Center" />
|
||||
<TextBlock x:Name="IconPathText" Grid.Column="1" VerticalAlignment="Center" Foreground="#AEB4BE" TextTrimming="CharacterEllipsis" />
|
||||
<Button Grid.Column="2" Content="Обрати іконку…" Click="ChooseIcon_Click" Padding="10,5" Margin="10,0,0,0" />
|
||||
</Grid>
|
||||
<TextBlock Grid.Row="3" Text="PNG і JPG автоматично конвертуються у custom_icon.ico поруч із FoxBrowser.exe." Foreground="#AEB4BE" TextWrapping="Wrap" Margin="0,4,0,0" />
|
||||
<StackPanel Grid.Row="4" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,18,0,0">
|
||||
<Button Content="Скинути до стандартного" Click="Reset_Click" Padding="10,6" Margin="0,0,8,0" />
|
||||
<Button Content="Скасувати" Click="Cancel_Click" Padding="10,6" Margin="0,0,8,0" />
|
||||
<Button Content="Зберегти" Click="Save_Click" Padding="14,6" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Window>
|
||||
@@ -0,0 +1,107 @@
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using FoxBrowser.Core;
|
||||
|
||||
namespace FoxBrowser.UI;
|
||||
|
||||
public partial class AppSettingsWindow : Window
|
||||
{
|
||||
private readonly Action<AppSettings> _apply;
|
||||
private string? _iconPath;
|
||||
|
||||
public AppSettingsWindow(AppSettings current, Action<AppSettings> apply)
|
||||
{
|
||||
InitializeComponent();
|
||||
_apply = apply;
|
||||
NameBox.Text = current.AppDisplayName;
|
||||
_iconPath = current.AppIconPath;
|
||||
UpdateIconPathText();
|
||||
}
|
||||
|
||||
private void ChooseIcon_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var dialog = new Microsoft.Win32.OpenFileDialog
|
||||
{
|
||||
Title = "Оберіть іконку застосунку",
|
||||
Filter = "Зображення та іконки (*.ico;*.png;*.jpg;*.jpeg)|*.ico;*.png;*.jpg;*.jpeg|Усі файли (*.*)|*.*",
|
||||
CheckFileExists = true
|
||||
};
|
||||
if (dialog.ShowDialog(this) != true) return;
|
||||
|
||||
try
|
||||
{
|
||||
_iconPath = AppIconConverter.CopyAsPortableIcon(dialog.FileName);
|
||||
UpdateIconPathText();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Windows.MessageBox.Show(this, $"Не вдалося підготувати іконку: {ex.Message}", "Іконка", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void Save_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var name = NameBox.Text.Trim();
|
||||
if (name.Length == 0) name = "FoxBrowser";
|
||||
try
|
||||
{
|
||||
var settings = new AppSettings { AppDisplayName = name, AppIconPath = _iconPath };
|
||||
AppSettingsStore.Save(settings);
|
||||
_apply(settings);
|
||||
DialogResult = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Windows.MessageBox.Show(this, $"Не вдалося зберегти налаштування: {ex.Message}", "Налаштування", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void Reset_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
NameBox.Text = "FoxBrowser";
|
||||
_iconPath = null;
|
||||
try { File.Delete(Path.Combine(AppContext.BaseDirectory, "custom_icon.ico")); } catch { }
|
||||
UpdateIconPathText();
|
||||
}
|
||||
|
||||
private void Cancel_Click(object sender, RoutedEventArgs e) => Close();
|
||||
|
||||
private void UpdateIconPathText() => IconPathText.Text = string.IsNullOrWhiteSpace(_iconPath) ? "Вбудована іконка" : Path.GetFileName(_iconPath);
|
||||
}
|
||||
|
||||
internal static class AppIconConverter
|
||||
{
|
||||
private const string PortableIconName = "custom_icon.ico";
|
||||
|
||||
public static string CopyAsPortableIcon(string sourcePath)
|
||||
{
|
||||
var destination = Path.Combine(AppContext.BaseDirectory, PortableIconName);
|
||||
var extension = Path.GetExtension(sourcePath);
|
||||
if (extension.Equals(".ico", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (string.Equals(Path.GetFullPath(sourcePath), Path.GetFullPath(destination), StringComparison.OrdinalIgnoreCase))
|
||||
return destination;
|
||||
File.Copy(sourcePath, destination, overwrite: true);
|
||||
return destination;
|
||||
}
|
||||
|
||||
using var bitmap = new Bitmap(sourcePath);
|
||||
var handle = bitmap.GetHicon();
|
||||
try
|
||||
{
|
||||
using var icon = Icon.FromHandle(handle);
|
||||
using var stream = File.Create(destination);
|
||||
icon.Save(stream);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DestroyIcon(handle);
|
||||
}
|
||||
return destination;
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern bool DestroyIcon(nint handle);
|
||||
}
|
||||
+21
-10
@@ -9,28 +9,22 @@ public sealed class TrayIconManager : IDisposable
|
||||
private readonly Forms.NotifyIcon _icon;
|
||||
private readonly Window _window;
|
||||
private readonly Action _exit;
|
||||
private Icon? _ownedIcon;
|
||||
|
||||
public TrayIconManager(Window window, Action exit)
|
||||
{
|
||||
_window = window;
|
||||
_exit = exit;
|
||||
|
||||
Icon? icon = null;
|
||||
try
|
||||
{
|
||||
var icoPath = System.IO.Path.Combine(AppContext.BaseDirectory, "FoxBrowser.ico");
|
||||
if (System.IO.File.Exists(icoPath)) icon = new Icon(icoPath);
|
||||
}
|
||||
catch { }
|
||||
|
||||
_icon = new Forms.NotifyIcon
|
||||
{
|
||||
Icon = icon ?? SystemIcons.Application,
|
||||
Text = "FoxBrowser",
|
||||
Icon = SystemIcons.Application,
|
||||
Text = window.Title,
|
||||
Visible = true,
|
||||
ContextMenuStrip = BuildMenu()
|
||||
};
|
||||
_icon.MouseClick += OnMouseClick;
|
||||
UpdateIdentity(window.Title, System.IO.Path.Combine(AppContext.BaseDirectory, "FoxBrowser.ico"));
|
||||
}
|
||||
|
||||
public void MinimizeToTray()
|
||||
@@ -58,6 +52,23 @@ public sealed class TrayIconManager : IDisposable
|
||||
_icon.MouseClick -= OnMouseClick;
|
||||
_icon.Visible = false;
|
||||
_icon.Dispose();
|
||||
_ownedIcon?.Dispose();
|
||||
}
|
||||
|
||||
public void UpdateIdentity(string displayName, string iconPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
Icon? next = null;
|
||||
if (System.IO.File.Exists(iconPath)) next = new Icon(iconPath);
|
||||
var old = _ownedIcon;
|
||||
_ownedIcon = next;
|
||||
_icon.Icon = next ?? SystemIcons.Application;
|
||||
old?.Dispose();
|
||||
}
|
||||
catch { }
|
||||
|
||||
_icon.Text = string.IsNullOrWhiteSpace(displayName) ? "FoxBrowser" : displayName[..Math.Min(63, displayName.Length)];
|
||||
}
|
||||
|
||||
private void OnMouseClick(object? sender, Forms.MouseEventArgs e)
|
||||
|
||||
Reference in new issue
Block a user